Skip to content

Commit

Permalink
KAZOO-3889: ensure only the right data gets into #knm_phone_number{}
Browse files Browse the repository at this point in the history
  • Loading branch information
fenollp committed Mar 3, 2016
1 parent 2a4827e commit 63d148c
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 188 deletions.
7 changes: 2 additions & 5 deletions core/kazoo_number_manager/src/carriers/knm_bandwidth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,8 @@ found_number_to_object(Found, AccountId) ->
,{fun knm_phone_number:set_state/2, ?NUMBER_STATE_DISCOVERY}
,{fun knm_phone_number:set_assign_to/2, AccountId}
],
PhoneNumber = knm_phone_number:setters(knm_phone_number:new(), Updates),
knm_number:set_phone_number(
knm_number:new()
,PhoneNumber
).
{'ok', PhoneNumber} = knm_phone_number:setters(knm_phone_number:new(), Updates),
knm_number:set_phone_number(knm_number:new(), PhoneNumber).

%%--------------------------------------------------------------------
%% @public
Expand Down
38 changes: 16 additions & 22 deletions core/kazoo_number_manager/src/carriers/knm_carriers.erl
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,9 @@ check_for_existing_did(Number, Acc, Carrier, {'ok', ExistingPhoneNumber}) ->
create_discovery(Number, Acc) ->
DiscoveryUpdates =
[{fun knm_phone_number:set_state/2, ?NUMBER_STATE_DISCOVERY}],
PhoneNumber = knm_number:phone_number(Number),
DiscoveryNumber =
knm_number:set_phone_number(
Number
,knm_phone_number:setters(PhoneNumber, DiscoveryUpdates)
),
{'ok', PhoneNumber} =
knm_phone_number:setters(knm_number:phone_number(Number), DiscoveryUpdates),
DiscoveryNumber = knm_number:set_phone_number(Number, PhoneNumber),
collect_if_saved(DiscoveryNumber, Acc).

-spec collect_if_saved(knm_number:knm_number(), wh_json:objects()) ->
Expand All @@ -171,24 +168,21 @@ collect_if_saved(DiscoveryNumber, Acc) ->
-spec transition_existing_to_discovery(knm_number:knm_number(), knm_phone_number:knm_phone_number(), ne_binary()) ->
knm_number:knm_number().
transition_existing_to_discovery(Number, ExistingPhoneNumber, Carrier) ->
PhoneNumber = knm_number:phone_number(Number),
knm_number:set_phone_number(
Number
,knm_phone_number:setters(
ExistingPhoneNumber
,[{fun knm_phone_number:set_module_name/2, knm_phone_number:module_name(PhoneNumber)}
,{fun knm_phone_number:set_carrier_data/2, knm_phone_number:carrier_data(PhoneNumber)}
,{fun knm_phone_number:set_module_name/2, Carrier}
]
)
).
PhoneNumber0 = knm_number:phone_number(Number),
{'ok', PhoneNumber} =
knm_phone_number:setters(
ExistingPhoneNumber
,[{fun knm_phone_number:set_module_name/2, knm_phone_number:module_name(PhoneNumber0)}
,{fun knm_phone_number:set_carrier_data/2, knm_phone_number:carrier_data(PhoneNumber0)}
,{fun knm_phone_number:set_module_name/2, Carrier}
]
),
knm_number:set_phone_number(Number, PhoneNumber).

-spec check_existing_phone_number(knm_number:knm_number(), wh_json:objects(), knm_phone_number:knm_phone_number()) -> wh_json:objects().
-spec check_existing_phone_number(knm_number:knm_number(), wh_json:objects(), knm_phone_number:knm_phone_number()) ->
wh_json:objects().
check_existing_phone_number(Number, Acc, PhoneNumber) ->
case lists:member(knm_phone_number:state(PhoneNumber)
,?KNM_AVAILABLE_STATES
)
of
case lists:member(knm_phone_number:state(PhoneNumber), ?KNM_AVAILABLE_STATES) of
'true' -> [found_number_to_jobj(Number) | Acc];
'false' -> Acc
end.
Expand Down
17 changes: 5 additions & 12 deletions core/kazoo_number_manager/src/carriers/knm_managed.erl
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,16 @@ do_find_numbers_in_account(Number, Quantity, AccountId) ->
format_numbers_resp(JObjs) ->
[format_number_resp(JObj) || JObj <- JObjs].

