From 405b76c8853671d8a7814324cfbaf2b694effddb Mon Sep 17 00:00:00 2001 From: TeoV Date: Wed, 5 Jun 2019 15:47:50 +0300 Subject: [PATCH] For Balance operations via API (AddBalance, DebitBalance, etc..) use absolute value --- apier/v1/accounts.go | 7 +- apier/v1/accounts_it_test.go | 73 +++++++++++++++++++ .../samples/acc_balance_keep/cgrates.json | 3 + 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/apier/v1/accounts.go b/apier/v1/accounts.go index 1dbce0df6..c0fe86419 100644 --- a/apier/v1/accounts.go +++ b/apier/v1/accounts.go @@ -466,13 +466,14 @@ func (self *ApierV1) modifyBalance(aType string, attr *AttrAddBalance, reply *st if attr.Overwrite { aType += "_reset" // => *topup_reset/*debit_reset } + a := &engine.Action{ ActionType: aType, Balance: &engine.BalanceFilter{ Uuid: attr.BalanceUuid, ID: attr.BalanceId, Type: utils.StringPointer(attr.BalanceType), - Value: &utils.ValueFormula{Static: attr.Value}, + Value: &utils.ValueFormula{Static: math.Abs(attr.Value)}, ExpirationDate: expTime, RatingSubject: attr.RatingSubject, Weight: attr.Weight, @@ -559,7 +560,7 @@ func (self *ApierV1) SetBalance(attr *utils.AttrSetBalance, reply *string) error }, } if attr.Value != nil { - a.Balance.Value = &utils.ValueFormula{Static: *attr.Value} + a.Balance.Value = &utils.ValueFormula{Static: math.Abs(*attr.Value)} } if attr.DestinationIds != nil { a.Balance.DestinationIDs = utils.StringMapPointer(utils.ParseStringMap(*attr.DestinationIds)) @@ -629,7 +630,7 @@ func (self *ApierV1) RemoveBalances(attr *utils.AttrSetBalance, reply *string) e }, } if attr.Value != nil { - a.Balance.Value = &utils.ValueFormula{Static: *attr.Value} + a.Balance.Value = &utils.ValueFormula{Static: math.Abs(*attr.Value)} } if attr.DestinationIds != nil { a.Balance.DestinationIDs = utils.StringMapPointer(utils.ParseStringMap(*attr.DestinationIds)) diff --git a/apier/v1/accounts_it_test.go b/apier/v1/accounts_it_test.go index 9ef9cc6f1..3343939ef 100644 --- a/apier/v1/accounts_it_test.go +++ b/apier/v1/accounts_it_test.go @@ -52,6 +52,7 @@ var ( testAccITSetBalance, testAccITSetBalanceWithExtraData, testAccITSetBalanceWithExtraData2, + testAccITAddBalanceWithNegative, testAccITStopCgrEngine, } ) @@ -283,6 +284,78 @@ func testAccITSetBalanceWithExtraData2(t *testing.T) { } } +func testAccITAddBalanceWithNegative(t *testing.T) { + var acnt *engine.Account + attrAcc := &utils.AttrGetAccount{ + Tenant: "cgrates.org", + Account: "AddBalanceWithNegative", + } + + if err := accRPC.Call("ApierV2.GetAccount", attrAcc, &acnt); err.Error() != utils.ErrNotFound.Error() { + t.Error(err) + } + + //topup with a negative value + var reply string + attrs := &AttrAddBalance{ + Tenant: "cgrates.org", + Account: "AddBalanceWithNegative", + BalanceType: "*monetary", + Value: -3.5, + } + if err := accRPC.Call("ApierV1.AddBalance", attrs, &reply); err != nil { + t.Error("Got error on ApierV1.AddBalance: ", err.Error()) + } else if reply != "OK" { + t.Errorf("Calling ApierV1.AddBalance received: %s", reply) + } + //give time to create the account and execute the action + time.Sleep(50 * time.Millisecond) + + attrAcc = &utils.AttrGetAccount{ + Tenant: "cgrates.org", + Account: "AddBalanceWithNegative", + } + if err := accRPC.Call("ApierV2.GetAccount", attrAcc, &acnt); err != nil { + t.Error(err) + } else if acnt.BalanceMap[utils.MONETARY].GetTotalValue() != 3.5 { + t.Errorf("Unexpected balance received : %+v", acnt.BalanceMap[utils.MONETARY].GetTotalValue()) + } + + if err := accRPC.Call("ApierV1.DebitBalance", &AttrAddBalance{ + Tenant: "cgrates.org", + Account: "AddBalanceWithNegative", + BalanceType: utils.MONETARY, + Value: 2, + }, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("Received: %s", reply) + } + + if err := accRPC.Call("ApierV2.GetAccount", attrAcc, &acnt); err != nil { + t.Error(err) + } else if acnt.BalanceMap[utils.MONETARY].GetTotalValue() != 1.5 { + t.Errorf("Unexpected balance received : %+v", acnt.BalanceMap[utils.MONETARY].GetTotalValue()) + } + + if err := accRPC.Call("ApierV1.DebitBalance", &AttrAddBalance{ + Tenant: "cgrates.org", + Account: "AddBalanceWithNegative", + BalanceType: utils.MONETARY, + Value: -1, + }, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("Received: %s", reply) + } + + if err := accRPC.Call("ApierV2.GetAccount", attrAcc, &acnt); err != nil { + t.Error(err) + } else if acnt.BalanceMap[utils.MONETARY].GetTotalValue() != 0.5 { + t.Errorf("Unexpected balance received : %+v", acnt.BalanceMap[utils.MONETARY].GetTotalValue()) + } +} + func testAccITStopCgrEngine(t *testing.T) { if err := engine.KillEngine(100); err != nil { t.Error(err) diff --git a/data/conf/samples/acc_balance_keep/cgrates.json b/data/conf/samples/acc_balance_keep/cgrates.json index e9e085523..a667a4864 100644 --- a/data/conf/samples/acc_balance_keep/cgrates.json +++ b/data/conf/samples/acc_balance_keep/cgrates.json @@ -48,6 +48,9 @@ "cdrs": { "enabled": true, + "chargers_conns":[ + {"address": "127.0.0.1:2012", "transport":"*json"}, + ], },