For Balance operations via API (AddBalance, DebitBalance, etc..) use absolute value

This commit is contained in:
TeoV
2019-06-05 15:47:50 +03:00
committed by Dan Christian Bogos
parent ee28ac772c
commit 405b76c885
3 changed files with 80 additions and 3 deletions

View File

@@ -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))

View File

@@ -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)

View File

@@ -48,6 +48,9 @@
"cdrs": {
"enabled": true,
"chargers_conns":[
{"address": "127.0.0.1:2012", "transport":"*json"},
],
},