Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.3] Upstream 4.3 telnyx carrier api issues #6615

Closed
8 changes: 8 additions & 0 deletions core/kazoo_number_manager/include/knm_telnyx.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-ifndef(KNM_TELNYX_HRL).

-define(SEARCH_TYPE_NPA, 1).
-define(SEARCH_TYPE_INTERNATIONAL, 2).
-define(SEARCH_TYPE_TOLEFREE, 3).

-define(KNM_TELNYX_HRL, 'true').
-endif.
57 changes: 41 additions & 16 deletions core/kazoo_number_manager/src/carriers/knm_telnyx.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
-export([check_numbers/1]).

-include("knm.hrl").
-include("knm_telnyx.hrl").

-define(MOD_CONFIG_CAT, <<(?KNM_CONFIG_CAT)/binary, ".telnyx">>).

Expand All @@ -26,6 +27,9 @@
-define(IS_PROVISIONING_ENABLED
,kapps_config:get_is_true(?MOD_CONFIG_CAT, <<"enable_provisioning">>, 'true')
).
-define(SHOULD_KEEP_BEST_EFFORT
,kapps_config:get_is_true(?MOD_CONFIG_CAT, <<"should_keep_best_effort">>, 'false')
).

%%% API

Expand Down Expand Up @@ -65,24 +69,29 @@ check_numbers(_Numbers) -> {'error', 'not_implemented'}.
%%------------------------------------------------------------------------------
-spec find_numbers(kz_term:ne_binary(), pos_integer(), knm_search:options()) ->
{'ok', knm_number:knm_numbers()}.
find_numbers(<<"+1", Prefix:3/binary, _/binary>>, Quantity, Options)
when ?IS_US_TOLLFREE(Prefix) ->
Results = numbers('tollfree', Quantity, Prefix, 'undefined'),
find_numbers(<<"+1", NPA:3/binary, _/binary>>=Num, Quantity, Options)
when ?IS_US_TOLLFREE(NPA) ->
Results = numbers('tollfree', Quantity, NPA, get_nxx(Num)),
{'ok', numbers(Results, Options)};

find_numbers(<<"+1", NPA:3/binary, _/binary>>=Num, Quantity, Options) ->
NXX = case byte_size(Num) >= 2+3+3 of
'true' -> binary:part(Num, 2+3, 3);
'false' -> 'undefined'
end,
Results = numbers('npa', Quantity, NPA, NXX),
Results = numbers('npa', Quantity, NPA, get_nxx(Num)),
{'ok', numbers(Results, Options)};

find_numbers(<<"+",_/binary>>=_InternationalNum, Quantity, Options) ->
Country = knm_search:country(Options),
Results = numbers('region', Quantity, Country, 'undefined'),
{'ok', international_numbers(Results, Options)}.

