From 8d0c125b629fd92a09bc6cf7bbd6a360195f3175 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 12 Jan 2016 21:53:34 +0200 Subject: [PATCH] added *transfer_monetary_default action fixes #273 --- engine/action.go | 68 ++++++++++++++++++++++++++++-------------- engine/actions_test.go | 56 ++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 23 deletions(-) diff --git a/engine/action.go b/engine/action.go index abd90f72c..734b86c64 100644 --- a/engine/action.go +++ b/engine/action.go @@ -48,29 +48,30 @@ type Action struct { } const ( - LOG = "*log" - RESET_TRIGGERS = "*reset_triggers" - SET_RECURRENT = "*set_recurrent" - UNSET_RECURRENT = "*unset_recurrent" - ALLOW_NEGATIVE = "*allow_negative" - DENY_NEGATIVE = "*deny_negative" - RESET_ACCOUNT = "*reset_account" - REMOVE_ACCOUNT = "*remove_account" - REMOVE_BALANCE = "*remove_balance" - TOPUP_RESET = "*topup_reset" - TOPUP = "*topup" - DEBIT_RESET = "*debit_reset" - DEBIT = "*debit" - RESET_COUNTERS = "*reset_counters" - ENABLE_ACCOUNT = "*enable_account" - DISABLE_ACCOUNT = "*disable_account" - ENABLE_DISABLE_BALANCE = "*enable_disable_balance" - CALL_URL = "*call_url" - CALL_URL_ASYNC = "*call_url_async" - MAIL_ASYNC = "*mail_async" - UNLIMITED = "*unlimited" - CDRLOG = "*cdrlog" - SET_DDESTINATIONS = "*set_ddestinations" + LOG = "*log" + RESET_TRIGGERS = "*reset_triggers" + SET_RECURRENT = "*set_recurrent" + UNSET_RECURRENT = "*unset_recurrent" + ALLOW_NEGATIVE = "*allow_negative" + DENY_NEGATIVE = "*deny_negative" + RESET_ACCOUNT = "*reset_account" + REMOVE_ACCOUNT = "*remove_account" + REMOVE_BALANCE = "*remove_balance" + TOPUP_RESET = "*topup_reset" + TOPUP = "*topup" + DEBIT_RESET = "*debit_reset" + DEBIT = "*debit" + RESET_COUNTERS = "*reset_counters" + ENABLE_ACCOUNT = "*enable_account" + DISABLE_ACCOUNT = "*disable_account" + ENABLE_DISABLE_BALANCE = "*enable_disable_balance" + CALL_URL = "*call_url" + CALL_URL_ASYNC = "*call_url_async" + MAIL_ASYNC = "*mail_async" + UNLIMITED = "*unlimited" + CDRLOG = "*cdrlog" + SET_DDESTINATIONS = "*set_ddestinations" + TRANSFER_MONETARY_DEFAULT = "*transfer_monetary_default" ) func (a *Action) Clone() *Action { @@ -133,6 +134,8 @@ func getActionFunc(typ string) (actionTypeFunc, bool) { return removeAccount, true case REMOVE_BALANCE: return removeBalance, true + case TRANSFER_MONETARY_DEFAULT: + return transferMonetaryDefault, true } return nil, false } @@ -588,6 +591,25 @@ func removeBalance(ub *Account, sq *StatsQueueTriggered, a *Action, acs Actions) return accountingStorage.SetAccount(ub) } +func transferMonetaryDefault(acc *Account, sq *StatsQueueTriggered, a *Action, acs Actions) error { + if _, exists := acc.BalanceMap[utils.MONETARY]; !exists { + return utils.ErrNotFound + } + defaultBalance := acc.GetDefaultMoneyBalance() + bChain := acc.BalanceMap[utils.MONETARY] + for _, balance := range bChain { + if balance.Uuid != defaultBalance.Uuid && + balance.Id != defaultBalance.Id { // extra caution + if balance.Value > 0 { + defaultBalance.Value += balance.Value + balance.Value = 0 + } + } + } + // update account in storage + return accountingStorage.SetAccount(acc) +} + // Structure to store actions according to weight type Actions []*Action diff --git a/engine/actions_test.go b/engine/actions_test.go index a3904b150..bccf168c6 100644 --- a/engine/actions_test.go +++ b/engine/actions_test.go @@ -1451,6 +1451,62 @@ func TestActionRemoveBalance(t *testing.T) { } } +func TestActionTransferMonetaryDefault(t *testing.T) { + err := accountingStorage.SetAccount( + &Account{ + Id: "cgrates.org:trans", + BalanceMap: map[string]BalanceChain{ + utils.MONETARY: BalanceChain{ + &Balance{ + Uuid: utils.GenUUID(), + Id: utils.META_DEFAULT, + Value: 10, + }, + &Balance{ + Uuid: utils.GenUUID(), + Value: 3, + }, + &Balance{ + Uuid: utils.GenUUID(), + Value: 6, + }, + &Balance{ + Uuid: utils.GenUUID(), + Value: -2, + }, + }, + }, + }) + if err != nil { + t.Errorf("error setting account: %v", err) + } + + a := &Action{ + ActionType: TRANSFER_MONETARY_DEFAULT, + } + + at := &ActionTiming{ + accountIDs: map[string]struct{}{"cgrates.org:trans": struct{}{}}, + actions: Actions{a}, + } + at.Execute() + + afterUb, err := accountingStorage.GetAccount("cgrates.org:trans") + if err != nil { + t.Error("account not found: ", err, afterUb) + } + if afterUb.BalanceMap[utils.MONETARY].GetTotalValue() != 17 || + afterUb.BalanceMap[utils.MONETARY][0].Value != 19 || + afterUb.BalanceMap[utils.MONETARY][1].Value != 0 || + afterUb.BalanceMap[utils.MONETARY][2].Value != 0 || + afterUb.BalanceMap[utils.MONETARY][3].Value != -2 { + for _, b := range afterUb.BalanceMap[utils.MONETARY] { + t.Logf("B: %+v", b) + } + t.Error("ransfer balance value: ", afterUb.BalanceMap[utils.MONETARY].GetTotalValue()) + } +} + /**************** Benchmarks ********************************/ func BenchmarkUUID(b *testing.B) {