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 01:16:46 +00:00
|
|
|
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/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-25 18:37:51 +00:00
|
|
|
if (safe.success) {
|
|
|
|
if (method === "DELETE") {
|
|
|
|
// DELETE WILL DELETE THE EVENT TYPE
|
2022-03-25 19:17:37 +00:00
|
|
|
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.
|
|
|
|
res.status(200).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-25 18:37:51 +00:00
|
|
|
res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`, error: error });
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Reject any other HTTP method than POST
|
2022-03-25 19:17:37 +00:00
|
|
|
res.status(405).json({ message: "Only DELETE Method allowed in /api-keys/[id]/delete endpoint" });
|
2022-03-25 18:37:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 19:17:37 +00:00
|
|
|
export default withValidQueryIdString(apiKey);
|