Skip to content

Commit

Permalink
KAZOO-543: clean up pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
James Aimonetti committed Feb 27, 2013
1 parent c0bee01 commit b262c8c
Showing 1 changed file with 55 additions and 49 deletions.
104 changes: 55 additions & 49 deletions whistle_apps/apps/notify/src/notify_util.erl
Expand Up @@ -33,41 +33,47 @@
%%--------------------------------------------------------------------
-spec send_email(ne_binary(), 'undefined' | binary(), term()) ->
'ok' | {'error', _}.
send_email(_, undefined, _) -> ok;
send_email(_, <<>>, _) -> ok;
send_email(_, 'undefined', _) -> 'ok';
send_email(_, <<>>, _) -> 'ok';
send_email(From, To, Email) ->
Encoded = mimemail:encode(Email),
Relay = wh_util:to_list(whapps_config:get(<<"smtp_client">>, <<"relay">>, <<"localhost">>)),
Username = wh_util:to_list(whapps_config:get(<<"smtp_client">>, <<"username">>, <<"">>)),
Password = wh_util:to_list(whapps_config:get(<<"smtp_client">>, <<"password">>, <<"">>)),
Auth = wh_util:to_list(whapps_config:get(<<"smtp_client">>, <<"auth">>, <<"never">>)),
Port = wh_util:to_list(whapps_config:get(<<"smtp_client">>, <<"port">>, <<"25">>)),
Username = wh_util:to_list(whapps_config:get(<<"smtp_client">>, <<"username">>, <<"">>)),
Password = wh_util:to_list(whapps_config:get(<<"smtp_client">>, <<"password">>, <<"">>)),
Auth = wh_util:to_list(whapps_config:get(<<"smtp_client">>, <<"auth">>, <<"never">>)),
Port = wh_util:to_list(whapps_config:get(<<"smtp_client">>, <<"port">>, <<"25">>)),

lager:debug("sending email to ~s from ~s via ~s", [To, From, Relay]),
ReqId = get(callid),
ReqId = get('callid'),

Self = self(),

