2022-04-04 19:39:30 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
|
|
|
import type { SelectedCalendarResponse } from "@lib/types";
|
2022-04-20 22:31:42 +00:00
|
|
|
import { getCalcomUserId } from "@lib/utils/getCalcomUserId";
|
2022-04-04 19:39:30 +00:00
|
|
|
import {
|
|
|
|
schemaSelectedCalendarBodyParams,
|
|
|
|
schemaSelectedCalendarPublic,
|
|
|
|
} from "@lib/validations/selected-calendar";
|
|
|
|
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @swagger
|
2022-04-08 23:29:26 +00:00
|
|
|
* /v1/selected-calendars/{userId}_{integration}_{externalId}:
|
2022-04-04 19:39:30 +00:00
|
|
|
* get:
|
|
|
|
* summary: Get a selected-calendar by userID and teamID
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: userId
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-04-08 23:29:26 +00:00
|
|
|
* description: userId of the selected calendar to get
|
2022-04-04 19:39:30 +00:00
|
|
|
* - in: path
|
2022-04-08 23:29:26 +00:00
|
|
|
* name: externalId
|
2022-04-04 19:39:30 +00:00
|
|
|
* schema:
|
2022-04-08 23:29:26 +00:00
|
|
|
* type: string
|
|
|
|
* required: true
|
|
|
|
* description: externalId of the selected calendar to get
|
|
|
|
* - in: path
|
|
|
|
* name: integration
|
|
|
|
* schema:
|
|
|
|
* type: string
|
2022-04-04 19:39:30 +00:00
|
|
|
* required: true
|
2022-04-08 23:29:26 +00:00
|
|
|
* description: integration of the selected calendar to get
|
2022-04-17 14:39:38 +00:00
|
|
|
* security:
|
|
|
|
* - ApiKeyAuth: []
|
2022-04-04 19:39:30 +00:00
|
|
|
* tags:
|
|
|
|
* - selected-calendars
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: SelectedCalendar was not found
|
|
|
|
* patch:
|
2022-04-08 23:29:26 +00:00
|
|
|
* summary: Edit an existing selected calendar
|
2022-04-04 19:39:30 +00:00
|
|
|
* consumes:
|
|
|
|
* - application/json
|
|
|
|
* parameters:
|
|
|
|
* - in: body
|
|
|
|
* name: selected-calendar
|
|
|
|
* description: The selected-calendar to edit
|
|
|
|
* schema:
|
|
|
|
* type: object
|
|
|
|
* $ref: '#/components/schemas/SelectedCalendar'
|
|
|
|
* required: true
|
|
|
|
* - in: path
|
|
|
|
* name: userId
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-04-08 23:29:26 +00:00
|
|
|
* description: userId of the selected calendar to get
|
2022-04-04 19:39:30 +00:00
|
|
|
* - in: path
|
2022-04-08 23:29:26 +00:00
|
|
|
* name: externalId
|
2022-04-04 19:39:30 +00:00
|
|
|
* schema:
|
2022-04-08 23:29:26 +00:00
|
|
|
* type: string
|
2022-04-04 19:39:30 +00:00
|
|
|
* required: true
|
2022-04-08 23:29:26 +00:00
|
|
|
* description: externalId of the selected calendar to get
|
|
|
|
* - in: path
|
|
|
|
* name: integration
|
|
|
|
* schema:
|
|
|
|
* type: string
|
|
|
|
* required: true
|
|
|
|
* description: integration of the selected calendar to get
|
2022-04-17 14:39:38 +00:00
|
|
|
* security:
|
|
|
|
* - ApiKeyAuth: []
|
2022-04-04 19:39:30 +00:00
|
|
|
* tags:
|
|
|
|
* - selected-calendars
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, selected-calendar edited successfuly
|
|
|
|
* model: SelectedCalendar
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. SelectedCalendar body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* delete:
|
|
|
|
* summary: Remove an existing selected-calendar
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: userId
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-04-08 23:29:26 +00:00
|
|
|
* description: userId of the selected calendar to get
|
2022-04-04 19:39:30 +00:00
|
|
|
* - in: path
|
2022-04-08 23:29:26 +00:00
|
|
|
* name: externalId
|
2022-04-04 19:39:30 +00:00
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
2022-04-08 23:29:26 +00:00
|
|
|
* description: externalId of the selected-calendar to get
|
|
|
|
* - in: path
|
|
|
|
* name: integration
|
|
|
|
* schema:
|
|
|
|
* type: string
|
|
|
|
* required: true
|
|
|
|
* description: integration of the selected calendar to get
|
2022-04-17 14:39:38 +00:00
|
|
|
* security:
|
|
|
|
* - ApiKeyAuth: []
|
2022-04-04 19:39:30 +00:00
|
|
|
* tags:
|
|
|
|
* - selected-calendars
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, selected-calendar removed successfuly
|
|
|
|
* model: SelectedCalendar
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. SelectedCalendar id is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
|
|
|
export async function selectedCalendarById(
|
|
|
|
req: NextApiRequest,
|
|
|
|
res: NextApiResponse<SelectedCalendarResponse>
|
|
|
|
) {
|
|
|
|
const { method, query, body } = req;
|
2022-04-10 00:10:34 +00:00
|
|
|
const safeQuery = schemaQueryIdAsString.safeParse(query);
|
|
|
|
const safeBody = schemaSelectedCalendarBodyParams.safeParse(body);
|
2022-04-04 19:39:30 +00:00
|
|
|
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
|
2022-04-08 23:29:26 +00:00
|
|
|
// This is how we set the userId and externalId in the query for managing compoundId.
|
2022-04-20 22:31:42 +00:00
|
|
|
const [paramUserId, integration, externalId] = safeQuery.data.id.split("_");
|
|
|
|
const userId = getCalcomUserId(res);
|
|
|
|
if (userId === parseInt(paramUserId)) {
|
|
|
|
switch (method) {
|
|
|
|
case "GET":
|
|
|
|
await prisma.selectedCalendar
|
|
|
|
.findUnique({
|
|
|
|
where: {
|
|
|
|
userId_integration_externalId: {
|
|
|
|
userId: userId,
|
|
|
|
integration: integration,
|
|
|
|
externalId: externalId,
|
|
|
|
},
|
2022-04-04 19:39:30 +00:00
|
|
|
},
|
2022-04-20 22:31:42 +00:00
|
|
|
})
|
|
|
|
.then((selectedCalendar) => schemaSelectedCalendarPublic.parse(selectedCalendar))
|
|
|
|
.then((selected_calendar) => res.status(200).json({ selected_calendar }))
|
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `SelectedCalendar with id: ${safeQuery.data.id} not found`,
|
|
|
|
error,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
break;
|
2022-04-04 19:39:30 +00:00
|
|
|
|
2022-04-20 22:31:42 +00:00
|
|
|
case "PATCH":
|
|
|
|
if (!safeBody.success) {
|
|
|
|
throw new Error("Invalid request body");
|
|
|
|
}
|
|
|
|
await prisma.selectedCalendar
|
|
|
|
.update({
|
|
|
|
where: {
|
|
|
|
userId_integration_externalId: {
|
|
|
|
userId: userId,
|
|
|
|
integration: integration,
|
|
|
|
externalId: externalId,
|
|
|
|
},
|
2022-04-04 19:39:30 +00:00
|
|
|
},
|
2022-04-20 22:31:42 +00:00
|
|
|
data: safeBody.data,
|
|
|
|
})
|
|
|
|
.then((selectedCalendar) => schemaSelectedCalendarPublic.parse(selectedCalendar))
|
|
|
|
.then((selected_calendar) => res.status(200).json({ selected_calendar }))
|
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `SelectedCalendar with id: ${safeQuery.data.id} not found`,
|
|
|
|
error,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
break;
|
2022-04-04 19:39:30 +00:00
|
|
|
|
2022-04-20 22:31:42 +00:00
|
|
|
case "DELETE":
|
|
|
|
await prisma.selectedCalendar
|
|
|
|
.delete({
|
|
|
|
where: {
|
|
|
|
userId_integration_externalId: {
|
|
|
|
userId: userId,
|
|
|
|
integration: integration,
|
|
|
|
externalId: externalId,
|
|
|
|
},
|
2022-04-04 19:39:30 +00:00
|
|
|
},
|
2022-04-20 22:31:42 +00:00
|
|
|
})
|
|
|
|
.then(() =>
|
|
|
|
res.status(200).json({
|
|
|
|
message: `SelectedCalendar with id: ${safeQuery.data.id} deleted successfully`,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `SelectedCalendar with id: ${safeQuery.data.id} not found`,
|
|
|
|
error,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
break;
|
2022-04-04 19:39:30 +00:00
|
|
|
|
2022-04-20 22:31:42 +00:00
|
|
|
default:
|
|
|
|
res.status(405).json({ message: "Method not allowed" });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else res.status(401).json({ message: "Unauthorized" });
|
2022-04-04 19:39:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdString(selectedCalendarById));
|