From f985bbd1ff4100e15a11a1197621f86f93c85fb7 Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Tue, 12 Jul 2022 20:01:25 +0200 Subject: [PATCH] fix: move patch to no then/catch --- pages/api/teams/[teamId]/_patch.ts | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/pages/api/teams/[teamId]/_patch.ts b/pages/api/teams/[teamId]/_patch.ts index 7550cd49b5..f395ca7414 100644 --- a/pages/api/teams/[teamId]/_patch.ts +++ b/pages/api/teams/[teamId]/_patch.ts @@ -39,7 +39,7 @@ export async function patchHandler(req: NextApiRequest, res: NextApiResponse) { }); const userTeamIds = userWithMemberships.map((membership) => membership.teamId); // Here we only check for ownership of the user if the user is not admin, otherwise we let ADMIN's edit any user - if (!isAdmin && !userTeamIds.includes(query.teamId)) + if (!isAdmin || !userTeamIds.includes(query.teamId)) throw new HttpError({ statusCode: 401, message: "Unauthorized" }); if (!safeBody.success) { { @@ -47,16 +47,11 @@ export async function patchHandler(req: NextApiRequest, res: NextApiResponse) { return; } } - await prisma.team - .update({ where: { id: query.teamId }, data: safeBody.data }) - .then((team) => schemaTeamReadPublic.parse(team)) - .then((team) => res.status(200).json({ team })) - .catch((error: Error) => - res.status(404).json({ - message: `Team with id: ${query.teamId} not found`, - error, - }) - ); + const data = await prisma.team.update({ where: { id: query.teamId }, data: safeBody.data }); + if (!data) throw new HttpError({ statusCode: 404, message: `Team with id: ${query.teamId} not found` }); + const team = schemaTeamReadPublic.parse(data); + if (!team) throw new HttpError({ statusCode: 401, message: `Your request body wasn't valid` }); + return { team }; } export default defaultResponder(patchHandler);