chore: refactor all delete endpoints to use if/else instead of .catch() and .error()
parent
936572e7e1
commit
396c5b8d8c
|
@ -1,8 +1,10 @@
|
||||||
import prisma from "@calcom/prisma";
|
import prisma from "@calcom/prisma";
|
||||||
import { NextApiRequest, NextApiResponse } from "next";
|
|
||||||
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
|
|
||||||
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
|
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
|
||||||
|
|
||||||
|
|
||||||
type ResponseData = {
|
type ResponseData = {
|
||||||
message?: string;
|
message?: string;
|
||||||
error?: unknown;
|
error?: unknown;
|
||||||
|
@ -11,19 +13,13 @@ type ResponseData = {
|
||||||
export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||||
const { query, method } = req;
|
const { query, method } = req;
|
||||||
const safe = await schemaQueryIdAsString.safeParse(query);
|
const safe = await schemaQueryIdAsString.safeParse(query);
|
||||||
|
if (method === "DELETE" && safe.success && safe.data) {
|
||||||
if (method === "DELETE" && safe.success) {
|
const apiKey = await prisma.apiKey
|
||||||
// DELETE WILL DELETE THE EVENT TYPE
|
.delete({ where: { id: safe.data.id } })
|
||||||
await prisma.apiKey
|
// We only remove the apiKey type from the database if there's an existing resource.
|
||||||
.delete({ where: { id: safe.data.id } })
|
if (apiKey) res.status(200).json({ message: `apiKey with id: ${safe.data.id} deleted successfully` });
|
||||||
.then(() => {
|
// This catches the error thrown by prisma.apiKey.delete() if the resource is not found.
|
||||||
// We only remove the api key from the database if there's an existing resource.
|
else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`});
|
||||||
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 });
|
|
||||||
});
|
|
||||||
// Reject any other HTTP method than POST
|
// Reject any other HTTP method than POST
|
||||||
} else res.status(405).json({ message: "Only DELETE Method allowed" });
|
} else res.status(405).json({ message: "Only DELETE Method allowed" });
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,23 +13,15 @@ type ResponseData = {
|
||||||
export async function attendee(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
export async function attendee(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
||||||
const { query, method } = req;
|
const { query, method } = req;
|
||||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||||
|
if (method === "DELETE" && safe.success && safe.data) {
|
||||||
if (method === "DELETE" && safe.success) {
|
const attendee = await prisma.attendee
|
||||||
// DELETE WILL DELETE THE EVENT TYPE
|
|
||||||
prisma.attendee
|
|
||||||
.delete({ where: { id: safe.data.id } })
|
.delete({ where: { id: safe.data.id } })
|
||||||
.then(() => {
|
// We only remove the attendee type from the database if there's an existing resource.
|
||||||
// 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` });
|
||||||
res.status(200).json({ message: `attendee-type 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`});
|
||||||
.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 {
|
|
||||||
// Reject any other HTTP method than POST
|
// 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);
|
export default withValidQueryIdTransformParseInt(attendee);
|
||||||
|
|
|
@ -10,26 +10,18 @@ type ResponseData = {
|
||||||
error?: unknown;
|
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 { query, method } = req;
|
||||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||||
|
if (method === "DELETE" && safe.success && safe.data) {
|
||||||
if (method === "DELETE" && safe.success) {
|
const booking = await prisma.booking
|
||||||
// DELETE WILL DELETE THE EVENT TYPE
|
|
||||||
prisma.booking
|
|
||||||
.delete({ where: { id: safe.data.id } })
|
.delete({ where: { id: safe.data.id } })
|
||||||
.then(() => {
|
// We only remove the booking type from the database if there's an existing resource.
|
||||||
// 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` });
|
||||||
res.status(200).json({ message: `booking-type 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`});
|
||||||
.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 {
|
|
||||||
// Reject any other HTTP method than POST
|
// 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);
|
||||||
|
|
|
@ -10,25 +10,18 @@ type ResponseData = {
|
||||||
error?: unknown;
|
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 { query, method } = req;
|
||||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||||
if (method === "DELETE" && safe.success) {
|
if (method === "DELETE" && safe.success && safe.data) {
|
||||||
// DELETE WILL DELETE THE EVENT TYPE
|
const eventType = await prisma.eventType
|
||||||
prisma.eventType
|
|
||||||
.delete({ where: { id: safe.data.id } })
|
.delete({ where: { id: safe.data.id } })
|
||||||
.then(() => {
|
// We only remove the eventType type from the database if there's an existing resource.
|
||||||
// We only remove the event 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` });
|
||||||
res.status(200).json({ message: `event-type 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`});
|
||||||
.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 {
|
|
||||||
// Reject any other HTTP method than POST
|
// 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);
|
||||||
|
|
|
@ -10,25 +10,18 @@ type ResponseData = {
|
||||||
error?: unknown;
|
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 { query, method } = req;
|
||||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||||
if (method === "DELETE" && safe.success) {
|
if (method === "DELETE" && safe.success && safe.data) {
|
||||||
// DELETE WILL DELETE THE EVENT TYPE
|
const team = await prisma.team
|
||||||
prisma.team
|
|
||||||
.delete({ where: { id: safe.data.id } })
|
.delete({ where: { id: safe.data.id } })
|
||||||
.then(() => {
|
// We only remove the team type from the database if there's an existing resource.
|
||||||
// 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` });
|
||||||
res.status(200).json({ message: `team-type 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`});
|
||||||
.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 {
|
|
||||||
// Reject any other HTTP method than POST
|
// 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);
|
||||||
|
|
|
@ -10,25 +10,18 @@ type ResponseData = {
|
||||||
error?: unknown;
|
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 { query, method } = req;
|
||||||
const safe = await schemaQueryIdParseInt.safeParse(query);
|
const safe = await schemaQueryIdParseInt.safeParse(query);
|
||||||
if (method === "DELETE" && safe.success) {
|
if (method === "DELETE" && safe.success && safe.data) {
|
||||||
// DELETE WILL DELETE THE EVENT TYPE
|
const user = await prisma.user
|
||||||
prisma.user
|
|
||||||
.delete({ where: { id: safe.data.id } })
|
.delete({ where: { id: safe.data.id } })
|
||||||
.then(() => {
|
// We only remove the user type from the database if there's an existing resource.
|
||||||
// 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` });
|
||||||
res.status(200).json({ message: `user-type 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`});
|
||||||
.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 {
|
|
||||||
// Reject any other HTTP method than POST
|
// 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);
|
||||||
|
|
Loading…
Reference in New Issue