Skip to content

Commit 7a43df0

Browse files
committed
Add checks in missing places and fix typo in schema
1 parent 074ef33 commit 7a43df0

File tree

7 files changed

+176
-129
lines changed

7 files changed

+176
-129
lines changed

prisma/schema.prisma

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,21 @@ model Balance {
107107
}
108108

109109
model Group {
110-
id Int @id @default(autoincrement())
111-
publicId String @unique
112-
name String
113-
userId Int
114-
defaultCurrency String @default("USD")
115-
createdAt DateTime @default(now())
116-
updatedAt DateTime @updatedAt
117-
splitwiseGroupId String? @unique
118-
simplifyDebts Boolean @default(false)
119-
archivedAt DateTime?
120-
expenses Expense[]
121-
createdBy User @relation(fields: [userId], references: [id], onDelete: Cascade)
122-
groupBalances GroupBalance[]
123-
groupUsers GroupUser[]
124-
groupBalanceView BalanceView[]
110+
id Int @id @default(autoincrement())
111+
publicId String @unique
112+
name String
113+
userId Int
114+
defaultCurrency String @default("USD")
115+
createdAt DateTime @default(now())
116+
updatedAt DateTime @updatedAt
117+
splitwiseGroupId String? @unique
118+
simplifyDebts Boolean @default(false)
119+
archivedAt DateTime?
120+
expenses Expense[]
121+
createdBy User @relation(fields: [userId], references: [id], onDelete: Cascade)
122+
groupBalances GroupBalance[]
123+
groupUsers GroupUser[]
124+
groupBalanceViews BalanceView[]
125125
126126
@@schema("public")
127127
}
@@ -285,9 +285,9 @@ view BalanceView {
285285
createdAt DateTime
286286
updatedAt DateTime
287287
288-
user User @relation("UserBalanceView", fields: [userId], references: [id], onDelete: Cascade)
289-
friend User @relation("FriendBalanceView", fields: [friendId], references: [id], onDelete: Cascade)
290-
group Group? @relation(fields: [groupId], references: [id], onDelete: Cascade)
288+
user User @relation("UserBalanceView", fields: [userId], references: [id], onDelete: Cascade)
289+
friend User @relation("FriendBalanceView", fields: [friendId], references: [id], onDelete: Cascade)
290+
group Group? @relation(fields: [groupId], references: [id], onDelete: Cascade)
291291
292292
@@schema("public")
293293
}

