/
wh_service_items.erl
119 lines (107 loc) · 4.3 KB
/
wh_service_items.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
%%%-------------------------------------------------------------------
%%% @copyright (C) 2012, VoIP, INC
%%% @doc
%%%
%%% @end
%%% @contributors
%%%-------------------------------------------------------------------
-module(wh_service_items).
-export([empty/0]).
-export([to_list/1]).
-export([find/3]).
-export([update/2]).
-type(items() :: dict:new()).
-export_type([items/0]).
-include_lib("whistle_services/src/whistle_services.hrl").
%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec empty/0 :: () -> items().
empty() ->
dict:new().
%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec to_list/1 :: (items()) -> [wh_service_item:item(),...] | [].
to_list(ServiceItems) ->
[ServiceItem || {_, ServiceItem} <- dict:to_list(ServiceItems)].
%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec find/3 :: (ne_binary(), ne_binary(), items()) -> wh_service_item:item().
find(Category, Item, ServiceItems) ->
Key = {Category, Item},
case dict:find(Key, ServiceItems) of
{ok, I} -> I;
error -> wh_service_item:empty()
end.
%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec update/2 :: (wh_service_item:item(), items()) -> wh_service_item:items().
update(ServiceItem, ServiceItems) ->
_ = log_update(ServiceItem),
Key = {wh_service_item:category(ServiceItem), wh_service_item:item(ServiceItem)},
dict:store(Key, ServiceItem, ServiceItems).
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Nasty conditional logging (but functional).... bleh...
%% @end
%%--------------------------------------------------------------------
-spec log_update/1 :: (wh_service_item:item()) -> no_return.
log_update(ServiceItem) ->
Category = wh_service_item:category(ServiceItem),
Item = wh_service_item:item(ServiceItem),
_ = log_update_rate(Category, Item, ServiceItem),
_ = log_update_cumulative_discount(Category, Item, ServiceItem),
log_update_single_discount(Category, Item, ServiceItem).
-spec log_update_rate/3 :: (ne_binary(), ne_binary(), wh_service_item:item()) -> 'ok'.
log_update_rate(Category, Item, ServiceItem) ->
case wh_service_item:rate(ServiceItem) of
undefined ->
lager:debug("set '~s/~s' with quantity ~p @ default rate"
,[Category, Item, wh_service_item:quantity(ServiceItem)]);
Rate ->
lager:debug("set '~s/~s' with quantity ~p @ $~p"
,[Category, Item, wh_service_item:quantity(ServiceItem), Rate])
end.
-spec log_update_cumulative_discount/3 :: (ne_binary(), ne_binary(), wh_service_item:item()) -> 'ok'.
log_update_cumulative_discount(Category, Item, ServiceItem) ->
CumulativeDiscount = wh_service_item:cumulative_discount(ServiceItem),
case wh_util:is_empty(CumulativeDiscount)
orelse wh_service_item:cumulative_discount_rate(ServiceItem)
of
true -> ok;
undefined ->
lager:debug("set '~s/~s' cumulative discount with quantity ~p @ default rate"
,[Category, Item, CumulativeDiscount]);
CumulativeRate ->
lager:debug("set '~s/~s' cumulative discount with quantity ~p @ $~p"
,[Category, Item, CumulativeDiscount, CumulativeRate])
end.
-spec log_update_single_discount/3 :: (ne_binary(), ne_binary(), wh_service_item:item()) -> 'ok'.
log_update_single_discount(Category, Item, ServiceItem) ->
case wh_service_item:single_discount(ServiceItem)
andalso wh_service_item:single_discount_rate(ServiceItem)
of
false -> ok;
undefined ->
lager:debug("set '~s/~s' single discount at default rate"
,[Category, Item]);
SingleRate ->
lager:debug("set '~s/~s' single discount for $~p"
,[Category, Item, SingleRate])
end.