From 60ff0b54e180886734250b8a63a5532ad37fbe4b Mon Sep 17 00:00:00 2001 From: Dominik Stahl Date: Wed, 18 Jun 2025 10:40:17 +0200 Subject: [PATCH] feat(api): support user participant invitation when creating events --- src/app/api/event/route.ts | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/app/api/event/route.ts b/src/app/api/event/route.ts index 6d9ffda..f9d0936 100644 --- a/src/app/api/event/route.ts +++ b/src/app/api/event/route.ts @@ -10,10 +10,24 @@ const postEventSchema = z start_time: z.iso.datetime(), end_time: z.iso.datetime(), location: z.string().optional().default(''), + participants: z.array(z.string()).optional(), }) .refine((data) => new Date(data.start_time) < new Date(data.end_time), { error: 'Start time must be before end time', - }); + }) + .refine( + async (data) => + !data.participants || + (await Promise.all( + data.participants.map(async (userId) => { + const user = await prisma.user.findUnique({ where: { id: userId } }); + return !!user; + }), + ).then((results) => results.every(Boolean))), + { + error: 'One or more participant user IDs are invalid', + }, + ); /** * @swagger @@ -154,6 +168,11 @@ export const GET = auth(async (req) => { * format: date-time * location: * type: string + * participants: + * type: array + * items: + * type: string + * description: User ID of a participant * responses: * 200: * description: Event created successfully. @@ -215,7 +234,8 @@ export const POST = auth(async (req) => { { status: 400 }, ); } - const { title, description, start_time, end_time, location } = data.data; + const { title, description, start_time, end_time, location, participants } = + data.data; const newEvent = await prisma.meeting.create({ data: { @@ -225,6 +245,13 @@ export const POST = auth(async (req) => { end_time, location, organizer_id: req.auth.user.id, + participants: participants + ? { + create: participants.map((userId) => ({ + user: { connect: { id: userId } }, + })), + } + : undefined, }, select: { id: true,