From 396c5b8d8caca612b38078cbc862966b9b49b4e7 Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Sun, 27 Mar 2022 01:08:00 +0100 Subject: [PATCH] chore: refactor all delete endpoints to use if/else instead of .catch() and .error() --- pages/api/api-keys/[id]/delete.ts | 24 ++++++++++-------------- pages/api/attendees/[id]/delete.ts | 22 +++++++--------------- pages/api/bookings/[id]/delete.ts | 26 +++++++++----------------- pages/api/event-types/[id]/delete.ts | 25 +++++++++---------------- pages/api/teams/[id]/delete.ts | 25 +++++++++---------------- pages/api/users/[id]/delete.ts | 25 +++++++++---------------- 6 files changed, 53 insertions(+), 94 deletions(-) diff --git a/pages/api/api-keys/[id]/delete.ts b/pages/api/api-keys/[id]/delete.ts index b5644e6ce8..10e6aaef98 100644 --- a/pages/api/api-keys/[id]/delete.ts +++ b/pages/api/api-keys/[id]/delete.ts @@ -1,8 +1,10 @@ import prisma from "@calcom/prisma"; -import { NextApiRequest, NextApiResponse } from "next"; + +import type { NextApiRequest, NextApiResponse } from "next"; import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString"; + type ResponseData = { message?: string; error?: unknown; @@ -11,19 +13,13 @@ type ResponseData = { export async function apiKey(req: NextApiRequest, res: NextApiResponse) { const { query, method } = req; const safe = await schemaQueryIdAsString.safeParse(query); - - if (method === "DELETE" && safe.success) { - // DELETE WILL DELETE THE EVENT TYPE - await prisma.apiKey - .delete({ where: { id: safe.data.id } }) - .then(() => { - // We only remove the api key from the database if there's an existing resource. - res.status(204).json({ message: `api-key with id: ${safe.data.id} deleted successfully` }); - }) - .catch((error) => { - // This catches the error thrown by prisma.apiKey.delete() if the resource is not found. - res.status(404).json({ message: `Resource with id:${safe.data.id} was not found`, error: error }); - }); + if (method === "DELETE" && safe.success && safe.data) { + const apiKey = await prisma.apiKey + .delete({ where: { id: safe.data.id } }) + // We only remove the apiKey type from the database if there's an existing resource. + if (apiKey) res.status(200).json({ message: `apiKey with id: ${safe.data.id} deleted successfully` }); + // This catches the error thrown by prisma.apiKey.delete() if the resource is not found. + else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`}); // Reject any other HTTP method than POST } else res.status(405).json({ message: "Only DELETE Method allowed" }); } diff --git a/pages/api/attendees/[id]/delete.ts b/pages/api/attendees/[id]/delete.ts index 767860af93..47b4a783b7 100644 --- a/pages/api/attendees/[id]/delete.ts +++ b/pages/api/attendees/[id]/delete.ts @@ -13,23 +13,15 @@ type ResponseData = { export async function attendee(req: NextApiRequest, res: NextApiResponse) { const { query, method } = req; const safe = await schemaQueryIdParseInt.safeParse(query); - - if (method === "DELETE" && safe.success) { - // DELETE WILL DELETE THE EVENT TYPE - prisma.attendee + if (method === "DELETE" && safe.success && safe.data) { + const attendee = await prisma.attendee .delete({ where: { id: safe.data.id } }) - .then(() => { - // We only remove the attendee type from the database if there's an existing resource. - res.status(200).json({ message: `attendee-type with id: ${safe.data.id} deleted successfully` }); - }) - .catch((error) => { - // This catches the error thrown by prisma.attendee.delete() if the resource is not found. - res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`, error: error }); - }); - } else { + // We only remove the attendee type from the database if there's an existing resource. + if (attendee) res.status(200).json({ message: `attendee with id: ${safe.data.id} deleted successfully` }); + // This catches the error thrown by prisma.attendee.delete() if the resource is not found. + else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`}); // Reject any other HTTP method than POST - res.status(405).json({ message: "Only DELETE Method allowed in /attendee-types/[id]/delete endpoint" }); - } + } else res.status(405).json({ message: "Only DELETE Method allowed in /availabilities/[id]/delete endpoint" }); } export default withValidQueryIdTransformParseInt(attendee); diff --git a/pages/api/bookings/[id]/delete.ts b/pages/api/bookings/[id]/delete.ts index 62fa836955..4150a66613 100644 --- a/pages/api/bookings/[id]/delete.ts +++ b/pages/api/bookings/[id]/delete.ts @@ -10,26 +10,18 @@ type ResponseData = { error?: unknown; }; -export async function booking(req: NextApiRequest, res: NextApiResponse) { +export async function deleteBooking(req: NextApiRequest, res: NextApiResponse) { const { query, method } = req; const safe = await schemaQueryIdParseInt.safeParse(query); - - if (method === "DELETE" && safe.success) { - // DELETE WILL DELETE THE EVENT TYPE - prisma.booking + if (method === "DELETE" && safe.success && safe.data) { + const booking = await prisma.booking .delete({ where: { id: safe.data.id } }) - .then(() => { - // We only remove the booking type from the database if there's an existing resource. - res.status(200).json({ message: `booking-type with id: ${safe.data.id} deleted successfully` }); - }) - .catch((error) => { - // This catches the error thrown by prisma.booking.delete() if the resource is not found. - res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`, error: error }); - }); - } else { + // We only remove the booking type from the database if there's an existing resource. + if (booking) res.status(200).json({ message: `booking with id: ${safe.data.id} deleted successfully` }); + // This catches the error thrown by prisma.booking.delete() if the resource is not found. + else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`}); // Reject any other HTTP method than POST - res.status(405).json({ message: "Only DELETE Method allowed in /booking-types/[id]/delete endpoint" }); - } + } else res.status(405).json({ message: "Only DELETE Method allowed in /availabilities/[id]/delete endpoint" }); } -export default withValidQueryIdTransformParseInt(booking); +export default withValidQueryIdTransformParseInt(deleteBooking); diff --git a/pages/api/event-types/[id]/delete.ts b/pages/api/event-types/[id]/delete.ts index 72bca44879..d94dc611a2 100644 --- a/pages/api/event-types/[id]/delete.ts +++ b/pages/api/event-types/[id]/delete.ts @@ -10,25 +10,18 @@ type ResponseData = { error?: unknown; }; -export async function eventType(req: NextApiRequest, res: NextApiResponse) { +export async function deleteEventType(req: NextApiRequest, res: NextApiResponse) { const { query, method } = req; const safe = await schemaQueryIdParseInt.safeParse(query); - if (method === "DELETE" && safe.success) { - // DELETE WILL DELETE THE EVENT TYPE - prisma.eventType + if (method === "DELETE" && safe.success && safe.data) { + const eventType = await prisma.eventType .delete({ where: { id: safe.data.id } }) - .then(() => { - // We only remove the event type from the database if there's an existing resource. - res.status(200).json({ message: `event-type with id: ${safe.data.id} deleted successfully` }); - }) - .catch((error) => { - // This catches the error thrown by prisma.eventType.delete() if the resource is not found. - res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`, error: error }); - }); - } else { + // We only remove the eventType type from the database if there's an existing resource. + if (eventType) res.status(200).json({ message: `eventType with id: ${safe.data.id} deleted successfully` }); + // This catches the error thrown by prisma.eventType.delete() if the resource is not found. + else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`}); // Reject any other HTTP method than POST - res.status(405).json({ message: "Only DELETE Method allowed in /event-types/[id]/delete endpoint" }); - } + } else res.status(405).json({ message: "Only DELETE Method allowed in /availabilities/[id]/delete endpoint" }); } -export default withValidQueryIdTransformParseInt(eventType); +export default withValidQueryIdTransformParseInt(deleteEventType); diff --git a/pages/api/teams/[id]/delete.ts b/pages/api/teams/[id]/delete.ts index 935d24c12f..9ca57af0a4 100644 --- a/pages/api/teams/[id]/delete.ts +++ b/pages/api/teams/[id]/delete.ts @@ -10,25 +10,18 @@ type ResponseData = { error?: unknown; }; -export async function team(req: NextApiRequest, res: NextApiResponse) { +export async function deleteTeam(req: NextApiRequest, res: NextApiResponse) { const { query, method } = req; const safe = await schemaQueryIdParseInt.safeParse(query); - if (method === "DELETE" && safe.success) { - // DELETE WILL DELETE THE EVENT TYPE - prisma.team + if (method === "DELETE" && safe.success && safe.data) { + const team = await prisma.team .delete({ where: { id: safe.data.id } }) - .then(() => { - // We only remove the team type from the database if there's an existing resource. - res.status(200).json({ message: `team-type with id: ${safe.data.id} deleted successfully` }); - }) - .catch((error) => { - // This catches the error thrown by prisma.team.delete() if the resource is not found. - res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`, error: error }); - }); - } else { + // We only remove the team type from the database if there's an existing resource. + if (team) res.status(200).json({ message: `team with id: ${safe.data.id} deleted successfully` }); + // This catches the error thrown by prisma.team.delete() if the resource is not found. + else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`}); // Reject any other HTTP method than POST - res.status(405).json({ message: "Only DELETE Method allowed in /team-types/[id]/delete endpoint" }); - } + } else res.status(405).json({ message: "Only DELETE Method allowed" }); } -export default withValidQueryIdTransformParseInt(team); +export default withValidQueryIdTransformParseInt(deleteTeam); diff --git a/pages/api/users/[id]/delete.ts b/pages/api/users/[id]/delete.ts index 3c9b608ef3..f7bbf96856 100644 --- a/pages/api/users/[id]/delete.ts +++ b/pages/api/users/[id]/delete.ts @@ -10,25 +10,18 @@ type ResponseData = { error?: unknown; }; -export async function user(req: NextApiRequest, res: NextApiResponse) { +export async function deleteUser(req: NextApiRequest, res: NextApiResponse) { const { query, method } = req; const safe = await schemaQueryIdParseInt.safeParse(query); - if (method === "DELETE" && safe.success) { - // DELETE WILL DELETE THE EVENT TYPE - prisma.user + if (method === "DELETE" && safe.success && safe.data) { + const user = await prisma.user .delete({ where: { id: safe.data.id } }) - .then(() => { - // We only remove the user type from the database if there's an existing resource. - res.status(200).json({ message: `user-type with id: ${safe.data.id} deleted successfully` }); - }) - .catch((error) => { - // This catches the error thrown by prisma.user.delete() if the resource is not found. - res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`, error: error }); - }); - } else { + // We only remove the user type from the database if there's an existing resource. + if (user) res.status(200).json({ message: `user with id: ${safe.data.id} deleted successfully` }); + // This catches the error thrown by prisma.user.delete() if the resource is not found. + else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`}); // Reject any other HTTP method than POST - res.status(405).json({ message: "Only DELETE Method allowed in /user-types/[id]/delete endpoint" }); - } + } else res.status(405).json({ message: "Only DELETE Method allowed" }); } -export default withValidQueryIdTransformParseInt(user); +export default withValidQueryIdTransformParseInt(deleteUser);