Skip to content

Commit

Permalink
KAZOO-4539: Customers update over teletype
Browse files Browse the repository at this point in the history
  • Loading branch information
onnet committed Feb 21, 2016
1 parent cb7d61c commit a506b3f
Show file tree
Hide file tree
Showing 5 changed files with 382 additions and 7 deletions.
96 changes: 96 additions & 0 deletions applications/crossbar/doc/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,99 @@ All accounts will continue to be processed by the `notify` app until the Crossba
```
curl -H 'X-Auth-Token: {AUTH_TOKEN}' -H 'Content-Type: text/plain' http://server:8000/v2/accounts/{ACCOUNT_ID}/notifications/smtplog
```

## Customer update

* POST - Send a message to all reseller's children or to a particular account.

Send message to all reseller's accounts:
```
curl -X POST -H 'X-Auth-Token: {AUTH_TOKEN}' -H 'Content-Type: text/plain' http://server:8000/v2/notifications/customer_update/send_message -d '{}'
curl -X POST -H 'X-Auth-Token: {AUTH_TOKEN}' -H 'Content-Type: text/plain' http://server:8000/v2/accounts/{SENDER(RESELLER)_ACCOUNT_ID}/notifications/customer_update/send_message -d '{}'
```

Send message to a particular acount:
```
curl -X POST -H 'X-Auth-Token: {AUTH_TOKEN}' -H 'Content-Type: text/plain' http://server:8000/v2/notifications/customer_update/send_message/{RECIPIENT_ACCOUNT_ID} -d '{}'
curl -X POST -H 'X-Auth-Token: {AUTH_TOKEN}' -H 'Content-Type: text/plain' http://server:8000/v2/accounts/{SENDER(RESELLER)_ACCOUNT_ID}/notifications/customer_update/send_message/{RECIPIENT_ACCOUNT_ID} -d '{}'
```

You can send a message to all users, admins only or a particular user within an account:

