Skip to content

Commit

Permalink
Merged in KAZ-327 (pull request 2600hz#28)
Browse files Browse the repository at this point in the history
[KAZ-327] Added summary of account_plans for children to cb_account_plans
  • Loading branch information
BorigTheDwarf committed Apr 21, 2016
2 parents 55a5d53 + 2a1d080 commit e3632a5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
37 changes: 36 additions & 1 deletion applications/crossbar/src/modules/cb_account_plans.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
,authorize/1
,allowed_methods/0, allowed_methods/1
,resource_exists/0, resource_exists/1
,content_types_provided/2
,validate/1, validate/2
,put/1
]).
Expand All @@ -24,6 +25,7 @@

-define(ACCOUNT_PLANS, <<"account_plans">>).
-define(PLAN_PRICES, <<"price">>).
-define(SUMMARY, <<"summary">>).

%%%===================================================================
%%% API
Expand All @@ -40,6 +42,7 @@ init() ->
_ = crossbar_bindings:bind(<<"*.authorize.account_plans">>, ?MODULE, 'authorize'),
_ = crossbar_bindings:bind(<<"*.allowed_methods.account_plans">>, ?MODULE, 'allowed_methods'),
_ = crossbar_bindings:bind(<<"*.resource_exists.account_plans">>, ?MODULE, 'resource_exists'),
_ = crossbar_bindings:bind(<<"*.content_types_provided.account_plans">>, ?MODULE, 'content_types_provided'),
_ = crossbar_bindings:bind(<<"*.validate.account_plans">>, ?MODULE, 'validate'),
_ = crossbar_bindings:bind(<<"*.execute.get.account_plans">>, ?MODULE, 'get'),
_ = crossbar_bindings:bind(<<"*.execute.put.account_plans">>, ?MODULE, 'put').
Expand Down Expand Up @@ -86,6 +89,21 @@ allowed_methods(_) ->
resource_exists() -> 'true'.
resource_exists(_) -> 'true'.

%%--------------------------------------------------------------------
%% @public
%% @doc
%% What content-types will the module be using to respond (matched against
%% client's accept header)
%% Of the form {atom, [{Type, SubType}]} :: {to_json, [{<<"application">>, <<"json">>}]}
%% @end
%%--------------------------------------------------------------------
-spec content_types_provided(cb_context:context(), path_token()) -> cb_context:context().
content_types_provided(Context, ?SUMMARY) ->
CTPs = [{'to_csv', ?CSV_CONTENT_TYPES}
,{'to_json', ?JSON_CONTENT_TYPES}
],
cb_context:add_content_types_provided(Context, CTPs).

%%--------------------------------------------------------------------
%% @public
%% @doc
Expand All @@ -100,6 +118,8 @@ resource_exists(_) -> 'true'.
-spec validate(cb_context:context(), path_token()) -> cb_context:context().
validate(Context) ->
validate_account_plans(Context, cb_context:req_verb(Context), cb_context:req_nouns(Context)).
validate(Context, ?SUMMARY) ->
summarize_accounts(Context, cb_context:req_verb(Context));
validate(Context, Path) ->
validate_plan(Context, Path, cb_context:req_verb(Context), cb_context:req_nouns(Context)).

Expand Down Expand Up @@ -275,8 +295,23 @@ request_price(AccountId, PlanId, Acc) ->
Acc
end.

-spec summarize_accounts(cb_context:context(), path_token()) -> cb_context:context().
summarize_accounts(Context, ?HTTP_GET) ->
AccountId = cb_context:account_id(Context),
Accounts = [AccountId] ++ business_partner_util:get_children(AccountId),
Response = lists:map(fun summarize_account/1, Accounts),
crossbar_util:response(Response, Context).

-spec summarize_account(ne_binary()) -> wh_json:object().
summarize_account(AccountId) ->
{'ok', AccountDoc} = kz_account:fetch(AccountId),
wh_json:set_values([{<<"account_id">>, AccountId}
,{<<"account_name">>, kz_account:name(AccountDoc)}
,{<<"reseller_id">>, kz_account:reseller_id(AccountDoc)}
,{<<"business_partner_search_key">>, business_partner_util:get_search_key(AccountId)}
], account_plans:summary(AccountId)).

%%--------------------------------------------------------------------
%% @private
%% @doc
%% Normalizes the resuts of a view
%% @end
Expand Down
22 changes: 22 additions & 0 deletions core/account_plans-1.0.0/src/account_plans.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
-export([check_category_limits/2]).
-export([can_add_entity/2]).

-export([summary/1]).

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

-define(INFINITE_QTY, -1).
Expand Down Expand Up @@ -197,5 +199,25 @@ check_category_limits(_AccountId, Category, Strict, Plan, Services) ->
{'true', 0, Qty}
end.

%%--------------------------------------------------------------------
%% @public
%% @doc
%% Gives a summary of the limits and usage of plans on an account
%% @end
%%--------------------------------------------------------------------
-spec summary(ne_binary()) -> wh_json:object().
summary(AccountId) ->
Services = wh_services:reconcile(AccountId),
lists:foldl(fun(Category, Summary) ->
Plan = merge_account_plans(AccountId),
%% Use many arg version for performance
{Valid, Allowed, Usage} = check_category_limits(AccountId, Category, 'false', Plan, Services),
NewSummary = wh_json:set_values([{<<Category/binary, ".allowed">>, Allowed}, {<<Category/binary, ".using">>, Usage}], Summary),
case Valid of
'false' -> wh_json:set_value(<<"error">>, 'true', NewSummary);
'true' -> NewSummary
end
end, {[{<<"error">>, 'false'}]}, get_checked_categories()).

-spec can_add_entity(ne_binary(), ne_binary()) -> 'true'|'false'.
can_add_entity(AccountId, Category) -> check_category_limits(AccountId, Category, 'true').

0 comments on commit e3632a5

Please sign in to comment.