Files
splitt-the-bill/server/api/pots/[slug]/participants/[id].delete.ts
T
anbraten cbe1b42f6e
tinyedge/deploy Deployed by TinyEdge
🐛 add env to useDb and apply prettier
2026-08-07 13:28:26 +02:00

47 lines
1.2 KiB
TypeScript

import { and, eq } from 'drizzle-orm';
import { useDb } from '~~/server/database/client';
import {
expenseParticipants,
expenses,
participants,
} from '~~/server/database/schema';
export default defineEventHandler(async (event) => {
const slug = getRouterParam(event, 'slug')!;
const id = Number(getRouterParam(event, 'id'));
await getPotOrThrow(slug);
const db = useDb(event);
const [existing] = await db
.select()
.from(participants)
.where(and(eq(participants.id, id), eq(participants.potSlug, slug)));
if (!existing) {
throw createError({
statusCode: 404,
statusMessage: 'Participant not found',
});
}
const [asPayer] = await db
.select()
.from(expenses)
.where(eq(expenses.payerId, id))
.limit(1);
const [asSplit] = await db
.select()
.from(expenseParticipants)
.where(eq(expenseParticipants.participantId, id))
.limit(1);
if (asPayer || asSplit) {
throw createError({
statusCode: 409,
statusMessage: 'Cannot remove a participant with expenses or payments',
});
}
await db.delete(participants).where(eq(participants.id, id));
return { success: true };
});