Skip to content

Commit

Permalink
[4.3] KZOO-59: calculate account CID name (#6413)
Browse files Browse the repository at this point in the history
* [4.3] KZOO-59: calculate account CID name

Prior, the caller's CID name would be returned from
kz_attributes:get_account_external_cid/1 along with the calculated
account CID number. Particularly for DISA callers, this meant their
CID name was passed through (often their mobile number) instead of
being set to the account's external CID name.

This change calculates the CID name and number of the account and
returns them both if configured.

* catch if fixturedb isn't running
  • Loading branch information
jamesaimonetti authored Mar 27, 2020
1 parent 31919b8 commit 9fa0753
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 53 deletions.
3 changes: 3 additions & 0 deletions core/kazoo_endpoint/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ kze-test:
$(MAKE) compile-test -C $(ROOT)/core/kazoo_sip/
$(MAKE) compile-test -C $(ROOT)/core/kazoo_stdlib/
$(MAKE) compile-test -C $(ROOT)/core/kazoo_schemas/
$(MAKE) compile-test -C $(ROOT)/core/kazoo_call/
$(MAKE) compile-test -C $(ROOT)/core/kazoo_data/
$(MAKE) compile-test -C $(ROOT)/core/kazoo_fixturedb/

include $(ROOT)/make/kz.mk
65 changes: 40 additions & 25 deletions core/kazoo_endpoint/src/kz_attributes.erl
Original file line number Diff line number Diff line change
Expand Up @@ -256,48 +256,61 @@ get_account_external_cid(Call) ->
-spec maybe_get_account_cid(kz_term:ne_binary(), kz_term:ne_binary(), kapps_call:call()) -> cid().
maybe_get_account_cid(Number, Name, Call) ->
case kzd_accounts:fetch(kapps_call:account_id(Call)) of
{'error', _} -> maybe_get_assigned_number(Number, Name, Call);
{'ok', JObj} -> maybe_get_account_external_number(Number, Name, JObj, Call)
{'error', _E} ->
?LOG_INFO("failed to open ~s: ~p", [kapps_call:account_id(Call), _E]),
maybe_get_assigned_number(Number, Name, Call);
{'ok', AccountDoc} -> maybe_get_account_external_cid(Number, Name, Call, AccountDoc)
end.

-spec maybe_get_account_external_number(kz_term:ne_binary(), kz_term:ne_binary(), kz_json:object(), kapps_call:call()) -> cid().
maybe_get_account_external_number(Number, Name, Account, Call) ->
External = kz_json:get_ne_value([<<"caller_id">>, <<"external">>, <<"number">>], Account),
case is_valid_caller_id(External, Call) of
-spec maybe_get_account_external_cid(kz_term:ne_binary(), kz_term:ne_binary(), kapps_call:call(), kzd_accounts:doc()) -> cid().
maybe_get_account_external_cid(Number, Name, Call, AccountDoc) ->
ExternalNumber = kz_json:get_ne_value([<<"caller_id">>, <<"external">>, <<"number">>], AccountDoc),
ExternalName = kz_json:get_ne_value([<<"caller_id">>, <<"external">>, <<"name">>], AccountDoc, Name),

case is_valid_caller_id(ExternalNumber, Call) of
'true' ->
lager:info("determined valid account external caller id is <~s> ~s", [Name, External]),
{External, Name};
?LOG_INFO("determined valid account external caller id is <~s> ~s", [ExternalName, ExternalNumber]),
{ExternalNumber, ExternalName};
'false' ->
maybe_get_account_default_number(Number, Name, Account, Call)
?LOG_DEBUG("external number ~s not valid, trying default", [ExternalNumber]),
maybe_get_account_default_number(Number, ExternalName, Call, AccountDoc)
end.

-spec maybe_get_account_default_number(kz_term:ne_binary(), kz_term:ne_binary(), kz_json:object(), kapps_call:call()) -> cid().
maybe_get_account_default_number(Number, Name, Account, Call) ->
Default = kz_json:get_ne_value([<<"caller_id">>, <<"default">>, <<"number">>], Account),
case is_valid_caller_id(Default, Call) of
-spec maybe_get_account_default_number(kz_term:ne_binary(), kz_term:ne_binary(), kapps_call:call(), kzd_accounts:doc()) -> cid().
maybe_get_account_default_number(Number, Name, Call, AccountDoc) ->
DefaultNumber = kz_json:get_ne_value([<<"caller_id">>, <<"default">>, <<"number">>], AccountDoc),
DefaultName = kz_json:get_ne_value([<<"caller_id">>, <<"default">>, <<"name">>], AccountDoc, Name),

case is_valid_caller_id(DefaultNumber, Call) of
'true' ->
lager:info("determined valid account default caller id is <~s> ~s", [Name, Default]),
{Default, Name};
?LOG_INFO("determined valid account default caller id is <~s> ~s", [DefaultName, DefaultNumber]),
{DefaultNumber, DefaultName};
'false' ->
?LOG_DEBUG("default number ~s not valid, trying assigned numbers", [DefaultNumber]),
maybe_get_assigned_number(Number, Name, Call)
end.

-spec maybe_get_assigned_number(kz_term:api_ne_binary(), kz_term:api_ne_binary(), kz_term:api_ne_binary()|kapps_call:call()) -> cid().
maybe_get_assigned_number(CandidateNumber, Name, ?MATCH_ACCOUNT_ENCODED(_)=AccountDb) ->
AccountId = kz_util:format_account_id(AccountDb),
case knm_numbers:account_listing(AccountDb) of
[_|_] = NumbersList ->
Numbers = [Num
|| {Num,JObj} <- NumbersList,
kz_json:get_value(<<"state">>, JObj) =:= ?NUMBER_STATE_IN_SERVICE
|| {Num, JObj} <- NumbersList,
kz_json:get_ne_binary_value(<<"state">>, JObj) =:= ?NUMBER_STATE_IN_SERVICE
],
case lists:member(CandidateNumber, Numbers) of
'true' -> {CandidateNumber, Name};
'false' -> maybe_get_assigned_numbers(AccountId, Numbers, Name)
'true' ->
?LOG_DEBUG("using assigned number <~s> ~s", [Name, CandidateNumber]),
{CandidateNumber, Name};
'false' ->
AccountId = kz_util:format_account_id(AccountDb),
maybe_get_assigned_numbers(AccountId, Numbers, Name)
end;
_ ->
_Else ->
?LOG_DEBUG("failed to list account numbers: ~p", [_Else]),
AccountId = kz_util:format_account_id(AccountDb),
Number = default_cid_number(AccountId),
lager:warning("no numbers available, proceed with <~s> ~s", [Name, Number]),
?LOG_WARNING("no numbers available, proceed with <~s> ~s", [Name, Number]),
{Number, Name}
end;
maybe_get_assigned_number(CandidateNumber, Name, Call) ->
Expand All @@ -307,12 +320,12 @@ maybe_get_assigned_number(CandidateNumber, Name, Call) ->
-spec maybe_get_assigned_numbers(kz_term:ne_binary(), kz_term:ne_binaries(), kz_term:ne_binary()) -> cid().
maybe_get_assigned_numbers(AccountId, [], Name) ->
Number = default_cid_number(AccountId),
lager:info("failed to find any in-service numbers, using default <~s> ~s", [Name, Number]),
?LOG_INFO("failed to find any in-service numbers, using default <~s> ~s", [Name, Number]),
{Number, Name};
maybe_get_assigned_numbers(_AccountId, [Number|_], Name) ->
%% This could optionally cycle all found numbers and ensure they valid
%% but that could be a lot of wasted db lookups...
lager:info("using first assigned number caller id <~s> ~s", [Name, Number]),
?LOG_INFO("using first assigned number caller id <~s> ~s", [Name, Number]),
{Number, Name}.

-spec is_valid_caller_id(kz_term:api_binary(), kapps_call:call()) -> boolean().
Expand All @@ -321,7 +334,9 @@ is_valid_caller_id(Number, Call) ->
AccountId = kapps_call:account_id(Call),
case knm_number:lookup_account(Number) of
{'ok', AccountId, _} -> 'true';
_Else -> 'false'
_Else ->
?LOG_DEBUG("failed to find ~s in account ~s: ~p", [Number, AccountId, _Else]),
'false'
end.

-spec maybe_get_presence_number(kz_json:object(), kapps_call:call()) -> kz_term:api_binary().
Expand Down
43 changes: 22 additions & 21 deletions core/kazoo_endpoint/test/kz_attributes_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,24 @@
,fun(C) -> kapps_call:set_authorizing_id(<<"trunkstore0000000000000000000002">>, C) end
]).

