Skip to content

Commit

Permalink
Number services for im (2600hz#6291)
Browse files Browse the repository at this point in the history
  • Loading branch information
lazedo authored and jamesaimonetti committed Feb 12, 2020
1 parent 7fe240b commit 5becca2
Show file tree
Hide file tree
Showing 45 changed files with 1,438 additions and 207 deletions.
77 changes: 77 additions & 0 deletions applications/crossbar/priv/api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -17332,6 +17332,9 @@
"Body": {
"type": "string"
},
"Charges": {
"type": "string"
},
"Custom-SIP-Headers": {
"type": "object"
},
Expand Down Expand Up @@ -19544,6 +19547,80 @@
],
"type": "object"
},
"kapi.notifications.number_feature_manual_action": {
"description": "AMQP API for notifications.number_feature_manual_action",
"properties": {
"Account-DB": {
"type": "string"
},
"Account-ID": {
"minLength": 1,
"type": "string"
},
"Attachment-URL": {
"type": "string"
},
"Bcc": {
"type": "string"
},
"Cc": {
"type": "string"
},
"Event-Category": {
"enum": [
"notification"
],
"type": "string"
},
"Event-Name": {
"enum": [
"number_feature_manual_action"
],
"type": "string"
},
"Feature": {
"type": "object"
},
"From": {
"type": "string"
},
"HTML": {
"type": "string"
},
"Number": {
"minLength": 1,
"type": "string"
},
"Preview": {
"type": "boolean"
},
"Reply-To": {
"type": "string"
},
"Subject": {
"type": "string"
},
"Text": {
"type": "string"
},
"To": {
"items": {
"type": "string"
},
"type": [
"string",
"array"
]
}
},
"required": [
"Account-ID",
"Feature",
"Number",
"To"
],
"type": "object"
},
"kapi.notifications.outbound_fax": {
"description": "AMQP API for notifications.outbound_fax",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"Body": {
"type": "string"
},
"Charges": {
"type": "string"
},
"Custom-SIP-Headers": {
"type": "object"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"_id": "kapi.notifications.number_feature_manual_action",
"description": "AMQP API for notifications.number_feature_manual_action",
"properties": {
"Account-DB": {
"type": "string"
},
"Account-ID": {
"minLength": 1,
"type": "string"
},
"Attachment-URL": {
"type": "string"
},
"Bcc": {
"type": "string"
},
"Cc": {
"type": "string"
},
"Event-Category": {
"enum": [
"notification"
],
"type": "string"
},
"Event-Name": {
"enum": [
"number_feature_manual_action"
],
"type": "string"
},
"Feature": {
"type": "object"
},
"From": {
"type": "string"
},
"HTML": {
"type": "string"
},
"Number": {
"minLength": 1,
"type": "string"
},
"Preview": {
"type": "boolean"
},
"Reply-To": {
"type": "string"
},
"Subject": {
"type": "string"
},
"Text": {
"type": "string"
},
"To": {
"items": {
"type": "string"
},
"type": [
"string",
"array"
]
}
},
"required": [
"Account-ID",
"Feature",
"Number",
"To"
],
"type": "object"
}
47 changes: 41 additions & 6 deletions applications/crossbar/src/modules/cb_mms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,18 @@ validate_mms(Context, Id, ?HTTP_DELETE) ->
-spec put(cb_context:context()) -> cb_context:context().
put(Context) ->
Payload = cb_context:doc(Context),
_ = case kz_im:route_type(Payload) of
<<"onnet">> -> kz_amqp_worker:cast(Payload, fun kapi_im:publish_inbound/1);
<<"offnet">> -> kz_amqp_worker:cast(Payload, fun kapi_im:publish_outbound/1)
end,
crossbar_util:response(kz_im:set_body(kz_api:remove_defaults(Payload), 'null'), Context).
Ctx = case kz_im:route_type(Payload) of
<<"onnet">> ->
kz_amqp_worker:cast(Payload, fun kapi_im:publish_inbound/1),
Context;
<<"offnet">> ->
kz_amqp_worker:cast(Payload, fun kapi_im:publish_outbound/1),
AccountId = cb_context:account_id(Context),
Rate = kz_services_im:flat_rate(AccountId, 'mms', 'outbound'),
Charges = kz_json:from_list([{<<"charges">>, Rate}]),
cb_context:set_resp_envelope(Context, kz_json:merge(cb_context:resp_envelope(Context), Charges))
end,
crossbar_util:response(kz_json:normalize(kz_im:set_body(kz_api:remove_defaults(Payload), 'null')), Ctx).

%%------------------------------------------------------------------------------
%% @doc If the HTTP verb is DELETE, execute the actual action, usually a db delete
Expand Down Expand Up @@ -162,8 +169,10 @@ on_successful_validation(Context) ->
,fun body_is_mime_encoded/1
,fun body_from_files/1
,fun account_is_enabled/1
,fun account_is_in_good_standing/1
,fun account_has_mms_enabled/1
,fun account_is_in_good_standing/1
,fun reseller_has_mms_enabled/1
,fun reseller_is_in_good_standing/1
,fun create_request/1
,fun validate_from/1
],
Expand Down Expand Up @@ -312,6 +321,32 @@ account_has_mms_enabled(Context) ->
'false' -> cb_context:add_system_error('account', <<"mms services not enabled for account">>, Context)
end.

