26 lines
1.0 KiB
TypeScript
26 lines
1.0 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()
|
|
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 }
|
|
})
|