diff --git a/engine/action.go b/engine/action.go index 0ed3b4774..bf7b86b2a 100644 --- a/engine/action.go +++ b/engine/action.go @@ -107,7 +107,7 @@ func getActionFunc(typ string) (actionTypeFunc, bool) { utils.MetaRemoveSessionCosts: removeSessionCosts, utils.MetaRemoveExpired: removeExpired, utils.MetaPostEvent: postEvent, - utils.MetaCDRAccount: resetAccount, + utils.MetaCDRAccount: resetAccountCDR, } f, exists := actionFuncMap[typ] return f, exists @@ -1051,7 +1051,8 @@ func postEvent(ub *Account, a *Action, acs Actions, extraData interface{}) error return err } -func resetAccount(ub *Account, action *Action, acts Actions, _ interface{}) error { +// resetAccountCDR resets the account out of values from CDR +func resetAccountCDR(ub *Account, action *Action, acts Actions, _ interface{}) error { if ub == nil { return errors.New("nil account") } diff --git a/engine/actions_it_test.go b/engine/actions_it_test.go index 1d26e5b4e..013c9aa0e 100644 --- a/engine/actions_it_test.go +++ b/engine/actions_it_test.go @@ -52,6 +52,7 @@ var ( testActionsitThresholdCgrRpcAction, testActionsitThresholdPostEvent, testActionsitSetSDestinations, + testActionsitresetAccountCDR, testActionsitStopCgrEngine, } ) @@ -809,6 +810,97 @@ func testActionsitSetSDestinations(t *testing.T) { } +func testActionsitresetAccountCDR(t *testing.T) { + var reply string + account := "123456789" + + attrsSetAccount := &utils.AttrSetAccount{ + Tenant: "cgrates.org", + Account: "123456789", + } + if err := actsLclRpc.Call(utils.APIerSv1SetAccount, attrsSetAccount, &reply); err != nil { + t.Error("Got error on APIerSv1.SetAccount: ", err.Error()) + } else if reply != utils.OK { + t.Errorf("Calling APIerSv1.SetAccount received: %s", reply) + } + + attrsAA := &utils.AttrSetActions{ + ActionsId: "resetAccountCDR", + Actions: []*utils.TPAction{ + {Identifier: utils.MetaCDRAccount, ExpiryTime: utils.UNLIMITED, Weight: 20.0}, + }, + } + if err := actsLclRpc.Call(utils.APIerSv2SetActions, attrsAA, &reply); err != nil && err.Error() != utils.ErrExists.Error() { + t.Error("Got error on APIerSv2.SetActions: ", err.Error()) + } else if reply != utils.OK { + t.Errorf("Calling APIerSv2.SetActions received: %s", reply) + } + + var acc Account + attrs2 := &utils.AttrGetAccount{Tenant: "cgrates.org", Account: account} + var uuid string + if err := actsLclRpc.Call(utils.APIerSv2GetAccount, attrs2, &acc); err != nil { + t.Error("Got error on APIerSv1.GetAccount: ", err.Error()) + } else { + voice := acc.BalanceMap[utils.VOICE] + for _, u := range voice { + uuid = u.Uuid + break + } + } + args := &CDRWithArgDispatcher{ + CDR: &CDR{ + Tenant: "cgrates.org", + OriginID: "testDsp", + OriginHost: "192.168.1.1", + Source: "testDsp", + RequestType: utils.META_RATED, + RunID: utils.MetaDefault, + PreRated: true, + Account: account, + Subject: account, + Destination: "1002", + AnswerTime: time.Date(2018, 8, 24, 16, 00, 26, 0, time.UTC), + Usage: time.Duration(2) * time.Minute, + CostDetails: &EventCost{ + CGRID: utils.UUIDSha1Prefix(), + RunID: utils.MetaDefault, + AccountSummary: &AccountSummary{ + Tenant: "cgrates.org", + ID: account, + BalanceSummaries: []*BalanceSummary{ + { + UUID: uuid, + ID: "ID", + Type: utils.VOICE, + Value: float64(10 * time.Second), + }, + }, + }, + }, + }, + } + if err := actsLclRpc.Call(utils.CDRsV1ProcessCDR, args, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("Received: %s", reply) + } + time.Sleep(100 * time.Millisecond) + + attrsEA := &utils.AttrExecuteAction{Tenant: "cgrates.org", Account: account, ActionsId: attrsAA.ActionsId} + if err := actsLclRpc.Call(utils.APIerSv1ExecuteAction, attrsEA, &reply); err != nil { + t.Error("Got error on APIerSv1.ExecuteAction: ", err.Error()) + } else if reply != utils.OK { + t.Errorf("Calling APIerSv1.ExecuteAction received: %s", reply) + } + + if err := actsLclRpc.Call(utils.APIerSv2GetAccount, attrs2, &acc); err != nil { + t.Error("Got error on APIerSv1.GetAccount: ", err.Error()) + } else if tv := acc.BalanceMap[utils.VOICE].GetTotalValue(); tv != float64(10*time.Second) { + t.Errorf("Calling APIerSv1.GetBalance expected: %f, received: %f", float64(10*time.Second), tv) + } +} + func testActionsitStopCgrEngine(t *testing.T) { if err := KillEngine(*waitRater); err != nil { t.Error(err) diff --git a/utils/consts.go b/utils/consts.go index 84b833031..b224142f1 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -843,7 +843,7 @@ const ( MetaRemoveSessionCosts = "*remove_session_costs" MetaRemoveExpired = "*remove_expired" MetaPostEvent = "*post_event" - MetaCDRAccount = "*cdr_account" + MetaCDRAccount = "*reset_account_cdr" ) // Migrator Metas