gen_smtp_client:send({From, [To], Encoded}, [{relay, Relay},{username, Username}, {password, Password}, {port, Port}, {auth, Auth}]
gen_smtp_client:send({From, [To], Encoded}
,[{'relay', Relay}
,{'username', Username}
,{'password', Password}
,{'port', Port}
,{'auth', Auth}
]
,fun(X) ->
put(callid, ReqId),
put('callid', ReqId),
lager:debug("email relay responded: ~p, send to ~p", [X, Self]),
Self ! {relay_response, X}
Self ! {'relay_response', X}
end),
%% The callback will receive either `{ok, Receipt}' where Receipt is the SMTP server's receipt
%% identifier, `{error, Type, Message}' or `{exit, ExitReason}', as the single argument.
%% The callback will receive either `{ok, Receipt}' where Receipt is the SMTP server's receipt
%% identifier, `{error, Type, Message}' or `{exit, ExitReason}', as the single argument.
receive
{relay_response, {ok, _Msg}} -> ok;
{relay_response, {error, _Type, Message}} -> {error, Message};
{relay_response, {exit, Reason}} -> {error, Reason}
after 10000 -> {error, timeout}
{'relay_response', {'ok', _Msg}} -> 'ok';
{'relay_response', {'error', _Type, Message}} -> {'error', Message};
{'relay_response', {'exit', Reason}} -> {'error', Reason}
after 10000 -> {'error', 'timeout'}
end.

-spec send_update(api_binary(), ne_binary(), ne_binary()) -> 'ok'.
-spec send_update(api_binary(), ne_binary(), ne_binary(), api_binary()) -> 'ok'.
send_update(RespQ, MsgId, Status) ->
send_update(RespQ, MsgId, Status, undefined).
send_update(undefined, _, _, _) -> lager:debug("no response queue to send update");
send_update(RespQ, MsgId, Status, 'undefined').
send_update('undefined', _, _, _) -> lager:debug("no response queue to send update");
send_update(RespQ, MsgId, Status, Msg) ->
Prop = props:filter_undefined(
[{<<"Status">>, Status}
Expand All @@ -84,8 +90,8 @@ send_update(RespQ, MsgId, Status, Msg) ->
%%
%% @end
%%--------------------------------------------------------------------
-spec json_to_template_props('undefined' | wh_json:object()) -> 'undefined' | wh_proplist().
json_to_template_props(undefined) -> undefined;
-spec json_to_template_props(api_object()) -> 'undefined' | wh_proplist().
json_to_template_props('undefined') -> 'undefined';
json_to_template_props(JObj) ->
normalize_proplist(wh_json:recursive_to_proplist(JObj)).

Expand All @@ -109,7 +115,7 @@ normalize_proplist_element(Else) ->
Else.

normalize_value(Value) ->
binary:replace(wh_util:to_lower_binary(Value), <<"-">>, <<"_">>, [global]).
binary:replace(wh_util:to_lower_binary(Value), <<"-">>, <<"_">>, ['global']).

%%--------------------------------------------------------------------
%% @public
Expand All @@ -123,16 +129,16 @@ normalize_value(Value) ->
-spec compile_default_template(atom(), ne_binary(), atom()) -> {'ok', atom()}.

compile_default_text_template(TemplateModule, Category) ->
compile_default_template(TemplateModule, Category, default_text_template).
compile_default_template(TemplateModule, Category, 'default_text_template').

compile_default_html_template(TemplateModule, Category) ->
compile_default_template(TemplateModule, Category, default_html_template).
compile_default_template(TemplateModule, Category, 'default_html_template').

compile_default_subject_template(TemplateModule, Category) ->
compile_default_template(TemplateModule, Category, default_subject_template).
compile_default_template(TemplateModule, Category, 'default_subject_template').

compile_default_template(TemplateModule, Category, Key) ->
{ok, TemplateModule} = erlydtl:compile(whapps_config:get(Category, Key), TemplateModule).
{'ok', TemplateModule} = erlydtl:compile(whapps_config:get(Category, Key), TemplateModule).

%%--------------------------------------------------------------------
%% @public
Expand All @@ -143,7 +149,7 @@ compile_default_template(TemplateModule, Category, Key) ->
-spec render_template(api_binary(), atom(), wh_proplist()) ->
{'ok', iolist()} |
{'error', term()}.
render_template(undefined, DefaultTemplate, Props) ->
render_template('undefined', DefaultTemplate, Props) ->
lager:debug("rendering default ~s template", [DefaultTemplate]),
DefaultTemplate:render(Props);
render_template(Template, DefaultTemplate, Props) ->
Expand All @@ -153,7 +159,7 @@ render_template(Template, DefaultTemplate, Props) ->
])
,true),
lager:debug("compiling custom ~s template", [DefaultTemplate]),
{ok, CustomTemplate} = erlydtl:compile(Template, CustomTemplate),
{'ok', CustomTemplate} = erlydtl:compile(Template, CustomTemplate),

lager:debug("rendering custom template ~s", [CustomTemplate]),
Result = CustomTemplate:render(Props),
Expand All @@ -164,7 +170,7 @@ render_template(Template, DefaultTemplate, Props) ->
catch
_:_E ->
lager:debug("error compiling custom ~s template: ~p", [DefaultTemplate, _E]),
render_template(undefined, DefaultTemplate, Props)
render_template('undefined', DefaultTemplate, Props)
end.

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -230,21 +236,21 @@ get_service_props(Request, Account, ConfigCat) ->
get_rep_email(JObj) ->
AccountId = wh_json:get_value(<<"pvt_account_id">>, JObj),
case wh_json:get_value(<<"pvt_tree">>, JObj, []) of
[] -> undefined;
[] -> 'undefined';
Tree -> get_rep_email(lists:reverse(Tree), AccountId)
end.

