Files
splitt-the-bill/server/api/pots/[slug]/participants/[id].patch.ts
T
anbraten 67727f96ab
tinyedge/deploy Deployed by TinyEdge
🐛 fix typecheck and cleanup imports
2026-08-07 12:57:45 +02:00

30 lines
1.1 KiB
TypeScript

import { and, eq } from 'drizzle-orm'
import { isValidPaymentLink } from '~~/shared/utils/paymentLink'
import { useDb } from '~~/server/database/client'
import { 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 body = await readBody(event)
const paymentLink = typeof body?.paymentLink === 'string' ? body.paymentLink.trim() : ''
if (paymentLink && !isValidPaymentLink(paymentLink)) {
throw createError({ statusCode: 400, statusMessage: 'paymentLink must be a valid http(s) URL' })
}
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 [updated] = await db.update(participants)
.set({ paymentLink: paymentLink || null })
.where(eq(participants.id, id))
.returning()
return updated
})