Skip to content

Commit

Permalink
WHISTLE-1236: update reconcile
Browse files Browse the repository at this point in the history
  • Loading branch information
k-anderson committed Jun 20, 2012
1 parent e85d023 commit 3bc2fa7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
25 changes: 17 additions & 8 deletions lib/whistle_number_manager-1.0.0/src/wh_number_manager.erl
Original file line number Diff line number Diff line change
Expand Up @@ -187,25 +187,34 @@ port_in(Number, AssignTo, AuthBy, PublicFields) ->
-spec reconcile_number/3 :: (ne_binary(), ne_binary(), ne_binary()) -> operation_return().
reconcile_number(Number, AssignTo, AuthBy) ->
Routines = [fun(_) -> wnm_number:get(Number) end
,fun({not_found, #number{}=N}) ->
,fun({not_found, #number{}=N}) when is_binary(AssignTo) ->
NewNumber = N#number{number=Number
,assign_to=AssignTo
,auth_by=AuthBy
,auth_by=wh_util:to_binary(AuthBy)
},
wnm_number:create_available(NewNumber);
({_, #number{}}=E) -> E;
(#number{}=N) -> N#number{assign_to=AssignTo
,auth_by=AuthBy
}
(#number{}=N) when is_binary(AssignTo) ->
N#number{assign_to=AssignTo};
(#number{assigned_to=undefined}=N) ->
wnm_number:error_unauthorized(N);
(#number{assigned_to=AssignedTo}=N) ->
N#number{assign_to=AssignedTo}
end
,fun({_, #number{}}=E) -> E;
(#number{}=N) when is_binary(AuthBy) ->
N#number{auth_by=AuthBy};
(#number{assign_to=ATo}=N) ->
N#number{auth_by=ATo}
end
,fun({_, #number{}}=E) -> E;
(#number{assign_to=Assign}=N) ->
lager:debug("attempting to reconcile number ~s with account ~s", [Number, Assign]),
wnm_number:in_service(N)
end
,fun({no_change_required, #number{}=N}) ->
wnm_number:save_phone_number_docs(N);
({unauthorized, #number{auth_by=system}=N}) ->
,fun({no_change_required, #number{assigned_to=undefined}}=E) ->
E;
({no_change_required, #number{}=N}) ->
wnm_number:save_phone_number_docs(N);
({_, #number{}}=E) -> E;
(#number{}=N) -> wnm_number:save(N)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
-spec reconcile/1 :: (string() | ne_binary() | 'all') -> 'no_return'.

reconcile() ->
io:format("This command is depreciated, please use reconcile_numbers() or for older systems reconcile_accounts(). See the wiki for details on the differences.", []).
io:format("This command is depreciated, please use reconcile_numbers() or for older systems reconcile_accounts(). See the wiki for details on the differences.", []),
no_return.

reconcile(Arg) ->
io:format("This command is depreciated, please use reconcile_numbers() or for older systems reconcile_accounts(~s). See the wiki for details on the differences.", [Arg]).
io:format("This command is depreciated, please use reconcile_numbers() or for older systems reconcile_accounts(~s). See the wiki for details on the differences.", [Arg]),
no_return.

%%--------------------------------------------------------------------
%% @public
Expand All @@ -48,8 +50,8 @@ reconcile(Arg) ->
%% in the accounts
%% @end
%%--------------------------------------------------------------------
-spec reconcile_numbers/0 :: () -> 'done' | {'error', _}.
-spec reconcile_numbers/1 :: (string() | ne_binary() | 'all') -> 'done' | {'error', _}.
-spec reconcile_numbers/0 :: () -> 'no_return' | {'error', _}.
-spec reconcile_numbers/1 :: (string() | ne_binary() | 'all') -> 'no_return' | {'error', _}.

reconcile_numbers() ->
reconcile_numbers(all).
Expand All @@ -58,7 +60,7 @@ reconcile_numbers(all) ->
_ = [reconcile_numbers(Db)
|| Db <- wnm_util:get_all_number_dbs()
],
done;
no_return;
reconcile_numbers(NumberDb) when not is_binary(NumberDb) ->
reconcile_numbers(wh_util:to_binary(NumberDb));
reconcile_numbers(NumberDb) ->
Expand All @@ -74,7 +76,7 @@ reconcile_numbers(NumberDb) ->
end
],
_ = reconcile_numbers(Numbers, system),
done
no_return
end.

%%--------------------------------------------------------------------
Expand All @@ -84,23 +86,23 @@ reconcile_numbers(NumberDb) ->
%% to the account (first account found with a duplicate number will win)
%% @end
%%--------------------------------------------------------------------
-spec reconcile_accounts/0 :: () -> 'done'.
-spec reconcile_accounts/1 :: (string() | ne_binary() | 'all') -> 'done'.
-spec reconcile_accounts/0 :: () -> 'no_return'.
-spec reconcile_accounts/1 :: (string() | ne_binary() | 'all') -> 'no_return'.

reconcile_accounts() ->
reconcile_accounts(all).

reconcile_accounts(all) ->
_ = [reconcile_accounts(AccountId) || AccountId <- whapps_util:get_all_accounts(raw)],
done;
no_return;
reconcile_accounts(AccountId) when not is_binary(AccountId) ->
reconcile_accounts(wh_util:to_binary(AccountId));
reconcile_accounts(AccountId) ->
AccountDb = wh_util:format_account_id(AccountId, encoded),
Numbers = get_callflow_account_numbers(AccountDb),
Numbers1 = get_trunkstore_account_numbers(AccountId, AccountDb) ++ Numbers,
_ = reconcile_numbers(Numbers1, wh_util:format_account_id(AccountId, raw)),
done.
no_return.

%%--------------------------------------------------------------------
%% @private
Expand Down Expand Up @@ -189,19 +191,20 @@ get_trunkstore_account_numbers(AccountId) ->
%% provided numbers
%% @end
%%--------------------------------------------------------------------
-spec reconcile_numbers/2 :: ([ne_binary(),...] | [], 'undefined' | ne_binary()) -> 'ok'.
-spec reconcile_numbers/2 :: ([ne_binary(),...] | [], 'system' | ne_binary()) -> 'ok'.
reconcile_numbers(Numbers, AccountId) ->
reconcile_numbers(Numbers, AccountId, length(Numbers), 1).

reconcile_numbers([Number|Numbers], AccountId, Total, Count) ->
Db = wnm_util:number_to_db_name(Number),
ReconcileWith = case is_binary(AccountId) of true -> AccountId; false -> Db end,
try wh_number_manager:reconcile_number(Number, AccountId, AccountId) of
_ ->
io:format("reconciled ~s number (~p/~p): ~s~n", [Db, Count, Total, Number]),
io:format("reconciled ~s number (~p/~p): ~s~n", [ReconcileWith, Count, Total, Number]),
reconcile_numbers(Numbers, AccountId, Total, Count + 1)
catch
_E:_R ->
io:format("error reconciling ~s number (~p/~p) ~s: ~p:~p~n", [Db, Count, Total, Number, _E, _R]),
io:format("error reconciling ~s number (~p/~p) ~s: ~p:~p~n", [ReconcileWith, Count, Total, Number, _E, _R]),
reconcile_numbers(Numbers, AccountId, Total, Count + 1)
end;
reconcile_numbers([], _, _, _) ->
Expand Down
6 changes: 3 additions & 3 deletions lib/whistle_number_manager-1.0.0/src/wnm_number.erl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ create_available(#number{number=Number, auth_by=AuthBy, number_doc=Doc}=N) ->
,fun(J) -> wh_json:set_value(<<"pvt_number_state">>, <<"available">>, J) end
,fun(J) -> wh_json:set_value(<<"pvt_db_name">>, Db, J) end
,fun(J) -> wh_json:set_value(<<"pvt_created">>, wh_util:current_tstamp(), J) end
,fun(J) -> wh_json:set_value(<<"pvt_authorizing_account">>, wh_util:to_binary(AuthBy), J) end
,fun(J) -> wh_json:set_value(<<"pvt_authorizing_account">>, AuthBy, J) end
],
JObj = lists:foldl(fun(F, J) -> F(J) end, wh_json:public_fields(Doc), Routines),
json_to_record(JObj, N).
Expand Down Expand Up @@ -368,7 +368,7 @@ reserved(Number) ->
-spec in_service/1 :: (wnm_number()) -> wnm_number().
in_service(#number{state = <<"discovery">>}=Number) ->
Routines = [fun(#number{assign_to=AssignTo, auth_by=AuthBy}=N) ->
case AuthBy =:= system orelse wh_util:is_in_account_hierarchy(AuthBy, AssignTo, true) of
case wh_util:is_in_account_hierarchy(AuthBy, AssignTo, true) of
false -> error_unauthorized(N);
true -> N
end
Expand Down Expand Up @@ -399,7 +399,7 @@ in_service(#number{state = <<"port_in">>}=Number) ->
lists:foldl(fun(F, J) -> F(J) end, Number, Routines);
in_service(#number{state = <<"available">>}=Number) ->
Routines = [fun(#number{assign_to=AssignTo, auth_by=AuthBy}=N) ->
case AuthBy =:= system orelse wh_util:is_in_account_hierarchy(AuthBy, AssignTo, true) of
case wh_util:is_in_account_hierarchy(AuthBy, AssignTo, true) of
false -> error_unauthorized(N);
true -> N
end
Expand Down

0 comments on commit 3bc2fa7

Please sign in to comment.