-define(DISA_CID_CALL, [fun(C) -> kapps_call:set_account_id(?FIXTURE_PARENT_ACCOUNT_ID, C) end
,fun(C) -> kapps_call:set_caller_id_name(<<"disa">>, C) end
,fun(C) -> kapps_call:set_caller_id_number(<<"+12225552600">>, C) end
]).

kz_attributes_test_() ->
{setup
,fun setup_db/0
,fun terminate_db/1
{'setup'
,fun kzd_test_fixtures:setup/0
,fun kzd_test_fixtures:cleanup/1
,fun(_ReturnOfSetup) ->
[test_get_flags_callflow()
,test_get_flags_trunkstore()
,test_process_dynamic_flags()
,test_account_cid()
]
end
}.

setup_db() ->
?LOG_DEBUG(":: Starting Kazoo FixtureDB"),
{ok, _} = application:ensure_all_started(kazoo_config),
kazoo_fixturedb:start().

terminate_db(Pid) ->
_DataLink = erlang:exit(Pid, normal),
Ref = monitor(process, Pid),
receive
{'DOWN', Ref, process, Pid, _Reason} ->
_KConfig = application:stop(kazoo_config),
?LOG_DEBUG(":: Stopped Kazoo FixtureDB, data_link: ~p kazoo_config: ~p", [_DataLink, _KConfig])
after 1000 ->
_KConfig = application:stop(kazoo_config),
?LOG_DEBUG(":: Stopped Kazoo FixtureDB, data_link: timeout kazoo_config: ~p", [_KConfig])
end.


test_get_flags_callflow() ->
Call = kapps_call_tests:create_callflow_call(),
ExpectedOld = [<<"user_old_static_flag">>
Expand Down Expand Up @@ -115,3 +103,16 @@ test_process_dynamic_flags() ->
,?_assertEqual([<<"local">>, <<"static">>], kz_attributes:process_dynamic_flags([<<"zone">>], [<<"static">>], Call))
}
].

