2022-03-25 19:17:37 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
import { NextApiRequest, NextApiResponse } from "next";
|
2022-03-25 18:37:51 +00:00
|
|
|
|
2022-03-26 21:29:30 +00:00
|
|
|
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
|
2022-03-25 18:37:51 +00:00
|
|
|
|
|
|
|
type ResponseData = {
|
|
|
|
message?: string;
|
|
|
|
error?: unknown;
|
|
|
|
};
|
|
|
|
|
2022-03-25 19:17:37 +00:00
|
|
|
export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
2022-03-25 18:37:51 +00:00
|
|
|
const { query, method } = req;
|
2022-03-26 01:16:46 +00:00
|
|
|
const safe = await schemaQueryIdAsString.safeParse(query);
|
2022-03-26 23:58:22 +00:00
|
|
|
|
2022-03-26 04:28:53 +00:00
|
|
|
if (method === "DELETE" && safe.success) {
|
2022-03-25 18:37:51 +00:00
|
|
|
// DELETE WILL DELETE THE EVENT TYPE
|
2022-03-26 04:28:53 +00:00
|
|
|
await prisma.apiKey
|
2022-03-25 18:37:51 +00:00
|
|
|
.delete({ where: { id: safe.data.id } })
|
|
|
|
.then(() => {
|
2022-03-25 19:17:37 +00:00
|
|
|
// We only remove the api key from the database if there's an existing resource.
|
2022-03-26 04:28:53 +00:00
|
|
|
res.status(204).json({ message: `api-key with id: ${safe.data.id} deleted successfully` });
|
2022-03-25 18:37:51 +00:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2022-03-25 19:17:37 +00:00
|
|
|
// This catches the error thrown by prisma.apiKey.delete() if the resource is not found.
|
2022-03-26 04:28:53 +00:00
|
|
|
res.status(404).json({ message: `Resource with id:${safe.data.id} was not found`, error: error });
|
2022-03-25 18:37:51 +00:00
|
|
|
});
|
2022-03-26 23:58:22 +00:00
|
|
|
// Reject any other HTTP method than POST
|
|
|
|
} else res.status(405).json({ message: "Only DELETE Method allowed" });
|
2022-03-25 18:37:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-25 19:17:37 +00:00
|
|
|
export default withValidQueryIdString(apiKey);
|