2022-03-28 22:27:14 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-30 14:56:24 +00:00
|
|
|
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";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-30 14:56:24 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
2022-04-01 23:55:41 +00:00
|
|
|
* /api/selected-calendars/{userId}_{teamId}/delete:
|
2022-03-30 14:56:24 +00:00
|
|
|
* delete:
|
2022-03-31 20:14:37 +00:00
|
|
|
* summary: Remove an existing selectedCalendar
|
2022-04-01 23:55:41 +00:00
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* - name: userId
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: Numeric ID of the user to get the selectedCalendar of
|
|
|
|
* * - name: teamId
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: Numeric ID of the team to get the selectedCalendar of
|
|
|
|
* tags:
|
|
|
|
* - selectedCalendars
|
2022-03-30 14:56:24 +00:00
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, selectedCalendar removed successfuly
|
|
|
|
* model: SelectedCalendar
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. SelectedCalendar 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);
|
2022-03-30 14:56:24 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2022-03-30 14:56:24 +00:00
|
|
|
|
|
|
|
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-03-28 22:27:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 23:55:41 +00:00
|
|
|
export default withMiddleware("HTTP_DELETE")(withValidQueryIdString(deleteSelectedCalendar));
|