This commit is contained in:
@@ -1,29 +1,40 @@
|
||||
import { and, eq } from 'drizzle-orm'
|
||||
import { isValidPaymentLink } from '~~/shared/utils/paymentLink'
|
||||
import { useDb } from '~~/server/database/client'
|
||||
import { participants } from '~~/server/database/schema'
|
||||
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 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() : ''
|
||||
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' })
|
||||
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)))
|
||||
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' })
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
statusMessage: 'Participant not found',
|
||||
});
|
||||
}
|
||||
|
||||
const [updated] = await db.update(participants)
|
||||
const [updated] = await db
|
||||
.update(participants)
|
||||
.set({ paymentLink: paymentLink || null })
|
||||
.where(eq(participants.id, id))
|
||||
.returning()
|
||||
.returning();
|
||||
|
||||
return updated
|
||||
})
|
||||
return updated;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user