fix account setting checks

This commit is contained in:
Radu Ioan Fericean
2014-10-07 11:01:49 +03:00
parent 578453c766
commit 4b3c405dda
4 changed files with 24 additions and 4 deletions

View File

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

View File

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

View File

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

View File

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