diff --git a/engine/account.go b/engine/account.go index e0453043a..a089c59ac 100644 --- a/engine/account.go +++ b/engine/account.go @@ -479,6 +479,17 @@ func (ub *Account) CleanExpiredBalances() { } } +func (ub *Account) allBalancesExpired() bool { + for _, bm := range ub.BalanceMap { + for i := 0; i < len(bm); i++ { + if !bm[i].IsExpired() { + return false + } + } + } + return true +} + // returns the shared groups that this user balance belnongs to func (ub *Account) GetSharedGroups() (groups []string) { for _, balanceChain := range ub.BalanceMap { diff --git a/engine/actions_test.go b/engine/actions_test.go index 113900b05..bb75d88bb 100644 --- a/engine/actions_test.go +++ b/engine/actions_test.go @@ -1049,6 +1049,8 @@ func TestTopupActionLoaded(t *testing.T) { initialValue := initialUb.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() afterValue := afterUb.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() if initialValue != 100 || afterValue != 125 { + t.Logf("Initial: %+v", initialUb) + t.Logf("After: %+v", afterUb) t.Error("Bad topup before and after: ", initialValue, afterValue) } } diff --git a/engine/storage_map.go b/engine/storage_map.go index 944bc4007..55db2969f 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -491,9 +491,14 @@ func (ms *MapStorage) GetAccount(key string) (ub *Account, err error) { func (ms *MapStorage) SetAccount(ub *Account) (err error) { // never override existing account with an empty one + // UPDATE: if all balances expired and were clean it makes + // sense to write empty balance map if len(ub.BalanceMap) == 0 { - if ac, err := ms.GetAccount(ub.Id); err == nil { + if ac, err := ms.GetAccount(ub.Id); err == nil && !ac.allBalancesExpired() { ac.ActionTriggers = ub.ActionTriggers + ac.UnitCounters = ub.UnitCounters + ac.AllowNegative = ub.AllowNegative + ac.Disabled = ub.Disabled ub = ac } } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 74003d733..ffdbf50d7 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -23,6 +23,7 @@ import ( "compress/zlib" "errors" "fmt" + "log" "github.com/cgrates/cgrates/cache2go" "github.com/cgrates/cgrates/utils" @@ -627,15 +628,16 @@ func (rs *RedisStorage) SetAccount(ub *Account) (err error) { // never override existing account with an empty one // UPDATE: if all balances expired and were clean it makes // sense to write empty balance map - /*if len(ub.BalanceMap) == 0 { - if ac, err := rs.GetAccount(ub.Id); err == nil { + if len(ub.BalanceMap) == 0 { + if ac, err := rs.GetAccount(ub.Id); err == nil && !ac.allBalancesExpired() { ac.ActionTriggers = ub.ActionTriggers ac.UnitCounters = ub.UnitCounters ac.AllowNegative = ub.AllowNegative ac.Disabled = ub.Disabled ub = ac } - }*/ + } + log.Print("Acc: ", ub) result, err := rs.ms.Marshal(ub) err = rs.db.Set(ACCOUNT_PREFIX+ub.Id, result) return