mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-14 12:49:54 +05:00
fix account setting checks
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user