Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KAZOO-798: Add to transaction metadata field to be able to add call info... #17

Merged
merged 10 commits into from
Apr 6, 2013
20 changes: 10 additions & 10 deletions ecallmgr/src/ecallmgr_call_command.erl
Original file line number Diff line number Diff line change
Expand Up @@ -675,15 +675,7 @@ bridge_handle_ringback(Node, UUID, JObj) ->
case wh_json:get_value(<<"Ringback">>, JObj) of
'undefined' ->
case wh_json:get_value([<<"Custom-Channel-Vars">>, <<"Ringback">>], JObj) of
'undefined' ->
case ecallmgr_fs_channel:import_moh(UUID) of
'true' ->
[{"application", "export hold_music=${hold_music}"}
,{"application", "set import=hold_music"}
|DP
];
'false' -> DP
end;
'undefined' -> 'ok';
Media ->
Stream = ecallmgr_util:media_path(Media, extant, UUID, JObj),
lager:debug("bridge has custom ringback in channel vars: ~s", [Stream]),
Expand Down Expand Up @@ -712,7 +704,15 @@ bridge_handle_hold_media(DP, Node, UUID, JObj) ->
case wh_json:get_value(<<"Hold-Media">>, JObj) of
'undefined' ->
case wh_json:get_value([<<"Custom-Channel-Vars">>, <<"Hold-Media">>], JObj) of
'undefined' -> DP;
'undefined' ->
case ecallmgr_fs_channel:import_moh(UUID) of
'true' ->
[{"application", "export hold_music=${hold_music}"}
,{"application", "set import=hold_music"}
|DP
];
'false' -> DP
end;
Media ->
Stream = ecallmgr_util:media_path(Media, extant, UUID, JObj),
lager:debug("bridge has custom music-on-hold in channel vars: ~s", [Stream]),
Expand Down
22 changes: 19 additions & 3 deletions ecallmgr/src/ecallmgr_call_control.erl
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,11 @@ handle_cast({channel_redirected, _}, #state{callid=CallId
,controller_q=ControllerQ
}=State) ->
lager:debug("call control has been redirected, shutting down immediately"),
spawn(fun() -> publish_control_transfer(ControllerQ, CallId) end),
ConsumerPid = wh_amqp_channel:consumer_pid(),
spawn(fun() ->
wh_amqp_channel:consumer_pid(ConsumerPid),
publish_control_transfer(ControllerQ, CallId)
end),
{stop, normal, State};
handle_cast({transferer, _}, #state{last_removed_leg=undefined
,other_legs=[]
Expand All @@ -298,7 +302,11 @@ handle_cast({transferer, _}, #state{callid=CallId
,controller_q=ControllerQ
}=State) ->
lager:debug("call control has been transfered"),
spawn(fun() -> publish_control_transfer(ControllerQ, CallId) end),
ConsumerPid = wh_amqp_channel:consumer_pid(),
spawn(fun() ->
wh_amqp_channel:consumer_pid(ConsumerPid),
publish_control_transfer(ControllerQ, CallId)
end),
{stop, normal, State};
handle_cast({transferee, JObj}, #state{other_legs=Legs
,node=Node
Expand All @@ -314,7 +322,11 @@ handle_cast({transferee, JObj}, #state{other_legs=Legs
lager:debug("new callid is the same as the old callid"),
{noreply, State};
NewCallId ->
spawn(fun() -> publish_callid_update(PrevCallId, NewCallId, queue_name(Self)) end),
ConsumerPid = wh_amqp_channel:consumer_pid(),
spawn(fun() ->
wh_amqp_channel:consumer_pid(ConsumerPid),
publish_callid_update(PrevCallId, NewCallId, queue_name(Self))
end),
lager:debug("updating callid from ~s to ~s", [PrevCallId, NewCallId]),
put(callid, NewCallId),
lager:debug("removing call event bindings for ~s", [PrevCallId]),
Expand Down Expand Up @@ -350,8 +362,10 @@ handle_cast({add_leg, JObj}, #state{other_legs=Legs
true -> {noreply, State};
false ->
lager:debug("added leg ~s to call", [LegId]),
ConsumerPid = wh_amqp_channel:consumer_pid(),
_ = spawn(fun() ->
_ = put(callid, CallId),
wh_amqp_channel:consumer_pid(ConsumerPid),
publish_leg_addition(JObj)
end),
{noreply, State#state{other_legs=[LegId|Legs]}}
Expand All @@ -370,8 +384,10 @@ handle_cast({rm_leg, JObj}, #state{other_legs=Legs
{noreply, State};
true ->
lager:debug("removed leg ~s from call", [LegId]),
ConsumerPid = wh_amqp_channel:consumer_pid(),
_ = spawn(fun() ->
put(callid, CallId),
wh_amqp_channel:consumer_pid(ConsumerPid),
publish_leg_removal(JObj)
end),
{noreply, State#state{other_legs=lists:delete(LegId, Legs)
Expand Down
4 changes: 2 additions & 2 deletions lib/whistle-1.0.0/src/wh_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ a1hash(User, Realm, Password) ->
-spec floor(integer() | float()) -> integer().
floor(X) when X < 0 ->
T = trunc(X),
case X - T =:= 0 of
case X - T == 0 of
'true' -> T;
'false' -> T - 1
end;
Expand All @@ -701,7 +701,7 @@ floor(X) -> trunc(X).
ceiling(X) when X < 0 -> trunc(X);
ceiling(X) ->
T = trunc(X),
case X - T =:= 0 of
case X - T == 0 of
'true' -> T;
'false' -> T + 1
end.
Expand Down
24 changes: 24 additions & 0 deletions lib/whistle_transactions-1.0.0/src/wh_transaction.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
-export([number/1]).
-export([feature/1]).
-export([bookkeeper_info/1]).
-export([metadata/1]).
-export([reason/1]).
-export([code/1]).
-export([amount/1]).
Expand All @@ -34,6 +35,7 @@
-export([set_number/2]).
-export([set_feature/2]).
-export([set_bookkeeper_info/2]).
-export([set_metadata/2]).
-export([set_description/2]).
-export([set_call_id/2]).
-export([set_sub_account_id/2]).
Expand All @@ -53,6 +55,7 @@
,number :: api_binary()
,feature :: api_binary()
,bookkeeper_info :: 'undefined' | wh_json:object()
,metadata :: 'undefined' | wh_json:object()
,pvt_reason :: ne_binary()
,pvt_code :: non_neg_integer()
,pvt_amount :: non_neg_integer()
Expand Down Expand Up @@ -139,6 +142,15 @@ feature(#wh_transaction{feature=Feature}) ->
bookkeeper_info(#wh_transaction{bookkeeper_info=BookkeeperInfo}) ->
BookkeeperInfo.

%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
metadata(#wh_transaction{metadata=MetaData}) ->
MetaData.

%%--------------------------------------------------------------------
%% @public
%% @doc
Expand Down Expand Up @@ -283,6 +295,16 @@ set_feature(Feature, #wh_transaction{}=Transaction) ->
set_bookkeeper_info(BookkeeperInfo, #wh_transaction{}=Transaction) ->
Transaction#wh_transaction{bookkeeper_info=BookkeeperInfo}.

%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
set_metadata(MetaData, #wh_transaction{}=Transaction) ->
Transaction#wh_transaction{metadata=MetaData}.


%%--------------------------------------------------------------------
%% @public
%% @doc
Expand Down Expand Up @@ -344,6 +366,7 @@ to_json(#wh_transaction{}=T) ->
,{<<"number">>, T#wh_transaction.number}
,{<<"feature">>, T#wh_transaction.feature}
,{<<"bookkeeper_info">>, T#wh_transaction.bookkeeper_info}
,{<<"metadata">>, T#wh_transaction.metadata}
,{<<"pvt_reason">>, T#wh_transaction.pvt_reason}
,{<<"pvt_code">>, T#wh_transaction.pvt_code}
,{<<"pvt_amount">>, T#wh_transaction.pvt_amount}
Expand Down Expand Up @@ -423,6 +446,7 @@ from_json(JObj) ->
,number = wh_json:get_ne_value(<<"number">>, JObj)
,feature = wh_json:get_ne_value(<<"feature">>, JObj)
,bookkeeper_info = wh_json:get_ne_value(<<"bookkeeper_info">>, JObj)
,metadata = wh_json:get_ne_value(<<"metadata">>, JObj)
,pvt_reason = wh_json:get_ne_value(<<"pvt_reason">>, JObj)
,pvt_code = wh_json:get_integer_value(<<"pvt_code">>, JObj, 0)
,pvt_amount = wh_json:get_integer_value(<<"pvt_amount">>, JObj, 0)
Expand Down
13 changes: 6 additions & 7 deletions lib/whistle_transactions-1.0.0/src/wh_transactions.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
,call_charges/4
]).
-export([filter_by_reason/2]).
-export([fetch_since/2]).
-export([fetch_since/3]).
-export([fetch_last/2]).
-export([save/1]).
-export([remove/1]).
Expand Down Expand Up @@ -150,17 +150,16 @@ fetch_last(AccountId, Count) ->
%% fetch last transaction from date to now
%% @end
%%--------------------------------------------------------------------
-spec fetch_since(ne_binary(), integer()) -> wh_transactions().
fetch_since(AccountId, Date) ->
-spec fetch_since(ne_binary(), integer(), integer()) -> wh_transactions().
fetch_since(AccountId, From, To) ->
AccountDB = wh_util:format_account_id(AccountId, 'encoded'),
Now = wh_util:current_tstamp(),
ViewOptions = [{'startkey', Date}
,{'endkey', Now}
ViewOptions = [{'startkey', From}
,{'endkey', To}
,'include_docs'
],
case couch_mgr:get_results(AccountDB, <<"transactions/by_timestamp">>, ViewOptions) of
{'ok', []} ->
lager:debug("no transactions for that range from ~p to ~p on ~p", [Date, Now, AccountId]),
lager:debug("no transactions for that range from ~p to ~p on ~p", [From, To, AccountId]),
[];
{'ok', ViewRes} ->
viewres_to_recordlist(ViewRes)
Expand Down
22 changes: 22 additions & 0 deletions lib/whistle_transactions-1.0.0/src/wht_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ collapse_call_transaction(CallId, JObj, Calls) ->
Routines = [fun(C) -> collapse_created_time(C, JObj) end
,fun(C) -> collapse_ended_time(C, JObj) end
,fun(C) -> collapse_amount(C, JObj) end
,fun(C) -> collapse_metadata(C, JObj) end
],
C = lists:foldl(fun(F, C) -> F(C) end, Call, Routines),
dict:store(CallId, C, Calls)
Expand Down Expand Up @@ -328,6 +329,27 @@ collapse_amount(Call, JObj) ->
CurrentAmount = wh_json:get_value(<<"amount">>, Call, 0),
MaybeAmount = get_amount(JObj),
wh_json:set_value(<<"amount">>, MaybeAmount+CurrentAmount, Call).


%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec collapse_metadata(wh_json:object(), [wh_json:object(), ...]) -> wh_json:object().
collapse_metadata(Call, JObj) ->
case wh_json:get_value(<<"metadata">>, Call, 'undefined') of
'undefined' ->
case wh_json:get_value(<<"metadata">>, JObj, 'undefined') of
'undefined' ->
Call;
MetaData ->
wh_json:set_value(<<"metadata">>, MetaData, Call)
end;
_ ->
Call
end.

%%--------------------------------------------------------------------
%% @private
Expand Down
2 changes: 1 addition & 1 deletion whistle_apps/apps/crossbar/priv/couchdb/account/cdrs.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"language": "javascript",
"views": {
"crossbar_listing": {
"map": "function (doc) {if (doc.pvt_deleted || doc.pvt_type != 'cdr') return;if (doc.pvt_type == 'cdr' && doc.custom_channel_vars && (doc.custom_channel_vars.account_billing == 'per_minute' || doc.custom_channel_vars.account_billing == 'per_minute_limit') && doc.custom_channel_vars.account_id == doc.pvt_account_id && doc.pvt_vsn && parseInt(doc.pvt_vsn) == 2) {var Secs = (parseInt(doc.billing_seconds) || 0) - (parseInt(doc.custom_channel_vars.billing_seconds_offset) || 0);var Cost = 0;if (Secs >= 1) {var R = parseInt(doc.custom_channel_vars.rate) || 0;var RInc = parseInt(doc.custom_channel_vars.increment) || 60;var RMin = parseInt(doc.custom_channel_vars.rate_minimum) || 0;var Sur = parseInt(doc.custom_channel_vars.surcharge) || 0;if (RInc < 1) {RInc = 60;}if (Secs <= RMin) {Cost = Sur + ((RMin / 60) * R);} else {Cost = Sur + ((RMin / 60) * R) + (Math.ceil((Secs - RMin) / RInc) * ((RInc / 60) * R));}}}if (Cost) {var Discount = (parseInt(doc.custom_channel_vars.discount_percentage) || 0) * 0.01 * Cost;emit(doc.pvt_created, {id: doc._id,caller_id_name: doc.caller_id_name,caller_id_number: doc.caller_id_number,callee_id_name: doc.callee_id_name,callee_id_number: doc.callee_id_number,duration_seconds: doc.duration_seconds,billing_seconds: doc.billing_seconds,timestamp: doc.timestamp,hangup_cause: doc.hangup_cause,other_leg_call_id: doc.other_leg_call_id,owner_id: doc.custom_channel_vars.owner_id,to: doc.to_uri,from: doc.from_uri,inception: doc.custom_channel_vars.inception,cost: Cost - Discount});} else {emit(doc.pvt_created, {id: doc._id,caller_id_name: doc.caller_id_name,caller_id_number: doc.caller_id_number,callee_id_name: doc.callee_id_name,callee_id_number: doc.callee_id_number,duration_seconds: doc.duration_seconds,billing_seconds: doc.billing_seconds,timestamp: doc.timestamp,hangup_cause: doc.hangup_cause,other_leg_call_id: doc.other_leg_call_id,owner_id: doc.custom_channel_vars.owner_id,to: doc.to_uri,from: doc.from_uri,inception: doc.custom_channel_vars.inception});}}"
"map": "function (doc) {if (doc.pvt_deleted || doc.pvt_type != 'cdr') return;if (doc.pvt_type == 'cdr' && doc.custom_channel_vars && (doc.custom_channel_vars.account_billing == 'per_minute' || doc.custom_channel_vars.account_billing == 'per_minute_limit') && doc.custom_channel_vars.account_id == doc.pvt_account_id && doc.pvt_vsn && parseInt(doc.pvt_vsn) == 2) {var Secs = (parseInt(doc.billing_seconds) || 0) - (parseInt(doc.custom_channel_vars.billing_seconds_offset) || 0);var Cost = 0;if (Secs >= 1) {var R = parseInt(doc.custom_channel_vars.rate) || 0;var RInc = parseInt(doc.custom_channel_vars.rate_increment) || 60;var RMin = parseInt(doc.custom_channel_vars.rate_minimum) || 0;var Sur = parseInt(doc.custom_channel_vars.surcharge) || 0;if (RInc < 1) {RInc = 60;}if (Secs <= RMin) {Cost = Sur + ((RMin / 60) * R);} else {Cost = Sur + ((RMin / 60) * R) + (Math.ceil((Secs - RMin) / RInc) * ((RInc / 60) * R));}}}if (Cost) {var Discount = (parseInt(doc.custom_channel_vars.discount_percentage) || 0) * 0.01 * Cost;emit(doc.pvt_created, {id: doc._id,caller_id_name: doc.caller_id_name,caller_id_number: doc.caller_id_number,callee_id_name: doc.callee_id_name,callee_id_number: doc.callee_id_number,duration_seconds: doc.duration_seconds,billing_seconds: doc.billing_seconds,timestamp: doc.timestamp,hangup_cause: doc.hangup_cause,other_leg_call_id: doc.other_leg_call_id,owner_id: doc.custom_channel_vars.owner_id,to: doc.to_uri,from: doc.from_uri,inception: doc.custom_channel_vars.inception,cost: Cost - Discount});} else {emit(doc.pvt_created, {id: doc._id,caller_id_name: doc.caller_id_name,caller_id_number: doc.caller_id_number,callee_id_name: doc.callee_id_name,callee_id_number: doc.callee_id_number,duration_seconds: doc.duration_seconds,billing_seconds: doc.billing_seconds,timestamp: doc.timestamp,hangup_cause: doc.hangup_cause,other_leg_call_id: doc.other_leg_call_id,owner_id: doc.custom_channel_vars.owner_id,to: doc.to_uri,from: doc.from_uri,inception: doc.custom_channel_vars.inception});}}"
},
"listing_by_owner" : {
"map": "function(doc) { if(doc.pvt_deleted || !doc.custom_channel_vars.owner_id || doc.pvt_type != 'cdr') return; emit([doc.custom_channel_vars.owner_id, doc.pvt_created], {'id':doc._id, 'caller_id_name': doc.caller_id_name, 'caller_id_number': doc.caller_id_number, 'callee_id_name': doc.callee_id_name, 'callee_id_number': doc.callee_id_number, 'duration_seconds': doc.duration_seconds, 'timestamp': doc.timestamp, 'hangup_cause':doc.hangup_cause, 'other_leg_call_id':doc.other_leg_call_id, 'call_direction':doc.call_direction, 'to': doc.to_uri, 'from': doc.from_uri, 'inception':doc.custom_channel_vars.inception})}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"map": "function(doc) { if ( (doc.pvt_type != 'credit' && doc.pvt_type != 'debit') || doc.pvt_deleted) return; emit(doc.pvt_created, doc._id); }"
},
"reconcile_by_callid": {
"map": "function (doc) {if (doc.pvt_deleted || (doc.pvt_type != 'cdr' && doc.pvt_type != 'credit' && doc.pvt_type != 'debit') || !doc.pvt_vsn || parseInt(doc.pvt_vsn) != 2) return;if (doc.pvt_type == 'cdr' && doc.custom_channel_vars && (doc.custom_channel_vars.account_billing == 'per_minute' || doc.custom_channel_vars.account_billing == 'per_minute_limit') && doc.custom_channel_vars.account_id == doc.pvt_account_id) {var Secs = (parseInt(doc.billing_seconds) || 0) - (parseInt(doc.custom_channel_vars.billing_seconds_offset) || 0);var Cost = 0;if (Secs >= 1) {var R = parseInt(doc.custom_channel_vars.rate) || 0;var RInc = parseInt(doc.custom_channel_vars.increment) || 60;var RMin = parseInt(doc.custom_channel_vars.rate_minimum) || 0;var Sur = parseInt(doc.custom_channel_vars.surcharge) || 0;if (RInc < 1) {RInc = 60;}if (Secs <= RMin) {Cost = Sur + ((RMin / 60) * R);} else {Cost = Sur + ((RMin / 60) * R) + (Math.ceil((Secs - RMin) / RInc) * ((RInc / 60) * R));}}if (Cost) {var Discount = (parseInt(doc.custom_channel_vars.discount_percentage) || 0) * 0.01 * Cost;emit(doc._id, Cost - Discount);}} else if (doc.pvt_code && parseInt(doc.pvt_code) == 1001 && doc.call_id) {var modifier = (doc.pvt_type == 'credit' ? 1 : -1);var amount = doc.pvt_amount;emit(doc.call_id, amount * modifier);}}",
"map": "function (doc) {if (doc.pvt_deleted || (doc.pvt_type != 'cdr' && doc.pvt_type != 'credit' && doc.pvt_type != 'debit') || !doc.pvt_vsn || parseInt(doc.pvt_vsn) != 2) return;if (doc.pvt_type == 'cdr' && doc.custom_channel_vars && (doc.custom_channel_vars.account_billing == 'per_minute' || doc.custom_channel_vars.account_billing == 'per_minute_limit') && doc.custom_channel_vars.account_id == doc.pvt_account_id) {var Secs = (parseInt(doc.billing_seconds) || 0) - (parseInt(doc.custom_channel_vars.billing_seconds_offset) || 0);var Cost = 0;if (Secs >= 1) {var R = parseInt(doc.custom_channel_vars.rate) || 0;var RInc = parseInt(doc.custom_channel_vars.rate_increment) || 60;var RMin = parseInt(doc.custom_channel_vars.rate_minimum) || 0;var Sur = parseInt(doc.custom_channel_vars.surcharge) || 0;if (RInc < 1) {RInc = 60;}if (Secs <= RMin) {Cost = Sur + ((RMin / 60) * R);} else {Cost = Sur + ((RMin / 60) * R) + (Math.ceil((Secs - RMin) / RInc) * ((RInc / 60) * R));}}if (Cost) {var Discount = (parseInt(doc.custom_channel_vars.discount_percentage) || 0) * 0.01 * Cost;emit(doc._id, Cost - Discount);}} else if (doc.pvt_code && parseInt(doc.pvt_code) == 1001 && doc.call_id) {var modifier = (doc.pvt_type == 'credit' ? 1 : -1);var amount = doc.pvt_amount;emit(doc.call_id, amount * modifier);}}",
"reduce": "_sum"
}
}
Expand Down
Loading