-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5b7b7d
commit f1f34cb
Showing
5 changed files
with
25 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 8 additions & 44 deletions
52
my-app/src/routes/groups/balance/[id=integer]/+page.server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,21 @@ | ||
import { getUserId } from '$lib/auth'; | ||
import { groupService, spendingService } from '$lib/server/api'; | ||
import { groupService } from '$lib/server/api'; | ||
import type { PageServerLoad } from './$types'; | ||
|
||
type Balance = { | ||
id: number; | ||
email: string; | ||
balance: number; | ||
}; | ||
|
||
export const load: PageServerLoad = async ({ cookies, params }) => { | ||
const id = Number(params.id); | ||
const group: Group = await groupService.get(id, cookies); | ||
const groupMembers: User[] = await groupService.listAllMembers(id, cookies); | ||
const spendings: Spending[] = await spendingService.list(id, cookies); | ||
|
||
const memberBalances = computeBalancesFromSpendings(groupMembers, spendings); | ||
const memberBalances: Balance[] = await groupService.listAllMemberBalances(id, cookies); | ||
|
||
const userId = getUserId(cookies); | ||
const userBalance = memberBalances.find(({ id }) => id === userId)!.balance; | ||
const otherBalances = memberBalances.filter(({ id }) => id !== userId); | ||
const userBalance = memberBalances.find(({ user_id }) => user_id === userId)!.current_balance; | ||
const otherBalances = memberBalances.filter(({ user_id }) => user_id !== userId); | ||
|
||
let balances; | ||
if (userBalance === 0) { | ||
balances = otherBalances.map((mb) => ({ ...mb, balance: 0 })); | ||
} else { | ||
balances = distributeBalances(otherBalances, userBalance); | ||
} | ||
const balances = otherBalances.map(({ user_id, current_balance }) => { | ||
const email = groupMembers.find(({ id }) => id === user_id)!.email; | ||
return { email, balance: current_balance }; | ||
}); | ||
return { group, balances, userBalance }; | ||
}; | ||
|
||
function computeBalancesFromSpendings(groupMembers: User[], spendings: Spending[]) { | ||
// TODO: move this computation to backend | ||
const groupSize = groupMembers.length; | ||
return groupMembers.map(({ id, email }) => { | ||
const balance = spendings | ||
.map(({ owner_id, amount }) => | ||
owner_id === id ? (amount * (groupSize - 1)) / groupSize : -amount / groupSize | ||
) | ||
.reduce((a, b) => a + b, 0); | ||
return { id, email, balance }; | ||
}); | ||
} | ||
|
||
function distributeBalances(memberBalances: Balance[], userBalance: number) { | ||
const sign = Math.sign(userBalance); | ||
const balances = memberBalances.map((mb) => { | ||
if (sign * mb.balance >= 0) { | ||
return { ...mb, balance: 0 }; | ||
} | ||
const balanceLeft = userBalance + mb.balance; | ||
userBalance = sign * Math.max(0, sign * balanceLeft); | ||
const memberBalance = sign * balanceLeft < 0 ? mb.balance - balanceLeft : mb.balance; | ||
return { ...mb, balance: memberBalance }; | ||
}); | ||
return balances; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters