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