%%------------------------------------------------------------------------------
%% @doc For a given Number, parse out the NXX value.
%% Return `undefined' if the NXX value can not be parsed.
%% @end
%%------------------------------------------------------------------------------
-spec get_nxx(kz_term:ne_binary()) -> kz_term:api_ne_binary().
get_nxx(<<_PlusOneAreaCode:5/binary, NXX:3/binary, _/binary>>) -> NXX;
get_nxx(_) -> 'undefined'.

%%------------------------------------------------------------------------------
%% @doc Acquire a given number from the carrier
%% @end
Expand Down Expand Up @@ -174,19 +183,35 @@ international_numbers(JObjs, Options) ->
ugly_hack(Dialcode, Num) ->
kz_binary:pad(<<Dialcode/binary, Num/binary>>, 9, <<"0">>).

search_kind('npa') -> 1;
search_kind('region') -> 2;
search_kind('tollfree') -> 3.

search_prefix('tollfree', Prefix, _) ->
[{<<"prefix">>, Prefix}];
-spec search_kind(kind()) -> integer().
search_kind('npa') -> ?SEARCH_TYPE_NPA;
search_kind('region') -> ?SEARCH_TYPE_INTERNATIONAL;
search_kind('tollfree') -> ?SEARCH_TYPE_TOLEFREE.

-spec search_prefix(kind(), kz_term:ne_binary(), kz_term:api_ne_binary()) -> kz_json:json_proplist().
search_prefix('tollfree', NPA, 'undefined') ->
[{<<"npa">>, NPA}
,should_keep_best_effort()
];
search_prefix('tollfree', NPA, NXX) ->
[{<<"nxx">>, NXX}
,should_keep_best_effort()
|search_prefix('tollfree', NPA, 'undefined')
];
search_prefix('region', Country, _) ->
[{<<"country_iso">>, Country}];
[{<<"country_iso">>, Country}
,should_keep_best_effort()
];
search_prefix('npa', NPA, 'undefined') ->
[{<<"npa">>, NPA}];
[{<<"npa">>, NPA}
,should_keep_best_effort()
];
search_prefix('npa', NPA, NXX) ->
[{<<"nxx">>, NXX}
,should_keep_best_effort()
|search_prefix('npa', NPA, 'undefined')
].

-spec should_keep_best_effort() -> {kz_term:ne_binary(), boolean()}.
should_keep_best_effort() -> {<<"best_effort">>, ?SHOULD_KEEP_BEST_EFFORT}.
%%% End of Module
10 changes: 5 additions & 5 deletions core/kazoo_number_manager/src/knm_search.erl
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ first(Options) ->
QID = query_id(Options),
gen_listener:cast(?MODULE, {'reset_search', QID}),
Self = self(),
Opts = [{'quantity', ?MAX_SEARCH}
,{'offset', 0}
,{'normalized_prefix', normalized_prefix(Options)}
| Options
],
Defaults = [{'quantity', ?MAX_SEARCH}
,{'offset', 0}
,{'normalized_prefix', normalized_prefix(Options)}
],
Opts = props:insert_values(Defaults, Options),
lists:foreach(fun(Carrier) -> search_spawn(Self, Carrier, Opts) end, Carriers),
wait_for_search(length(Carriers)),
gen_listener:call(?MODULE, {'first', Options}).
Expand Down
42 changes: 12 additions & 30 deletions core/kazoo_number_manager/src/knm_telnyx_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
-export([req/2, req/3]).

-include("knm.hrl").
-include("knm_telnyx.hrl").

-define(MOD_CONFIG_CAT, <<(?KNM_CONFIG_CAT)/binary, ".telnyx">>).

Expand All @@ -32,10 +33,6 @@
).
-endif.

-define(SHOULD_KEEP_BEST_EFFORT
,kapps_config:get_is_true(?MOD_CONFIG_CAT, <<"should_keep_best_effort">>, 'false')
).

-define(SHOULD_FILTER_RATES
,kapps_config:get_is_true(?MOD_CONFIG_CAT, <<"should_filter_rates">>, 'false')
).
Expand Down Expand Up @@ -66,13 +63,10 @@ req(Method, Path) ->

-spec req(atom(), [nonempty_string()], kz_json:object()) -> kz_json:object().
req('post', ["number_searches"], JObj) ->
case kz_json:get_value([<<"search_descriptor">>, <<"prefix">>], JObj) of
<<"800">> -> rep_fixture("telnyx_tollfree_search.json");
'undefined' ->
case kz_json:get_value([<<"search_descriptor">>, <<"country_iso">>], JObj) of
<<"GB">> -> rep_fixture("telnyx_international_search.json");
'undefined' -> rep_fixture("telnyx_npa_search.json")
end
case kz_json:get_integer_value([<<"search_type">>], JObj) of
?SEARCH_TYPE_NPA -> rep_fixture("telnyx_npa_search.json");
?SEARCH_TYPE_INTERNATIONAL -> rep_fixture("telnyx_international_search.json");
?SEARCH_TYPE_TOLEFREE -> rep_fixture("telnyx_tollfree_search.json")
end;
req('post', ["e911_addresses"], Body) ->
<<"301 MARINA BLVD">> = kz_json:get_value(<<"line_1">>, Body),
Expand Down Expand Up @@ -142,9 +136,7 @@ http_options() ->
rep({'ok', 200=Code, _Headers, <<"{",_/binary>>=Response}) ->
?DEBUG_APPEND("Response:~n~p~n~p~n~s~n", [Code, _Headers, Response]),

Routines = [fun(JObj) -> maybe_remove_best_effort(?SHOULD_KEEP_BEST_EFFORT, JObj) end
,fun(JObj) -> maybe_filter_rates(?SHOULD_FILTER_RATES, JObj) end
],
Routines = [fun maybe_filter_rates/1],

maybe_apply_limit(
lists:foldl(fun(F, J) -> F(J) end, kz_json:decode(Response), Routines)
Expand All @@ -167,22 +159,12 @@ http_code(404) -> 'not_found';
http_code(Code) when Code >= 500 -> 'server_error';
http_code(_Code) -> 'empty_response'.

-spec maybe_remove_best_effort(boolean(), kz_json:object()) -> kz_json:object().
maybe_remove_best_effort('true', JObj) -> JObj;
maybe_remove_best_effort('false', JObj) ->
case kz_json:is_true(<<"any_best_effort">>, JObj) of
'false' -> JObj;
'true' ->
Results = [Result
|| Result <- kz_json:get_value(<<"result">>, JObj, []),
'true' =/= kz_json:is_true(<<"best_effort">>, Result)
],
kz_json:set_value(<<"result">>, Results, JObj)
end.

-spec maybe_filter_rates(boolean(), kz_json:object()) -> kz_json:object().
maybe_filter_rates('false', JObj) -> JObj;
maybe_filter_rates('true', JObj) ->
-spec maybe_filter_rates(kz_json:object()) -> kz_json:object().
maybe_filter_rates(JObj) -> maybe_filter_rates(JObj, ?SHOULD_FILTER_RATES).

-spec maybe_filter_rates(kz_json:object(), boolean()) -> kz_json:object().
maybe_filter_rates(JObj, 'false') -> JObj;
maybe_filter_rates(JObj, 'true') ->
UpfrontCost = kapps_config:get_float(?MOD_CONFIG_CAT, <<"upfront_cost">>, 1.0),
MonthlyRecurringCost = kapps_config:get_float(?MOD_CONFIG_CAT, <<"monthly_recurring_cost">>, 1.0),
Results = [Result
Expand Down