-spec reseller_is_in_good_standing(cb_context:context()) -> cb_context:context().
reseller_is_in_good_standing(Context) ->
case kz_services_standing:acceptable(cb_context:reseller_id(Context)) of
{'true', _} -> Context;
{'false', #{message := Msg}} ->
lager:warning("reseller ~s for account ~s is not in good standing => ~p"
,[cb_context:reseller_id(Context)
,cb_context:account_id(Context)
,Msg
]
),
cb_context:add_system_error('account', <<"service temporarily unavailable">>, Context)
end.

-spec reseller_has_mms_enabled(cb_context:context()) -> cb_context:context().
reseller_has_mms_enabled(Context) ->
case kz_services_im:is_sms_enabled(cb_context:reseller_id(Context)) of
'true' -> Context;
'false' ->
lager:warning("mms services not enabled for reseller ~s of account ~s"
,[cb_context:reseller_id(Context)
,cb_context:account_id(Context)
]
),
cb_context:add_system_error('account', <<"service temporarily unavailable">>, Context)
end.

-spec validate_from(cb_context:context()) -> cb_context:context().
validate_from(Context) ->
Expand Down
11 changes: 9 additions & 2 deletions applications/crossbar/src/modules/cb_notifications.erl
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,11 @@ maybe_add_extra_data(<<"port_", _/binary>>, API) ->
props:set_value(<<"Reason">>, kz_json:new(), API);
maybe_add_extra_data(<<"ported">>, API) ->
props:set_value(<<"Reason">>, kz_json:new(), API);
maybe_add_extra_data(<<"number_feature_manual_action">>, API) ->
props:set_values([{<<"Number">>, <<"+15557770104">>}
,{<<"Feature">>, kz_json:new()}
]
, API);
maybe_add_extra_data(_Id, API) -> API.

-spec publish_fun(kz_term:ne_binary()) -> fun((kz_term:api_terms()) -> 'ok').
Expand Down Expand Up @@ -609,6 +614,8 @@ publish_fun(<<"voicemail_to_email">>) ->
fun kapi_notifications:publish_voicemail_new/1;
publish_fun(<<"webhook_disabled">>) ->
fun kapi_notifications:publish_webhook_disabled/1;
publish_fun(<<"number_feature_manual_action">>) ->
fun kapi_notifications:publish_number_feature_manual_action/1;
publish_fun(_Id) ->
lager:debug("no kapi_notifications:publish_~s/1 defined", [_Id]),
fun(_Any) -> 'ok' end.
Expand Down Expand Up @@ -740,9 +747,9 @@ maybe_read(Context, Id, _Acceptable, []) ->
lager:debug("no accept headers, using json"),
read(Context, Id).

-spec is_acceptable_accept(kz_term:proplist(), kz_term:ne_binary(), kz_term:ne_binary()) -> boolean().
-spec is_acceptable_accept(cowboy_content_types(), kz_term:ne_binary(), kz_term:ne_binary()) -> boolean().
is_acceptable_accept(Acceptable, Type, SubType) ->
lists:member({Type,SubType}, Acceptable).
api_util:content_type_matches({Type,SubType,'*'}, Acceptable).

-type load_from() :: 'system' | 'account' | 'system_migrate'.

Expand Down
38 changes: 12 additions & 26 deletions applications/crossbar/src/modules/cb_phone_numbers.erl
Original file line number Diff line number Diff line change
Expand Up @@ -507,20 +507,20 @@ normalize_port_number(JObj, Num, AuthBy) ->
%%------------------------------------------------------------------------------
-spec summary(cb_context:context()) -> cb_context:context().
summary(Context) ->
Context1 = view_account_phone_numbers(Context),
IsAdmin = knm_phone_number:is_admin(cb_context:auth_account_id(Context)),
ProviderContext = knm_providers:setup_account_context(cb_context:account_id(Context), IsAdmin),
Context1 = view_account_phone_numbers(cb_context:store(Context, 'ctx_num', ProviderContext)),
case cb_context:resp_status(Context1) of
'success' -> maybe_update_locality(Context1);
_Status -> Context1
end.

-spec view_account_phone_numbers(cb_context:context()) -> cb_context:context().
view_account_phone_numbers(Context) ->
Ctx = rename_qs_filters(Context),
Context1 = crossbar_doc:load_view(?CB_LIST, [], Ctx, fun normalize_view_results/2),
Context1 = crossbar_doc:load_view(?CB_LIST, [], rename_qs_filters(Context), fun normalize_view_results/3),
case cb_context:resp_status(Context1) of
'success' ->
IsAdmin = knm_phone_number:is_admin(cb_context:auth_account_id(Context)),
ListOfNumProps = [fix_available(IsAdmin, NumJObj) || NumJObj <- cb_context:resp_data(Context1)],
ListOfNumProps = cb_context:resp_data(Context1),
PortNumberJObj = maybe_add_port_request_numbers(Context),
NumbersJObj = lists:foldl(fun kz_json:merge_jobjs/2, PortNumberJObj, ListOfNumProps),
Services = kz_services:fetch(cb_context:account_id(Context)),
Expand All @@ -533,20 +533,6 @@ view_account_phone_numbers(Context) ->
Context1
end.

-spec fix_available(boolean(), kz_json:object()) -> kz_json:object().
fix_available(IsAdmin, NumJObj) ->
[{Num, JObj}] = kz_json:to_proplist(NumJObj),
IsLocal = lists:member(?FEATURE_LOCAL, kz_json:get_list_value(<<"features">>, JObj, [])),
Allowed = knm_providers:available_features(IsLocal
,IsAdmin
,kz_json:get_ne_binary_value(<<"assigned_to">>, JObj)
,kz_json:get_ne_binary_value(<<"used_by">>, JObj)
,kz_json:get_list_value(<<"features_allowed">>, JObj, [])
,kz_json:get_list_value(<<"features_denied">>, JObj, [])
),
NewJObj = kz_json:set_value(<<"features_available">>, Allowed, JObj),
kz_json:from_list([{Num, NewJObj}]).

-spec should_include_ports(cb_context:context()) -> boolean().
should_include_ports(Context) ->
kz_term:is_true(cb_context:req_value(Context, <<"include_ports">>, 'true')).
Expand Down Expand Up @@ -584,14 +570,14 @@ rename_qs_filters(Context) ->
NewQS = kz_json:map(Renamer, cb_context:query_string(Context)),
cb_context:set_query_string(Context, NewQS).

-spec normalize_view_results(kz_json:object(), kz_json:objects()) -> kz_json:objects().
normalize_view_results(JObj, Acc) ->
-spec normalize_view_results(cb_context:context(), kz_json:object(), kz_json:objects()) -> kz_json:objects().
normalize_view_results(Context, JObj, Acc) ->
ProviderContext = cb_context:fetch(Context, 'ctx_num'),
Number = kz_json:get_value(<<"key">>, JObj),
Properties = kz_json:get_value(<<"value">>, JObj),
[kz_json:from_list([{Number, Properties}])
| Acc
].

RowObj = kz_json:get_value(<<"value">>, JObj),
Allowed = knm_providers:available_features(RowObj, ProviderContext),
NewJObj = kz_json:set_value([<<"features_available">>], Allowed, kz_doc:public_fields(RowObj)),
[kz_json:from_list([{Number, NewJObj}]) | Acc].

-spec normalize_port_view_result(kz_json:object()) -> kz_json:object().
normalize_port_view_result(JObj) ->
Expand Down
Loading

0 comments on commit 5becca2

Please sign in to comment.