Skip to content

Commit

Permalink
HELP-14771: Add min_shortdial_destination config option (#6552)
Browse files Browse the repository at this point in the history
Allows for shortdial correction to only be applied if the destination
is of at least a certain length.

And also refactor short-dial correction code in order to be able to
add unit tests for that functionality.
  • Loading branch information
harenson authored and jamesaimonetti committed Jun 12, 2020
1 parent cdaa2ea commit d3ad514
Show file tree
Hide file tree
Showing 4 changed files with 286 additions and 42 deletions.
11 changes: 8 additions & 3 deletions applications/crossbar/priv/api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -35251,7 +35251,7 @@
"type": "boolean"
},
"fixed_length_shortdial_correction": {
"description": "stepswitch fixed_length_shortdial_correction",
"description": "add {FIXED} digits from caller ID to destination",
"minimum": 1,
"type": "integer"
},
Expand All @@ -35262,13 +35262,18 @@
},
"max_shortdial_correction": {
"default": 5,
"description": "stepswitch maximum shortdial correction",
"description": "add up to {MAX} digits from caller ID to destination number",
"minimum": 1,
"type": "integer"
},
"min_shortdial_correction": {
"default": 2,
"description": "stepswitch minimum shortdial correction",
"description": "add at least {MIN} digits from caller ID to destination number",
"minimum": 1,
"type": "integer"
},
"min_shortdial_destination": {
"description": "do short-dial correction if dialed number's length is equal to this value",
"minimum": 1,
"type": "integer"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
"type": "boolean"
},
"fixed_length_shortdial_correction": {
"description": "stepswitch fixed_length_shortdial_correction",
"description": "add {FIXED} digits from caller ID to destination",
"minimum": 1,
"type": "integer"
},
Expand All @@ -125,13 +125,18 @@
},
"max_shortdial_correction": {
"default": 5,
"description": "stepswitch maximum shortdial correction",
"description": "add up to {MAX} digits from caller ID to destination number",
"minimum": 1,
"type": "integer"
},
"min_shortdial_correction": {
"default": 2,
"description": "stepswitch minimum shortdial correction",
"description": "add at least {MIN} digits from caller ID to destination number",
"minimum": 1,
"type": "integer"
},
"min_shortdial_destination": {
"description": "do short-dial correction if dialed number's length is equal to this value",
"minimum": 1,
"type": "integer"
},
Expand Down
120 changes: 84 additions & 36 deletions applications/stepswitch/src/stepswitch_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
-export([resources_to_endpoints/3]).
-export([json_to_template_props/1]).

-ifdef(TEST).
-export([maybe_constrain_shortdial_correction/3
,shortdial_correction_length/3
,maybe_deny_reclassified_number/3
,do_correct_shortdial/3
,should_correct_shortdial/2
,correct_shortdial/4
]).
-endif.

-include("stepswitch.hrl").
-include_lib("kazoo_stdlib/include/kazoo_json.hrl").
-include_lib("kazoo_amqp/include/kapi_offnet_resource.hrl").
Expand Down Expand Up @@ -91,61 +101,100 @@ correct_shortdial(<<"+", Number/binary>>, CIDNum, DeniedCallRestrictions) ->
correct_shortdial(Number, <<"+", CIDNum/binary>>, DeniedCallRestrictions) ->
correct_shortdial(Number, CIDNum, DeniedCallRestrictions);
correct_shortdial(Number, CIDNum, DeniedCallRestrictions) when is_binary(CIDNum) ->
correct_shortdial(Number, CIDNum, DeniedCallRestrictions, should_correct_shortdial(Number)).

-spec correct_shortdial(kz_term:ne_binary(), kz_term:ne_binary(), kz_json:object(), {boolean(), pos_integer()}) -> kz_term:api_ne_binary().
correct_shortdial(Number, CIDNum, DeniedCallRestrictions, {'true', _Length}) ->
case shortdial_correction_length(Number, CIDNum) of
0 ->
lager:debug("unable to correct shortdial ~s via CID ~s"
,[Number, CIDNum]
),
'undefined';
Length ->
try_to_correct(Number, CIDNum, DeniedCallRestrictions, Length)
end.

-spec try_to_correct(kz_term:ne_binary(), kz_term:ne_binary(), kz_json:object(), non_neg_integer()) -> kz_term:api_ne_binary().
try_to_correct(Number, CIDNum, DeniedCallRestrictions, Length) ->
maybe_deny_reclassified_number(do_correct_shortdial(Number, CIDNum, Length)
,CIDNum
,DeniedCallRestrictions
)
end;
correct_shortdial(Number, _CIDNum, _DeniedCallRestrictions, {'false', Length}) ->
lager:debug("~s's lenght (~p) doesn't match min_shortdial_destination param's value (~p), skipping"
,[Number, byte_size(Number), Length]
),
'undefined'.

-spec should_correct_shortdial(kz_term:ne_binary()) -> {boolean(), pos_integer()}.
should_correct_shortdial(Number) ->
should_correct_shortdial(Number
,kapps_config:get_integer(?SS_CONFIG_CAT
,<<"min_shortdial_destination">>
)
).