-spec format_number_resp(wh_json:object()) ->
knm_number:knm_number().
-spec format_number_resp(wh_json:object()) -> knm_number:knm_number().
format_number_resp(JObj) ->
Doc = wh_json:get_value(<<"doc">>, JObj),
Id = wh_doc:id(Doc),

Updates = [{fun knm_phone_number:set_number/2, Id}
Updates = [{fun knm_phone_number:set_number/2, wh_doc:id(Doc)}
,{fun knm_phone_number:set_carrier_data/2, Doc}
,{fun knm_phone_number:set_module_name/2, ?CARRIER_MANAGED}
],
knm_number:set_phone_number(
knm_number:new()
,knm_phone_number:setters(
knm_phone_number:new()
,Updates
)
).
{'ok', PhoneNumber} =
knm_phone_number:setters(knm_phone_number:new(), Updates),
knm_number:set_phone_number(knm_number:new(), PhoneNumber).

-spec is_number_billable(knm_number:knm_number()) -> boolean().
is_number_billable(_Number) -> 'false'.
Expand Down
4 changes: 2 additions & 2 deletions core/kazoo_number_manager/src/carriers/knm_other.erl
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ format_number_resp(DID, CarrierData, AccountId) ->
,{fun knm_phone_number:set_state/2, ?NUMBER_STATE_DISCOVERY}
,{fun knm_phone_number:set_assign_to/2, AccountId}
],
PhoneNumber = knm_phone_number:setters(knm_phone_number:new(), Updates),
{'ok', PhoneNumber} = knm_phone_number:setters(knm_phone_number:new(), Updates),
knm_number:set_phone_number(knm_number:new(), PhoneNumber).

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -375,7 +375,7 @@ block_resp(JObj, AccountId, Num) ->
,{fun knm_phone_number:set_number_db/2, NumberDb}
,{fun knm_phone_number:set_assign_to/2, AccountId}
],
PhoneNumber = knm_phone_number:setters(knm_phone_number:new(), Updates),
{'ok', PhoneNumber} = knm_phone_number:setters(knm_phone_number:new(), Updates),
knm_number:set_phone_number(knm_number:new(), PhoneNumber).

