2022-04-02 01:51:33 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
|
|
|
import type { BaseResponse } from "@lib/types";
|
|
|
|
import {
|
|
|
|
schemaQueryIdParseInt,
|
|
|
|
withValidQueryIdTransformParseInt,
|
|
|
|
} from "@lib/validations/shared/queryIdTransformParseInt";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @swagger
|
2022-04-11 13:10:16 +00:00
|
|
|
* /v1/resources/{id}/delete:
|
2022-04-02 01:51:33 +00:00
|
|
|
* delete:
|
|
|
|
* summary: Remove an existing resource
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: Numeric ID of the resource to delete
|
2022-04-17 14:39:38 +00:00
|
|
|
* security:
|
|
|
|
* - ApiKeyAuth: []
|
2022-04-02 01:51:33 +00:00
|
|
|
* tags:
|
|
|
|
* - resources
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, resource removed successfuly
|
|
|
|
* model: Resource
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Resource id is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
|
|
|
export async function deleteResource(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
|
2022-04-10 00:10:34 +00:00
|
|
|
const safe = schemaQueryIdParseInt.safeParse(req.query);
|
2022-04-02 01:51:33 +00:00
|
|
|
if (!safe.success) throw new Error("Invalid request query", safe.error);
|
|
|
|
|
|
|
|
const data = await prisma.resource.delete({ where: { id: safe.data.id } });
|
|
|
|
|
|
|
|
if (data) res.status(200).json({ message: `Resource with id: ${safe.data.id} deleted successfully` });
|
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(400).json({
|
|
|
|
message: `Resource with id: ${safe.data.id} was not able to be processed`,
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteResource));
|