Skip to content

Commit

Permalink
Round 2
Browse files Browse the repository at this point in the history
  • Loading branch information
onnet committed Feb 21, 2016
1 parent da672c3 commit 0385750
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 39 deletions.
23 changes: 11 additions & 12 deletions applications/crossbar/src/modules/cb_notifications.erl
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ put(Context) ->
%% @end
%%--------------------------------------------------------------------
-spec post(cb_context:context(), path_token()) -> cb_context:context().
-spec post(cb_context:context(), path_token(), path_token()) -> cb_context:context().
post(Context, Id) ->
case cb_context:req_files(Context) of
[] ->
Expand Down Expand Up @@ -352,20 +351,20 @@ set_system_macros(Context) ->
Context
end.

-spec post(cb_context:context(), path_token(), path_token()) -> cb_context:context().
post(Context, ?CUSTOMER_UPDATE, ?MESSAGE) ->
case
whapps_util:amqp_pool_request(
build_customer_update_payload(Context)
,fun wapi_notifications:publish_customer_update/1
,fun wapi_notifications:customer_update_v/1
)
case wh_amqp_worker:call(build_customer_update_payload(Context)
,fun wapi_notifications:publish_customer_update/1
,fun wapi_notifications:customer_update_v/1
)
of
{'ok', _Resp} ->
lager:debug("published customer_update notification");
lager:debug("published customer_update notification"),
Context;
{'error', _E} ->
lager:debug("failed to publish_customer update notification: ~p", [_E])
end,
Context;
lager:debug("failed to publish_customer update notification: ~p", [_E]),
crossbar_util:response('error', <<"Failed to send message">>, Context)
end;

post(Context, Id, ?PREVIEW) ->
Notification = cb_context:doc(Context),
Expand Down Expand Up @@ -405,7 +404,7 @@ build_customer_update_payload(Context) ->
,{<<"CC">>, cb_context:req_value(Context, <<"cc">>)}
,{<<"BCC">>, cb_context:req_value(Context, <<"bcc">>)}
,{<<"HTML">>, cb_context:req_value(Context, <<"html">>)}
,{<<"Plain">>, cb_context:req_value(Context, <<"plain">>)}
,{<<"Text">>, cb_context:req_value(Context, <<"plain">>)}
| wh_api:default_headers(?APP_NAME, ?APP_VERSION)
]).

Expand Down
45 changes: 24 additions & 21 deletions applications/teletype/src/templates/teletype_customer_update.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
-define(TEMPLATE_ID, <<"customer_update">>).
-define(MOD_CONFIG_CAT, <<(?NOTIFY_CONFIG_CAT)/binary, ".", (?TEMPLATE_ID)/binary>>).

-define(AGG_VIEW_CHILDREN, <<"accounts/listing_by_children">>).
-define(ACC_CHILDREN_LIST, <<"accounts/listing_by_children">>).
-define(ACC_USERS_LIST, <<"users/crossbar_listing">>).

