2022-03-27 00:08:00 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-25 18:37:51 +00:00
|
|
|
|
2022-03-29 01:59:57 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-30 12:17:55 +00:00
|
|
|
|
2022-03-29 01:59:57 +00:00
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
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-29 01:59:57 +00:00
|
|
|
export async function deleteApiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
|
|
|
const safe = await schemaQueryIdAsString.safeParse(req.query);
|
|
|
|
if (safe.success) {
|
2022-03-30 12:17:55 +00:00
|
|
|
const data = await prisma.apiKey.delete({ where: { id: safe.data.id } });
|
2022-03-27 00:08:00 +00:00
|
|
|
// We only remove the apiKey type from the database if there's an existing resource.
|
2022-03-29 01:59:57 +00:00
|
|
|
if (data) res.status(200).json({ message: `ApiKey with id: ${safe.data.id} deleted successfully` });
|
2022-03-27 00:08:00 +00:00
|
|
|
// This catches the error thrown by prisma.apiKey.delete() if the resource is not found.
|
2022-03-29 01:59:57 +00:00
|
|
|
else res.status(400).json({ message: `ApiKey with id: ${safe.data.id} was not able to be processed` });
|
|
|
|
}
|
2022-03-25 18:37:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
export default withMiddleware("HTTP_DELETE", "addRequestId")(withValidQueryIdString(deleteApiKey));
|