Updated the FilterExpenseParticipants methods
This commit is contained in:
@@ -2,46 +2,68 @@ package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
"sort"
|
||||
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
func sortBySum(a, b mod.Debt) int {
|
||||
if a.Sum.LessThan(b.Sum) {
|
||||
return -1
|
||||
}
|
||||
type UserTab struct {
|
||||
User mod.User
|
||||
Sum dec.Decimal
|
||||
}
|
||||
type UserTabs []UserTab
|
||||
|
||||
if a.Sum.GreaterThan(b.Sum) {
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
func (ut UserTabs) SortBySum() {
|
||||
sort.Slice(ut, func(i, j int) bool {
|
||||
return ut[i].Sum.Cmp(ut[j].Sum) > 0
|
||||
})
|
||||
}
|
||||
|
||||
func filterExpenseParticipants(e *mod.Expense,
|
||||
) (map[mod.User]dec.Decimal, map[mod.User]dec.Decimal) {
|
||||
func (ut UserTabs) Equal(t UserTabs) bool {
|
||||
if len(ut) != len(t) {
|
||||
return false
|
||||
}
|
||||
|
||||
for k := range ut {
|
||||
if ut[k].User != t[k].User || !ut[k].Sum.Equal(t[k].Sum) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func filterExpenseParticipants(e *mod.Expense) (UserTabs, UserTabs) {
|
||||
shares := mapShares(e)
|
||||
payments := mapPayments(e)
|
||||
|
||||
debtors := make(map[mod.User]dec.Decimal)
|
||||
creditors := make(map[mod.User]dec.Decimal)
|
||||
debtors := UserTabs{}
|
||||
creditors := UserTabs{}
|
||||
|
||||
for user, share := range shares {
|
||||
debt := (share.Mul(e.Value)).Sub(payments[user])
|
||||
if debt.LessThan(dec.NewFromFloat(0.0)) {
|
||||
creditors[user] = debt.Abs()
|
||||
creditors = append(creditors, UserTab{
|
||||
User: user,
|
||||
Sum: debt.Abs(),
|
||||
})
|
||||
}
|
||||
|
||||
if debt.GreaterThan(dec.NewFromFloat(0.0)) {
|
||||
debtors[user] = debt.Abs()
|
||||
debtors = append(debtors, UserTab{
|
||||
User: user,
|
||||
Sum: debt.Abs(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
debtors.SortBySum()
|
||||
creditors.SortBySum()
|
||||
|
||||
return debtors, creditors
|
||||
}
|
||||
|
||||
func resolveDebts(debtors map[mod.User]dec.Decimal,
|
||||
creditors map[mod.User]dec.Decimal) []mod.Debt {
|
||||
func resolveDebts(debtors UserTabs, creditors UserTabs) []mod.Debt {
|
||||
debts := []mod.Debt{}
|
||||
|
||||
// maybe create a map point to the debt of each user and count from there?
|
||||
@@ -58,7 +80,7 @@ func CalculateDebts(e *mod.Expense) ([]mod.Debt, error) {
|
||||
// debts := dc.getDebts()
|
||||
|
||||
debtors, creditors := filterExpenseParticipants(e)
|
||||
debts := resolveDebts(debtors, creditors)
|
||||
_ = resolveDebts(debtors, creditors)
|
||||
|
||||
return []mod.Debt{}, nil
|
||||
}
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
// "slices"
|
||||
"testing"
|
||||
|
||||
dec "github.com/shopspring/decimal"
|
||||
// "github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -66,42 +63,26 @@ Summary:
|
||||
|
||||
func TestFilterExpenseParticipants(t *testing.T) {
|
||||
// Use expense2 here for the test
|
||||
expectedCreditors := map[mod.User]dec.Decimal{
|
||||
user1: dec.NewFromFloat(32.0),
|
||||
user3: dec.NewFromFloat(22.0),
|
||||
expectedCreditors := UserTabs{
|
||||
{User: user1, Sum: dec.NewFromFloat(32.0)},
|
||||
{User: user3, Sum: dec.NewFromFloat(22.0)},
|
||||
}
|
||||
|
||||
expectedDebtors := map[mod.User]dec.Decimal{
|
||||
user2: dec.NewFromFloat(43.0),
|
||||
user4: dec.NewFromFloat(11.0),
|
||||
expectedDebtors := UserTabs{
|
||||
{User: user2, Sum: dec.NewFromFloat(43.0)},
|
||||
{User: user4, Sum: dec.NewFromFloat(11.0)},
|
||||
}
|
||||
|
||||
debtors, creditors := filterExpenseParticipants(&expense2)
|
||||
|
||||
// Compare the creditors
|
||||
for user, cred := range creditors {
|
||||
val, ok := expectedCreditors[user]
|
||||
if !ok {
|
||||
t.Errorf("Expected user `%+v` to be present in expectedCreditors `%+v`",
|
||||
user, expectedCreditors)
|
||||
}
|
||||
|
||||
if !val.Equal(cred) {
|
||||
t.Errorf("Expected value `%+v` to be `%+v`", cred, val)
|
||||
}
|
||||
if !expectedDebtors.Equal(debtors) {
|
||||
t.Errorf("Expected debtors:\n%+v\nGotten debtors:\n%+v\n", expectedDebtors,
|
||||
debtors)
|
||||
}
|
||||
|
||||
// Compare the debtors
|
||||
for user, cred := range debtors {
|
||||
val, ok := expectedDebtors[user]
|
||||
if !ok {
|
||||
t.Errorf("Expected user `%+v` to be present in expectedCreditors `%+v`",
|
||||
user, expectedDebtors)
|
||||
}
|
||||
|
||||
if !val.Equal(cred) {
|
||||
t.Errorf("Expected value `%+v` to be `%+v`", cred, val)
|
||||
}
|
||||
if !expectedCreditors.Equal(creditors) {
|
||||
t.Errorf("Expected creditors:\n%+v\nGotten creditors:\n%+v\n", expectedCreditors,
|
||||
creditors)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user