From 00ccb4ffd102d1e22b75ec20016ae10ef0fb148b Mon Sep 17 00:00:00 2001 From: Joe Au-Yeung Date: Thu, 6 Oct 2022 14:41:50 -0400 Subject: [PATCH] Simplify get /schedules --- pages/api/schedules/index.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pages/api/schedules/index.ts b/pages/api/schedules/index.ts index d6156c9c8b..b486431440 100644 --- a/pages/api/schedules/index.ts +++ b/pages/api/schedules/index.ts @@ -10,7 +10,9 @@ async function createOrlistAllSchedules( { method, body, userId, isAdmin, prisma }: NextApiRequest, res: NextApiResponse ) { - if (body.userId && !isAdmin) { + const safeBody = schemaScheduleBodyParams.safeParse(body); + + if (safeBody.data.userId && !isAdmin) { res.status(401).json({ message: "Unauthorized" }); return; } else { @@ -31,11 +33,14 @@ async function createOrlistAllSchedules( * 404: * description: No schedules were found */ + + const userIds = Array.isArray(safeBody.data.userId) + ? safeBody.data.userId + : [safeBody.data.userId || userId]; + const data = await prisma.schedule.findMany({ where: { - ...(Array.isArray(body.userId) - ? { userId: { in: body.userId } } - : { userId: body.userId || userId }), + userId: { in: userIds }, }, ...(Array.isArray(body.userId) && { orderBy: { userId: "asc" } }), });