Skip to content

Commit

Permalink
support fax single email notify (2600hz#5511)
Browse files Browse the repository at this point in the history
  • Loading branch information
lazedo authored and k-anderson committed Feb 15, 2019
1 parent 2adcecf commit 2db2dd4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 23 deletions.
49 changes: 31 additions & 18 deletions applications/fax/src/fax_request.erl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@
}
]).

-define(NOTIFICATION_INBOUND_EMAIL, [<<"notifications">>
,<<"inbound">>
,<<"email">>
,<<"send_to">>
]
).
-define(NOTIFICATION_EMAIL, [<<"notifications">>
,<<"email">>
,<<"send_to">>
]
).

%%%=============================================================================
%%% API
%%%=============================================================================
Expand Down Expand Up @@ -325,31 +337,32 @@ maybe_update_fax_settings(#state{call=Call
{'error', _} -> maybe_update_fax_settings_from_account(State)
end.

-spec get_faxbox_notify_list(kz_json:object(), kz_term:ne_binary()) -> kz_json:object().
get_faxbox_notify_list(FaxBoxDoc, AccountDb) ->
DefaultNotify = default_notify(FaxBoxDoc),
-spec faxbox_notify_emails(kz_json:object()) -> kz_term:ne_binaries().
faxbox_notify_emails(JObj) ->
Emails = kz_json:get_first_defined([?NOTIFICATION_INBOUND_EMAIL
,?NOTIFICATION_EMAIL
], JObj, []),
fax_util:notify_email_list(Emails).

-spec get_faxbox_user_email(kz_json:object(), kz_term:ne_binary()) -> kz_term:api_ne_binary().
get_faxbox_user_email(FaxBoxDoc, AccountDb) ->
case kz_json:get_value(<<"owner_id">>, FaxBoxDoc) of
'undefined' -> DefaultNotify;
'undefined' -> 'undefined';
OwnerId ->
case kz_datamgr:open_cache_doc(AccountDb, OwnerId) of
{'ok', UserDoc} ->
List = kz_json:get_value([<<"email">>,<<"send_to">>], DefaultNotify, []),
maybe_add_owner_to_notify_list(List, kz_json:get_value(<<"email">>, UserDoc));
{'ok', UserDoc} -> kz_json:get_ne_binary_value(<<"email">>, UserDoc);
_ ->
DefaultNotify
lager:debug("faxbox ~s has invalid owner_id ~s", [kz_doc:id(FaxBoxDoc), OwnerId]),
'undefined'
end
end.

-spec default_notify(kz_json:object()) -> kz_json:object().
default_notify(FaxBoxDoc) ->
kz_json:get_value([<<"notifications">>,<<"inbound">>], FaxBoxDoc, kz_json:new()).

-spec maybe_add_owner_to_notify_list(list(), kz_term:api_binary()) -> kz_json:object().
maybe_add_owner_to_notify_list(List, 'undefined') ->
kz_json:set_value([<<"email">>, <<"send_to">>], List, kz_json:new());
maybe_add_owner_to_notify_list(List, OwnerEmail) ->
NotifyList = fax_util:notify_email_list('undefined', OwnerEmail, List),
kz_json:set_value([<<"email">>, <<"send_to">>], NotifyList, kz_json:new()).
-spec get_faxbox_notify_list(kz_json:object(), kz_term:ne_binary()) -> kz_json:object().
get_faxbox_notify_list(FaxBoxDoc, AccountDb) ->
UserEmail = get_faxbox_user_email(FaxBoxDoc, AccountDb),
FaxBoxEmails = faxbox_notify_emails(FaxBoxDoc),
EMails = fax_util:notify_email_list(UserEmail, FaxBoxEmails),
kz_json:set_value([<<"email">>, <<"send_to">>], EMails, kz_json:new()).

-spec maybe_update_fax_settings_from_account(state()) -> any().
maybe_update_fax_settings_from_account(#state{call=Call}=State) ->
Expand Down
10 changes: 9 additions & 1 deletion applications/fax/src/fax_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

-export([fax_properties/1]).
-export([collect_channel_props/1]).
-export([notify_email_list/3]).
-export([notify_email_list/1, notify_email_list/2, notify_email_list/3]).
-export([filter_numbers/1]).
-export([is_valid_caller_id/2]).

Expand Down Expand Up @@ -45,6 +45,14 @@ collect_channel_prop(<<"Hangup-Code">> = Key, JObj) ->
collect_channel_prop(Key, JObj) ->
{Key, kz_json:get_value(Key, JObj)}.

-spec notify_email_list(kz_term:ne_binary() | list()) -> list().
notify_email_list(Email) ->
notify_email_list('undefined', 'undefined', Email).

-spec notify_email_list(kz_term:api_binary(), kz_term:ne_binary() | list()) -> list().
notify_email_list(OwnerEmail, Email) ->
notify_email_list('undefined', OwnerEmail, Email).

-spec notify_email_list(kz_term:api_binary(), kz_term:api_binary(), kz_term:ne_binary() | list()) -> list().
notify_email_list(From, OwnerEmail, Email) when is_binary(Email) ->
notify_email_list(From, OwnerEmail, [Email]);
Expand Down
24 changes: 20 additions & 4 deletions applications/fax/src/fax_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@
-define(ENSURE_CID_KEY, <<"ensure_valid_caller_id">>).
-define(DEFAULT_ENSURE_CID, kapps_config:get_is_true(?CONFIG_CAT, ?ENSURE_CID_KEY, 'true')).

-define(NOTIFICATION_OUTBOUND_EMAIL, [<<"notifications">>
,<<"outbound">>
,<<"email">>
,<<"send_to">>
]
).
-define(NOTIFICATION_EMAIL, [<<"notifications">>
,<<"email">>
,<<"send_to">>
]
).

%%%=============================================================================
%%% API
%%%=============================================================================
Expand Down Expand Up @@ -685,6 +697,13 @@ fax_error(JObj) ->
, [<<"tx_result">>, <<"result_text">>]
], JObj).

-spec notify_emails(kz_json:object()) -> kz_term:ne_binaries().
notify_emails(JObj) ->
Emails = kz_json:get_first_defined([?NOTIFICATION_OUTBOUND_EMAIL
,?NOTIFICATION_EMAIL
], JObj, []),
fax_util:notify_email_list(Emails).

-spec notify_fields(kz_json:object(), kz_json:object()) -> kz_term:proplist().
notify_fields(JObj, Resp) ->
<<"sip:", HangupCode/binary>> = kz_json:get_value(<<"Hangup-Code">>, Resp, <<"sip:0">>),
Expand All @@ -696,10 +715,7 @@ notify_fields(JObj, Resp) ->

ToNumber = kz_term:to_binary(kz_json:get_value(<<"to_number">>, JObj)),
ToName = kz_term:to_binary(kz_json:get_value(<<"to_name">>, JObj, ToNumber)),
Emails = kz_json:get_first_defined([[<<"notifications">>, <<"email">>, <<"send_to">>]
,[<<"notifications">>, <<"outbound">>, <<"email">>, <<"send_to">>]
], JObj, []),
Notify = [E || E <- Emails, not kz_term:is_empty(E)],
Notify = [E || E <- notify_emails(JObj), not kz_term:is_empty(E)],

props:filter_empty(
[{<<"Caller-ID-Name">>, kz_json:get_value(<<"from_name">>, JObj)}
Expand Down

0 comments on commit 2db2dd4

Please sign in to comment.