-define(TEMPLATE_MACROS
,wh_json:from_list(
Expand Down Expand Up @@ -72,14 +73,15 @@ process_req(DataJObj) ->
ViewOpts = [{'startkey', [SenderId]}
,{'endkey', [SenderId, wh_json:new()]}
],
case couch_mgr:get_results(?WH_ACCOUNTS_DB, <<"accounts/listing_by_children">>, ViewOpts) of
case couch_mgr:get_results(?WH_ACCOUNTS_DB, ?ACC_CHILDREN_LIST, ViewOpts) of
{'ok', Accounts} ->
process_accounts(Accounts, DataJObj);
{'error', _Reason} = E ->
lager:info("failed to load children. error: ~p", [E]),
E
end.

-spec process_accounts(wh_json:objects(), wh_json:object()) -> wh_proplist().
process_accounts(Accounts, DataJObj) ->
case wh_json:get_value(<<"recipient_id">>, DataJObj) of
'undefined' ->
Expand All @@ -88,37 +90,38 @@ process_accounts(Accounts, DataJObj) ->
[process_account(wh_json:get_value(<<"value">>, Account), DataJObj) || Account <- Accounts, wh_json:get_value(<<"id">>, Account) == RecipientId]
end.

-spec process_account(wh_json:object(), wh_json:object()) -> 'ok'|wh_proplist().
process_account(AccountJObj, DataJObj) ->
AccountId = wh_json:get_value(<<"id">>, AccountJObj),
AccountDb = wh_util:format_account_id(AccountId, 'encoded'),
case couch_mgr:get_results(AccountDb,<<"users/crossbar_listing">>,[]) of
{'ok', Users} ->
Templates = teletype_templates:fetch(?TEMPLATE_ID, DataJObj),
select_users_to_update(lists:map(fun(User) -> wh_json:get_value(<<"value">>, User) end, Users), DataJObj, Templates);
{'error', _Reason} = E ->
lager:info("failed to load users from: ~p", [AccountDb]),
E
case wh_json:get_value(<<"user_type">>, DataJObj) of
<<UserId:32/binary>> ->
{'ok', UserJObj} = kzd_user:fetch(UserId, AccountId),
send_update_to_user(UserJObj, DataJObj);
_ ->
AccountDb = wh_util:format_account_id(AccountId, 'encoded'),
{'ok', Users} = couch_mgr:get_results(AccountDb, ?ACC_USERS_LIST, []),
select_users_to_update(lists:map(fun(User) -> wh_json:get_value(<<"value">>, User) end, Users), DataJObj)
end.

select_users_to_update(Users, DataJObj, Templates) ->
-spec select_users_to_update(wh_proplist(), wh_json:object()) -> wh_proplist().
select_users_to_update(Users, DataJObj) ->
case wh_json:get_value(<<"user_type">>, DataJObj) of
<<UserId:32/binary>> ->
[send_update_to_user(User, DataJObj, Templates) || User <- Users, wh_json:get_value(<<"id">>, User) == UserId];
<<"all_users">> ->
[send_update_to_user(User, DataJObj, Templates) || User <- Users];
[send_update_to_user(User, DataJObj) || User <- Users];
_ ->
[send_update_to_user(User, DataJObj, Templates) || User <- Users, wh_json:get_value(<<"priv_level">>, User) == <<"admin">>]
[send_update_to_user(User, DataJObj) || User <- Users, wh_json:get_value(<<"priv_level">>, User) == <<"admin">>]
end.

send_update_to_user(UserJObj, DataJObj, Templates) ->
-spec send_update_to_user(wh_json:object(), wh_json:object()) -> 'ok'.
send_update_to_user(UserJObj, DataJObj) ->
Macros = [{<<"system">>, teletype_util:system_params()}
,{<<"system">>, teletype_util:account_params(DataJObj)}
,{<<"account">>, teletype_util:account_params(DataJObj)}
| build_macro_data(UserJObj)
],
RenderedTemplates = [{ContentType, teletype_util:render(?TEMPLATE_ID, Template, Macros)}
|| {ContentType, Template} <- Templates
],
{'ok', TemplateMetaJObj} = teletype_templates:fetch_meta(?TEMPLATE_ID, teletype_util:find_account_id(DataJObj)),

RenderedTemplates = teletype_templates:render(?TEMPLATE_ID, Macros, DataJObj),
{'ok', TemplateMetaJObj} = teletype_templates:fetch_notification(?TEMPLATE_ID, teletype_util:find_account_id(DataJObj)),

Subject = teletype_util:render_subject(
wh_json:find(<<"subject">>, [DataJObj, TemplateMetaJObj])
,Macros
Expand Down
8 changes: 8 additions & 0 deletions core/kazoo_documents/src/kzd_user.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
,enable/1, disable/1
,type/0
,devices/1
,fetch/2
]).

-include("kz_documents.hrl").
Expand Down Expand Up @@ -254,3 +255,10 @@ devices(UserJObj) ->
lager:warning("unable to find documents owned by ~s: ~p", [UserId, _R]),
[]
end.

-spec fetch(ne_binary(), api_binary()) -> {'ok', doc()} | {'error', any()}.
fetch(<<_/binary>> = UserId, <<_/binary>> = Account) ->
AccountDb = wh_util:format_account_id(Account, 'encoded'),
couch_mgr:open_cache_doc(AccountDb, UserId);

fetch(_, _) -> {'error', 'invalid_parametres'}.
6 changes: 0 additions & 6 deletions core/whistle/src/api/wapi_notifications.erl
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,6 @@
-define(CUSTOMER_UPDATE_HEADERS, [<<"Account-ID">>]).
-define(OPTIONAL_CUSTOMER_UPDATE_HEADERS, [<<"Recipient-ID">>
,<<"User-Type">>
,<<"Subject">>
,<<"From">>
,<<"Reply-To">>
,<<"To">>,<<"CC">>,<<"BCC">>
,<<"HTML">>
,<<"Plain">>
| ?DEFAULT_OPTIONAL_HEADERS
]).
-define(CUSTOMER_UPDATE_VALUES, [{<<"Event-Category">>, <<"notification">>}
Expand Down

0 comments on commit 0385750

Please sign in to comment.