import { PrismaClient } from "@prisma/client"; import type { NextApiRequest, NextApiResponse } from "next"; import { schemaQueryId, withValidQueryId } from "@lib/validations/queryIdTransformParseInt"; const prisma = new PrismaClient(); type ResponseData = { message?: string; error?: unknown; }; export async function eventType(req: NextApiRequest, res: NextApiResponse) { const { query, method } = req; const safe = await schemaQueryId.safeParse(query); if (safe.success) { if (method === "DELETE") { // DELETE WILL DELETE THE EVENT TYPE 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 { // Reject any other HTTP method than POST res.status(405).json({ message: "Only DELETE Method allowed in /event-types/[id]/delete endpoint" }); } } } export default withValidQueryId(eventType);