test_account_cid() ->
Call = kapps_call_tests:create_callflow_call(),
DISACall = kapps_call:exec(?DISA_CID_CALL, Call),
{CIDNumber, CIDName} = kz_attributes:get_account_external_cid(DISACall),

[{"account external caller id number chosen"
,?_assertEqual(<<"+19995552600">>, CIDNumber)
}
,{"account external caller id name chosen"
,?_assertEqual(<<"account-external-name">>, CIDName)
}
].
1 change: 0 additions & 1 deletion core/kazoo_endpoint/test/kz_endpoint_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ device_undefined_user_undefined_account_on() ->
DeviceDoc = kzd_devices:new(),

Merged = kz_endpoint:merge_attribute(<<"call_recording">>, AccountDoc, DeviceDoc, UserDoc),
?debugFmt("merged: ~p~n", [Merged]),

check_expectations(?FUNCTION_NAME, Merged, Expectations).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
},
"external": {
"name": "account-external-name",
"number": "account-external-number"
"number": "+19995552600"
},
"internal": {
"name": "account-internal-name",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"id": "+19995552600",
"key": "+19995552600",
"value": {
"assigned_to": "account0000000000000000000000003"
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"_id": "+19995552600",
"pvt_assigned_to": "account0000000000000000000000003",
"pvt_created": 63752475360,
"pvt_modified": 63752475360,
"pvt_state": "in_service"
}
1 change: 1 addition & 0 deletions core/kazoo_number_manager/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ compile-test: compile-also
compile-also:
$(MAKE) compile-test -C $(ROOT)/core/kazoo_config/
$(MAKE) compile-test -C $(ROOT)/core/kazoo_apps/
$(MAKE) compile-test -C $(ROOT)/core/kazoo_data/

include $(ROOT)/make/kz.mk

Expand Down
6 changes: 4 additions & 2 deletions core/kazoo_number_manager/src/knm_number.erl
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,10 @@ lookup_account(Num) ->

fetch_account_from_number(Num) ->
case knm_phone_number:fetch(Num) of
{'ok', PN} -> check_number(PN);
{'error', _}=Error -> maybe_fetch_account_from_ports(Num, Error)
{'ok', PN} ->
check_number(PN);
{'error', _}=Error ->
maybe_fetch_account_from_ports(Num, Error)
end.

check_number(PN) ->
Expand Down
11 changes: 8 additions & 3 deletions core/kazoo_number_manager/src/knm_phone_number.erl
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ from_number_with_options(DID, Options) ->
-spec fetch(kz_term:ne_binary() | knm_numbers:collection()) ->
knm_phone_number_return() |
knm_numbers:collection().
fetch(?NE_BINARY=Num) ->
fetch(<<Num/binary>>) ->
fetch(Num, knm_number_options:default());
fetch(T0=#{todo := Nums, options := Options}) ->
Pairs = group_by_db(lists:usort(knm_converters:normalize(Nums))),
Expand Down Expand Up @@ -466,8 +466,13 @@ test_fetch(?TEST_PORT_IN_NUM) ->
test_fetch(?TEST_PORT_IN2_NUM) ->
{'ok', ?PORT_IN2_NUMBER};
test_fetch(?TEST_PORT_IN3_NUM) -> {'ok', ?PORT_IN3_NUMBER};
test_fetch(_DID=?NE_BINARY) ->
{'error', 'not_found'}.
test_fetch(<<DID/binary>>) ->
NormalizedNum = knm_converters:normalize(DID),
NumberDb = knm_converters:to_db(NormalizedNum),
try kz_datamgr:open_cache_doc(NumberDb, NormalizedNum)
catch _:_ -> {'error', 'not_found'}
end.

-else.

-spec fetch(kz_term:ne_binary(), knm_number_options:options()) -> knm_phone_number_return().
Expand Down

0 comments on commit 9fa0753

Please sign in to comment.