%%--------------------------------------------------------------------
Expand Down
7 changes: 2 additions & 5 deletions core/kazoo_number_manager/src/carriers/knm_simwood.erl
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,5 @@ response_jobj_to_number(JObj, AccountId) ->
,{fun knm_phone_number:set_number_db/2, NumberDb}
,{fun knm_phone_number:set_assign_to/2, AccountId}
],
PhoneNumber = knm_phone_number:setters(knm_phone_number:new(), Updates),
knm_number:set_phone_number(
knm_number:new()
,PhoneNumber
).
{'ok', PhoneNumber} = knm_phone_number:setters(knm_phone_number:new(), Updates),
knm_number:set_phone_number(knm_number:new(), PhoneNumber).
7 changes: 2 additions & 5 deletions core/kazoo_number_manager/src/carriers/knm_vitelity.erl
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,8 @@ response_pair_to_number(DID, CarrierData, Acc, AccountId) ->
,{fun knm_phone_number:set_state/2, ?NUMBER_STATE_DISCOVERY}
,{fun knm_phone_number:set_assign_to/2, AccountId}
],
PhoneNumber = knm_phone_number:setters(knm_phone_number:new(), Updates),
[knm_number:set_phone_number(
knm_number:new()
,PhoneNumber
)
{'ok', PhoneNumber} = knm_phone_number:setters(knm_phone_number:new(), Updates),
[knm_number:set_phone_number(knm_number:new(), PhoneNumber)
| Acc
].

Expand Down
36 changes: 8 additions & 28 deletions core/kazoo_number_manager/src/knm_number.erl
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,13 @@ create_or_load(Num, Props) ->
create_or_load(Num, Props, {'error', 'not_found'}) ->
ensure_can_create(Num, Props),
Updates = create_updaters(Num, Props),

Number = set_phone_number(new()
,knm_phone_number:setters(knm_phone_number:new(), Updates)
),
create_phone_number(Number);
{'ok', PhoneNumber} = knm_phone_number:setters(knm_phone_number:new(), Updates),
create_phone_number(set_phone_number(new(), PhoneNumber));
create_or_load(Num, Props, {'ok', PhoneNumber}) ->
ensure_can_load_to_create(PhoneNumber),
Updates = create_updaters(Num, Props),
create_phone_number(
set_phone_number(new()
,knm_phone_number:setters(PhoneNumber, Updates)
)
).
{'ok', NewPhoneNumber} = knm_phone_number:setters(PhoneNumber, Updates),
create_phone_number(set_phone_number(new(), NewPhoneNumber)).

-spec ensure_can_load_to_create(knm_phone_number:knm_phone_number()) -> 'true'.
ensure_can_load_to_create(PhoneNumber) ->
Expand Down Expand Up @@ -334,11 +328,6 @@ update_phone_number(Number, Routines) ->
case knm_phone_number:setters(PhoneNumber, Routines) of
{'error', _R}=Error -> Error;
{'ok', UpdatedPhoneNumber} ->
wrap_phone_number_return(
knm_phone_number:save(UpdatedPhoneNumber)
,Number
);
UpdatedPhoneNumber ->
wrap_phone_number_return(
knm_phone_number:save(UpdatedPhoneNumber)
,Number
Expand Down Expand Up @@ -445,13 +434,8 @@ delete(Num, Options) ->
knm_number_return().
delete_number(Number, Options) ->
Routines = [fun knm_phone_number:release/1],
unwind_or_disconnect(
set_phone_number(
Number
,apply_phone_number_routines(Number, Routines)
)
,Options
).
{'ok', PhoneNumber} = knm_phone_number:setters(phone_number(Number), Routines),
unwind_or_disconnect(set_phone_number(Number, PhoneNumber), Options).

-spec unwind_or_disconnect(knm_number(), knm_number_options:options()) ->
knm_number_return().
Expand Down Expand Up @@ -585,19 +569,15 @@ buy(Num, Account, Options) ->
%%%===================================================================
%%% Internal functions
%%%===================================================================

-spec wrap_phone_number_routines(knm_number(), knm_phone_number:set_functions()) ->
knm_number_return().
wrap_phone_number_routines(Number, Routines) ->
wrap_phone_number_return(
apply_phone_number_routines(Number, Routines)
knm_phone_number:setters(phone_number(Number), Routines)
,Number
).

-spec apply_phone_number_routines(knm_number(), knm_phone_number:set_functions()) ->
knm_phone_number:knm_phone_number().
apply_phone_number_routines(Number, Routines) ->
knm_phone_number:setters(phone_number(Number), Routines).

%%--------------------------------------------------------------------
%% @private
%% @doc
Expand Down
23 changes: 10 additions & 13 deletions core/kazoo_number_manager/src/knm_number_states.erl
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,15 @@ move_phone_number_to_state(PhoneNumber, ToState) ->
-spec move_phone_number_to_state(knm_phone_number:knm_phone_number(), ne_binary(), api_binary()) ->
knm_phone_number:knm_phone_number().
move_phone_number_to_state(PhoneNumber, ToState, 'undefined') ->
Setters = [{fun knm_phone_number:set_assigned_to/2
,knm_phone_number:assign_to(PhoneNumber)
}
,{fun knm_phone_number:set_state/2, ToState}
],
knm_phone_number:setters(PhoneNumber, Setters);
Setters =
[{fun knm_phone_number:set_assigned_to/2, knm_phone_number:assign_to(PhoneNumber)}
,{fun knm_phone_number:set_state/2, ToState}
],
{'ok', NewPhoneNumber} = knm_phone_number:setters(PhoneNumber, Setters),
NewPhoneNumber;
move_phone_number_to_state(PhoneNumber, ToState, AssignedTo) ->
move_phone_number_to_state(
PhoneNumber
,ToState
,AssignedTo
,knm_phone_number:assign_to(PhoneNumber)
).
AssignTo = knm_phone_number:assign_to(PhoneNumber),
move_phone_number_to_state(PhoneNumber, ToState, AssignedTo, AssignTo).

move_phone_number_to_state(PhoneNumber, ToState, AssignTo, AssignTo) ->
knm_phone_number:set_state(PhoneNumber, ToState);
Expand All @@ -267,7 +263,8 @@ move_phone_number_to_state(PhoneNumber, ToState, AssignedTo, AssignTo) ->
,{fun knm_phone_number:set_assigned_to/2, AssignTo}
,{fun knm_phone_number:set_state/2, ToState}
],
knm_phone_number:setters(PhoneNumber, Setters).
{'ok', NewPhoneNumber} = knm_phone_number:setters(PhoneNumber, Setters),
NewPhoneNumber.

move_to_deleted_state(Number) ->
PhoneNumber = knm_number:phone_number(Number),
Expand Down
Loading

0 comments on commit 63d148c

Please sign in to comment.