get_rep_email([], _) -> undefined;
get_rep_email([], _) -> 'undefined';
get_rep_email([Parent|Parents], AccountId) ->
ParentDb = wh_util:format_account_id(Parent, encoded),
ViewOptions = [include_docs
,{key, AccountId}
ParentDb = wh_util:format_account_id(Parent, 'encoded'),
ViewOptions = ['include_docs'
,{'key', AccountId}
],
lager:debug("attempting to find sub account rep for ~s in parent account ~s", [AccountId, Parent]),
case couch_mgr:get_results(ParentDb, <<"sub_account_reps/find_assignments">>, ViewOptions) of
{ok, [Result|_]} ->
{'ok', [Result|_]} ->
case wh_json:get_value([<<"doc">>, <<"email">>], Result) of
undefined ->
'undefined' ->
lager:debug("found rep but they have no email, attempting to get email of admin"),
wh_json:get_value(<<"email">>, find_admin(ParentDb));
Else ->
Expand All @@ -265,28 +271,28 @@ get_rep_email([Parent|Parents], AccountId) ->
%%--------------------------------------------------------------------
-type account_ids() :: ne_binaries().
-spec find_admin(api_binary() | account_ids() | wh_json:object()) -> wh_json:object().
find_admin(undefined) -> wh_json:new();
find_admin('undefined') -> wh_json:new();
find_admin([]) -> wh_json:new();
find_admin(Account) when is_binary(Account) ->
AccountDb = wh_util:format_account_id(Account, encoded),
AccountId = wh_util:format_account_id(Account, raw),
AccountDb = wh_util:format_account_id(Account, 'encoded'),
AccountId = wh_util:format_account_id(Account, 'raw'),
case couch_mgr:open_cache_doc(AccountDb, AccountId) of
{error, _} -> find_admin([AccountId]);
{ok, JObj} ->
{'error', _} -> find_admin([AccountId]);
{'ok', JObj} ->
Tree = wh_json:get_value(<<"pvt_tree">>, JObj, []),
find_admin([AccountId | lists:reverse(Tree)])
end;
find_admin([AcctId|Tree]) ->
AccountDb = wh_util:format_account_id(AcctId),
ViewOptions = [{key, <<"user">>}
,include_docs
ViewOptions = [{'key', <<"user">>}
,'include_docs'
],
case couch_mgr:get_results(AccountDb, <<"maintenance/listing_by_type">>, ViewOptions) of
{ok, Users} ->
{'ok', Users} ->
case [User
|| User <- Users
,wh_json:get_value([<<"doc">>, <<"priv_level">>], User) =:= <<"admin">>
,wh_json:get_ne_value([<<"doc">>, <<"email">>], User) =/= undefined
,wh_json:get_ne_value([<<"doc">>, <<"email">>], User) =/= 'undefined'
]
of
[] -> find_admin(Tree);
Expand All @@ -313,11 +319,11 @@ find_admin(Account) ->
'undefined'.
get_account_doc(JObj) ->
case {wh_json:get_value(<<"Account-DB">>, JObj), wh_json:get_value(<<"Account-ID">>, JObj)} of
{undefined, undefined} -> undefined;
{undefined, Id1} ->
couch_mgr:open_doc(wh_util:format_account_id(Id1, encoded), Id1);
{Id2, undefined} ->
couch_mgr:open_doc(Id2, wh_util:format_account_id(Id2, raw));
{'undefined', 'undefined'} -> 'undefined';
{'undefined', Id1} ->
couch_mgr:open_doc(wh_util:format_account_id(Id1, 'encoded'), Id1);
{Id2, 'undefined'} ->
couch_mgr:open_doc(Id2, wh_util:format_account_id(Id2, 'raw'));
{Db, AccountId} ->
couch_mgr:open_doc(Db, AccountId)
end.

0 comments on commit b262c8c

Please sign in to comment.