Skip to content

Commit 378a6c3

Browse files
committed
Allow managing old single user expenses
1 parent 46e723f commit 378a6c3

2 files changed

Lines changed: 45 additions & 45 deletions

File tree

src/server/api/routers/group.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,7 @@ export const groupRouter = createTRPCRouter({
146146
)
147147
.mutation(async ({ input, ctx }) => {
148148
if (input.expenseId) {
149-
const expenseParticipant = await db.expenseParticipant.findUnique({
150-
where: {
151-
expenseId_userId: {
152-
expenseId: input.expenseId,
153-
userId: ctx.session.user.id,
154-
},
155-
},
156-
});
157-
158-
if (!expenseParticipant) {
159-
throw new TRPCError({
160-
code: 'UNAUTHORIZED',
161-
message: 'You are not the participant of the expense',
162-
});
163-
}
149+
await validateEditExpensePermission(input.expenseId, ctx.session.user.id);
164150
}
165151

166152
try {
@@ -471,4 +457,25 @@ export const groupRouter = createTRPCRouter({
471457
}),
472458
});
473459

460+
const validateEditExpensePermission = async (expenseId: string, userId: number): Promise<void> => {
461+
const [expenseParticipant, addedBy] = await Promise.all([
462+
db.expenseParticipant.findUnique({
463+
where: {
464+
expenseId_userId: {
465+
expenseId: expenseId,
466+
userId: userId,
467+
},
468+
},
469+
}),
470+
db.expense.findUnique({ where: { id: expenseId }, select: { addedBy: true } }),
471+
]);
472+
473+
if (!expenseParticipant && !addedBy?.addedBy) {
474+
throw new TRPCError({
475+
code: 'UNAUTHORIZED',
476+
message: 'You are not the participant of the expense',
477+
});
478+
}
479+
};
480+
474481
export type GroupRouter = typeof groupRouter;

src/server/api/routers/user.ts

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -166,21 +166,7 @@ export const userRouter = createTRPCRouter({
166166
)
167167
.mutation(async ({ input, ctx }) => {
168168
if (input.expenseId) {
169-
const expenseParticipant = await db.expenseParticipant.findUnique({
170-
where: {
171-
expenseId_userId: {
172-
expenseId: input.expenseId,
173-
userId: ctx.session.user.id,
174-
},
175-
},
176-
});
177-
178-
if (!expenseParticipant) {
179-
throw new TRPCError({
180-
code: 'UNAUTHORIZED',
181-
message: 'You are not the participant of the expense',
182-
});
183-
}
169+
await validateEditExpensePermission(input.expenseId, ctx.session.user.id);
184170
}
185171

186172
try {
@@ -438,21 +424,7 @@ export const userRouter = createTRPCRouter({
438424
deleteExpense: protectedProcedure
439425
.input(z.object({ expenseId: z.string() }))
440426
.mutation(async ({ input, ctx }) => {
441-
const expenseParticipant = await db.expenseParticipant.findUnique({
442-
where: {
443-
expenseId_userId: {
444-
expenseId: input.expenseId,
445-
userId: ctx.session.user.id,
446-
},
447-
},
448-
});
449-
450-
if (!expenseParticipant) {
451-
throw new TRPCError({
452-
code: 'UNAUTHORIZED',
453-
message: 'You are not the participant of the expense',
454-
});
455-
}
427+
await validateEditExpensePermission(input.expenseId, ctx.session.user.id);
456428

457429
await deleteExpense(input.expenseId, ctx.session.user.id);
458430
}),
@@ -558,4 +530,25 @@ export const userRouter = createTRPCRouter({
558530
}),
559531
});
560532

533+
const validateEditExpensePermission = async (expenseId: string, userId: number): Promise<void> => {
534+
const [expenseParticipant, addedBy] = await Promise.all([
535+
db.expenseParticipant.findUnique({
536+
where: {
537+
expenseId_userId: {
538+
expenseId: expenseId,
539+
userId: userId,
540+
},
541+
},
542+
}),
543+
db.expense.findUnique({ where: { id: expenseId }, select: { addedBy: true } }),
544+
]);
545+
546+
if (!expenseParticipant && !addedBy?.addedBy) {
547+
throw new TRPCError({
548+
code: 'UNAUTHORIZED',
549+
message: 'You are not the participant of the expense',
550+
});
551+
}
552+
};
553+
561554
export type UserRouter = typeof userRouter;

0 commit comments

Comments
 (0)