debit minutes even if balance goes negative

This commit is contained in:
Radu Ioan Fericean
2013-08-28 18:02:54 +03:00
parent 17fe5b3351
commit 20f1bf27c3
3 changed files with 17 additions and 25 deletions

View File

@@ -40,6 +40,10 @@ const (
MINUTES = "*minutes"
)
var (
AMOUNT_TOO_BIG = errors.New("Amount excedes balance!")
)
/*
Structure containing information about user's credit (minutes, cents, sms...).'
*/
@@ -128,15 +132,6 @@ func (bc BalanceChain) Equal(o BalanceChain) bool {
return true
}
/*
Error type for overflowed debit methods.
*/
type AmountTooBig struct{}
func (a AmountTooBig) Error() string {
return "Amount excedes balance!"
}
/*
Returns user's available minutes for the specified destination
*/
@@ -207,12 +202,9 @@ func (ub *UserBalance) debitMinutesBalance(amount float64, prefix string, count
}
avaliableNbSeconds, _, bucketList := ub.getSecondsForPrefix(prefix)
if avaliableNbSeconds < amount {
return new(AmountTooBig)
return AMOUNT_TOO_BIG
}
credit := ub.BalanceMap[CREDIT+OUTBOUND]
// calculating money debit
// this is needed because if the credit is less then the amount needed to be debited
// we need to keep everything in place and return an error
for _, mb := range bucketList {
if mb.Seconds < amount {
if mb.Price > 0 { // debit the money if the bucket has price
@@ -224,12 +216,6 @@ func (ub *UserBalance) debitMinutesBalance(amount float64, prefix string, count
}
break
}
if credit.GetTotalValue() < 0 {
break
}
}
if credit.GetTotalValue() < 0 {
return new(AmountTooBig)
}
ub.BalanceMap[CREDIT+OUTBOUND] = credit // credit is > 0

View File

@@ -254,7 +254,10 @@ func TestDebitPriceMoreMinuteBalance(t *testing.T) {
b2 := &MinuteBucket{Seconds: 100, Weight: 20, Price: 1.0, DestinationId: "RET"}
rifsBalance := &UserBalance{Id: "other", MinuteBuckets: []*MinuteBucket{b1, b2}, BalanceMap: map[string]BalanceChain{CREDIT + OUTBOUND: BalanceChain{&Balance{Value: 21}}}}
err := rifsBalance.debitMinutesBalance(25, "0723", false)
if b2.Seconds != 100 || b1.Seconds != 10 || err == nil || rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != -4 {
if b2.Seconds != 75 || b1.Seconds != 10 || err != nil || rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value != -4 {
t.Log(b2.Seconds)
t.Log(b1.Seconds)
t.Log(err)
t.Errorf("Expected %v was %v", -4, rifsBalance.BalanceMap[CREDIT+OUTBOUND][0].Value)
}
}