diff --git a/apier/apier.go b/apier/apier.go index 83ddd25b9..8a08420d5 100644 --- a/apier/apier.go +++ b/apier/apier.go @@ -113,9 +113,12 @@ func (self *ApierV1) AddBalance(attr *AttrAddBalance, reply *string) error { if attr.Direction == "" { attr.Direction = engine.OUTBOUND } - aType := engine.TOPUP + aType := engine.DEBIT + // reverse the sign as it is a debit + attr.Value = -attr.Value + if attr.Overwrite { - aType = engine.TOPUP_RESET + aType = engine.DEBIT_RESET } at.SetActions(engine.Actions{ &engine.Action{ diff --git a/console/balance_add.go b/console/balance_set.go similarity index 96% rename from console/balance_add.go rename to console/balance_set.go index f29b71165..bc971f587 100644 --- a/console/balance_add.go +++ b/console/balance_set.go @@ -25,7 +25,7 @@ import ( func init() { c := &CmdAddBalance{ - name: "balance_add", + name: "balance_set", rpcMethod: "ApierV1.AddBalance", } commands[c.Name()] = c @@ -50,7 +50,7 @@ func (self *CmdAddBalance) RpcMethod() string { func (self *CmdAddBalance) RpcParams() interface{} { if self.rpcParams == nil { - self.rpcParams = &apier.AttrAddBalance{BalanceType: engine.CREDIT} + self.rpcParams = &apier.AttrAddBalance{BalanceType: engine.CREDIT, Overwrite: false} } return self.rpcParams } diff --git a/engine/action.go b/engine/action.go index ce2ed100c..4c0fe7d92 100644 --- a/engine/action.go +++ b/engine/action.go @@ -55,6 +55,7 @@ const ( RESET_ACCOUNT = "*reset_account" TOPUP_RESET = "*topup_reset" TOPUP = "*topup" + DEBIT_RESET = "*debit_reset" DEBIT = "*debit" RESET_COUNTER = "*reset_counter" RESET_COUNTERS = "*reset_counters" @@ -88,6 +89,8 @@ func getActionFunc(typ string) (actionTypeFunc, bool) { return topupResetAction, true case TOPUP: return topupAction, true + case DEBIT_RESET: + return debitResetAction, true case DEBIT: return debitAction, true case RESET_COUNTER: @@ -149,14 +152,20 @@ func topupResetAction(ub *Account, a *Action) (err error) { } ub.BalanceMap[a.BalanceType+a.Direction] = BalanceChain{} genericMakeNegative(a) - genericDebit(ub, a) - return + return genericDebit(ub, a) } func topupAction(ub *Account, a *Action) (err error) { genericMakeNegative(a) - genericDebit(ub, a) - return + return genericDebit(ub, a) +} + +func debitResetAction(ub *Account, a *Action) (err error) { + if ub.BalanceMap == nil { // Init the map since otherwise will get error if nil + ub.BalanceMap = make(map[string]BalanceChain, 0) + } + ub.BalanceMap[a.BalanceType+a.Direction] = BalanceChain{} + return genericDebit(ub, a) } func debitAction(ub *Account, a *Action) (err error) {