From 9f4a62e2ef04945e0b0a102fcc8eac27aeb2e20c Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Sat, 2 Apr 2022 01:55:41 +0200 Subject: [PATCH] feat: yarn build no errors --- lib/helpers/withCost.ts | 2 - lib/types.ts | 9 +++ lib/validations/event-type-custom-input.ts | 29 ++++++++ lib/validations/schedule.ts | 12 ++- lib/validations/selected-calendar.ts | 24 +++++- .../api/daily-event-references/[id]/delete.ts | 55 +++++++++----- pages/api/daily-event-references/[id]/edit.ts | 74 ++++++++++++------- .../api/daily-event-references/[id]/index.ts | 57 ++++++++++---- pages/api/daily-event-references/index.ts | 43 +++++++++-- pages/api/daily-event-references/new.ts | 64 +++++++++++----- .../api/destination-calendars/[id]/delete.ts | 56 +++++++++----- pages/api/destination-calendars/[id]/edit.ts | 74 ++++++++++++------- pages/api/destination-calendars/[id]/index.ts | 57 ++++++++++---- pages/api/destination-calendars/index.ts | 43 +++++++++-- pages/api/destination-calendars/new.ts | 64 +++++++++++----- .../event-type-custom-inputs/[id]/delete.ts | 56 +++++++++----- .../api/event-type-custom-inputs/[id]/edit.ts | 41 ---------- .../[id]/edit.ts.fixme | 65 ++++++++++++++++ .../event-type-custom-inputs/[id]/index.ts | 57 ++++++++++---- pages/api/event-type-custom-inputs/index.ts | 43 +++++++++-- pages/api/event-type-custom-inputs/new.ts | 31 -------- .../api/event-type-custom-inputs/new.ts.fixme | 55 ++++++++++++++ pages/api/selected-calendars/[id]/delete.ts | 37 +++++++--- pages/api/selected-calendars/[id]/edit.ts | 24 +++--- pages/api/selected-calendars/[id]/index.ts | 36 ++++++--- 25 files changed, 786 insertions(+), 322 deletions(-) create mode 100644 lib/validations/event-type-custom-input.ts delete mode 100644 pages/api/event-type-custom-inputs/[id]/edit.ts create mode 100644 pages/api/event-type-custom-inputs/[id]/edit.ts.fixme delete mode 100644 pages/api/event-type-custom-inputs/new.ts create mode 100644 pages/api/event-type-custom-inputs/new.ts.fixme diff --git a/lib/helpers/withCost.ts b/lib/helpers/withCost.ts index 55f8e06ee5..d462536956 100644 --- a/lib/helpers/withCost.ts +++ b/lib/helpers/withCost.ts @@ -3,8 +3,6 @@ // Initially to test out 0,5 cent per booking via API call // withCost(5)(endpoint) // Should add a charge of 0.5 cent per booking to the subscription of the user making the request -// Should be called in the middleware of the e -// import { NextMiddleware } from "next-api-middleware"; export const withCost = (priceInCents: number): NextMiddleware => { diff --git a/lib/types.ts b/lib/types.ts index 806f84b18f..d0c33538c3 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -5,6 +5,7 @@ import { Credential, SelectedCalendar, EventType, + EventTypeCustomInput, Attendee, Availability, BookingReference, @@ -120,6 +121,14 @@ export type MembershipsResponse = BaseResponse & { data?: Partial[]; }; +// EventTypeCustomInput +export type EventTypeCustomInputResponse = BaseResponse & { + data?: Partial; +}; +export type EventTypeCustomInputsResponse = BaseResponse & { + data?: Partial[]; +}; + // EventType export type EventTypeResponse = BaseResponse & { data?: Partial; diff --git a/lib/validations/event-type-custom-input.ts b/lib/validations/event-type-custom-input.ts new file mode 100644 index 0000000000..4898af83bd --- /dev/null +++ b/lib/validations/event-type-custom-input.ts @@ -0,0 +1,29 @@ +import { withValidation } from "next-validations"; +import { z } from "zod"; + +import { EventTypeModel, _EventTypeCustomInputModel as EventTypeCustomInput } from "@calcom/prisma/zod"; + +export const schemaEventTypeCustomInputBaseBodyParams = EventTypeCustomInput.omit({ + id: true, + eventTypeId: true, +}).partial(); + +export const schemaEventTypeCustomInputPublic = EventTypeCustomInput.omit({}); + +const schemaEventTypeCustomInputRequiredParams = z.object({ + label: z.string(), + // uid: z.string(), + eventType: EventTypeModel.optional(), + + // eventType: z.instanceof(EventTypeInputModel), +}); + +export const schemaEventTypeCustomInputBodyParams = schemaEventTypeCustomInputBaseBodyParams.merge( + schemaEventTypeCustomInputRequiredParams +); + +export const withValidEventTypeCustomInput = withValidation({ + schema: schemaEventTypeCustomInputBodyParams, + type: "Zod", + mode: "body", +}); diff --git a/lib/validations/schedule.ts b/lib/validations/schedule.ts index dca4a77345..e71ecbfba8 100644 --- a/lib/validations/schedule.ts +++ b/lib/validations/schedule.ts @@ -1,8 +1,18 @@ import { withValidation } from "next-validations"; +import { z } from "zod"; import { _ScheduleModel as Schedule } from "@calcom/prisma/zod"; -export const schemaScheduleBodyParams = Schedule.omit({ id: true }).partial(); +// import { jsonSchema } from "./shared/jsonSchema"; + +const schemaScheduleBaseBodyParams = Schedule.omit({ id: true }).partial(); + +const schemaScheduleRequiredParams = z.object({ + userId: z.number(), + name: z.string(), +}); + +export const schemaScheduleBodyParams = schemaScheduleBaseBodyParams.merge(schemaScheduleRequiredParams); export const schemaSchedulePublic = Schedule.omit({}); diff --git a/lib/validations/selected-calendar.ts b/lib/validations/selected-calendar.ts index f1bb0483ae..64eee4e401 100644 --- a/lib/validations/selected-calendar.ts +++ b/lib/validations/selected-calendar.ts @@ -1,10 +1,28 @@ import { withValidation } from "next-validations"; +import { z } from "zod"; -import { _SelectedCalendarModel as SelectedCalendar } from "@calcom/prisma/zod"; +import { _UserModel, _SelectedCalendarModel as SelectedCalendar } from "@calcom/prisma/zod"; -export const schemaSelectedCalendarBodyParams = SelectedCalendar.omit({}).partial(); +export const schemaSelectedCalendarBaseBodyParams = SelectedCalendar.omit({ userId: true }).partial(); -export const schemaSelectedCalendarPublic = SelectedCalendar.omit({ userId: true }); +export const schemaSelectedCalendarPublic = SelectedCalendar.omit({}); + +const schemaSelectedCalendarRequiredParams = z.object({ + externalId: z.string(), + integration: z.string(), + user: z.object({ + connect: z.object({ + id: z.number().optional(), + username: z.string().optional(), + email: z.string().optional(), + }), + create: z.any(), + }), +}); + +export const schemaSelectedCalendarBodyParams = schemaSelectedCalendarBaseBodyParams.merge( + schemaSelectedCalendarRequiredParams +); export const withValidSelectedCalendar = withValidation({ schema: schemaSelectedCalendarBodyParams, diff --git a/pages/api/daily-event-references/[id]/delete.ts b/pages/api/daily-event-references/[id]/delete.ts index 77c97782a6..71b61d86c4 100644 --- a/pages/api/daily-event-references/[id]/delete.ts +++ b/pages/api/daily-event-references/[id]/delete.ts @@ -3,30 +3,49 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; +import type { BaseResponse } from "@lib/types"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -type ResponseData = { - message?: string; - error?: unknown; -}; - -export async function deleteDailyEventReference(req: NextApiRequest, res: NextApiResponse) { +/** + * @swagger + * /api/daily-event-references/{id}/delete: + * delete: + * summary: Remove an existing dailyEventReference + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the dailyEventReference to delete + * tags: + * - dailyEventReferences + * responses: + * 201: + * description: OK, dailyEventReference removed successfuly + * model: DailyEventReference + * 400: + * description: Bad request. DailyEventReference id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function deleteDailyEventReference(req: NextApiRequest, res: NextApiResponse) { const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (safe.success) { - const deletedDailyEventReference = await prisma.dailyEventReference.delete({ - where: { id: safe.data.id }, - }); - // We only remove the dailyEventReference type from the database if there's an existing resource. - if (deletedDailyEventReference) - res.status(200).json({ message: `dailyEventReference with id: ${safe.data.id} deleted successfully` }); - // This catches the error thrown by prisma.dailyEventReference.delete() if the resource is not found. - else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found` }); - // Reject any other HTTP method than POST - } else res.status(405).json({ message: "Only DELETE Method allowed" }); + if (!safe.success) throw new Error("Invalid request query", safe.error); + + const data = await prisma.dailyEventReference.delete({ where: { id: safe.data.id } }); + + if (data) + res.status(200).json({ message: `DailyEventReference with id: ${safe.data.id} deleted successfully` }); + else + (error: Error) => + res.status(400).json({ + message: `DailyEventReference with id: ${safe.data.id} was not able to be processed`, + error, + }); } -// export default withValidQueryIdTransformParseInt(deleteDailyEventReference); export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteDailyEventReference)); diff --git a/pages/api/daily-event-references/[id]/edit.ts b/pages/api/daily-event-references/[id]/edit.ts index 372e258163..e3d9f73765 100644 --- a/pages/api/daily-event-references/[id]/edit.ts +++ b/pages/api/daily-event-references/[id]/edit.ts @@ -1,10 +1,12 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; -import { DailyEventReference } from "@calcom/prisma/client"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import type { DailyEventReferenceResponse } from "@lib/types"; import { - schemaDailyEventReference, + schemaDailyEventReferenceBodyParams, + schemaDailyEventReferencePublic, withValidDailyEventReference, } from "@lib/validations/daily-event-reference"; import { @@ -12,30 +14,52 @@ import { withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -type ResponseData = { - data?: DailyEventReference; - message?: string; - error?: unknown; -}; +/** + * @swagger + * /api/daily-event-references/{id}/edit: + * patch: + * summary: Edit an existing dailyEventReference + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the dailyEventReference to edit + * tags: + * - dailyEventReferences + * responses: + * 201: + * description: OK, dailyEventReference edited successfuly + * model: DailyEventReference + * 400: + * description: Bad request. DailyEventReference body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function editDailyEventReference( + req: NextApiRequest, + res: NextApiResponse +) { + const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); + const safeBody = await schemaDailyEventReferenceBodyParams.safeParse(req.body); -export async function editDailyEventReference(req: NextApiRequest, res: NextApiResponse) { - const { query, body, method } = req; - const safeQuery = await schemaQueryIdParseInt.safeParse(query); - const safeBody = await schemaDailyEventReference.safeParse(body); + if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); + const dailyEventReference = await prisma.dailyEventReference.update({ + where: { id: safeQuery.data.id }, + data: safeBody.data, + }); + const data = schemaDailyEventReferencePublic.parse(dailyEventReference); - if (method === "PATCH" && safeQuery.success && safeBody.success) { - const data = await prisma.dailyEventReference.update({ - where: { id: safeQuery.data.id }, - data: safeBody.data, - }); - if (data) res.status(200).json({ data }); - else - res - .status(404) - .json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error }); - - // Reject any other HTTP method than POST - } else res.status(405).json({ message: "Only PATCH Method allowed for updating dailyEventReferences" }); + 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, + }); } -export default withValidQueryIdTransformParseInt(withValidDailyEventReference(editDailyEventReference)); +export default withMiddleware("HTTP_PATCH")( + withValidQueryIdTransformParseInt(withValidDailyEventReference(editDailyEventReference)) +); diff --git a/pages/api/daily-event-references/[id]/index.ts b/pages/api/daily-event-references/[id]/index.ts index 459c4f5381..338a64470c 100644 --- a/pages/api/daily-event-references/[id]/index.ts +++ b/pages/api/daily-event-references/[id]/index.ts @@ -1,29 +1,54 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; -import { DailyEventReference } from "@calcom/prisma/client"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import type { DailyEventReferenceResponse } from "@lib/types"; +import { schemaDailyEventReferencePublic } from "@lib/validations/daily-event-reference"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -type ResponseData = { - data?: DailyEventReference; - message?: string; - error?: unknown; -}; +/** + * @swagger + * /api/daily-event-references/{id}: + * get: + * summary: Get a dailyEventReference by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the dailyEventReference to get + * tags: + * - dailyEventReferences + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: DailyEventReference was not found + */ +export async function dailyEventReferenceById( + req: NextApiRequest, + res: NextApiResponse +) { + const safe = await schemaQueryIdParseInt.safeParse(req.query); + if (!safe.success) throw new Error("Invalid request query"); -export async function dailyEventReference(req: NextApiRequest, res: NextApiResponse) { - const { query, method } = req; - const safe = await schemaQueryIdParseInt.safeParse(query); - if (method === "GET" && safe.success) { - const data = await prisma.dailyEventReference.findUnique({ where: { id: safe.data.id } }); + const dailyEventReference = await prisma.dailyEventReference.findUnique({ where: { id: safe.data.id } }); + const data = schemaDailyEventReferencePublic.parse(dailyEventReference); - if (data) res.status(200).json({ data }); - else res.status(404).json({ message: "Event type not found" }); - // Reject any other HTTP method than POST - } else res.status(405).json({ message: "Only GET Method allowed" }); + if (dailyEventReference) res.status(200).json({ data }); + else + (error: Error) => + res.status(404).json({ + message: "DailyEventReference was not found", + error, + }); } -export default withValidQueryIdTransformParseInt(dailyEventReference); +export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(dailyEventReferenceById)); diff --git a/pages/api/daily-event-references/index.ts b/pages/api/daily-event-references/index.ts index a5509f8a90..774d39358c 100644 --- a/pages/api/daily-event-references/index.ts +++ b/pages/api/daily-event-references/index.ts @@ -1,15 +1,42 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; -import { DailyEventReference } from "@calcom/prisma/client"; -type ResponseData = { - data?: DailyEventReference[]; - error?: unknown; -}; +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import { DailyEventReferencesResponse } from "@lib/types"; +import { schemaDailyEventReferencePublic } from "@lib/validations/daily-event-reference"; + +/** + * @swagger + * /api/daily-event-references: + * get: + * summary: Get all dailyEventReferences + * tags: + * - dailyEventReferences + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: No dailyEventReferences were found + */ +async function allDailyEventReferences( + _: NextApiRequest, + res: NextApiResponse +) { + const dailyEventReferences = await prisma.dailyEventReference.findMany(); + const data = dailyEventReferences.map((dailyEventReference) => + schemaDailyEventReferencePublic.parse(dailyEventReference) + ); -export default async function dailyEventReference(req: NextApiRequest, res: NextApiResponse) { - const data = await prisma.dailyEventReference.findMany(); if (data) res.status(200).json({ data }); - else res.status(400).json({ error: "No data found" }); + else + (error: Error) => + res.status(404).json({ + message: "No DailyEventReferences were found", + error, + }); } + +export default withMiddleware("HTTP_GET")(allDailyEventReferences); diff --git a/pages/api/daily-event-references/new.ts b/pages/api/daily-event-references/new.ts index 5bbc67fb2c..baf5e83d10 100644 --- a/pages/api/daily-event-references/new.ts +++ b/pages/api/daily-event-references/new.ts @@ -1,31 +1,55 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; -import { DailyEventReference } from "@calcom/prisma/client"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import type { DailyEventReferenceResponse } from "@lib/types"; import { - schemaDailyEventReference, + schemaDailyEventReferenceBodyParams, + schemaDailyEventReferencePublic, withValidDailyEventReference, } from "@lib/validations/daily-event-reference"; -type ResponseData = { - data?: DailyEventReference; - message?: string; - error?: string; -}; +/** + * @swagger + * /api/daily-event-references/new: + * post: + * summary: Creates a new dailyEventReference + * requestBody: + * description: Optional description in *Markdown* + * required: true + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/DailyEventReference' + * tags: + * - dailyEventReferences + * responses: + * 201: + * description: OK, dailyEventReference created + * model: DailyEventReference + * 400: + * description: Bad request. DailyEventReference body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +async function createDailyEventReference( + req: NextApiRequest, + res: NextApiResponse +) { + const safe = schemaDailyEventReferenceBodyParams.safeParse(req.body); + if (!safe.success) throw new Error("Invalid request body", safe.error); -async function createDailyEventReference(req: NextApiRequest, res: NextApiResponse) { - const { body, method } = req; - const safe = schemaDailyEventReference.safeParse(body); - if (method === "POST" && safe.success) { - await prisma.dailyEventReference - .create({ data: safe.data }) - .then((data) => res.status(201).json({ data })) - .catch((error) => - res.status(400).json({ message: "Could not create dailyEventReference type", error: error }) - ); - // Reject any other HTTP method than POST - } else res.status(405).json({ error: "Only POST Method allowed" }); + const dailyEventReference = await prisma.dailyEventReference.create({ data: safe.data }); + const data = schemaDailyEventReferencePublic.parse(dailyEventReference); + + if (data) res.status(201).json({ data, message: "DailyEventReference created successfully" }); + else + (error: Error) => + res.status(400).json({ + message: "Could not create new dailyEventReference", + error, + }); } -export default withValidDailyEventReference(createDailyEventReference); +export default withMiddleware("HTTP_POST")(withValidDailyEventReference(createDailyEventReference)); diff --git a/pages/api/destination-calendars/[id]/delete.ts b/pages/api/destination-calendars/[id]/delete.ts index b734fbdf9b..2573532efd 100644 --- a/pages/api/destination-calendars/[id]/delete.ts +++ b/pages/api/destination-calendars/[id]/delete.ts @@ -2,28 +2,50 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import type { BaseResponse } from "@lib/types"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -type ResponseData = { - message?: string; - error?: unknown; -}; +/** + * @swagger + * /api/destination-calendars/{id}/delete: + * delete: + * summary: Remove an existing destinationCalendar + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the destinationCalendar to delete + * tags: + * - destinationCalendars + * responses: + * 201: + * description: OK, destinationCalendar removed successfuly + * model: DestinationCalendar + * 400: + * description: Bad request. DestinationCalendar id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function deleteDestinationCalendar(req: NextApiRequest, res: NextApiResponse) { + const safe = await schemaQueryIdParseInt.safeParse(req.query); + if (!safe.success) throw new Error("Invalid request query", safe.error); -export async function deleteDestinationCalendar(req: NextApiRequest, res: NextApiResponse) { - const { query, method } = req; - const safe = await schemaQueryIdParseInt.safeParse(query); - if (method === "DELETE" && safe.success && safe.data) { - const destinationCalendar = await prisma.destinationCalendar.delete({ where: { id: safe.data.id } }); - // We only remove the destinationCalendar type from the database if there's an existing resource. - if (destinationCalendar) - res.status(200).json({ message: `destinationCalendar with id: ${safe.data.id} deleted successfully` }); - // This catches the error thrown by prisma.destinationCalendar.delete() if the resource is not found. - else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found` }); - // Reject any other HTTP method than POST - } else res.status(405).json({ message: "Only DELETE Method allowed" }); + const data = await prisma.destinationCalendar.delete({ where: { id: safe.data.id } }); + + if (data) + res.status(200).json({ message: `DestinationCalendar with id: ${safe.data.id} deleted successfully` }); + else + (error: Error) => + res.status(400).json({ + message: `DestinationCalendar with id: ${safe.data.id} was not able to be processed`, + error, + }); } -export default withValidQueryIdTransformParseInt(deleteDestinationCalendar); +export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteDestinationCalendar)); diff --git a/pages/api/destination-calendars/[id]/edit.ts b/pages/api/destination-calendars/[id]/edit.ts index 09c042b7e1..9dc4ae38fb 100644 --- a/pages/api/destination-calendars/[id]/edit.ts +++ b/pages/api/destination-calendars/[id]/edit.ts @@ -1,10 +1,12 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; -import { DestinationCalendar } from "@calcom/prisma/client"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import type { DestinationCalendarResponse } from "@lib/types"; import { - schemaDestinationCalendar, + schemaDestinationCalendarBodyParams, + schemaDestinationCalendarPublic, withValidDestinationCalendar, } from "@lib/validations/destination-calendar"; import { @@ -12,30 +14,52 @@ import { withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -type ResponseData = { - data?: DestinationCalendar; - message?: string; - error?: unknown; -}; +/** + * @swagger + * /api/destination-calendars/{id}/edit: + * patch: + * summary: Edit an existing destinationCalendar + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the destinationCalendar to edit + * tags: + * - destinationCalendars + * responses: + * 201: + * description: OK, destinationCalendar edited successfuly + * model: DestinationCalendar + * 400: + * description: Bad request. DestinationCalendar body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function editDestinationCalendar( + req: NextApiRequest, + res: NextApiResponse +) { + const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); + const safeBody = await schemaDestinationCalendarBodyParams.safeParse(req.body); -export async function editDestinationCalendar(req: NextApiRequest, res: NextApiResponse) { - const { query, body, method } = req; - const safeQuery = await schemaQueryIdParseInt.safeParse(query); - const safeBody = await schemaDestinationCalendar.safeParse(body); + if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); + const destinationCalendar = await prisma.destinationCalendar.update({ + where: { id: safeQuery.data.id }, + data: safeBody.data, + }); + const data = schemaDestinationCalendarPublic.parse(destinationCalendar); - if (method === "PATCH" && safeQuery.success && safeBody.success) { - const data = await prisma.destinationCalendar.update({ - where: { id: safeQuery.data.id }, - data: safeBody.data, - }); - if (data) res.status(200).json({ data }); - else - res - .status(404) - .json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error }); - - // Reject any other HTTP method than POST - } else res.status(405).json({ message: "Only PATCH Method allowed for updating destinationCalendars" }); + 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, + }); } -export default withValidQueryIdTransformParseInt(withValidDestinationCalendar(editDestinationCalendar)); +export default withMiddleware("HTTP_PATCH")( + withValidQueryIdTransformParseInt(withValidDestinationCalendar(editDestinationCalendar)) +); diff --git a/pages/api/destination-calendars/[id]/index.ts b/pages/api/destination-calendars/[id]/index.ts index fde198f77f..2639e70538 100644 --- a/pages/api/destination-calendars/[id]/index.ts +++ b/pages/api/destination-calendars/[id]/index.ts @@ -1,29 +1,54 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; -import { DestinationCalendar } from "@calcom/prisma/client"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import type { DestinationCalendarResponse } from "@lib/types"; +import { schemaDestinationCalendarPublic } from "@lib/validations/destination-calendar"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -type ResponseData = { - data?: DestinationCalendar; - message?: string; - error?: unknown; -}; +/** + * @swagger + * /api/destination-calendars/{id}: + * get: + * summary: Get a destinationCalendar by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the destinationCalendar to get + * tags: + * - destinationCalendars + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: DestinationCalendar was not found + */ +export async function destinationCalendarById( + req: NextApiRequest, + res: NextApiResponse +) { + const safe = await schemaQueryIdParseInt.safeParse(req.query); + if (!safe.success) throw new Error("Invalid request query"); -export async function destinationCalendar(req: NextApiRequest, res: NextApiResponse) { - const { query, method } = req; - const safe = await schemaQueryIdParseInt.safeParse(query); - if (method === "GET" && safe.success) { - const data = await prisma.destinationCalendar.findUnique({ where: { id: safe.data.id } }); + const destinationCalendar = await prisma.destinationCalendar.findUnique({ where: { id: safe.data.id } }); + const data = schemaDestinationCalendarPublic.parse(destinationCalendar); - if (data) res.status(200).json({ data }); - else res.status(404).json({ message: "Event type not found" }); - // Reject any other HTTP method than POST - } else res.status(405).json({ message: "Only GET Method allowed" }); + if (destinationCalendar) res.status(200).json({ data }); + else + (error: Error) => + res.status(404).json({ + message: "DestinationCalendar was not found", + error, + }); } -export default withValidQueryIdTransformParseInt(destinationCalendar); +export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(destinationCalendarById)); diff --git a/pages/api/destination-calendars/index.ts b/pages/api/destination-calendars/index.ts index 933ef20568..6f1a9147dd 100644 --- a/pages/api/destination-calendars/index.ts +++ b/pages/api/destination-calendars/index.ts @@ -1,15 +1,42 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; -import { DestinationCalendar } from "@calcom/prisma/client"; -type ResponseData = { - data?: DestinationCalendar[]; - error?: unknown; -}; +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import { DestinationCalendarsResponse } from "@lib/types"; +import { schemaDestinationCalendarPublic } from "@lib/validations/destination-calendar"; + +/** + * @swagger + * /api/destination-calendars: + * get: + * summary: Get all destinationCalendars + * tags: + * - destinationCalendars + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: No destinationCalendars were found + */ +async function allDestinationCalendars( + _: NextApiRequest, + res: NextApiResponse +) { + const destinationCalendars = await prisma.destinationCalendar.findMany(); + const data = destinationCalendars.map((destinationCalendar) => + schemaDestinationCalendarPublic.parse(destinationCalendar) + ); -export default async function destinationCalendar(req: NextApiRequest, res: NextApiResponse) { - const data = await prisma.destinationCalendar.findMany(); if (data) res.status(200).json({ data }); - else res.status(400).json({ error: "No data found" }); + else + (error: Error) => + res.status(404).json({ + message: "No DestinationCalendars were found", + error, + }); } + +export default withMiddleware("HTTP_GET")(allDestinationCalendars); diff --git a/pages/api/destination-calendars/new.ts b/pages/api/destination-calendars/new.ts index 483ff56de1..896be9f06a 100644 --- a/pages/api/destination-calendars/new.ts +++ b/pages/api/destination-calendars/new.ts @@ -1,31 +1,55 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; -import { DestinationCalendar } from "@calcom/prisma/client"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import type { DestinationCalendarResponse } from "@lib/types"; import { - schemaDestinationCalendar, + schemaDestinationCalendarBodyParams, + schemaDestinationCalendarPublic, withValidDestinationCalendar, } from "@lib/validations/destination-calendar"; -type ResponseData = { - data?: DestinationCalendar; - message?: string; - error?: string; -}; +/** + * @swagger + * /api/destination-calendars/new: + * post: + * summary: Creates a new destinationCalendar + * requestBody: + * description: Optional description in *Markdown* + * required: true + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/DestinationCalendar' + * tags: + * - destinationCalendars + * responses: + * 201: + * description: OK, destinationCalendar created + * model: DestinationCalendar + * 400: + * description: Bad request. DestinationCalendar body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +async function createDestinationCalendar( + req: NextApiRequest, + res: NextApiResponse +) { + const safe = schemaDestinationCalendarBodyParams.safeParse(req.body); + if (!safe.success) throw new Error("Invalid request body", safe.error); -async function createDestinationCalendar(req: NextApiRequest, res: NextApiResponse) { - const { body, method } = req; - const safe = schemaDestinationCalendar.safeParse(body); - if (method === "POST" && safe.success) { - await prisma.destinationCalendar - .create({ data: safe.data }) - .then((data) => res.status(201).json({ data })) - .catch((error) => - res.status(400).json({ message: "Could not create destinationCalendar type", error: error }) - ); - // Reject any other HTTP method than POST - } else res.status(405).json({ error: "Only POST Method allowed" }); + const destinationCalendar = await prisma.destinationCalendar.create({ data: safe.data }); + const data = schemaDestinationCalendarPublic.parse(destinationCalendar); + + if (data) res.status(201).json({ data, message: "DestinationCalendar created successfully" }); + else + (error: Error) => + res.status(400).json({ + message: "Could not create new destinationCalendar", + error, + }); } -export default withValidDestinationCalendar(createDestinationCalendar); +export default withMiddleware("HTTP_POST")(withValidDestinationCalendar(createDestinationCalendar)); diff --git a/pages/api/event-type-custom-inputs/[id]/delete.ts b/pages/api/event-type-custom-inputs/[id]/delete.ts index acccbac7d7..94c6bb2610 100644 --- a/pages/api/event-type-custom-inputs/[id]/delete.ts +++ b/pages/api/event-type-custom-inputs/[id]/delete.ts @@ -2,28 +2,50 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import type { BaseResponse } from "@lib/types"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -type ResponseData = { - message?: string; - error?: unknown; -}; +/** + * @swagger + * /api/event-type-custom-inputs/{id}/delete: + * delete: + * summary: Remove an existing eventTypeCustomInput + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the eventTypeCustomInput to delete + * tags: + * - eventTypeCustomInputs + * responses: + * 201: + * description: OK, eventTypeCustomInput removed successfuly + * model: EventTypeCustomInput + * 400: + * description: Bad request. EventTypeCustomInput id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function deleteEventTypeCustomInput(req: NextApiRequest, res: NextApiResponse) { + const safe = await schemaQueryIdParseInt.safeParse(req.query); + if (!safe.success) throw new Error("Invalid request query", safe.error); -export async function deleteEventTypeCustomInput(req: NextApiRequest, res: NextApiResponse) { - const { query, method } = req; - const safe = await schemaQueryIdParseInt.safeParse(query); - if (method === "DELETE" && safe.success && safe.data) { - const eventTypeCustomInput = await prisma.eventTypeCustomInput.delete({ where: { id: safe.data.id } }); - // We only remove the eventTypeCustomInput type from the database if there's an existing resource. - if (eventTypeCustomInput) - res.status(200).json({ message: `eventTypeCustomInput with id: ${safe.data.id} deleted successfully` }); - // This catches the error thrown by prisma.eventTypeCustomInput.delete() if the resource is not found. - else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found` }); - // Reject any other HTTP method than POST - } else res.status(405).json({ message: "Only DELETE Method allowed" }); + const data = await prisma.eventTypeCustomInput.delete({ where: { id: safe.data.id } }); + + if (data) + res.status(200).json({ message: `EventTypeCustomInput with id: ${safe.data.id} deleted successfully` }); + else + (error: Error) => + res.status(400).json({ + message: `EventTypeCustomInput with id: ${safe.data.id} was not able to be processed`, + error, + }); } -export default withValidQueryIdTransformParseInt(deleteEventTypeCustomInput); +export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteEventTypeCustomInput)); diff --git a/pages/api/event-type-custom-inputs/[id]/edit.ts b/pages/api/event-type-custom-inputs/[id]/edit.ts deleted file mode 100644 index 1c632034c2..0000000000 --- a/pages/api/event-type-custom-inputs/[id]/edit.ts +++ /dev/null @@ -1,41 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; -import { EventTypeCustomInput } from "@calcom/prisma/client"; - -import { - schemaEventTypeCustomInput, - withValidEventTypeCustomInput, -} from "@lib/validations/eventTypeCustomInput"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -type ResponseData = { - data?: EventTypeCustomInput; - message?: string; - error?: unknown; -}; - -export async function editEventTypeCustomInput(req: NextApiRequest, res: NextApiResponse) { - const { query, body, method } = req; - const safeQuery = await schemaQueryIdParseInt.safeParse(query); - const safeBody = await schemaEventTypeCustomInput.safeParse(body); - - if (method === "PATCH" && safeQuery.success && safeBody.success) { - const data = await prisma.eventTypeCustomInput.update({ - where: { id: safeQuery.data.id }, - data: safeBody.data, - }); - if (data) res.status(200).json({ data }); - else - res - .status(404) - .json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error }); - - // Reject any other HTTP method than POST - } else res.status(405).json({ message: "Only PATCH Method allowed for updating eventTypeCustomInputs" }); -} - -export default withValidQueryIdTransformParseInt(withValidEventTypeCustomInput(editEventTypeCustomInput)); diff --git a/pages/api/event-type-custom-inputs/[id]/edit.ts.fixme b/pages/api/event-type-custom-inputs/[id]/edit.ts.fixme new file mode 100644 index 0000000000..c95d4aa876 --- /dev/null +++ b/pages/api/event-type-custom-inputs/[id]/edit.ts.fixme @@ -0,0 +1,65 @@ +import type { NextApiRequest, NextApiResponse } from "next"; + +import prisma from "@calcom/prisma"; + +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import type { EventTypeCustomInputResponse } from "@lib/types"; +import { + schemaEventTypeCustomInputBodyParams, + schemaEventTypeCustomInputPublic, + withValidEventTypeCustomInput, +} from "@lib/validations/event-type-custom-input"; +import { + schemaQueryIdParseInt, + withValidQueryIdTransformParseInt, +} from "@lib/validations/shared/queryIdTransformParseInt"; + +/** + * @swagger + * /api/event-type-custom-inputs/{id}/edit: + * patch: + * summary: Edit an existing eventTypeCustomInput + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the eventTypeCustomInput to edit + * tags: + * - eventTypeCustomInputs + * responses: + * 201: + * description: OK, eventTypeCustomInput edited successfuly + * model: EventTypeCustomInput + * 400: + * description: Bad request. EventTypeCustomInput body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function editEventTypeCustomInput( + req: NextApiRequest, + res: NextApiResponse +) { + const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); + const safeBody = await schemaEventTypeCustomInputBodyParams.safeParse(req.body); + + if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); + const eventTypeCustomInput = await prisma.eventTypeCustomInput.update({ + where: { id: safeQuery.data.id }, + data: safeBody.data, + }); + const data = schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput); + + 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, + }); +} + +export default withMiddleware("HTTP_PATCH")( + withValidQueryIdTransformParseInt(withValidEventTypeCustomInput(editEventTypeCustomInput)) +); diff --git a/pages/api/event-type-custom-inputs/[id]/index.ts b/pages/api/event-type-custom-inputs/[id]/index.ts index 47170af37d..5ceea367b4 100644 --- a/pages/api/event-type-custom-inputs/[id]/index.ts +++ b/pages/api/event-type-custom-inputs/[id]/index.ts @@ -1,29 +1,54 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; -import { EventTypeCustomInput } from "@calcom/prisma/client"; +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import type { EventTypeCustomInputResponse } from "@lib/types"; +import { schemaEventTypeCustomInputPublic } from "@lib/validations/event-type-custom-input"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -type ResponseData = { - data?: EventTypeCustomInput; - message?: string; - error?: unknown; -}; +/** + * @swagger + * /api/event-type-custom-inputs/{id}: + * get: + * summary: Get a eventTypeCustomInput by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the eventTypeCustomInput to get + * tags: + * - eventTypeCustomInputs + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: EventTypeCustomInput was not found + */ +export async function eventTypeCustomInputById( + req: NextApiRequest, + res: NextApiResponse +) { + const safe = await schemaQueryIdParseInt.safeParse(req.query); + if (!safe.success) throw new Error("Invalid request query"); -export async function eventTypeCustomInput(req: NextApiRequest, res: NextApiResponse) { - const { query, method } = req; - const safe = await schemaQueryIdParseInt.safeParse(query); - if (method === "GET" && safe.success) { - const data = await prisma.eventTypeCustomInput.findUnique({ where: { id: safe.data.id } }); + const eventTypeCustomInput = await prisma.eventTypeCustomInput.findUnique({ where: { id: safe.data.id } }); + const data = schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput); - if (data) res.status(200).json({ data }); - else res.status(404).json({ message: "Event type not found" }); - // Reject any other HTTP method than POST - } else res.status(405).json({ message: "Only GET Method allowed" }); + if (eventTypeCustomInput) res.status(200).json({ data }); + else + (error: Error) => + res.status(404).json({ + message: "EventTypeCustomInput was not found", + error, + }); } -export default withValidQueryIdTransformParseInt(eventTypeCustomInput); +export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(eventTypeCustomInputById)); diff --git a/pages/api/event-type-custom-inputs/index.ts b/pages/api/event-type-custom-inputs/index.ts index b39cd14bca..689d23c7ae 100644 --- a/pages/api/event-type-custom-inputs/index.ts +++ b/pages/api/event-type-custom-inputs/index.ts @@ -1,15 +1,42 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; -import { EventTypeCustomInput } from "@calcom/prisma/client"; -type ResponseData = { - data?: EventTypeCustomInput[]; - error?: unknown; -}; +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import { EventTypeCustomInputsResponse } from "@lib/types"; +import { schemaEventTypeCustomInputPublic } from "@lib/validations/event-type-custom-input"; + +/** + * @swagger + * /api/event-type-custom-inputs: + * get: + * summary: Get all eventTypeCustomInputs + * tags: + * - eventTypeCustomInputs + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: No eventTypeCustomInputs were found + */ +async function allEventTypeCustomInputs( + _: NextApiRequest, + res: NextApiResponse +) { + const eventTypeCustomInputs = await prisma.eventTypeCustomInput.findMany(); + const data = eventTypeCustomInputs.map((eventTypeCustomInput) => + schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput) + ); -export default async function eventTypeCustomInput(req: NextApiRequest, res: NextApiResponse) { - const data = await prisma.eventTypeCustomInput.findMany(); if (data) res.status(200).json({ data }); - else res.status(400).json({ error: "No data found" }); + else + (error: Error) => + res.status(404).json({ + message: "No EventTypeCustomInputs were found", + error, + }); } + +export default withMiddleware("HTTP_GET")(allEventTypeCustomInputs); diff --git a/pages/api/event-type-custom-inputs/new.ts b/pages/api/event-type-custom-inputs/new.ts deleted file mode 100644 index e2a4078a59..0000000000 --- a/pages/api/event-type-custom-inputs/new.ts +++ /dev/null @@ -1,31 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; -import { EventTypeCustomInput } from "@calcom/prisma/client"; - -import { - schemaEventTypeCustomInput, - withValidEventTypeCustomInput, -} from "@lib/validations/eventTypeCustomInput"; - -type ResponseData = { - data?: EventTypeCustomInput; - message?: string; - error?: string; -}; - -async function createEventTypeCustomInput(req: NextApiRequest, res: NextApiResponse) { - const { body, method } = req; - const safe = schemaEventTypeCustomInput.safeParse(body); - if (method === "POST" && safe.success) { - await prisma.eventTypeCustomInput - .create({ data: safe.data }) - .then((data) => res.status(201).json({ data })) - .catch((error) => - res.status(400).json({ message: "Could not create eventTypeCustomInput type", error: error }) - ); - // Reject any other HTTP method than POST - } else res.status(405).json({ error: "Only POST Method allowed" }); -} - -export default withValidEventTypeCustomInput(createEventTypeCustomInput); diff --git a/pages/api/event-type-custom-inputs/new.ts.fixme b/pages/api/event-type-custom-inputs/new.ts.fixme new file mode 100644 index 0000000000..165ed6cd20 --- /dev/null +++ b/pages/api/event-type-custom-inputs/new.ts.fixme @@ -0,0 +1,55 @@ +import type { NextApiRequest, NextApiResponse } from "next"; + +import prisma from "@calcom/prisma"; + +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import type { EventTypeCustomInputResponse } from "@lib/types"; +import { + schemaEventTypeCustomInputBodyParams, + schemaEventTypeCustomInputPublic, + withValidEventTypeCustomInput, +} from "@lib/validations/event-type-custom-input"; + +/** + * @swagger + * /api/event-type-custom-inputs/new: + * post: + * summary: Creates a new eventTypeCustomInput + * requestBody: + * description: Optional description in *Markdown* + * required: true + * content: + * application/json: + * schema: + * $ref: '#/components/schemas/EventTypeCustomInput' + * tags: + * - eventTypeCustomInputs + * responses: + * 201: + * description: OK, eventTypeCustomInput created + * model: EventTypeCustomInput + * 400: + * description: Bad request. EventTypeCustomInput body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +async function createEventTypeCustomInput( + req: NextApiRequest, + res: NextApiResponse +) { + const safe = schemaEventTypeCustomInputBodyParams.safeParse(req.body); + if (!safe.success) throw new Error("Invalid request body", safe.error); + + const eventTypeCustomInput = await prisma.eventTypeCustomInput.create({ data: safe.data }); + const data = schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput); + + if (data) res.status(201).json({ data, message: "EventTypeCustomInput created successfully" }); + else + (error: Error) => + res.status(400).json({ + message: "Could not create new eventTypeCustomInput", + error, + }); +} + +export default withMiddleware("HTTP_POST")(withValidEventTypeCustomInput(createEventTypeCustomInput)); diff --git a/pages/api/selected-calendars/[id]/delete.ts b/pages/api/selected-calendars/[id]/delete.ts index d4f0fac746..76b949972a 100644 --- a/pages/api/selected-calendars/[id]/delete.ts +++ b/pages/api/selected-calendars/[id]/delete.ts @@ -4,16 +4,27 @@ import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; import type { BaseResponse } from "@lib/types"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; +import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString"; /** * @swagger - * /api/selectedCalendars/:id/delete: + * /api/selected-calendars/{userId}_{teamId}/delete: * delete: * summary: Remove an existing selectedCalendar + * 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 * responses: * 201: * description: OK, selectedCalendar removed successfuly @@ -24,10 +35,18 @@ import { * description: Authorization information is missing or invalid. */ export async function deleteSelectedCalendar(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); + const safe = await schemaQueryIdAsString.safeParse(req.query); if (!safe.success) throw new Error("Invalid request query", safe.error); - - const data = await prisma.selectedCalendar.delete({ where: { id: safe.data.id } }); + 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` }); @@ -39,4 +58,4 @@ export async function deleteSelectedCalendar(req: NextApiRequest, res: NextApiRe }); } -export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteSelectedCalendar)); +export default withMiddleware("HTTP_DELETE")(withValidQueryIdString(deleteSelectedCalendar)); diff --git a/pages/api/selected-calendars/[id]/edit.ts b/pages/api/selected-calendars/[id]/edit.ts index 950c4c04eb..717afb9c1f 100644 --- a/pages/api/selected-calendars/[id]/edit.ts +++ b/pages/api/selected-calendars/[id]/edit.ts @@ -9,16 +9,15 @@ import { schemaSelectedCalendarPublic, withValidSelectedCalendar, } from "@lib/validations/selected-calendar"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; +import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString"; /** * @swagger - * /api/selectedCalendars/:id/edit: + * /api/selected-calendars/{id}/edit: * patch: * summary: Edits an existing selectedCalendar + * tags: + * - selectedCalendars * responses: * 201: * description: OK, selectedCalendar edited successfuly @@ -32,12 +31,19 @@ export async function editSelectedCalendar( req: NextApiRequest, res: NextApiResponse ) { - const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); + const safeQuery = await schemaQueryIdAsString.safeParse(req.query); const safeBody = await schemaSelectedCalendarBodyParams.safeParse(req.body); - if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); + const [userId, integration, externalId] = safeQuery.data.id.split("_"); + const selectedCalendar = await prisma.selectedCalendar.update({ - where: { id: safeQuery.data.id }, + where: { + userId_integration_externalId: { + userId: parseInt(userId), + integration: integration, + externalId: externalId, + }, + }, data: safeBody.data, }); const data = schemaSelectedCalendarPublic.parse(selectedCalendar); @@ -52,5 +58,5 @@ export async function editSelectedCalendar( } export default withMiddleware("HTTP_PATCH")( - withValidQueryIdTransformParseInt(withValidSelectedCalendar(editSelectedCalendar)) + withValidQueryIdString(withValidSelectedCalendar(editSelectedCalendar)) ); diff --git a/pages/api/selected-calendars/[id]/index.ts b/pages/api/selected-calendars/[id]/index.ts index 3867c3834a..d88e0116ff 100644 --- a/pages/api/selected-calendars/[id]/index.ts +++ b/pages/api/selected-calendars/[id]/index.ts @@ -5,25 +5,28 @@ import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; import type { SelectedCalendarResponse } from "@lib/types"; import { schemaSelectedCalendarPublic } from "@lib/validations/selected-calendar"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; +import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString"; /** * @swagger - * /api/selectedCalendars/{id}: + * /api/selected-calendars/{userId}_{teamId}: * get: - * summary: find selectedCalendar by ID + * summary: find selectedCalendar by userID and teamID * parameters: * - in: path - * name: id + * name: userId * schema: * type: integer * required: true - * description: Numeric ID of the user to delete + * 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 * tags: - * - selected-calendars + * - selectedCalendars * responses: * 200: * description: OK @@ -36,10 +39,19 @@ export async function selectedCalendarById( req: NextApiRequest, res: NextApiResponse ) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); + const safe = await schemaQueryIdAsString.safeParse(req.query); if (!safe.success) throw new Error("Invalid request query"); + const [userId, integration, externalId] = safe.data.id.split("_"); - const selectedCalendar = await prisma.selectedCalendar.findUnique({ where: { id: safe.data.id } }); + const selectedCalendar = await prisma.selectedCalendar.findUnique({ + where: { + userId_integration_externalId: { + userId: parseInt(userId), + integration: integration, + externalId: externalId, + }, + }, + }); const data = schemaSelectedCalendarPublic.parse(selectedCalendar); if (selectedCalendar) res.status(200).json({ data }); @@ -51,4 +63,4 @@ export async function selectedCalendarById( }); } -export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(selectedCalendarById)); +export default withMiddleware("HTTP_GET")(withValidQueryIdString(selectedCalendarById));