diff --git a/apier/v1/apier.go b/apier/v1/apier.go index 70673cc57..b337c3410 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -93,11 +93,11 @@ type AttrAddBalance struct { BalanceId string Direction string Value float64 + Overwrite bool // When true it will reset if the balance is already there } func (self *ApierV1) AddBalance(attr *AttrAddBalance, reply *string) error { tag := fmt.Sprintf("%s:%s:%s", attr.Direction, attr.Tenant, attr.Account) - if _, err := self.AccountDb.GetUserBalance(tag); err != nil { // create user balance if not exists ub := &engine.UserBalance{ @@ -108,7 +108,6 @@ func (self *ApierV1) AddBalance(attr *AttrAddBalance, reply *string) error { return err } } - at := &engine.ActionTiming{ UserBalanceIds: []string{tag}, } @@ -116,9 +115,11 @@ func (self *ApierV1) AddBalance(attr *AttrAddBalance, reply *string) error { if attr.Direction == "" { attr.Direction = engine.OUTBOUND } - - at.SetActions(engine.Actions{&engine.Action{ActionType: engine.TOPUP, BalanceId: attr.BalanceId, Direction: attr.Direction, Balance: &engine.Balance{Value: attr.Value}}}) - + aType := engine.TOPUP + if attr.Overwrite { + aType = engine.TOPUP_RESET + } + at.SetActions(engine.Actions{&engine.Action{ActionType: aType, BalanceId: attr.BalanceId, Direction: attr.Direction, Balance: &engine.Balance{Value: attr.Value}}}) if err := at.Execute(); err != nil { *reply = err.Error() return err diff --git a/apier/v1/apier_local_test.go b/apier/v1/apier_local_test.go index 0c3ff8b51..71e9b2255 100644 --- a/apier/v1/apier_local_test.go +++ b/apier/v1/apier_local_test.go @@ -909,6 +909,32 @@ func TestApierAddBalance(t *testing.T) { } else if reply != "OK" { t.Errorf("Calling ApierV1.AddBalance received: %s", reply) } + attrs = &AttrAddBalance{Tenant: "cgrates.org", Account: "dan3", BalanceId: "*monetary", Direction: "*out", Value: 1.5} + if err := rater.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) + } + attrs = &AttrAddBalance{Tenant: "cgrates.org", Account: "dan3", BalanceId: "*monetary", Direction: "*out", Value: 2.1} + if err := rater.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) + } + attrs = &AttrAddBalance{Tenant: "cgrates.org", Account: "dan6", BalanceId: "*monetary", Direction: "*out", Value: 2.1} + if err := rater.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) + } + attrs = &AttrAddBalance{Tenant: "cgrates.org", Account: "dan6", BalanceId: "*monetary", Direction: "*out", Value: 1, Overwrite: true} + if err := rater.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) + } + + } // Test here ExecuteAction @@ -985,7 +1011,7 @@ func TestApierAddTriggeredAction(t *testing.T) { reply2 := "" attrs2 := new(AttrAddActionTrigger) *attrs2 = *attrs - attrs2.Account = "dan3" // Does not exist so it should error when adding triggers on it + attrs2.Account = "dan10" // Does not exist so it should error when adding triggers on it // Add trigger to an account which does n exist if err := rater.Call("ApierV1.AddTriggeredAction", attrs2, &reply2); err == nil || reply2 == "OK" { t.Error("Expecting error on ApierV1.AddTriggeredAction.", err, reply2) @@ -1122,6 +1148,18 @@ func TestApierGetBalance(t *testing.T) { } else if reply != 10 { t.Errorf("Calling ApierV1.GetBalance expected: 10, received: %f", reply) } + attrs = &AttrGetBalance{Tenant: "cgrates.org", Account: "dan3", BalanceId: "*monetary", Direction: "*out"} + if err := rater.Call("ApierV1.GetBalance", attrs, &reply); err != nil { + t.Error("Got error on ApierV1.GetBalance: ", err.Error()) + } else if reply != 3.6 { + t.Errorf("Calling ApierV1.GetBalance expected: 3.6, received: %f", reply) + } + attrs = &AttrGetBalance{Tenant: "cgrates.org", Account: "dan6", BalanceId: "*monetary", Direction: "*out"} + if err := rater.Call("ApierV1.GetBalance", attrs, &reply); err != nil { + t.Error("Got error on ApierV1.GetBalance: ", err.Error()) + } else if reply != 1 { + t.Errorf("Calling ApierV1.GetBalance expected: 1, received: %f", reply) + } } // Test here LoadTariffPlanFromFolder