2022-03-30 12:17:55 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
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 { SelectedCalendarResponse } from "@lib/types";
|
|
|
|
import {
|
|
|
|
schemaSelectedCalendarBodyParams,
|
|
|
|
schemaSelectedCalendarPublic,
|
|
|
|
withValidSelectedCalendar,
|
|
|
|
} from "@lib/validations/selected-calendar";
|
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/{id}/edit:
|
2022-03-30 14:56:24 +00:00
|
|
|
* patch:
|
2022-03-31 20:14:37 +00:00
|
|
|
* summary: Edits an existing selectedCalendar
|
2022-04-01 23:55:41 +00:00
|
|
|
* tags:
|
|
|
|
* - selectedCalendars
|
2022-03-30 14:56:24 +00:00
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, selectedCalendar edited successfuly
|
|
|
|
* model: SelectedCalendar
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. SelectedCalendar body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
|
|
|
export async function editSelectedCalendar(
|
|
|
|
req: NextApiRequest,
|
|
|
|
res: NextApiResponse<SelectedCalendarResponse>
|
|
|
|
) {
|
2022-04-01 23:55:41 +00:00
|
|
|
const safeQuery = await schemaQueryIdAsString.safeParse(req.query);
|
2022-03-30 14:56:24 +00:00
|
|
|
const safeBody = await schemaSelectedCalendarBodyParams.safeParse(req.body);
|
|
|
|
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
|
2022-04-01 23:55:41 +00:00
|
|
|
const [userId, integration, externalId] = safeQuery.data.id.split("_");
|
|
|
|
|
2022-03-30 14:56:24 +00:00
|
|
|
const selectedCalendar = await prisma.selectedCalendar.update({
|
2022-04-01 23:55:41 +00:00
|
|
|
where: {
|
|
|
|
userId_integration_externalId: {
|
|
|
|
userId: parseInt(userId),
|
|
|
|
integration: integration,
|
|
|
|
externalId: externalId,
|
|
|
|
},
|
|
|
|
},
|
2022-03-30 14:56:24 +00:00
|
|
|
data: safeBody.data,
|
|
|
|
});
|
|
|
|
const data = schemaSelectedCalendarPublic.parse(selectedCalendar);
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-30 14:56:24 +00:00
|
|
|
if (data) res.status(200).json({ data });
|
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
|
|
|
|
error,
|
|
|
|
});
|
2022-03-28 22:27:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 14:56:24 +00:00
|
|
|
export default withMiddleware("HTTP_PATCH")(
|
2022-04-01 23:55:41 +00:00
|
|
|
withValidQueryIdString(withValidSelectedCalendar(editSelectedCalendar))
|
2022-03-30 14:56:24 +00:00
|
|
|
);
|