renamed Account Type attribute

It's now AllowNegative and it's a boolean (was *prepaid/*postpaid)
This commit is contained in:
Radu Ioan Fericean
2014-02-21 15:52:47 +02:00
parent 7831a53797
commit a37dbf0734
13 changed files with 79 additions and 103 deletions

View File

@@ -157,11 +157,11 @@ func (self *ApierV1) RemAccountActionTriggers(attrs AttrRemAcntActionTriggers, r
}
type AttrSetAccount struct {
Tenant string
Direction string
Account string
Type string // <*prepaid|*postpaid>
ActionPlanId string
Tenant string
Direction string
Account string
ActionPlanId string
AllowNegative bool
}
// Ads a new account into dataDb. If already defined, returns success.
@@ -176,14 +176,9 @@ func (self *ApierV1) SetAccount(attr AttrSetAccount, reply *string) error {
if bal, _ := self.AccountDb.GetAccount(balanceId); bal != nil {
ub = bal
} else { // Not found in db, create it here
if len(attr.Type) == 0 {
attr.Type = engine.UB_TYPE_PREPAID
} else if !utils.IsSliceMember([]string{engine.UB_TYPE_POSTPAID, engine.UB_TYPE_PREPAID}, attr.Type) {
return 0, fmt.Errorf("%s:%s", utils.ERR_MANDATORY_IE_MISSING, "Type")
}
ub = &engine.Account{
Id: balanceId,
Type: attr.Type,
Id: balanceId,
AllowNegative: attr.AllowNegative,
}
}

View File

@@ -1060,7 +1060,7 @@ func TestApierSetAccount(t *testing.T) {
return
}
reply := ""
attrs := &AttrSetAccount{Tenant: "cgrates.org", Direction: "*out", Account: "dan7", Type: "*prepaid", ActionPlanId: "ATMS_1"}
attrs := &AttrSetAccount{Tenant: "cgrates.org", Direction: "*out", Account: "dan7", ActionPlanId: "ATMS_1"}
if err := rater.Call("ApierV1.SetAccount", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.SetAccount: ", err.Error())
} else if reply != "OK" {
@@ -1160,7 +1160,7 @@ func TestTriggersExecute(t *testing.T) {
return
}
reply := ""
attrs := &AttrSetAccount{Tenant: "cgrates.org", Direction: "*out", Account: "dan8", Type: "*prepaid"}
attrs := &AttrSetAccount{Tenant: "cgrates.org", Direction: "*out", Account: "dan8"}
if err := rater.Call("ApierV1.SetAccount", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.SetAccount: ", err.Error())
} else if reply != "OK" {

View File

@@ -20,6 +20,8 @@ package console
import (
"fmt"
"strconv"
"github.com/cgrates/cgrates/apier"
)
@@ -36,7 +38,7 @@ type CmdAddAccount struct {
// name should be exec's name
func (self *CmdAddAccount) Usage(name string) string {
return fmt.Sprintf("\n\tUsage: cgr-console [cfg_opts...{-h}] add_account <tenant> <account> <type=prepaid|postpaid> <actiontimingsid> [<direction>]")
return fmt.Sprintf("\n\tUsage: cgr-console [cfg_opts...{-h}] add_account <tenant> <account> <allownegative> <actiontimingsid> [<direction>]")
}
// set param defaults
@@ -55,7 +57,11 @@ func (self *CmdAddAccount) FromArgs(args []string) error {
self.defaults()
self.rpcParams.Tenant = args[2]
self.rpcParams.Account = args[3]
self.rpcParams.Type = args[4]
if an, err := strconv.ParseBool(args[4]); err != nil {
return fmt.Errorf("Error parsing allownegative boolean: ", args[4])
} else {
self.rpcParams.AllowNegative = an
}
self.rpcParams.ActionPlanId = args[5]
if len(args) > 6 {
self.rpcParams.Direction = args[6]

View File

@@ -29,8 +29,6 @@ import (
)
const (
UB_TYPE_POSTPAID = "*unlimited"
UB_TYPE_PREPAID = "*limited"
// Direction type
INBOUND = "*in"
OUTBOUND = "*out"
@@ -60,14 +58,14 @@ This can represent a user or a shared group.
*/
type Account struct {
Id string
Type string // UB_TYPE_POSTPAID/UB_TYPE_PREPAID
BalanceMap map[string]BalanceChain
UnitCounters []*UnitsCounter
ActionTriggers ActionTriggerPriotityList
Groups GroupLinks // user info about groups
// group information
UserIds []string // group info about users
Disabled bool
UserIds []string // group info about users
AllowNegative bool
Disabled bool
}
// Returns user's available minutes for the specified destination
@@ -119,7 +117,7 @@ func (ub *Account) debitBalanceAction(a *Action) error {
func (ub *Account) getBalancesForPrefix(prefix string, balances BalanceChain) BalanceChain {
var usefulBalances BalanceChain
for _, b := range balances {
if b.IsExpired() || (ub.Type != UB_TYPE_POSTPAID && b.Value <= 0) {
if b.IsExpired() || (ub.AllowNegative == false && b.Value <= 0) {
continue
}
if b.DestinationId != "" && b.DestinationId != utils.ANY {

View File

@@ -831,9 +831,9 @@ func TestDebitNegativeSMSBalance(t *testing.T) {
func TestAccountdebitBalance(t *testing.T) {
ub := &Account{
Id: "rif",
Type: UB_TYPE_POSTPAID,
BalanceMap: map[string]BalanceChain{SMS: BalanceChain{&Balance{Value: 14}}, TRAFFIC: BalanceChain{&Balance{Value: 1204}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
Id: "rif",
AllowNegative: true,
BalanceMap: map[string]BalanceChain{SMS: BalanceChain{&Balance{Value: 14}}, TRAFFIC: BalanceChain{&Balance{Value: 1204}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
}
newMb := &Balance{Weight: 20, DestinationId: "NEW"}
a := &Action{BalanceType: MINUTES, Direction: OUTBOUND, Balance: newMb}
@@ -846,9 +846,9 @@ func TestAccountdebitBalance(t *testing.T) {
func TestAccountdebitBalanceExists(t *testing.T) {
ub := &Account{
Id: "rif",
Type: UB_TYPE_POSTPAID,
BalanceMap: map[string]BalanceChain{SMS + OUTBOUND: BalanceChain{&Balance{Value: 14}}, TRAFFIC + OUTBOUND: BalanceChain{&Balance{Value: 1024}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 15, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
Id: "rif",
AllowNegative: true,
BalanceMap: map[string]BalanceChain{SMS + OUTBOUND: BalanceChain{&Balance{Value: 14}}, TRAFFIC + OUTBOUND: BalanceChain{&Balance{Value: 1024}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 15, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
}
newMb := &Balance{Value: -10, Weight: 20, DestinationId: "NAT"}
a := &Action{BalanceType: MINUTES, Direction: OUTBOUND, Balance: newMb}
@@ -860,9 +860,9 @@ func TestAccountdebitBalanceExists(t *testing.T) {
func TestAccountAddMinuteNil(t *testing.T) {
ub := &Account{
Id: "rif",
Type: UB_TYPE_POSTPAID,
BalanceMap: map[string]BalanceChain{SMS + OUTBOUND: BalanceChain{&Balance{Value: 14}}, TRAFFIC + OUTBOUND: BalanceChain{&Balance{Value: 1024}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
Id: "rif",
AllowNegative: true,
BalanceMap: map[string]BalanceChain{SMS + OUTBOUND: BalanceChain{&Balance{Value: 14}}, TRAFFIC + OUTBOUND: BalanceChain{&Balance{Value: 1024}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
}
ub.debitBalanceAction(nil)
if len(ub.BalanceMap[MINUTES+OUTBOUND]) != 2 {
@@ -938,7 +938,7 @@ func TestAccountExecuteTriggeredActionsOrder(t *testing.T) {
}
ub.countUnits(&Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 1}})
if len(ub.BalanceMap[CREDIT+OUTBOUND]) != 1 || ub.BalanceMap[CREDIT+OUTBOUND][0].Value != 10 {
t.Error("Error executing triggered actions in order", ub.BalanceMap[CREDIT+OUTBOUND])
t.Error("Error executing triggered actions in order", ub.BalanceMap[CREDIT+OUTBOUND][0].Value)
}
}

View File

@@ -50,10 +50,9 @@ type Action struct {
const (
LOG = "*log"
RESET_TRIGGERS = "*reset_triggers"
SET_POSTPAID = "*set_postpaid"
RESET_POSTPAID = "*reset_postpaid"
SET_PREPAID = "*set_prepaid"
RESET_PREPAID = "*reset_prepaid"
ALLOW_NEGATIVE = "*allow_negative"
DENY_NEGATIVE = "*deny_negative"
RESET_ACCOUNT = "*reset_account"
TOPUP_RESET = "*topup_reset"
TOPUP = "*topup"
DEBIT = "*debit"
@@ -75,14 +74,12 @@ func getActionFunc(typ string) (actionTypeFunc, bool) {
return logAction, true
case RESET_TRIGGERS:
return resetTriggersAction, true
case SET_POSTPAID:
return setPostpaidAction, true
case RESET_POSTPAID:
return resetPostpaidAction, true
case SET_PREPAID:
return setPrepaidAction, true
case RESET_PREPAID:
return resetPrepaidAction, true
case ALLOW_NEGATIVE:
return allowNegativeAction, true
case DENY_NEGATIVE:
return denyNegativeAction, true
case RESET_ACCOUNT:
return resetAccountAction, true
case TOPUP_RESET:
return topupResetAction, true
case TOPUP:
@@ -118,24 +115,18 @@ func resetTriggersAction(ub *Account, a *Action) (err error) {
return
}
func setPostpaidAction(ub *Account, a *Action) (err error) {
ub.Type = UB_TYPE_POSTPAID
func allowNegativeAction(ub *Account, a *Action) (err error) {
ub.AllowNegative = true
return
}
func resetPostpaidAction(ub *Account, a *Action) (err error) {
genericReset(ub)
return setPostpaidAction(ub, a)
}
func setPrepaidAction(ub *Account, a *Action) (err error) {
ub.Type = UB_TYPE_PREPAID
func denyNegativeAction(ub *Account, a *Action) (err error) {
ub.AllowNegative = false
return
}
func resetPrepaidAction(ub *Account, a *Action) (err error) {
genericReset(ub)
return setPrepaidAction(ub, a)
func resetAccountAction(ub *Account, a *Action) (err error) {
return genericReset(ub)
}
func topupResetAction(ub *Account, a *Action) (err error) {
@@ -198,12 +189,13 @@ func disableUserAction(ub *Account, a *Action) (err error) {
return
}
func genericReset(ub *Account) {
func genericReset(ub *Account) error {
for k, _ := range ub.BalanceMap {
ub.BalanceMap[k] = BalanceChain{&Balance{Value: 0}}
}
ub.UnitCounters = make([]*UnitsCounter, 0)
ub.resetActionTriggers(nil)
return nil
}
func callUrl(ub *Account, a *Action) error {

View File

@@ -616,13 +616,12 @@ func TestActionResetTriggresActionFilter(t *testing.T) {
func TestActionSetPostpaid(t *testing.T) {
ub := &Account{
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
setPostpaidAction(ub, nil)
if ub.Type != UB_TYPE_POSTPAID {
allowNegativeAction(ub, nil)
if !ub.AllowNegative {
t.Error("Set postpaid action failed!")
}
}
@@ -630,13 +629,13 @@ func TestActionSetPostpaid(t *testing.T) {
func TestActionSetPrepaid(t *testing.T) {
ub := &Account{
Id: "TEST_UB",
Type: UB_TYPE_POSTPAID,
AllowNegative: true,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
setPrepaidAction(ub, nil)
if ub.Type != UB_TYPE_PREPAID {
denyNegativeAction(ub, nil)
if ub.AllowNegative {
t.Error("Set prepaid action failed!")
}
}
@@ -644,13 +643,13 @@ func TestActionSetPrepaid(t *testing.T) {
func TestActionResetPrepaid(t *testing.T) {
ub := &Account{
Id: "TEST_UB",
Type: UB_TYPE_POSTPAID,
AllowNegative: true,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: SMS, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: SMS, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
resetPrepaidAction(ub, nil)
if ub.Type != UB_TYPE_PREPAID ||
resetAccountAction(ub, nil)
if !ub.AllowNegative ||
ub.BalanceMap[CREDIT].GetTotalValue() != 0 ||
len(ub.UnitCounters) != 0 ||
ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 0 ||
@@ -663,14 +662,12 @@ func TestActionResetPrepaid(t *testing.T) {
func TestActionResetPostpaid(t *testing.T) {
ub := &Account{
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: SMS, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: SMS, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
resetPostpaidAction(ub, nil)
if ub.Type != UB_TYPE_POSTPAID ||
ub.BalanceMap[CREDIT].GetTotalValue() != 0 ||
resetAccountAction(ub, nil)
if ub.BalanceMap[CREDIT].GetTotalValue() != 0 ||
len(ub.UnitCounters) != 0 ||
ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 0 ||
ub.ActionTriggers[0].Executed == true || ub.ActionTriggers[1].Executed == true {
@@ -681,14 +678,13 @@ func TestActionResetPostpaid(t *testing.T) {
func TestActionTopupResetCredit(t *testing.T) {
ub := &Account{
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
a := &Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}}
topupResetAction(ub, a)
if ub.Type != UB_TYPE_PREPAID ||
if ub.AllowNegative ||
ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() != 10 ||
len(ub.UnitCounters) != 1 ||
len(ub.BalanceMap[MINUTES+OUTBOUND]) != 2 ||
@@ -699,8 +695,7 @@ func TestActionTopupResetCredit(t *testing.T) {
func TestActionTopupResetMinutes(t *testing.T) {
ub := &Account{
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
Id: "TEST_UB",
BalanceMap: map[string]BalanceChain{
CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}},
MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
@@ -709,7 +704,7 @@ func TestActionTopupResetMinutes(t *testing.T) {
}
a := &Action{BalanceType: MINUTES, Direction: OUTBOUND, Balance: &Balance{Value: 5, Weight: 20, DestinationId: "NAT"}}
topupResetAction(ub, a)
if ub.Type != UB_TYPE_PREPAID ||
if ub.AllowNegative ||
ub.BalanceMap[MINUTES+OUTBOUND].GetTotalValue() != 5 ||
ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() != 100 ||
len(ub.UnitCounters) != 1 ||
@@ -722,14 +717,13 @@ func TestActionTopupResetMinutes(t *testing.T) {
func TestActionTopupCredit(t *testing.T) {
ub := &Account{
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
a := &Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}}
topupAction(ub, a)
if ub.Type != UB_TYPE_PREPAID ||
if ub.AllowNegative ||
ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() != 110 ||
len(ub.UnitCounters) != 1 ||
len(ub.BalanceMap[MINUTES+OUTBOUND]) != 2 ||
@@ -741,14 +735,13 @@ func TestActionTopupCredit(t *testing.T) {
func TestActionTopupMinutes(t *testing.T) {
ub := &Account{
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
a := &Action{BalanceType: MINUTES, Direction: OUTBOUND, Balance: &Balance{Value: 5, Weight: 20, DestinationId: "NAT"}}
topupAction(ub, a)
if ub.Type != UB_TYPE_PREPAID ||
if ub.AllowNegative ||
ub.BalanceMap[MINUTES+OUTBOUND].GetTotalValue() != 15 ||
ub.BalanceMap[CREDIT].GetTotalValue() != 100 ||
len(ub.UnitCounters) != 1 ||
@@ -761,14 +754,13 @@ func TestActionTopupMinutes(t *testing.T) {
func TestActionDebitCredit(t *testing.T) {
ub := &Account{
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
a := &Action{BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}}
debitAction(ub, a)
if ub.Type != UB_TYPE_PREPAID ||
if ub.AllowNegative ||
ub.BalanceMap[CREDIT+OUTBOUND].GetTotalValue() != 90 ||
len(ub.UnitCounters) != 1 ||
len(ub.BalanceMap[MINUTES+OUTBOUND]) != 2 ||
@@ -780,14 +772,13 @@ func TestActionDebitCredit(t *testing.T) {
func TestActionDebitMinutes(t *testing.T) {
ub := &Account{
Id: "TEST_UB",
Type: UB_TYPE_PREPAID,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}, &ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
a := &Action{BalanceType: MINUTES, Direction: OUTBOUND, Balance: &Balance{Value: 5, Weight: 20, DestinationId: "NAT"}}
debitAction(ub, a)
if ub.Type != UB_TYPE_PREPAID ||
if ub.AllowNegative ||
ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 5 ||
ub.BalanceMap[CREDIT].GetTotalValue() != 100 ||
len(ub.UnitCounters) != 1 ||
@@ -799,8 +790,8 @@ func TestActionDebitMinutes(t *testing.T) {
func TestActionResetAllCounters(t *testing.T) {
ub := &Account{
Id: "TEST_UB",
Type: UB_TYPE_POSTPAID,
Id: "TEST_UB",
AllowNegative: true,
BalanceMap: map[string]BalanceChain{
CREDIT: BalanceChain{&Balance{Value: 100}},
MINUTES: BalanceChain{
@@ -810,7 +801,7 @@ func TestActionResetAllCounters(t *testing.T) {
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
resetCountersAction(ub, nil)
if ub.Type != UB_TYPE_POSTPAID ||
if !ub.AllowNegative ||
ub.BalanceMap[CREDIT].GetTotalValue() != 100 ||
len(ub.UnitCounters) != 1 ||
len(ub.UnitCounters[0].Balances) != 2 ||
@@ -829,8 +820,8 @@ func TestActionResetAllCounters(t *testing.T) {
func TestActionResetCounterMinutes(t *testing.T) {
ub := &Account{
Id: "TEST_UB",
Type: UB_TYPE_POSTPAID,
Id: "TEST_UB",
AllowNegative: true,
BalanceMap: map[string]BalanceChain{
CREDIT: BalanceChain{&Balance{Value: 100}},
MINUTES: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
@@ -839,7 +830,7 @@ func TestActionResetCounterMinutes(t *testing.T) {
}
a := &Action{BalanceType: MINUTES}
resetCounterAction(ub, a)
if ub.Type != UB_TYPE_POSTPAID ||
if !ub.AllowNegative ||
ub.BalanceMap[CREDIT].GetTotalValue() != 100 ||
len(ub.UnitCounters) != 2 ||
len(ub.UnitCounters[1].Balances) != 2 ||
@@ -862,14 +853,14 @@ func TestActionResetCounterMinutes(t *testing.T) {
func TestActionResetCounterCREDIT(t *testing.T) {
ub := &Account{
Id: "TEST_UB",
Type: UB_TYPE_POSTPAID,
AllowNegative: true,
BalanceMap: map[string]BalanceChain{CREDIT: BalanceChain{&Balance{Value: 100}}, MINUTES + OUTBOUND: BalanceChain{&Balance{Value: 10, Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{&UnitsCounter{BalanceType: CREDIT, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}, &UnitsCounter{BalanceType: SMS, Direction: OUTBOUND, Balances: BalanceChain{&Balance{Value: 1}}}},
ActionTriggers: ActionTriggerPriotityList{&ActionTrigger{BalanceType: CREDIT, Direction: OUTBOUND, ThresholdValue: 2, ActionsId: "TEST_ACTIONS", Executed: true}},
}
a := &Action{BalanceType: CREDIT, Direction: OUTBOUND}
resetCounterAction(ub, a)
if ub.Type != UB_TYPE_POSTPAID ||
if !ub.AllowNegative ||
ub.BalanceMap[CREDIT].GetTotalValue() != 100 ||
len(ub.UnitCounters) != 2 ||
len(ub.BalanceMap[MINUTES+OUTBOUND]) != 2 ||

View File

@@ -438,7 +438,7 @@ func (origCd *CallDescriptor) GetMaxSessionDuration() (time.Duration, error) {
var availableDuration time.Duration
availableCredit := 0.0
if userBalance, err := cd.getAccount(); err == nil && userBalance != nil {
if userBalance.Type == UB_TYPE_POSTPAID {
if userBalance.AllowNegative {
return -1, nil
} else {
availableDuration, availableCredit, _ = userBalance.getCreditForPrefix(cd)

View File

@@ -43,11 +43,10 @@ func populateDB() {
}
ats1 := []*Action{
&Action{ActionType: "*topup", BalanceType: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: 10}, Weight: 20},
&Action{ActionType: "*reset_prepaid", Weight: 10},
&Action{ActionType: "*reset_account", Weight: 10},
}
minu := &Account{
Id: "*out:vdf:minu",
Type: UB_TYPE_PREPAID,
Id: "*out:vdf:minu",
BalanceMap: map[string]BalanceChain{
CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 50}},
MINUTES + OUTBOUND: BalanceChain{
@@ -56,8 +55,7 @@ func populateDB() {
}},
}
broker := &Account{
Id: "*out:vdf:broker",
Type: UB_TYPE_PREPAID,
Id: "*out:vdf:broker",
BalanceMap: map[string]BalanceChain{
MINUTES + OUTBOUND: BalanceChain{
&Balance{Value: 20, DestinationId: "NAT", Weight: 10, RateSubject: "rif"},

View File

@@ -596,7 +596,6 @@ func (csvr *CSVReader) LoadAccountActions() (err error) {
return errors.New(fmt.Sprintf("Could not get action triggers for tag %v", record[4]))
}
ub := &Account{
Type: UB_TYPE_PREPAID,
Id: tag,
ActionTriggers: aTriggers,
}

View File

@@ -674,7 +674,6 @@ func TestLoadAccountActions(t *testing.T) {
aa := csvr.accountActions[0]
expected := &Account{
Id: "*out:vdf:minitsboy",
Type: UB_TYPE_PREPAID,
ActionTriggers: csvr.actionsTriggers["STANDARD_TRIGGER"],
}
if !reflect.DeepEqual(aa, expected) {

View File

@@ -487,7 +487,6 @@ func (dbr *DbReader) LoadAccountActions() (err error) {
return errors.New(fmt.Sprintf("Could not get action triggers for tag %v", aa.ActionTriggersId))
}
ub := &Account{
Type: UB_TYPE_PREPAID,
Id: aa.KeyId(),
ActionTriggers: aTriggers,
}
@@ -652,8 +651,7 @@ func (dbr *DbReader) LoadAccountActionsFiltered(qriedAA *utils.TPAccountActions)
ub, err := dbr.accountDb.GetAccount(id)
if err != nil {
ub = &Account{
Type: UB_TYPE_PREPAID,
Id: id,
Id: id,
}
}
ub.ActionTriggers = actionTriggers

View File

@@ -138,7 +138,7 @@ func GetUB() *Account {
zeroTime = zeroTime.UTC() // for deep equal to find location
ub := &Account{
Id: "rif",
Type: UB_TYPE_POSTPAID,
AllowNegative: true,
BalanceMap: map[string]BalanceChain{SMS + OUTBOUND: BalanceChain{&Balance{Value: 14, ExpirationDate: zeroTime}}, TRAFFIC + OUTBOUND: BalanceChain{&Balance{Value: 1024, ExpirationDate: zeroTime}}, MINUTES: BalanceChain{&Balance{Weight: 20, DestinationId: "NAT"}, &Balance{Weight: 10, DestinationId: "RET"}}},
UnitCounters: []*UnitsCounter{uc, uc},
ActionTriggers: ActionTriggerPriotityList{at, at, at},