From 362d8ca7697aeb69d1b553e1eb2a80e33870013c Mon Sep 17 00:00:00 2001 From: Joe Au-Yeung Date: Mon, 3 Oct 2022 14:26:00 -0400 Subject: [PATCH] Connect user to new event type --- pages/api/event-types/index.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/pages/api/event-types/index.ts b/pages/api/event-types/index.ts index b3a9d74138..fc05909e2c 100644 --- a/pages/api/event-types/index.ts +++ b/pages/api/event-types/index.ts @@ -98,12 +98,32 @@ async function createOrlistAllEventTypes( return; } if (!isAdmin) { - const data = await prisma.eventType.create({ data: { ...safe.data, userId } }); + const data = await prisma.eventType.create({ + data: { + ...safe.data, + userId, + users: { + connect: { + id: userId, + }, + }, + }, + }); const event_type = schemaEventTypeReadPublic.parse(data); if (data) res.status(201).json({ event_type, message: "EventType created successfully" }); } else { // if admin don't re-set userId from input - const data = await prisma.eventType.create({ data: { ...safe.data } }); + const data = await prisma.eventType.create({ + data: { + ...safe.data, + ...(!safe.data.userId && { userId }), + users: { + connect: { + id: safe.data.userId || userId, + }, + }, + }, + }); const event_type = schemaEventTypeReadPublic.parse(data); if (data) res.status(201).json({ event_type, message: "EventType created successfully" }); }