-spec should_correct_shortdial(kz_term:ne_binary(), pos_integer()) -> {boolean(), pos_integer()}.
should_correct_shortdial(_Number, 'undefined') ->
{'true', 0};
should_correct_shortdial(Number, Length) ->
%% The length is also included on the response just to avoid having to read this configuration
%% parameter again when printing the debug line. So it is only useful for the `{false, N}' branch.
{byte_size(Number) =:= Length, Length}.

-spec do_correct_shortdial(kz_term:ne_binary(), kz_term:ne_binary(), non_neg_integer()) -> kz_term:api_ne_binary().
do_correct_shortdial(Number, CIDNum, Length) ->
Correction = kz_binary:truncate_right(CIDNum, Length),
CorrectedNumber = knm_converters:normalize(<<Correction/binary, Number/binary>>),
lager:debug("corrected shortdial ~s via CID ~s to ~s"
,[Number, CIDNum, CorrectedNumber]
),
case should_deny_reclassified_number(CorrectedNumber, DeniedCallRestrictions) of
'true' ->
lager:info("unable to correct shortdial ~s via CID ~s due to a call restriction"
,[Number, CIDNum]
),
'undefined';
'false' ->
CorrectedNumber
end.
lager:debug("corrected shortdial ~s via CID ~s to ~s", [Number, CIDNum, CorrectedNumber]),
CorrectedNumber.

-spec should_deny_reclassified_number(kz_term:ne_binary(), kz_json:object()) -> boolean().
should_deny_reclassified_number(CorrectedNumber, DeniedCallRestrictions) ->
-spec maybe_deny_reclassified_number(kz_term:ne_binary(), kz_term:ne_binary(), kz_json:object()) -> kz_term:api_ne_binary().
maybe_deny_reclassified_number(CorrectedNumber, CIDNum, DeniedCallRestrictions) ->
Classification = knm_converters:classify(CorrectedNumber),
lager:debug("re-classified corrected number ~s as ~s, testing for call restrictions"
,[CorrectedNumber, Classification]
),
kz_json:get_ne_binary_value([Classification, <<"action">>], DeniedCallRestrictions)
=:= <<"deny">>.
case kz_json:get_ne_binary_value([Classification, <<"action">>], DeniedCallRestrictions) of
<<"deny">> ->
lager:info("unable to correct shortdial via CID ~s due to a call restriction", [CIDNum]),
'undefined';
_ ->
CorrectedNumber
end.

-spec shortdial_correction_length(kz_term:ne_binary(), kz_term:ne_binary()) -> integer().
shortdial_correction_length(Number, CIDNum) ->
case kapps_config:get_integer(?SS_CONFIG_CAT, <<"fixed_length_shortdial_correction">>) of
'undefined' ->
Length = byte_size(CIDNum) - byte_size(Number),
maybe_constrain_shortdial_correction(Length);
FixedLength -> FixedLength
end.
shortdial_correction_length(Number
,CIDNum
,kapps_config:get_integer(?SS_CONFIG_CAT
,<<"fixed_length_shortdial_correction">>
)
).

-spec shortdial_correction_length(kz_term:ne_binary(), kz_term:ne_binary(), integer()) -> integer().
shortdial_correction_length(Number, CIDNum, 'undefined') ->
Length = byte_size(CIDNum) - byte_size(Number),
maybe_constrain_shortdial_correction(Length);
shortdial_correction_length(_Number, _CIDNum, FixedLength) ->
FixedLength.

-spec maybe_constrain_shortdial_correction(integer()) -> integer().
maybe_constrain_shortdial_correction(Length) ->
MaxCorrection = kapps_config:get_integer(?SS_CONFIG_CAT, <<"max_shortdial_correction">>, 5),
MinCorrection = kapps_config:get_integer(?SS_CONFIG_CAT, <<"min_shortdial_correction">>, 2),
case Length =< MaxCorrection
andalso Length >= MinCorrection
of
'true' -> Length;
'false' -> 0
end.
maybe_constrain_shortdial_correction(Length
,kapps_config:get_integer(?SS_CONFIG_CAT
,<<"min_shortdial_correction">>
,2
)
,kapps_config:get_integer(?SS_CONFIG_CAT
,<<"max_shortdial_correction">>
,5
)
).

-spec maybe_constrain_shortdial_correction(integer(), integer(), integer()) -> integer().
maybe_constrain_shortdial_correction(Length, Min, Max) when Length >= Min
andalso Length =< Max ->
Length;
maybe_constrain_shortdial_correction(_Length, _Min, _Max) ->
0.

-spec get_sip_headers(kapi_offnet_resource:req()) -> kz_json:object().
get_sip_headers(OffnetReq) ->
Expand All @@ -165,7 +214,6 @@ get_sip_headers(OffnetReq) ->
maybe_remove_diversions(JObj) ->
kz_json:delete_key(<<"Diversions">>, JObj).


-spec get_diversions(kz_json:object()) ->
'undefined' |
kz_term:ne_binaries().
Expand Down
Loading

0 comments on commit d3ad514

Please sign in to comment.