Skip to content

Commit

Permalink
Merged in KAZ-233 (pull request 2600hz#29)
Browse files Browse the repository at this point in the history
KAZ-233 Changed price formatting
  • Loading branch information
BorigTheDwarf committed Apr 21, 2016
2 parents 6252fc5 + 25fe59f commit 5f52e0e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 54 deletions.
41 changes: 21 additions & 20 deletions applications/crossbar/src/modules/cb_purchase_numbers.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

-define(LINE_REGIONS, <<"regions">>).
-define(PRICES, <<"prices">>).
-define(CANCEL, <<"cancel">>).

%%%===================================================================
%%% API
Expand Down Expand Up @@ -55,8 +54,7 @@ init() ->
allowed_methods() -> [?HTTP_PUT].
allowed_methods(?LINE_REGIONS) -> [?HTTP_GET];
allowed_methods(?PRICES) -> [?HTTP_GET].
allowed_methods(?LINE_REGIONS, _Region) -> [?HTTP_GET];
allowed_methods(_Number, ?CANCEL) -> [?HTTP_PUT].
allowed_methods(?LINE_REGIONS, _Region) -> [?HTTP_GET].

%%--------------------------------------------------------------------
%% @public
Expand All @@ -73,8 +71,7 @@ allowed_methods(_Number, ?CANCEL) -> [?HTTP_PUT].
resource_exists() -> 'true'.
resource_exists(?LINE_REGIONS) -> 'true';
resource_exists(?PRICES) -> 'true'.
resource_exists(?LINE_REGIONS, _) -> 'true';
resource_exists(_Number, ?CANCEL) -> 'true'.
resource_exists(?LINE_REGIONS, _) -> 'true'.

%%--------------------------------------------------------------------
%% @public
Expand All @@ -96,25 +93,23 @@ validate(Context, ?LINE_REGIONS) ->
validate(Context, ?PRICES) ->
validate_prices(Context, cb_context:req_verb(Context)).
validate(Context, ?LINE_REGIONS, Region) ->
validate_line_region(Context, Region, cb_context:req_verb(Context));
validate(Context, Number, ?CANCEL) ->
cancel_number(Context, Number, cb_context:req_verb(Context)).
validate_line_region(Context, Region, cb_context:req_verb(Context)).

-spec validate_prices(cb_context:context(), http_method()) -> cb_context:context().
validate_prices(Context, ?HTTP_GET) ->
AccountId = cb_context:account_id(Context),
crossbar_util:response(number_source_util:get_prices(AccountId), Context).

-spec cancel_number(cb_context:context(), ne_binary(), http_method()) -> cb_context:context().
cancel_number(Context, Number, ?HTTP_PUT) ->
case number_source_provider:cancel_number(Number) of
'ok' ->
cb_context:set_resp_status(Context, 'success');
{'error', 'invalidnumber'} ->
cb_context:add_validation_error(<<"number">>, <<"not_found">>, <<"Number to cancel not found">>, Context);
{'error', _Reason} ->
cb_context:add_system_error('number_source_error', Context)
end.
%-spec cancel_number(cb_context:context(), ne_binary(), http_method()) -> cb_context:context().
%cancel_number(Context, Number, ?HTTP_PUT) ->
% case number_source_provider:cancel_number(Number) of
% 'ok' ->
% cb_context:set_resp_status(Context, 'success');
% {'error', 'invalidnumber'} ->
% cb_context:add_validation_error(<<"number">>, <<"not_found">>, <<"Number to cancel not found">>, Context);
% {'error', _Reason} ->
% cb_context:add_system_error('number_source_error', Context)
% end.

-spec validate_line_regions(cb_context:context(), http_method()) -> cb_context:context().
validate_line_regions(Context, ?HTTP_GET) ->
Expand All @@ -137,7 +132,13 @@ validate_line_region(Context, Region, ?HTTP_GET) ->

-spec validate_purchase_numbers(cb_context:context(), http_method()) -> cb_context:context().
validate_purchase_numbers(Context, ?HTTP_PUT) ->
cb_context:validate_request_data(<<"purchase_numbers">>, Context, fun purchase_numbers/1).
BPLocationId = wh_json:get_binary_value(<<"location_id">>, cb_context:req_data(Context)),
case cb_business_partner:is_valid_location_id(Context, BPLocationId) of
'true' ->
cb_context:validate_request_data(<<"purchase_numbers">>, Context, fun purchase_numbers/1);
'false' ->
{'error', cb_context:add_validation_error([<<"location_id">>], <<"not_found">>, <<"Location ID does not exist">>, Context)}
end.

-spec purchase_numbers(cb_context:context()) -> cb_context:context().
purchase_numbers(Context) ->
Expand Down Expand Up @@ -183,7 +184,7 @@ add_number(Context, Number) ->
-spec camel_create(cb_context:context(), ne_binary(), atom(), boolean()) -> 'ok' | {'error', atom(), ne_binary()}.
camel_create(Context, Number, Type, IsFax) ->
AccountId = cb_context:account_id(Context),
Charges = number_source_util:get_charges_for_number(AccountId, Number),
Charges = number_source_util:get_charges_for_number(AccountId, Number, Type),
try camel_numbers:create_number(AccountId, Number, Type, IsFax, Charges) of
{'ok', _} -> camel_provision(Context, AccountId, Number, IsFax)
catch
Expand Down
63 changes: 29 additions & 34 deletions core/number_source-1.0.0/src/number_source_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

-export([get_country_and_area_code/1]).
-export([get_prices/1]).
-export([get_charges_for_number/2]).
-export([get_charges_for_number/3]).

-include_lib("number_source/include/number_source.hrl").

Expand All @@ -29,44 +29,39 @@ get_prices(AccountId) ->
%% @public
%% @doc
%% Get charges for a number, in the form {setup_cost, monthly_charge}
%% Will choose the first matching price in the parent accounts "number_charges"
%% section of the account document.
%% @end
%%--------------------------------------------------------------------
-spec get_charges_for_number(ne_binary(), ne_binary()) -> {ne_binary(), ne_binary()}.
get_charges_for_number(AccountId, Number) ->
{CountryCode, Type} = get_country_and_area_code(Number),
case get_potential_charges(AccountId, CountryCode, wh_util:to_binary(Type)) of
[] ->
lager:error("Failed to get charges for country code ~p, type ~p", [CountryCode, Type]),
{<<"0">>, <<"0">>};
PotentialCharges ->
[{_, Charges}|_] = sort_charges(PotentialCharges),
lager:debug("Charging ~p for number", [Charges]),
Charges
end.

-spec get_potential_charges(ne_binary(), ne_binary(), ne_binary()) -> [{{ne_binary(), ne_binary()}, {ne_binary(), ne_binary()}}].
get_potential_charges(AccountId, NumberCountryCode, NumberType) ->
AllCharges = get_prices(AccountId),
[{{CountryCode, Type}, {SetupCost, MonthlyCharge}} || Charges <- AllCharges,
-spec get_charges_for_number(ne_binary(), ne_binary(), atom()) -> {ne_binary(), ne_binary()}.
get_charges_for_number(AccountId, Number, NumberType) ->
E164 = wnm_util:to_e164(Number),
lager:debug("Findng number charge for account ~p with number ~p and type ~p", [AccountId, E164, NumberType]),
case [{SetupCost, MonthlyCharge} || Charges <- get_prices(AccountId),
begin
CountryCode = wh_json:get_binary_value(<<"country_code">>, Charges),
Prefix = wh_json:get_binary_value(<<"prefix">>, Charges),
Type = wh_json:get_binary_value(<<"type">>, Charges),
SetupCost = wh_json:get_binary_value(<<"setup_cost">>, Charges),
MonthlyCharge = wh_json:get_binary_value(<<"monthly_charge">>, Charges),

(CountryCode =:= NumberCountryCode orelse CountryCode =:= <<"*">>) andalso
(Type =:= NumberType orelse Type =:= <<"*">>)
end
].

-spec sort_charges([{{ne_binary(), ne_binary()}, {ne_binary(), ne_binary()}}]) -> [{{ne_binary(), ne_binary()}, {ne_binary(), ne_binary()}}].
sort_charges(PotentialCharges) ->
lists:sort(fun({A, _ACharges}, {B, _BCharges}) ->
count_wildcards(A) >= count_wildcards(B)
end, PotentialCharges).
(number_matches_prefix(E164, Prefix) orelse Prefix =:= <<"*">>) andalso
(Type =:= wh_util:to_binary(NumberType) orelse Type =:= <<"*">>)
end] of
[] ->
lager:warning("Failed to find number charge for account ~p with number ~p and type ~p", [AccountId, E164, NumberType]),
{<<"0.00">>, <<"0.00">>};
[Charges|_] ->
lager:debug("Found number charges: ~p for account ~p with number ~p and type ~p", [Charges, AccountId, E164, NumberType]),
Charges
end.

-spec count_wildcards({ne_binary(), ne_binary()}) -> boolean().
count_wildcards(Charges) ->
lists:foldl(fun(X, Accum) ->
case X of <<"*">> -> Accum; _ -> Accum + 1 end
end, 0, tuple_to_list(Charges)).
-spec number_matches_prefix(ne_binary(), ne_binary()) -> boolean().
number_matches_prefix(<<"+", Number/binary>>, Prefix) ->
number_matches_prefix(Number, Prefix);
number_matches_prefix(Number, Prefix) when byte_size(Number) < byte_size(Prefix) ->
'false';
number_matches_prefix(Number, Prefix) ->
case split_binary(Number, byte_size(Prefix)) of
{Prefix, _} -> 'true';
_ -> 'false'
end.

0 comments on commit 5f52e0e

Please sign in to comment.