Skip to content

Commit

Permalink
KAZOO-5493_master: KNM: no more picking state on account type (#3642)
Browse files Browse the repository at this point in the history
* KAZOO-5493: do not pick state based on type of account to assign_to

* KAZOO-5493: allow admins to pick the state or fallback to reserved

* KAZOO-5493: replace main authorize function in knm_number_states with the main KPN one
  • Loading branch information
fenollp authored and lazedo committed Apr 28, 2017
1 parent c8dcff7 commit 14e8343
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 65 deletions.
48 changes: 13 additions & 35 deletions core/kazoo_number_manager/src/knm_number.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
-export([attempt/2
,ensure_can_create/1
,ensure_can_load_to_create/1
,state_for_create/2
,state_for_create/1
]).

-ifdef(TEST).
Expand Down Expand Up @@ -142,44 +142,20 @@ get(Num, Options) ->
create(Num, Options) ->
?TRY2(create, Num, Options).

-spec state_for_create(ne_binary(), knm_number_options:options()) -> ne_binary().
-ifdef(TEST).
state_for_create(AccountId, Options) ->
-spec state_for_create(knm_number_options:options()) -> ne_binary().
state_for_create(Options) ->
case {knm_number_options:state(Options)
,knm_number_options:ported_in(Options)
,knm_number_options:module_name(Options)
,AccountId
,knm_phone_number:is_admin(knm_number_options:auth_by(Options))
}
of
{?NUMBER_STATE_PORT_IN=PortIn, _, _, _} -> PortIn;
{_, true, _, _} -> ?NUMBER_STATE_IN_SERVICE;
{_, _, ?CARRIER_MDN, _} -> ?NUMBER_STATE_IN_SERVICE;
{_, _, _, ?MASTER_ACCOUNT_ID} -> ?NUMBER_STATE_AVAILABLE;
{_, _, _, ?RESELLER_ACCOUNT_ID} -> ?NUMBER_STATE_RESERVED;
_ -> ?NUMBER_STATE_IN_SERVICE
end.
-else.
state_for_create(AccountId, Options) ->
case knm_number_options:state(Options) of
?NUMBER_STATE_PORT_IN=PortIn -> PortIn;
_ ->
case knm_number_options:ported_in(Options) of
true -> ?NUMBER_STATE_IN_SERVICE;
_ ->
case ?CARRIER_MDN =:= knm_number_options:module_name(Options) of
true -> ?NUMBER_STATE_IN_SERVICE;
_ ->
case kz_services:is_reseller(AccountId)
andalso kapps_util:get_master_account_id()
of
'false' -> ?NUMBER_STATE_IN_SERVICE;
{'ok', AccountId} -> ?NUMBER_STATE_AVAILABLE;
{'ok', _} -> ?NUMBER_STATE_RESERVED
end
end
end
{State=?NE_BINARY, _, _, true} -> State;
_ -> ?NUMBER_STATE_RESERVED
end.
-endif.

-spec ensure_can_load_to_create(knm_phone_number:knm_phone_number()) -> 'true';
(knm_numbers:collection()) -> knm_numbers:collection().
Expand Down Expand Up @@ -244,19 +220,21 @@ ensure_can_create(Num, Options) ->
kz_account:fetch(AccountId)).
-endif.

ensure_account_can_create(_, undefined) ->
knm_errors:unauthorized();
ensure_account_can_create(_, ?KNM_DEFAULT_AUTH_BY) ->
lager:info("bypassing auth"),
'true';
ensure_account_can_create(Options, _AccountId) ->
ensure_account_can_create(Options, ?MATCH_ACCOUNT_RAW(AccountId)) ->
knm_number_options:ported_in(Options)
orelse knm_number_options:state(Options) =:= ?NUMBER_STATE_PORT_IN
orelse begin
{'ok', JObj} = ?LOAD_ACCOUNT(Options, _AccountId),
{'ok', JObj} = ?LOAD_ACCOUNT(Options, AccountId),
kz_account:allow_number_additions(JObj)
end
orelse knm_errors:unauthorized().
orelse knm_phone_number:is_admin(AccountId)
orelse knm_errors:unauthorized();
ensure_account_can_create(_, _NotAnAccountId) ->
?LOG_DEBUG("'~p' is not an account id", [_NotAnAccountId]),
knm_errors:unauthorized().