src/components/Expense/BalanceList.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ export const BalanceList: React.FC<{
8585
<span className="text-gray-400">
8686
{' '}
8787
{isCurrentUser
88-
? t('ui.balance_list.are_settled_up')
89-
: t('ui.balance_list.is_settled_up')}
88+
? t('expense_details.balance_list.are_settled_up')
89+
: t('expense_details.balance_list.is_settled_up')}
9090
</span>
9191
) : (
9292
<>

src/server/api/routers/expense.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const expenseRouter = createTRPCRouter({
2828

2929
const balances = balancesRaw
3030
.reduce<((typeof balancesRaw)[number] & { hasMore?: boolean })[]>((acc, current) => {
31+
// @ts-ignore This will be resolved once we move away from balance tables
3132
const existing = acc.findIndex((item) => item.friendId === current.friendId);
3233
if (-1 === existing) {
3334
acc.push(current);
@@ -59,7 +60,12 @@ export const expenseRouter = createTRPCRouter({
5960
}
6061
}
6162

62-
return { balances, cumulatedBalances, youOwe, youGet };
63+
return {
64+
balances,
65+
cumulatedBalances,
66+
youOwe,
67+
youGet,
68+
};
6369
}),
6470

6571
addOrEditExpense: protectedProcedure

src/server/api/routers/group.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { simplifyDebts } from '~/lib/simplify';
66
import { createTRPCRouter, groupProcedure, protectedProcedure } from '~/server/api/trpc';
77

88
import { recalculateGroupBalances } from '../services/splitService';
9-
import { getGroupBalances } from '../services/balanceService';
9+
import { assertBalancesMatch, getGroupBalances } from '../services/balanceService';
1010

1111
export const groupRouter = createTRPCRouter({
1212
create: protectedProcedure
@@ -66,6 +66,10 @@ export const groupRouter = createTRPCRouter({
6666
groupBalances: {
6767
where: { userId: ctx.session.user.id },
6868
},
69+
groupBalanceViews: {
70+
where: { userId: ctx.session.user.id, NOT: { groupId: null } },
71+
},
72+
// We can sort by group balance view instead
6973
expenses: {
7074
orderBy: {
7175
createdAt: 'desc',
@@ -77,6 +81,12 @@ export const groupRouter = createTRPCRouter({
7781
},
7882
});
7983

84+
assertBalancesMatch(
85+
groups.flatMap((g) => g.group.groupBalances),
86+
groups.flatMap((g) => g.group.groupBalanceViews),
87+
'getAllGroupsWithBalances',
88+
);
89+
8090
const sortedGroupsByLatestExpense = groups.sort((a, b) => {
8191
const aDate = a.group.expenses[0]?.createdAt ?? new Date(0);
8292
const bDate = b.group.expenses[0]?.createdAt ?? new Date(0);
@@ -134,9 +144,16 @@ export const groupRouter = createTRPCRouter({
134144
},
135145
},
136146
groupBalances: true,
147+
groupBalanceViews: true,
137148
},
138149
});
139150

151+
assertBalancesMatch(
152+
group?.groupBalances || [],
153+
group?.groupBalanceViews || [],
154+
'getGroupDetails',
155+
);
156+
140157
if (group?.simplifyDebts) {
141158
group.groupBalances = simplifyDebts(group.groupBalances);
142159
}
@@ -259,6 +276,7 @@ export const groupRouter = createTRPCRouter({
259276

260277
const groupBalances = await getGroupBalances(input.groupId);
261278

279+
// @ts-ignore This will be resolved once we move away from balance tables
262280
const finalGroupBalances = group.simplifyDebts ? simplifyDebts(groupBalances) : groupBalances;
263281

264282
if (finalGroupBalances.some((b) => b.userId === userId && 0n !== b.amount)) {
@@ -318,9 +336,16 @@ export const groupRouter = createTRPCRouter({
318336
},
319337
include: {
320338
groupBalances: true,
339+
groupBalanceViews: true,
321340
},
322341
});
323342

343+
assertBalancesMatch(
344+
group?.groupBalances || [],
345+
group?.groupBalanceViews || [],
346+
'toggleArchive',
347+
);
348+
324349
if (!group) {
325350
throw new TRPCError({ code: 'NOT_FOUND', message: 'Group not found' });
326351
}
@@ -380,9 +405,16 @@ export const groupRouter = createTRPCRouter({
380405
},
381406
include: {
382407
groupBalances: true,
408+
groupBalanceViews: true,
383409
},
384410
});
385411

412+
assertBalancesMatch(
413+
group?.groupBalances || [],
414+
group?.groupBalanceViews || [],
415+
'deleteGroup',
416+
);
417+
386418
if (group?.userId !== ctx.session.user.id) {
387419
throw new TRPCError({ code: 'UNAUTHORIZED', message: 'Only creator can delete the group' });
388420
}

src/server/api/routers/user.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,6 @@ export const userRouter = createTRPCRouter({
110110
return user;
111111
}),
112112

113-
// sendExpensePushNotification: protectedProcedure
114-
// .input(z.object({ expenseId: z.string() }))
115-
// .mutation(async ({ input }) => {
116-
// sendExpensePushNotification(input.expenseId).catch((err) => {
117-
// console.error('Error sending push notification', err);
118-
// throw new TRPCError({
119-
// code: 'INTERNAL_SERVER_ERROR',
120-
// message: 'Failed to send push notification',
121-
// });
122-
// });
123-
// }),
124-
125113
getUserDetails: protectedProcedure
126114
.input(z.object({ userId: z.number() }))
127115
.query(async ({ input }) => {
@@ -154,6 +142,24 @@ export const userRouter = createTRPCRouter({
154142
},
155143
});
156144

145+
const viewFriend = await db.user.findUnique({
146+
where: {
147+
id: input.friendId,
148+
userBalanceViews: {
149+
some: {
150+
friendId: ctx.session.user.id,
151+
},
152+
},
153+
},
154+
});
155+
156+
if (friend?.id !== viewFriend?.id) {
157+
console.error(`[getFriend] Friend data mismatch for friendId ${input.friendId}:`, {
158+
old: friend,
159+
view: viewFriend,
160+
});
161+
}
162+
157163
return friend;
158164
}),
159165

0 commit comments

Comments
 (0)