Simplify get /schedules

pull/9078/head
Joe Au-Yeung 2022-10-06 14:41:50 -04:00
parent 2a7a111855
commit 00ccb4ffd1
1 changed files with 9 additions and 4 deletions

View File

@ -10,7 +10,9 @@ async function createOrlistAllSchedules(
{ method, body, userId, isAdmin, prisma }: NextApiRequest, { method, body, userId, isAdmin, prisma }: NextApiRequest,
res: NextApiResponse<SchedulesResponse | ScheduleResponse> res: NextApiResponse<SchedulesResponse | ScheduleResponse>
) { ) {
if (body.userId && !isAdmin) { const safeBody = schemaScheduleBodyParams.safeParse(body);
if (safeBody.data.userId && !isAdmin) {
res.status(401).json({ message: "Unauthorized" }); res.status(401).json({ message: "Unauthorized" });
return; return;
} else { } else {
@ -31,11 +33,14 @@ async function createOrlistAllSchedules(
* 404: * 404:
* description: No schedules were found * 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({ const data = await prisma.schedule.findMany({
where: { where: {
...(Array.isArray(body.userId) userId: { in: userIds },
? { userId: { in: body.userId } }
: { userId: body.userId || userId }),
}, },
...(Array.isArray(body.userId) && { orderBy: { userId: "asc" } }), ...(Array.isArray(body.userId) && { orderBy: { userId: "asc" } }),
}); });