cal.pub0.org/pages/api/selected-calendars/[id]/delete.ts

61 lines
2.0 KiB
TypeScript
Raw Normal View History

import type { NextApiRequest, NextApiResponse } from "next";
2022-03-30 12:17:55 +00:00
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BaseResponse } from "@lib/types";
2022-04-01 23:55:41 +00:00
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
/**
* @swagger
2022-04-01 23:55:41 +00:00
* /api/selected-calendars/{userId}_{teamId}/delete:
* delete:
* summary: Remove an existing record of a selected calendar
* parameters:
* - in: path
* - name: userId
2022-04-01 23:55:41 +00:00
* schema:
* type: integer
* required: true
* description: Numeric ID of the user to get the selected calendar of
* - name: teamId
2022-04-01 23:55:41 +00:00
* schema:
* type: integer
* required: true
* description: Numeric ID of the team to get the selected calendar of
2022-04-01 23:55:41 +00:00
* tags:
* - selected-calendars
* responses:
* 201:
* description: OK, selected calendar removed successfuly
* 400:
* description: Bad request. selected calendar id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function deleteSelectedCalendar(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
2022-04-01 23:55:41 +00:00
const safe = await schemaQueryIdAsString.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query", safe.error);
2022-04-01 23:55:41 +00:00
const [userId, integration, externalId] = safe.data.id.split("_");
const data = await prisma.selectedCalendar.delete({
where: {
userId_integration_externalId: {
userId: parseInt(userId),
integration: integration,
externalId: externalId,
},
},
});
if (data)
res.status(200).json({ message: `SelectedCalendar with id: ${safe.data.id} deleted successfully` });
else
(error: Error) =>
res.status(400).json({
message: `SelectedCalendar with id: ${safe.data.id} was not able to be processed`,
error,
});
}
2022-04-01 23:55:41 +00:00
export default withMiddleware("HTTP_DELETE")(withValidQueryIdString(deleteSelectedCalendar));