Skip to content

Commit 5551676

Browse files
committed
fix(expenses): Ensure shares are consistent for 'EVENLY' split mode
This commit addresses a data inconsistency issue where expenses with a split mode of 'EVENLY' could have non-normalized shares, leading to calculation errors in features like CSV exports. This is a comprehensive fix that includes two parts: 1. **Application-level fix:** The createExpense and updateExpense functions in src/lib/api.ts are updated to explicitly check if the split mode is 'EVENLY'. If it is, the shares for all participants are normalized to 1, ensuring that all new and updated expenses are stored with consistent data. 2. **Data migration:** A new data migration is introduced to clean up existing inconsistent data. The migration updates the shares to 1 for all ExpensePaidFor entries associated with an Expense where splitMode is 'EVENLY'. Together, these changes guarantee data integrity for all 'EVENLY' split expenses, both past and future.
1 parent 3b83a5f commit 5551676

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
@@ -64,6 +64,14 @@ export async function createExpense(
6464
groupId,
6565
)
6666

67+
const paidFor =
68+
expenseFormValues.splitMode === 'EVENLY'
69+
? expenseFormValues.paidFor.map(({ participant }) => ({
70+
participant,
71+
shares: 1,
72+
}))
73+
: expenseFormValues.paidFor
74+
6775
return prisma.expense.create({
6876
data: {
6977
id: expenseId,
@@ -87,7 +95,7 @@ export async function createExpense(
8795
},
8896
paidFor: {
8997
createMany: {
90-
data: expenseFormValues.paidFor.map((paidFor) => ({
98+
data: paidFor.map((paidFor) => ({
9199
participantId: paidFor.participant,
92100
shares: paidFor.shares,
93101
})),
@@ -204,6 +212,14 @@ export async function updateExpense(
204212
existingExpense.expenseDate,
205213
)
206214

215+
const paidFor =
216+
expenseFormValues.splitMode === 'EVENLY'
217+
? expenseFormValues.paidFor.map(({ participant }) => ({
218+
participant,
219+
shares: 1,
220+
}))
221+
: expenseFormValues.paidFor
222+
207223
return prisma.expense.update({
208224
where: { id: expenseId },
209225
data: {
@@ -218,7 +234,7 @@ export async function updateExpense(
218234
splitMode: expenseFormValues.splitMode,
219235
recurrenceRule: expenseFormValues.recurrenceRule,
220236
paidFor: {
221-
create: expenseFormValues.paidFor
237+
create: paidFor
222238
.filter(
223239
(p) =>
224240
!existingExpense.paidFor.some(
@@ -229,7 +245,7 @@ export async function updateExpense(
229245
participantId: paidFor.participant,
230246
shares: paidFor.shares,
231247
})),
232-
update: expenseFormValues.paidFor.map((paidFor) => ({
248+
update: paidFor.map((paidFor) => ({
233249
where: {
234250
expenseId_participantId: {
235251
expenseId,

0 commit comments

Comments
 (0)