chore: refactor all delete endpoints to use if/else instead of .catch() and .error()

pull/9078/head
Agusti Fernandez Pardo 2022-03-27 01:08:00 +01:00
parent 936572e7e1
commit 396c5b8d8c
6 changed files with 53 additions and 94 deletions

View File

@ -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" });
}

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);