diff --git a/engine/account.go b/engine/account.go index 13e24a7ff..5e16aa84d 100644 --- a/engine/account.go +++ b/engine/account.go @@ -116,13 +116,12 @@ func (ub *Account) debitBalanceAction(a *Action, reset bool) error { if b.IsExpired() { continue // just to be safe (cleaned expired balances above) } - if b.Equal(a.Balance) { + if b.MatchFilter(a.Balance) { if reset { b.Value = 0 } b.SubstractAmount(a.Balance.Value) found = true - break } } // if it is not found then we add it to the list diff --git a/engine/actions_test.go b/engine/actions_test.go index 158ab9c4b..fd5de81b2 100644 --- a/engine/actions_test.go +++ b/engine/actions_test.go @@ -763,6 +763,44 @@ func TestActionTopupResetCredit(t *testing.T) { } } +func TestActionTopupResetCreditId(t *testing.T) { + ub := &Account{ + Id: "TEST_UB", + BalanceMap: map[string]BalanceChain{ + CREDIT + OUTBOUND: BalanceChain{ + &Balance{Value: 100}, + &Balance{Id: "TEST_B", Value: 15}, + }, + }, + } + a := &Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Id: "TEST_B", Value: 10}} + topupResetAction(ub, nil, a) + if ub.AllowNegative || + ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() != 110 || + len(ub.BalanceMap[CREDIT+OUTBOUND]) != 2 { + t.Errorf("Topup reset action failed: %+v", ub.BalanceMap[CREDIT+OUTBOUND][0]) + } +} + +func TestActionTopupResetCreditNoId(t *testing.T) { + ub := &Account{ + Id: "TEST_UB", + BalanceMap: map[string]BalanceChain{ + CREDIT + OUTBOUND: BalanceChain{ + &Balance{Value: 100}, + &Balance{Id: "TEST_B", Value: 15}, + }, + }, + } + a := &Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}} + topupResetAction(ub, nil, a) + if ub.AllowNegative || + ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() != 20 || + len(ub.BalanceMap[CREDIT+OUTBOUND]) != 2 { + t.Errorf("Topup reset action failed: %+v", ub.BalanceMap[CREDIT+OUTBOUND][1]) + } +} + func TestActionTopupResetMinutes(t *testing.T) { ub := &Account{ Id: "TEST_UB", diff --git a/engine/balances.go b/engine/balances.go index fb931d54d..83bfabd01 100644 --- a/engine/balances.go +++ b/engine/balances.go @@ -43,7 +43,23 @@ type Balance struct { } func (b *Balance) Equal(o *Balance) bool { - if b.Id != "" || o.Id != "" { + if b.DestinationId == "" { + b.DestinationId = utils.ANY + } + if o.DestinationId == "" { + o.DestinationId = utils.ANY + } + return b.Id == o.Id && + b.ExpirationDate.Equal(o.ExpirationDate) && + b.Weight == o.Weight && + b.DestinationId == o.DestinationId && + b.RatingSubject == o.RatingSubject && + b.Category == o.Category && + b.SharedGroup == o.SharedGroup +} + +func (b *Balance) MatchFilter(o *Balance) bool { + if o.Id != "" { return b.Id == o.Id } if b.DestinationId == "" { @@ -121,6 +137,7 @@ func (b *Balance) MatchActionTrigger(at *ActionTrigger) bool { func (b *Balance) Clone() *Balance { return &Balance{ Uuid: b.Uuid, + Id: b.Id, Value: b.Value, // this value is in seconds DestinationId: b.DestinationId, ExpirationDate: b.ExpirationDate,