-spec ensure_number_is_not_porting(ne_binary(), knm_number_options:options()) -> 'true'.
-ifdef(TEST).
Expand Down
21 changes: 3 additions & 18 deletions core/kazoo_number_manager/src/knm_number_states.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
-spec to_options_state(t()) -> t().
to_options_state(T=#{options := Options}) ->
TargetState = knm_number_options:state(Options),
?LOG_DEBUG("attempting to change to state ~s", [TargetState]),
change_state(T, TargetState).

-spec change_state(t(), ne_binary()) -> t().
Expand Down Expand Up @@ -187,24 +188,8 @@ to_in_service(T, State) ->
-endif.

-spec authorize(t()) -> t().
authorize(T0=#{todo := Ns, options := Options}) ->
case knm_number_options:auth_by(Options) of
?KNM_DEFAULT_AUTH_BY ->
lager:info("bypassing auth"),
knm_numbers:ok(Ns, T0);
AuthBy ->
F = fun (N, T) -> authorize_fold(N, T, AuthBy) end,
lists:foldl(F, T0, Ns)
end.

authorize_fold(N, T, AuthBy) ->
AssignTo = knm_phone_number:assign_to(knm_number:phone_number(N)),
case ?ACCT_HIERARCHY(AuthBy, AssignTo, 'true') of
true -> knm_numbers:ok(N, T);
false ->
Reason = knm_errors:to_json(unauthorized),
knm_numbers:ko(N, Reason, T)
end.
authorize(T) ->
knm_numbers:do_in_wrap(fun knm_phone_number:is_authorized/1, T).

-spec in_service_from_reserved_authorize(kn()) -> kn();
(t()) -> t().
Expand Down
5 changes: 2 additions & 3 deletions core/kazoo_number_manager/src/knm_numbers.erl
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,8 @@ create(Nums, Options) ->
case take_not_founds(T0) of
{#{ok := []}, []} -> T0;
{T1, NotFounds} ->
AccountId = knm_number_options:assign_to(Options),
ToState = knm_number:state_for_create(AccountId, Options),
lager:debug("picked state ~s for ~s for ~p", [ToState, AccountId, Nums]),
ToState = knm_number:state_for_create(Options),
lager:debug("picked state ~s for ~s for ~p", [ToState, knm_number_options:assign_to(Options), Nums]),
NewOptions = [{'state', ToState} | Options],
ret(pipe(maybe_create(NotFounds, options(NewOptions, T1))
,[fun knm_number:new/1
Expand Down
5 changes: 3 additions & 2 deletions core/kazoo_number_manager/test/knm_create_new_number_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ fail_new_number_test_() ->
].

create_new_available_number_test_() ->
Props = [{'auth_by', ?KNM_DEFAULT_AUTH_BY}
Props = [{'auth_by', ?MASTER_ACCOUNT_ID}
,{'assign_to', ?MASTER_ACCOUNT_ID}
,{'dry_run', 'false'}
,{state, ?NUMBER_STATE_AVAILABLE}
,{<<"auth_by_account">>
,kz_account:set_allow_number_additions(?RESELLER_ACCOUNT_DOC, 'true')
}
Expand All @@ -154,7 +155,7 @@ create_new_available_number_test_() ->
,?_assertEqual(undefined, knm_phone_number:assigned_to(PN))
}
,{"Verify new phone number auth_by field was stored"
,?_assertEqual(?KNM_DEFAULT_AUTH_BY, knm_phone_number:auth_by(PN))
,?_assertEqual(?MASTER_ACCOUNT_ID, knm_phone_number:auth_by(PN))
}
,{"Verify new phone number database is properly set"
,?_assertEqual(<<"numbers%2F%2B1555">>, knm_phone_number:number_db(PN))
Expand Down
37 changes: 30 additions & 7 deletions core/kazoo_number_manager/test/knm_numbers_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,36 @@ create_test_() ->
,kz_json:get_value(<<"address_id">>, knm_phone_number:carrier_data(pn_x(1, Ret)))
)
}
,?_assertMatch(#{ko := #{?NOT_NUM := _
,?TEST_CREATE_NUM := _
}
,ok := [_]
}
,knm_numbers:create([Num, ?NOT_NUM, ?TEST_CREATE_NUM], Options)
)
].


not_reconcilable() ->
kz_json:from_list(
[{<<"code">>, 500}
,{<<"error">>, <<"unspecified_fault">>}
,{<<"message">>, <<"not_reconcilable">>}
]).


create_new_test_() ->
Num = ?TEST_TELNYX_NUM,
Options = [{'auth_by', ?MASTER_ACCOUNT_ID}
,{'assign_to', ?RESELLER_ACCOUNT_ID}
,{<<"auth_by_account">>, kz_json:new()}
],
Ret = knm_numbers:create([Num, ?NOT_NUM, ?TEST_CREATE_NUM], Options),
[?_assertEqual(#{?NOT_NUM => not_reconcilable()}, maps:get(ko, Ret))
,?_assertMatch([_, _], maps:get(ok, Ret))
,?_assertEqual(true, knm_number:is_number(n_x(1, Ret)))
,?_assertEqual(true, knm_number:is_number(n_x(2, Ret)))
,?_assertEqual(true, knm_phone_number:is_phone_number(pn_x(1, Ret)))
,?_assertEqual(true, knm_phone_number:is_phone_number(pn_x(2, Ret)))
,?_assert(knm_phone_number:is_dirty(pn_x(1, Ret)))
,?_assert(knm_phone_number:is_dirty(pn_x(2, Ret)))
,?_assertEqual(?RESELLER_ACCOUNT_ID, knm_phone_number:assigned_to(pn_x(1, Ret)))
,?_assertEqual(?RESELLER_ACCOUNT_ID, knm_phone_number:assigned_to(pn_x(2, Ret)))
,?_assertEqual(?NUMBER_STATE_RESERVED, knm_phone_number:state(pn_x(1, Ret)))
,?_assertEqual(?NUMBER_STATE_RESERVED, knm_phone_number:state(pn_x(2, Ret)))
].


Expand Down

0 comments on commit 14e8343

Please sign in to comment.