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 { schemaSelectedCalendarPublic } 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/{userId}_{teamId}:
|
2022-03-30 14:56:24 +00:00
|
|
|
* get:
|
2022-04-01 23:55:41 +00:00
|
|
|
* summary: find selectedCalendar by userID and teamID
|
2022-04-03 15:47:18 +00:00
|
|
|
* parameters:
|
2022-03-31 20:14:37 +00:00
|
|
|
* - in: path
|
2022-04-01 23:55:41 +00:00
|
|
|
* name: userId
|
2022-03-31 20:14:37 +00:00
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-04-01 23:55:41 +00:00
|
|
|
* description: Numeric userId of the selectedCalendar to get
|
|
|
|
* - in: path
|
|
|
|
* name: teamId
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: Numeric teamId of the selectedCalendar to get
|
2022-03-31 20:14:37 +00:00
|
|
|
* tags:
|
2022-04-03 15:47:18 +00:00
|
|
|
* - selected-calendars
|
2022-03-30 14:56:24 +00:00
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: SelectedCalendar was not found
|
|
|
|
*/
|
|
|
|
export async function selectedCalendarById(
|
|
|
|
req: NextApiRequest,
|
|
|
|
res: NextApiResponse<SelectedCalendarResponse>
|
|
|
|
) {
|
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");
|
2022-04-01 23:55:41 +00:00
|
|
|
const [userId, integration, externalId] = safe.data.id.split("_");
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-04-01 23:55:41 +00:00
|
|
|
const selectedCalendar = await prisma.selectedCalendar.findUnique({
|
|
|
|
where: {
|
|
|
|
userId_integration_externalId: {
|
|
|
|
userId: parseInt(userId),
|
|
|
|
integration: integration,
|
|
|
|
externalId: externalId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2022-03-30 14:56:24 +00:00
|
|
|
const data = schemaSelectedCalendarPublic.parse(selectedCalendar);
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-30 14:56:24 +00:00
|
|
|
if (selectedCalendar) res.status(200).json({ data });
|
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: "SelectedCalendar was not found",
|
|
|
|
error,
|
|
|
|
});
|
2022-03-28 22:27:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 23:55:41 +00:00
|
|
|
export default withMiddleware("HTTP_GET")(withValidQueryIdString(selectedCalendarById));
|