Skip to content

Commit af6b7a4

Browse files
committed
fix(expenses): ensure consistent shares for EVENLY split mode (spliit-app#376)
Normalizes shares to 1 for all participants when split mode is EVENLY in both createExpense and updateExpense. Includes migration to fix existing inconsistent data. Cherry-picked from spliit-app#376 by Uli-Z.
1 parent e131094 commit af6b7a4

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- This migration cleans up existing data where the split mode is 'EVENLY'
2+
-- but the shares are not set to 1. This ensures data consistency for
3+
-- all expenses that should be split equally.
4+
5+
UPDATE "ExpensePaidFor"
6+
SET "shares" = 1
7+
WHERE "expenseId" IN (
8+
SELECT "id"
9+
FROM "Expense"
10+
WHERE "splitMode" = 'EVENLY'
11+
);

src/lib/api.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ export async function createExpense(
6565
groupId,
6666
)
6767

68+
const paidFor =
69+
expenseFormValues.splitMode === 'EVENLY'
70+
? expenseFormValues.paidFor.map(({ participant }) => ({
71+
participant,
72+
shares: 1,
73+
}))
74+
: expenseFormValues.paidFor
75+
6876
// Build paidByList data
6977
const paidByListData =
7078
expenseFormValues.isMultiPayer && expenseFormValues.paidByList.length > 0
@@ -107,7 +115,7 @@ export async function createExpense(
107115
},
108116
paidFor: {
109117
createMany: {
110-
data: expenseFormValues.paidFor.map((paidFor) => ({
118+
data: paidFor.map((paidFor) => ({
111119
participantId: paidFor.participant,
112120
shares: paidFor.shares,
113121
})),
@@ -231,6 +239,14 @@ export async function updateExpense(
231239
existingExpense.expenseDate,
232240
)
233241

242+
const paidFor =
243+
expenseFormValues.splitMode === 'EVENLY'
244+
? expenseFormValues.paidFor.map(({ participant }) => ({
245+
participant,
246+
shares: 1,
247+
}))
248+
: expenseFormValues.paidFor
249+
234250
return prisma.expense.update({
235251
where: { id: expenseId },
236252
data: {
@@ -293,7 +309,7 @@ export async function updateExpense(
293309
}
294310
})(),
295311
paidFor: {
296-
create: expenseFormValues.paidFor
312+
create: paidFor
297313
.filter(
298314
(p) =>
299315
!existingExpense.paidFor.some(
@@ -304,7 +320,7 @@ export async function updateExpense(
304320
participantId: paidFor.participant,
305321
shares: paidFor.shares,
306322
})),
307-
update: expenseFormValues.paidFor.map((paidFor) => ({
323+
update: paidFor.map((paidFor) => ({
308324
where: {
309325
expenseId_participantId: {
310326
expenseId,

0 commit comments

Comments
 (0)