30 lines
1.1 KiB
TypeScript
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
|
|
})
|