diff --git a/public/locales/en/common.json b/public/locales/en/common.json index 4af88730..49a4f18b 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -534,6 +534,7 @@ "expense_participant": { "gets": "{actor, select, you { get } other { gets }}", "owes": "{actor, select, you { owe } other { owes }}", + "owes_and_gets": "{actor, select, you { owe and get back } other { owes and gets back }}", "paid": " paid ", "received": " received " }, diff --git a/src/components/Expense/ExpenseDetails.tsx b/src/components/Expense/ExpenseDetails.tsx index a2f9b972..a955598e 100644 --- a/src/components/Expense/ExpenseDetails.tsx +++ b/src/components/Expense/ExpenseDetails.tsx @@ -13,6 +13,7 @@ import { toast } from 'sonner'; import { useTranslationWithUtils } from '~/hooks/useTranslationWithUtils'; import { DEFAULT_CATEGORY } from '~/lib/category'; import { isCurrencyCode } from '~/lib/currency'; +import { getParticipantShare, hasShareBreakdown } from '~/lib/expenseShare'; import type { ExpenseRouter } from '~/server/api/routers/expense'; import { useAddExpenseStore } from '~/store/addStore'; import { api } from '~/utils/api'; @@ -41,6 +42,7 @@ const ExpenseDetails: React.FC = ({ user, expense }) => { const { displayName, toUIDate, t, getCurrencyHelpersCached } = useTranslationWithUtils(); const { toUIString } = getCurrencyHelpersCached(expense.currency); + const showShareBreakdown = hasShareBreakdown(expense); return ( <> @@ -109,7 +111,7 @@ const ExpenseDetails: React.FC = ({ user, expense }) => {
{expense.fileKey ? : null}
-
+
= ({ user, expense }) => { participant={participant} userId={user.id} currency={expense.currency} + payerShare={ + showShareBreakdown && participant.userId === expense.paidBy + ? getParticipantShare(participant, expense) + : undefined + } /> ))} {expense.conversionTo && ( @@ -174,19 +181,29 @@ const ExpenseParticipantEntry: React.FC<{ participant: ExpenseDetailsOutput['expenseParticipants'][number]; userId: number; currency: string; -}> = ({ participant, userId, currency }) => { + /** The payer's own share, shown alongside what they get back. Omitted for everyone else. */ + payerShare?: bigint; +}> = ({ participant, userId, currency, payerShare }) => { const { displayName, t, getCurrencyHelpersCached } = useTranslationWithUtils(); const { toUIString } = getCurrencyHelpersCached(currency); const isCurrentUser = userId === participant.userId; const isPositive = participant.amount > 0n; const amountColorClass = isPositive ? 'text-positive' : 'text-negative'; + const hasPayerShare = undefined !== payerShare && 0n < payerShare; + + const getI18nKey = () => { + if (hasPayerShare && isPositive) { + return 'expense_participant.owes_and_gets'; + } + return isPositive ? 'expense_participant.gets' : 'expense_participant.owes'; + }; return ( -
+
), amount: {toUIString(participant.amount)}, + share: {toUIString(payerShare ?? 0n)}, }} />
diff --git a/src/lib/expenseShare.ts b/src/lib/expenseShare.ts new file mode 100644 index 00000000..4d2b6a23 --- /dev/null +++ b/src/lib/expenseShare.ts @@ -0,0 +1,13 @@ +import { type SplitType } from '@prisma/client'; + +import { isSpendingSplitType } from '~/lib/splitTypes'; + +/** Whether participants can be shown as "owes X and gets back Y" instead of net amounts. */ +export const hasShareBreakdown = (expense: { amount: bigint; splitType: SplitType }) => + 0n < expense.amount && isSpendingSplitType(expense.splitType); + +/** A participant's share of an expense. Stored amounts are net: what they paid minus their share. */ +export const getParticipantShare = ( + participant: { userId: number; amount: bigint }, + expense: { paidBy: number; amount: bigint }, +) => (participant.userId === expense.paidBy ? expense.amount : 0n) - participant.amount; diff --git a/src/lib/groupStatistics.ts b/src/lib/groupStatistics.ts index aedbc028..fa1b13c7 100644 --- a/src/lib/groupStatistics.ts +++ b/src/lib/groupStatistics.ts @@ -1,14 +1,6 @@ -import { SplitType } from '@prisma/client'; - import { type CurrencyCode, isCurrencyCode } from '~/lib/currency'; import { BigMath, currencyConversion } from '~/utils/numbers'; -// Settlements and currency conversions move money between people; they aren't spending. -export const NON_SPENDING_SPLIT_TYPES = [ - SplitType.SETTLEMENT, - SplitType.CURRENCY_CONVERSION, -] as const; - export interface StatisticsExpense { paidBy: number; currency: string; diff --git a/src/lib/splitTypes.ts b/src/lib/splitTypes.ts new file mode 100644 index 00000000..8bbc3e98 --- /dev/null +++ b/src/lib/splitTypes.ts @@ -0,0 +1,10 @@ +import { SplitType } from '@prisma/client'; + +// Settlements and currency conversions move money between people; they aren't spending. +export const NON_SPENDING_SPLIT_TYPES = [ + SplitType.SETTLEMENT, + SplitType.CURRENCY_CONVERSION, +] as const satisfies readonly SplitType[]; + +export const isSpendingSplitType = (splitType: SplitType) => + !(NON_SPENDING_SPLIT_TYPES as readonly SplitType[]).includes(splitType); diff --git a/src/server/api/routers/group.ts b/src/server/api/routers/group.ts index e0dbd13a..b81123b7 100644 --- a/src/server/api/routers/group.ts +++ b/src/server/api/routers/group.ts @@ -2,12 +2,9 @@ import { TRPCError } from '@trpc/server'; import { nanoid } from 'nanoid'; import { z } from 'zod'; -import { - NON_SPENDING_SPLIT_TYPES, - computeMemberTotals, - currenciesByUse, -} from '~/lib/groupStatistics'; +import { computeMemberTotals, currenciesByUse } from '~/lib/groupStatistics'; import { simplifyDebts } from '~/lib/simplify'; +import { NON_SPENDING_SPLIT_TYPES } from '~/lib/splitTypes'; import { balanceProcedure, createTRPCRouter, diff --git a/src/tests/expenseShare.test.ts b/src/tests/expenseShare.test.ts new file mode 100644 index 00000000..5dd34866 --- /dev/null +++ b/src/tests/expenseShare.test.ts @@ -0,0 +1,60 @@ +import { SplitType } from '@prisma/client'; + +import { getParticipantShare, hasShareBreakdown } from '~/lib/expenseShare'; + +describe('getParticipantShare', () => { + describe('Equal split', () => { + const expense = { paidBy: 1, amount: 10000n }; + + it('should return the payer share from their net amount', () => { + expect(getParticipantShare({ userId: 1, amount: 7500n }, expense)).toBe(2500n); + }); + + it('should return the share of someone who did not pay', () => { + expect(getParticipantShare({ userId: 2, amount: -2500n }, expense)).toBe(2500n); + }); + }); + + describe('Payer not in the split', () => { + it('should return a zero share for the payer', () => { + expect( + getParticipantShare({ userId: 1, amount: 10000n }, { paidBy: 1, amount: 10000n }), + ).toBe(0n); + }); + }); + + describe('Exact split', () => { + const expense = { paidBy: 1, amount: 10000n }; + + it('should return each exact share', () => { + expect(getParticipantShare({ userId: 1, amount: 9000n }, expense)).toBe(1000n); + expect(getParticipantShare({ userId: 2, amount: -6000n }, expense)).toBe(6000n); + expect(getParticipantShare({ userId: 3, amount: -3000n }, expense)).toBe(3000n); + }); + }); +}); + +describe('hasShareBreakdown', () => { + describe('Spending', () => { + it('should allow a regular expense', () => { + expect(hasShareBreakdown({ amount: 10000n, splitType: SplitType.EQUAL })).toBe(true); + }); + + it('should not allow a refund', () => { + expect(hasShareBreakdown({ amount: -10000n, splitType: SplitType.EQUAL })).toBe(false); + }); + + it('should not allow a zero amount', () => { + expect(hasShareBreakdown({ amount: 0n, splitType: SplitType.EQUAL })).toBe(false); + }); + }); + + describe('Money moving between people', () => { + it('should not allow settlements or currency conversions', () => { + expect(hasShareBreakdown({ amount: 10000n, splitType: SplitType.SETTLEMENT })).toBe(false); + expect(hasShareBreakdown({ amount: 10000n, splitType: SplitType.CURRENCY_CONVERSION })).toBe( + false, + ); + }); + }); +}); diff --git a/tests/e2e/group-expense.spec.ts b/tests/e2e/group-expense.spec.ts index e499ce8b..a4728f01 100644 --- a/tests/e2e/group-expense.spec.ts +++ b/tests/e2e/group-expense.spec.ts @@ -29,6 +29,8 @@ test('records a two-member expense with exact persisted shares and debt', async .click(); await expect(page).toHaveURL(/\/groups\/\d+\/expenses\//); await expect(page.getByText(`${uniqueName} expense`, { exact: true })).toBeVisible(); + await expect(page.getByText(/^You owe \$6\.17 and get back \$6\.17$/)).toBeVisible(); + await expect(page.getByText(/^E2E Member owes \$6\.17$/)).toBeVisible(); const expense = await db.expense.findFirstOrThrow({ where: { groupId: scenario.group.id }, include: { expenseParticipants: true },