mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
more fixes and test for credit refounding
This commit is contained in:
@@ -137,7 +137,7 @@ func (b *Balance) DebitMinutes(cc *CallCost, count bool, ub *UserBalance, moneyB
|
||||
amount := increment.Duration.Seconds()
|
||||
if b.Value >= amount {
|
||||
b.Value -= amount
|
||||
increment.BalanceUuids = append(increment.BalanceUuids, b.Uuid)
|
||||
increment.SetMinuteBalance(b.Uuid)
|
||||
increment.MinuteInfo = &MinuteInfo{cc.Destination, amount, 0}
|
||||
increment.paid = true
|
||||
if count {
|
||||
@@ -178,7 +178,7 @@ func (b *Balance) DebitMinutes(cc *CallCost, count bool, ub *UserBalance, moneyB
|
||||
}
|
||||
cc.Timespans.RemoveOverlapedFromIndex(tsIndex)
|
||||
b.Value -= amount
|
||||
newTs.Increments[0].BalanceUuids = append(newTs.Increments[0].BalanceUuids, b.Uuid)
|
||||
newTs.Increments[0].SetMinuteBalance(b.Uuid)
|
||||
newTs.Increments[0].MinuteInfo = &MinuteInfo{cc.Destination, amount, 0}
|
||||
newTs.Increments[0].paid = true
|
||||
if count {
|
||||
@@ -217,8 +217,9 @@ func (b *Balance) DebitMinutes(cc *CallCost, count bool, ub *UserBalance, moneyB
|
||||
if moneyBal != nil && b.Value >= seconds {
|
||||
b.Value -= seconds
|
||||
moneyBal.Value -= cost
|
||||
nInc.BalanceUuids = append(nInc.BalanceUuids, b.Uuid)
|
||||
nInc.BalanceUuids = append(nInc.BalanceUuids, moneyBal.Uuid)
|
||||
|
||||
nInc.SetMinuteBalance(b.Uuid)
|
||||
nInc.SetMoneyBalance(moneyBal.Uuid)
|
||||
nInc.MinuteInfo = &MinuteInfo{newCC.Destination, seconds, 0}
|
||||
nInc.paid = true
|
||||
if count {
|
||||
@@ -261,7 +262,7 @@ func (b *Balance) DebitMoney(cc *CallCost, count bool, ub *UserBalance) error {
|
||||
amount := increment.Cost
|
||||
if b.Value >= amount {
|
||||
b.Value -= amount
|
||||
increment.BalanceUuids = append(increment.BalanceUuids, b.Uuid)
|
||||
increment.SetMoneyBalance(b.Uuid)
|
||||
increment.paid = true
|
||||
if count {
|
||||
ub.countUnits(&Action{BalanceId: CREDIT, Direction: cc.Direction, Balance: &Balance{Value: amount, DestinationId: cc.Destination}})
|
||||
@@ -289,7 +290,7 @@ func (b *Balance) DebitMoney(cc *CallCost, count bool, ub *UserBalance) error {
|
||||
amount := nInc.Cost
|
||||
if b.Value >= amount {
|
||||
b.Value -= amount
|
||||
nInc.BalanceUuids = append(nInc.BalanceUuids, b.Uuid)
|
||||
nInc.SetMoneyBalance(b.Uuid)
|
||||
nInc.paid = true
|
||||
if count {
|
||||
ub.countUnits(&Action{BalanceId: CREDIT, Direction: newCC.Direction, Balance: &Balance{Value: amount, DestinationId: newCC.Destination}})
|
||||
|
||||
@@ -512,10 +512,10 @@ func (cd *CallDescriptor) MaxDebit() (cc *CallCost, err error) {
|
||||
return cd.Debit()
|
||||
}
|
||||
|
||||
func (cd *CallDescriptor) RefoundIncrements() (left float64, err error) {
|
||||
func (cd *CallDescriptor) RefundIncrements() (left float64, err error) {
|
||||
if userBalance, err := cd.getUserBalance(); err == nil && userBalance != nil {
|
||||
defer storageGetter.SetUserBalance(userBalance)
|
||||
userBalance.refoundIncrements(cd.Increments, true)
|
||||
userBalance.refundIncrements(cd.Increments, cd.Direction, true)
|
||||
}
|
||||
return 0.0, err
|
||||
}
|
||||
@@ -527,7 +527,7 @@ The amount filed has to be filled in call descriptor.
|
||||
func (cd *CallDescriptor) DebitCents() (left float64, err error) {
|
||||
if userBalance, err := cd.getUserBalance(); err == nil && userBalance != nil {
|
||||
defer storageGetter.SetUserBalance(userBalance)
|
||||
return userBalance.debitGenericBalance(CREDIT+OUTBOUND, cd.Amount, true), nil
|
||||
return userBalance.debitGenericBalance(CREDIT, cd.Direction, cd.Amount, true), nil
|
||||
}
|
||||
return 0.0, err
|
||||
}
|
||||
@@ -539,7 +539,7 @@ The amount filed has to be filled in call descriptor.
|
||||
func (cd *CallDescriptor) DebitSMS() (left float64, err error) {
|
||||
if userBalance, err := cd.getUserBalance(); err == nil && userBalance != nil {
|
||||
defer storageGetter.SetUserBalance(userBalance)
|
||||
return userBalance.debitGenericBalance(SMS+OUTBOUND, cd.Amount, true), nil
|
||||
return userBalance.debitGenericBalance(SMS, cd.Direction, cd.Amount, true), nil
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
|
||||
@@ -77,12 +77,12 @@ func (rs *Responder) MaxDebit(arg CallDescriptor, reply *CallCost) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (rs *Responder) RefoundIncrements(arg CallDescriptor, reply *float64) (err error) {
|
||||
func (rs *Responder) RefundIncrements(arg CallDescriptor, reply *float64) (err error) {
|
||||
if rs.Bal != nil {
|
||||
*reply, err = rs.callMethod(&arg, "Responder.RefoundIncrements")
|
||||
*reply, err = rs.callMethod(&arg, "Responder.RefundIncrements")
|
||||
} else {
|
||||
r, e := AccLock.Guard(arg.GetUserBalanceKey(), func() (float64, error) {
|
||||
return arg.RefoundIncrements()
|
||||
return arg.RefundIncrements()
|
||||
})
|
||||
*reply, err = r, e
|
||||
}
|
||||
@@ -354,7 +354,7 @@ type Connector interface {
|
||||
GetCost(CallDescriptor, *CallCost) error
|
||||
Debit(CallDescriptor, *CallCost) error
|
||||
MaxDebit(CallDescriptor, *CallCost) error
|
||||
RefoundIncrements(CallDescriptor, *float64) error
|
||||
RefundIncrements(CallDescriptor, *float64) error
|
||||
DebitCents(CallDescriptor, *float64) error
|
||||
DebitSeconds(CallDescriptor, *float64) error
|
||||
GetMaxSessionTime(CallDescriptor, *float64) error
|
||||
@@ -375,8 +375,8 @@ func (rcc *RPCClientConnector) Debit(cd CallDescriptor, cc *CallCost) error {
|
||||
func (rcc *RPCClientConnector) MaxDebit(cd CallDescriptor, cc *CallCost) error {
|
||||
return rcc.Client.Call("Responder.MaxDebit", cd, cc)
|
||||
}
|
||||
func (rcc *RPCClientConnector) RefoundIncrements(cd CallDescriptor, resp *float64) error {
|
||||
return rcc.Client.Call("Responder.RefoundIncrements", cd, resp)
|
||||
func (rcc *RPCClientConnector) RefundIncrements(cd CallDescriptor, resp *float64) error {
|
||||
return rcc.Client.Call("Responder.RefundIncrements", cd, resp)
|
||||
}
|
||||
func (rcc *RPCClientConnector) DebitCents(cd CallDescriptor, resp *float64) error {
|
||||
return rcc.Client.Call("Responder.DebitCents", cd, resp)
|
||||
|
||||
@@ -150,6 +150,22 @@ func (incr *Increment) Clone() *Increment {
|
||||
return nIncr
|
||||
}
|
||||
|
||||
func (incr *Increment) SetMinuteBalance(bUuid string) {
|
||||
incr.BalanceUuids[0] = bUuid
|
||||
}
|
||||
|
||||
func (incr *Increment) GetMinuteBalance() string {
|
||||
return incr.BalanceUuids[0]
|
||||
}
|
||||
|
||||
func (incr *Increment) SetMoneyBalance(bUuid string) {
|
||||
incr.BalanceUuids[1] = bUuid
|
||||
}
|
||||
|
||||
func (incr *Increment) GetMoneyBalance() string {
|
||||
return incr.BalanceUuids[1]
|
||||
}
|
||||
|
||||
type Increments []*Increment
|
||||
|
||||
func (incs Increments) GetTotalCost() float64 {
|
||||
@@ -171,7 +187,7 @@ func (ts *TimeSpan) Contains(t time.Time) bool {
|
||||
}
|
||||
|
||||
// Returns the cost of the timespan according to the relevant cost interval.
|
||||
// It also sets the Cost field of this timespan (used for refound on session
|
||||
// It also sets the Cost field of this timespan (used for refund on session
|
||||
// manager debit loop where the cost cannot be recalculated)
|
||||
func (ts *TimeSpan) getCost() float64 {
|
||||
if ts.RateInterval == nil {
|
||||
@@ -207,7 +223,12 @@ func (ts *TimeSpan) createIncrementsSlice() {
|
||||
incrementCost := rate / rateUnit.Seconds() * rateIncrement.Seconds()
|
||||
totalCost := 0.0
|
||||
for s := 0; s < int(ts.GetDuration()/rateIncrement); s++ {
|
||||
ts.Increments = append(ts.Increments, &Increment{Duration: rateIncrement, Cost: incrementCost})
|
||||
inc := &Increment{
|
||||
Duration: rateIncrement,
|
||||
Cost: incrementCost,
|
||||
BalanceUuids: make([]string, 2),
|
||||
}
|
||||
ts.Increments = append(ts.Increments, inc)
|
||||
totalCost += incrementCost
|
||||
}
|
||||
ts.Cost = totalCost
|
||||
|
||||
@@ -152,7 +152,7 @@ func (ub *UserBalance) debitCreditBalance(cc *CallCost, count bool) error {
|
||||
for _, b := range usefulMoneyBalances {
|
||||
if b.Value >= amount {
|
||||
b.Value -= amount
|
||||
// the conect fee is not refoundable!
|
||||
// the conect fee is not refundable!
|
||||
if count {
|
||||
ub.countUnits(&Action{BalanceId: CREDIT, Direction: cc.Direction, Balance: &Balance{Value: amount, DestinationId: cc.Destination}})
|
||||
}
|
||||
@@ -164,7 +164,7 @@ func (ub *UserBalance) debitCreditBalance(cc *CallCost, count bool) error {
|
||||
// there are no money for the connect fee; go negative
|
||||
moneyBalance := ub.GetDefaultMoneyBalance(cc.Direction)
|
||||
moneyBalance.Value -= amount
|
||||
// the conect fee is not refoundable!
|
||||
// the conect fee is not refundable!
|
||||
if count {
|
||||
ub.countUnits(&Action{BalanceId: CREDIT, Direction: cc.Direction, Balance: &Balance{Value: amount, DestinationId: cc.Destination}})
|
||||
}
|
||||
@@ -240,34 +240,26 @@ func (ub *UserBalance) GetDefaultMoneyBalance(direction string) *Balance {
|
||||
return defaultBalance
|
||||
}
|
||||
|
||||
func (ub *UserBalance) refoundIncrements(increments Increments, count bool) {
|
||||
func (ub *UserBalance) refundIncrements(increments Increments, direction string, count bool) {
|
||||
for _, increment := range increments {
|
||||
var balance *Balance
|
||||
if increment.MinuteInfo != nil {
|
||||
if balance = ub.BalanceMap[MINUTES+OUTBOUND].GetBalance(increment.BalanceUuids[0]); balance != nil {
|
||||
break
|
||||
if increment.GetMinuteBalance() != "" {
|
||||
if balance = ub.BalanceMap[MINUTES+direction].GetBalance(increment.GetMinuteBalance()); balance == nil {
|
||||
continue
|
||||
}
|
||||
if balance != nil {
|
||||
balance.Value += increment.Duration.Seconds()
|
||||
if count {
|
||||
ub.countUnits(&Action{BalanceId: MINUTES, Direction: OUTBOUND, Balance: &Balance{Value: -increment.Duration.Seconds()}})
|
||||
}
|
||||
} else {
|
||||
// TODO: where should put the minutes?
|
||||
balance.Value += increment.Duration.Seconds()
|
||||
if count {
|
||||
ub.countUnits(&Action{BalanceId: MINUTES, Direction: direction, Balance: &Balance{Value: -increment.Duration.Seconds()}})
|
||||
}
|
||||
}
|
||||
// check money too
|
||||
if len(increment.BalanceUuids) == 2 && increment.BalanceUuids[1] != "" {
|
||||
if balance = ub.BalanceMap[CREDIT+OUTBOUND].GetBalance(increment.BalanceUuids[1]); balance != nil {
|
||||
break
|
||||
if increment.GetMoneyBalance() != "" {
|
||||
if balance = ub.BalanceMap[CREDIT+direction].GetBalance(increment.GetMoneyBalance()); balance == nil {
|
||||
continue
|
||||
}
|
||||
if balance != nil {
|
||||
balance.Value += increment.Cost
|
||||
if count {
|
||||
ub.countUnits(&Action{BalanceId: CREDIT, Direction: OUTBOUND, Balance: &Balance{Value: -increment.Cost}})
|
||||
}
|
||||
} else {
|
||||
// TODO: where should put the money?
|
||||
balance.Value += increment.Cost
|
||||
if count {
|
||||
ub.countUnits(&Action{BalanceId: CREDIT, Direction: direction, Balance: &Balance{Value: -increment.Cost}})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -276,12 +268,12 @@ func (ub *UserBalance) refoundIncrements(increments Increments, count bool) {
|
||||
/*
|
||||
Debits some amount of user's specified balance. Returns the remaining credit in user's balance.
|
||||
*/
|
||||
func (ub *UserBalance) debitGenericBalance(balanceId string, amount float64, count bool) float64 {
|
||||
func (ub *UserBalance) debitGenericBalance(balanceId string, direction string, amount float64, count bool) float64 {
|
||||
if count {
|
||||
ub.countUnits(&Action{BalanceId: balanceId, Direction: OUTBOUND, Balance: &Balance{Value: amount}})
|
||||
ub.countUnits(&Action{BalanceId: balanceId, Direction: direction, Balance: &Balance{Value: amount}})
|
||||
}
|
||||
ub.BalanceMap[balanceId+OUTBOUND].Debit(amount)
|
||||
return ub.BalanceMap[balanceId+OUTBOUND].GetTotalValue()
|
||||
ub.BalanceMap[balanceId+direction].Debit(amount)
|
||||
return ub.BalanceMap[balanceId+direction].GetTotalValue()
|
||||
}
|
||||
|
||||
// Scans the action trigers and execute the actions for which trigger is met
|
||||
|
||||
@@ -176,7 +176,7 @@ func TestDebitMoneyBalance(t *testing.T) {
|
||||
b1 := &Balance{Value: 10, Weight: 10, DestinationId: "NAT"}
|
||||
b2 := &Balance{Value: 100, Weight: 20, DestinationId: "RET"}
|
||||
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
|
||||
result := rifsBalance.debitGenericBalance(CREDIT, 6, false)
|
||||
result := rifsBalance.debitGenericBalance(CREDIT, OUTBOUND, 6, false)
|
||||
if rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 15 || result != rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value {
|
||||
t.Errorf("Expected %v was %v", 15, rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value)
|
||||
}
|
||||
@@ -186,8 +186,8 @@ func TestDebitAllMoneyBalance(t *testing.T) {
|
||||
b1 := &Balance{Value: 10, Weight: 10, DestinationId: "NAT"}
|
||||
b2 := &Balance{Value: 100, Weight: 20, DestinationId: "RET"}
|
||||
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
|
||||
rifsBalance.debitGenericBalance(CREDIT, 21, false)
|
||||
result := rifsBalance.debitGenericBalance(CREDIT, 0, false)
|
||||
rifsBalance.debitGenericBalance(CREDIT, OUTBOUND, 21, false)
|
||||
result := rifsBalance.debitGenericBalance(CREDIT, OUTBOUND, 0, false)
|
||||
if rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 0 || result != rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value {
|
||||
t.Errorf("Expected %v was %v", 0, rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value)
|
||||
}
|
||||
@@ -197,7 +197,7 @@ func TestDebitMoreMoneyBalance(t *testing.T) {
|
||||
b1 := &Balance{Value: 10, Weight: 10, DestinationId: "NAT"}
|
||||
b2 := &Balance{Value: 100, Weight: 20, DestinationId: "RET"}
|
||||
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
|
||||
result := rifsBalance.debitGenericBalance(CREDIT, 22, false)
|
||||
result := rifsBalance.debitGenericBalance(CREDIT, OUTBOUND, 22, false)
|
||||
if rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != -1 || result != rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value {
|
||||
t.Errorf("Expected %v was %v", -1, rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value)
|
||||
}
|
||||
@@ -207,7 +207,7 @@ func TestDebitNegativeMoneyBalance(t *testing.T) {
|
||||
b1 := &Balance{Value: 10, Weight: 10, DestinationId: "NAT"}
|
||||
b2 := &Balance{Value: 100, Weight: 20, DestinationId: "RET"}
|
||||
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
|
||||
result := rifsBalance.debitGenericBalance(CREDIT, -15, false)
|
||||
result := rifsBalance.debitGenericBalance(CREDIT, OUTBOUND, -15, false)
|
||||
if rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != 36 || result != rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value {
|
||||
t.Errorf("Expected %v was %v", 36, rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value)
|
||||
}
|
||||
@@ -233,7 +233,7 @@ func TestDebitCreditZeroSecond(t *testing.T) {
|
||||
t.Error("Error debiting balance: ", err)
|
||||
}
|
||||
t.Logf("%+v", cc.Timespans[0])
|
||||
if cc.Timespans[0].Increments[0].BalanceUuids[0] != "testb" {
|
||||
if cc.Timespans[0].Increments[0].GetMinuteBalance() != "testb" {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
if rifsBalance.BalanceMap[MINUTES+OUTBOUND][0].Value != 0 ||
|
||||
@@ -265,7 +265,7 @@ func TestDebitCreditZeroMinute(t *testing.T) {
|
||||
t.Error("Error debiting balance: ", err)
|
||||
}
|
||||
t.Logf("%+v", cc.Timespans)
|
||||
if cc.Timespans[0].Increments[0].BalanceUuids[0] != "testb" ||
|
||||
if cc.Timespans[0].Increments[0].GetMinuteBalance() != "testb" ||
|
||||
cc.Timespans[0].Increments[0].Duration != time.Minute {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
@@ -299,8 +299,8 @@ func TestDebitCreditZeroMixedMinute(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error("Error debiting balance: ", err)
|
||||
}
|
||||
if cc.Timespans[0].Increments[0].BalanceUuids[0] != "tests" ||
|
||||
cc.Timespans[1].Increments[0].BalanceUuids[0] != "testm" {
|
||||
if cc.Timespans[0].Increments[0].GetMinuteBalance() != "tests" ||
|
||||
cc.Timespans[1].Increments[0].GetMinuteBalance() != "testm" {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0], cc.Timespans[1].Increments[0])
|
||||
}
|
||||
if rifsBalance.BalanceMap[MINUTES+OUTBOUND][1].Value != 0 ||
|
||||
@@ -337,7 +337,7 @@ func TestDebitCreditNoCredit(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Error("Showing no enough credit error ")
|
||||
}
|
||||
if cc.Timespans[0].Increments[0].BalanceUuids[0] != "testb" ||
|
||||
if cc.Timespans[0].Increments[0].GetMinuteBalance() != "testb" ||
|
||||
cc.Timespans[0].Increments[0].Duration != time.Minute {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
@@ -378,7 +378,7 @@ func TestDebitCreditHasCredit(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error("Error debiting balance: ", err)
|
||||
}
|
||||
if cc.Timespans[0].Increments[0].BalanceUuids[0] != "testb" ||
|
||||
if cc.Timespans[0].Increments[0].GetMinuteBalance() != "testb" ||
|
||||
cc.Timespans[0].Increments[0].Duration != time.Minute {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
@@ -415,7 +415,7 @@ func TestDebitCreditSplitMinutesMoney(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error("Error debiting balance: ", err)
|
||||
}
|
||||
if cc.Timespans[0].Increments[0].BalanceUuids[0] != "testb" ||
|
||||
if cc.Timespans[0].Increments[0].GetMinuteBalance() != "testb" ||
|
||||
cc.Timespans[0].Increments[0].Duration != 10*time.Second {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
@@ -456,7 +456,7 @@ func TestDebitCreditMoreTimespans(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error("Error debiting balance: ", err)
|
||||
}
|
||||
if cc.Timespans[0].Increments[0].BalanceUuids[0] != "testb" ||
|
||||
if cc.Timespans[0].Increments[0].GetMinuteBalance() != "testb" ||
|
||||
cc.Timespans[0].Increments[0].Duration != time.Minute {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
@@ -494,7 +494,7 @@ func TestDebitCreditMoreTimespansMixed(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error("Error debiting balance: ", err)
|
||||
}
|
||||
if cc.Timespans[0].Increments[0].BalanceUuids[0] != "testb" ||
|
||||
if cc.Timespans[0].Increments[0].GetMinuteBalance() != "testb" ||
|
||||
cc.Timespans[0].Increments[0].Duration != time.Minute {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
@@ -567,9 +567,9 @@ func TestDebitCreditMoneyOnly(t *testing.T) {
|
||||
t.Error("Missing noy enough credit error ")
|
||||
}
|
||||
t.Logf("%+v", cc.Timespans[0].Increments)
|
||||
if cc.Timespans[0].Increments[0].BalanceUuids[0] != "money" ||
|
||||
if cc.Timespans[0].Increments[0].GetMoneyBalance() != "money" ||
|
||||
cc.Timespans[0].Increments[0].Duration != 10*time.Second {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0].Duration)
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0].BalanceUuids)
|
||||
}
|
||||
if rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != -30 {
|
||||
t.Error("Error extracting minutes from balance: ",
|
||||
@@ -607,8 +607,8 @@ func TestDebitCreditSubjectMinutes(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error("Error debiting balance: ", err)
|
||||
}
|
||||
if cc.Timespans[0].Increments[0].BalanceUuids[0] != "testb" ||
|
||||
cc.Timespans[0].Increments[0].BalanceUuids[1] != "moneya" ||
|
||||
if cc.Timespans[0].Increments[0].GetMinuteBalance() != "testb" ||
|
||||
cc.Timespans[0].Increments[0].GetMoneyBalance() != "moneya" ||
|
||||
cc.Timespans[0].Increments[0].Duration != time.Second {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
@@ -644,7 +644,7 @@ func TestDebitCreditSubjectMoney(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error("Error debiting balance: ", err)
|
||||
}
|
||||
if cc.Timespans[0].Increments[0].BalanceUuids[0] != "moneya" ||
|
||||
if cc.Timespans[0].Increments[0].GetMoneyBalance() != "moneya" ||
|
||||
cc.Timespans[0].Increments[0].Duration != time.Second {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
@@ -681,8 +681,8 @@ func TestDebitCreditSubjectMixed(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error("Error debiting balance: ", err)
|
||||
}
|
||||
if cc.Timespans[0].Increments[0].BalanceUuids[0] != "testb" ||
|
||||
cc.Timespans[0].Increments[0].BalanceUuids[1] != "moneya" ||
|
||||
if cc.Timespans[0].Increments[0].GetMinuteBalance() != "testb" ||
|
||||
cc.Timespans[0].Increments[0].GetMoneyBalance() != "moneya" ||
|
||||
cc.Timespans[0].Increments[0].Duration != time.Second {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
@@ -730,7 +730,7 @@ func TestDebitCreditSubjectMixedMoreTS(t *testing.T) {
|
||||
t.Error("Error showing debiting balance error: ", err)
|
||||
}
|
||||
//t.Logf("%+v %+v", cc.Timespans[0], cc.Timespans[1])
|
||||
if cc.Timespans[0].Increments[0].BalanceUuids[0] != "testb" ||
|
||||
if cc.Timespans[0].Increments[0].GetMinuteBalance() != "testb" ||
|
||||
cc.Timespans[0].Increments[0].Duration != time.Second {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
@@ -779,7 +779,7 @@ func TestDebitCreditSubjectMixedPartPay(t *testing.T) {
|
||||
t.Error("Error showing debiting balance error: ", err)
|
||||
}
|
||||
//t.Logf("%+v %+v", cc.Timespans[0], cc.Timespans[1])
|
||||
if cc.Timespans[0].Increments[0].BalanceUuids[0] != "testb" ||
|
||||
if cc.Timespans[0].Increments[0].GetMinuteBalance() != "testb" ||
|
||||
cc.Timespans[0].Increments[0].Duration != time.Second {
|
||||
t.Error("Error setting balance id to increment: ", cc.Timespans[0].Increments[0])
|
||||
}
|
||||
@@ -802,7 +802,7 @@ func TestDebitSMSBalance(t *testing.T) {
|
||||
b1 := &Balance{Value: 10, Weight: 10, DestinationId: "NAT"}
|
||||
b2 := &Balance{Value: 100, Weight: 20, DestinationId: "RET"}
|
||||
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}, SMS + OUTBOUND: BalanceChain{&Balance{Value: 100}}}}
|
||||
result := rifsBalance.debitGenericBalance(SMS, 12, false)
|
||||
result := rifsBalance.debitGenericBalance(SMS, OUTBOUND, 12, false)
|
||||
if rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value != 88 || result != rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value {
|
||||
t.Errorf("Expected %v was %v", 88, rifsBalance.BalanceMap[SMS+OUTBOUND])
|
||||
}
|
||||
@@ -812,7 +812,7 @@ func TestDebitAllSMSBalance(t *testing.T) {
|
||||
b1 := &Balance{Value: 10, Weight: 10, DestinationId: "NAT"}
|
||||
b2 := &Balance{Value: 100, Weight: 20, DestinationId: "RET"}
|
||||
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}, SMS + OUTBOUND: BalanceChain{&Balance{Value: 100}}}}
|
||||
result := rifsBalance.debitGenericBalance(SMS, 100, false)
|
||||
result := rifsBalance.debitGenericBalance(SMS, OUTBOUND, 100, false)
|
||||
if rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value != 0 || result != rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value {
|
||||
t.Errorf("Expected %v was %v", 0, rifsBalance.BalanceMap[SMS+OUTBOUND])
|
||||
}
|
||||
@@ -822,7 +822,7 @@ func TestDebitMoreSMSBalance(t *testing.T) {
|
||||
b1 := &Balance{Value: 10, Weight: 10, DestinationId: "NAT"}
|
||||
b2 := &Balance{Value: 100, Weight: 20, DestinationId: "RET"}
|
||||
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}, SMS + OUTBOUND: BalanceChain{&Balance{Value: 100}}}}
|
||||
result := rifsBalance.debitGenericBalance(SMS, 110, false)
|
||||
result := rifsBalance.debitGenericBalance(SMS, OUTBOUND, 110, false)
|
||||
if rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value != -10 || result != rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value {
|
||||
t.Errorf("Expected %v was %v", -10, rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value)
|
||||
}
|
||||
@@ -832,7 +832,7 @@ func TestDebitNegativeSMSBalance(t *testing.T) {
|
||||
b1 := &Balance{Value: 10, Weight: 10, DestinationId: "NAT"}
|
||||
b2 := &Balance{Value: 100, Weight: 20, DestinationId: "RET"}
|
||||
rifsBalance := &UserBalance{Id: "other", BalanceMap: map[string]BalanceChain{MINUTES + OUTBOUND: BalanceChain{b1, b2}, CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}, SMS + OUTBOUND: BalanceChain{&Balance{Value: 100}}}}
|
||||
result := rifsBalance.debitGenericBalance(SMS, -15, false)
|
||||
result := rifsBalance.debitGenericBalance(SMS, OUTBOUND, -15, false)
|
||||
if rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value != 115 || result != rifsBalance.BalanceMap[SMS+OUTBOUND][0].Value {
|
||||
t.Errorf("Expected %v was %v", 115, rifsBalance.BalanceMap[SMS+OUTBOUND])
|
||||
}
|
||||
@@ -1015,6 +1015,31 @@ func TestUserBalanceUnitCountingOutboundInbound(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserBalanceRefundSimple(t *testing.T) {
|
||||
ub := &UserBalance{
|
||||
BalanceMap: map[string]BalanceChain{
|
||||
CREDIT + OUTBOUND: BalanceChain{
|
||||
&Balance{Uuid: "moneya", Value: 100},
|
||||
},
|
||||
MINUTES + OUTBOUND: BalanceChain{
|
||||
&Balance{Uuid: "minutea", Value: 10, Weight: 20, DestinationId: "NAT"},
|
||||
&Balance{Uuid: "minuteb", Value: 10, DestinationId: "RET"},
|
||||
},
|
||||
},
|
||||
}
|
||||
increments := Increments{
|
||||
&Increment{Cost: 2, Duration: 3 * time.Second, BalanceUuids: []string{"minutea", "moneya"}},
|
||||
&Increment{Duration: 4 * time.Second, BalanceUuids: []string{"minuteb", ""}},
|
||||
}
|
||||
|
||||
ub.refundIncrements(increments, OUTBOUND, false)
|
||||
if ub.BalanceMap[CREDIT+OUTBOUND][0].Value != 102 ||
|
||||
ub.BalanceMap[MINUTES+OUTBOUND][0].Value != 13 ||
|
||||
ub.BalanceMap[MINUTES+OUTBOUND][1].Value != 14 {
|
||||
t.Error("Error refounding money: ", ub.BalanceMap[MINUTES+OUTBOUND][1].Value)
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************** Benchmarks *******************************/
|
||||
|
||||
func BenchmarkGetSecondForPrefix(b *testing.B) {
|
||||
|
||||
@@ -286,7 +286,7 @@ func (sm *FSSessionManager) OnChannelHangupComplete(ev Event) {
|
||||
// FallbackSubject: lastCC.FallbackSubject, // TODO: check how to best add it
|
||||
}
|
||||
var response float64
|
||||
err := sm.connector.DebitCents(*cd, &response)
|
||||
err := sm.connector.RefundIncrements(*cd, &response)
|
||||
if err != nil {
|
||||
engine.Logger.Err(fmt.Sprintf("Debit cents failed: %v", err))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user