All users:
```
"user_type": "all_users"
````
Particular user:
```
"user_type": "{ACCOUNT_ID}"
````
Admin privileged users only. Default. Could be omitted:
```
"user_type": "admins_only"
````

You can send a message with changed subject, html and plain text templates by providing full notification document payload:

```
{
"data": {
"user_type": "3d9b564d5c95d52d81a2e49ea0c57941"
"id": "customer_update",
"account_overridden": true,
"enabled": true,
"category": "user",
"friendly_name": "Customer update",
"from": "[email protected]",
"subject": "Test Reseller customer update",
"bcc": {
"email_addresses": [],
"type": ""
},
"cc": {
"email_addresses": [],
"type": ""
},
"macros": {
"user.email": {
"description": "Email of the user",
"friendly_name": "Email",
"i18n_label": "user_email"
},
"user.first_name": {
"description": "First Name",
"friendly_name": "First Name",
"i18n_label": "first_name"
},
"user.last_name": {
"description": "Last Name",
"friendly_name": "Last Name",
"i18n_label": "last_name"
},
"user.timezone": {
"description": "Timezone of the user",
"friendly_name": "Timezone",
"i18n_label": "user_timezone"
},
"user.username": {
"description": "Username",
"friendly_name": "Username",
"i18n_label": "username"
}
},
"template_charset": "utf-8",
"html": "PHA+RGVhciB7e3VzZXIuZmlyc3RfbmFtZX19IHt7dXNlci5sYXN0X25hbWV9fS48L3A+CjxwPkhlcmUgYXJlIHNvbWUgbmV3cyB0aGF0IHdlIGhhdmUgc2VsZWN0ZWQgZm9yIHlvdTwvcD4KPHA+QmVzdCByZWdhcmRzLDwvcD4KPHA+T25OZXQgSW5ub3ZhdGlvbnMgTGltaXRlZC48L3A+",
"plain": "Dear {{user.first_name}} {{user.last_name}}.\n\nHere are some more news that we have selected for you.\n\nBest regards,\nOnNet Innovations Limited.",
"templates": {
"text/html": {
"length": 161
},
"text/plain": {
"length": 136
}
},
}
}
```
79 changes: 72 additions & 7 deletions applications/crossbar/src/modules/cb_notifications.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
-module(cb_notifications).

-export([init/0
,allowed_methods/0, allowed_methods/1, allowed_methods/2
,resource_exists/0, resource_exists/1, resource_exists/2
,allowed_methods/0, allowed_methods/1, allowed_methods/2, allowed_methods/3
,resource_exists/0, resource_exists/1, resource_exists/2, resource_exists/3
,content_types_provided/2
,content_types_accepted/2
,validate/1, validate/2, validate/3
,validate/1, validate/2, validate/3, validate/4
,put/1
,post/2, post/3
,post/2, post/3, post/4
,delete/2

,flush/0
Expand All @@ -34,6 +34,8 @@
-define(CB_LIST, <<"notifications/crossbar_listing">>).
-define(PREVIEW, <<"preview">>).
-define(SMTP_LOG, <<"smtplog">>).
-define(CUSTOMER_UPDATE, <<"customer_update">>).
-define(SEND_MESSAGE, <<"send_message">>).
-define(CB_LIST_SMTP_LOG, <<"notifications/smtp_log">>).

-define(MACROS, <<"macros">>).
Expand Down Expand Up @@ -86,7 +88,12 @@ allowed_methods(_) ->
allowed_methods(_, ?PREVIEW) ->
[?HTTP_POST];
allowed_methods(?SMTP_LOG, _Id) ->
[?HTTP_GET].
[?HTTP_GET];
allowed_methods(?CUSTOMER_UPDATE, ?SEND_MESSAGE) ->
[?HTTP_POST].

allowed_methods(?CUSTOMER_UPDATE, ?SEND_MESSAGE, _Id) ->
[?HTTP_POST].

%%--------------------------------------------------------------------
%% @public
Expand All @@ -107,7 +114,10 @@ resource_exists(?SMTP_LOG) -> 'true';
resource_exists(_Id) -> 'true'.

resource_exists(_Id, ?PREVIEW) -> 'true';
resource_exists(?SMTP_LOG, _Id) -> 'true'.
resource_exists(?SMTP_LOG, _Id) -> 'true';
resource_exists(?CUSTOMER_UPDATE, ?SEND_MESSAGE) -> 'true'.

resource_exists(?CUSTOMER_UPDATE, ?SEND_MESSAGE, _Id) -> 'true'.

%%--------------------------------------------------------------------
%% @private
Expand Down Expand Up @@ -216,7 +226,12 @@ validate(Context, Id, ?PREVIEW) ->
DbId = kz_notification:db_id(Id),
update_notification(maybe_update_db(Context), DbId);
validate(Context, ?SMTP_LOG, Id) ->
load_smtp_log_doc(Id, Context).
load_smtp_log_doc(Id, Context);
validate(Context, ?CUSTOMER_UPDATE, ?SEND_MESSAGE) ->
cb_context:set_resp_status(Context, 'success').

validate(Context, ?CUSTOMER_UPDATE, ?SEND_MESSAGE, _Id) ->
cb_context:set_resp_status(Context, 'success').

-spec validate_notifications(cb_context:context(), http_method()) -> cb_context:context().
-spec validate_notification(cb_context:context(), path_token(), http_method()) ->
Expand Down Expand Up @@ -278,6 +293,7 @@ put(Context) ->
%%--------------------------------------------------------------------
-spec post(cb_context:context(), path_token()) -> cb_context:context().
-spec post(cb_context:context(), path_token(), path_token()) -> cb_context:context().
-spec post(cb_context:context(), path_token(), path_token(), path_token()) -> cb_context:context().
post(Context, Id) ->
case cb_context:req_files(Context) of
[] ->
Expand Down Expand Up @@ -313,6 +329,22 @@ set_system_macros(Context) ->
Context
end.


post(Context, ?CUSTOMER_UPDATE, ?SEND_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
)
of
{'ok', _Resp} ->
lager:debug("published customer_update notification");
{'error', _E} ->
lager:debug("failed to publish_customer update notification: ~p", [_E])
end,
Context;

post(Context, Id, ?PREVIEW) ->
Notification = cb_context:doc(Context),
Preview = build_preview_payload(Context, Notification),
Expand All @@ -334,6 +366,39 @@ post(Context, Id, ?PREVIEW) ->
crossbar_util:response('error', <<"Failed to process notification preview">>, Context)
end.

post(Context, ?CUSTOMER_UPDATE, ?SEND_MESSAGE, Id) ->
case
whapps_util:amqp_pool_request(
[{<<"Recipient-ID">>, Id}] ++ 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");
{'error', _E} ->
lager:debug("failed to publish_customer update notification: ~p", [_E])
end,
Context.

build_customer_update_payload(Context) ->
SenderId = case cb_context:account_id(Context) of
'undefined' -> cb_context:auth_account_id(Context);
AccountId -> AccountId
end,
[{<<"Account-ID">>, SenderId}
,{<<"User-Type">>, cb_context:req_value(Context, <<"user_type">>)}
,{<<"Subject">>, cb_context:req_value(Context, <<"subject">>)}
,{<<"From">>, cb_context:req_value(Context, <<"from">>)}
,{<<"Reply-To">>, cb_context:req_value(Context, <<"reply_to">>)}
,{<<"To">>, cb_context:req_value(Context, <<"to">>)}
,{<<"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">>)}
| wh_api:default_headers(?APP_NAME, ?APP_VERSION)
].

-spec build_preview_payload(cb_context:context(), wh_json:object()) -> wh_proplist().
build_preview_payload(Context, Notification) ->
props:filter_empty(
Expand Down
4 changes: 4 additions & 0 deletions applications/teletype/src/teletype_shared_listener.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
,{'teletype_template_skel'
,[{<<"notification">>, <<"skel">>}]
}
,{{'teletype_customer_update', 'handle_req'}
,[{<<"notification">>, <<"customer_update">>}]
}
,{{'teletype_deregister', 'handle_deregister'}
,[{<<"notification">>, <<"deregister">>}]
}
Expand Down Expand Up @@ -130,6 +133,7 @@
,'transaction'
,'voicemail_full'
,'webhook_disabled'
,'customer_update'
%%,'skel'
]).

Expand Down
Loading

0 comments on commit a506b3f

Please sign in to comment.