+
),
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 },