Scheduling infrastructure for absolutely everyone.
chore: refactor all delete endpoints to use if/else instead of .catch() and .error()
Agusti Fernandez Pardo396c5b8d8cac
diff --git a/pages/api/api-keys/[id]/delete.ts b/pages/api/api-keys/[id]/delete.tsindex b5644e6ce81..10e6aaef983 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<ResponseData>) {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.tsindex 767860af93d..47b4a783b70 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<ResponseData>) {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.tsindex 62fa836955c..4150a66613a 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<ResponseData>) {+export async function deleteBooking(req: NextApiRequest, res: NextApiResponse<ResponseData>) {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.tsindex 72bca448799..d94dc611a28 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<ResponseData>) {+export async function deleteEventType(req: NextApiRequest, res: NextApiResponse<ResponseData>) {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.tsindex 935d24c12fe..9ca57af0a4a 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<ResponseData>) {+export async function deleteTeam(req: NextApiRequest, res: NextApiResponse<ResponseData>) {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.tsindex 3c9b608ef3d..f7bbf96856b 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<ResponseData>) {+export async function deleteUser(req: NextApiRequest, res: NextApiResponse<ResponseData>) {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);