From 9bb0f82075998477a8872b4ad021d208ef4e8574 Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Fri, 29 Apr 2022 17:29:57 +0200 Subject: [PATCH] fix: improve comments, no anys --- pages/api/attendees/[id].ts | 2 +- pages/api/attendees/index.ts | 2 +- pages/api/availabilities/[id].ts | 2 +- pages/api/booking-references/[id].ts | 4 +- pages/api/booking-references/index.ts | 10 +- pages/api/bookings/[id].ts | 11 +- pages/api/bookings/index.ts | 67 ++++---- pages/api/custom-inputs/[id].ts | 9 +- pages/api/custom-inputs/index.ts | 65 ++++---- pages/api/destination-calendars/[id].ts | 167 +++++++++++-------- pages/api/destination-calendars/index.ts | 63 ++++---- pages/api/event-references/[id].ts | 8 +- pages/api/event-references/index.ts | 62 +++---- pages/api/event-types/[id].ts | 159 +++++++++--------- pages/api/event-types/index.ts | 71 ++++---- pages/api/memberships/[id].ts | 169 ++++++++++--------- pages/api/memberships/index.ts | 62 +++---- pages/api/payments/[id].ts | 10 +- pages/api/schedules/[id].ts | 124 +++++++------- pages/api/schedules/index.ts | 58 +++---- pages/api/selected-calendars/[id].ts | 198 ++++++++++++----------- pages/api/selected-calendars/index.ts | 72 +++++---- pages/api/teams/[id].ts | 2 +- pages/api/users/[id].ts | 2 +- templates/endpoints/[id]/index.ts | 2 +- 25 files changed, 742 insertions(+), 659 deletions(-) diff --git a/pages/api/attendees/[id].ts b/pages/api/attendees/[id].ts index b0bf73796f..4e044790a4 100644 --- a/pages/api/attendees/[id].ts +++ b/pages/api/attendees/[id].ts @@ -41,7 +41,7 @@ export async function attendeeById( * @swagger * /attendees/{id}: * get: - * summary: Find an attendee by ID + * summary: Find an attendee * parameters: * - in: path * name: id diff --git a/pages/api/attendees/index.ts b/pages/api/attendees/index.ts index 4f57b8e9b7..18bbce51d7 100644 --- a/pages/api/attendees/index.ts +++ b/pages/api/attendees/index.ts @@ -95,7 +95,7 @@ async function createOrlistAllAttendees( if (!userWithBookings) { throw new Error("User not found"); } - const userBookingIds = userWithBookings.bookings.map((booking: any) => booking.id).flat(); + const userBookingIds = userWithBookings.bookings.map((booking: { id: number }) => booking.id).flat(); // Here we make sure to only return attendee's of the user's own bookings. if (!userBookingIds.includes(safePost.data.bookingId)) res.status(401).json({ message: "Unauthorized" }); else { diff --git a/pages/api/availabilities/[id].ts b/pages/api/availabilities/[id].ts index de131eb0ff..139e36e22f 100644 --- a/pages/api/availabilities/[id].ts +++ b/pages/api/availabilities/[id].ts @@ -28,7 +28,7 @@ export async function availabilityById( * @swagger * /availabilities/{id}: * get: - * summary: Find an availability by ID + * summary: Find an availability * parameters: * - in: path * name: id diff --git a/pages/api/booking-references/[id].ts b/pages/api/booking-references/[id].ts index bbc2ea6fa2..cb0358d901 100644 --- a/pages/api/booking-references/[id].ts +++ b/pages/api/booking-references/[id].ts @@ -25,7 +25,7 @@ export async function bookingReferenceById( include: { bookings: true }, }); if (!userWithBookings) throw new Error("User not found"); - const userBookingIds = userWithBookings.bookings.map((booking: any) => booking.id).flat(); + const userBookingIds = userWithBookings.bookings.map((booking: { id: number }) => booking.id).flat(); const bookingReference = await prisma.bookingReference.findUnique({ where: { id: safeQuery.data.id } }); if (!bookingReference) throw new Error("BookingReference not found"); if (userBookingIds.includes(bookingReference.bookingId)) { @@ -34,7 +34,7 @@ export async function bookingReferenceById( * @swagger * /booking-references/{id}: * get: - * summary: Find a booking reference by ID + * summary: Find a booking reference * parameters: * - in: path * name: id diff --git a/pages/api/booking-references/index.ts b/pages/api/booking-references/index.ts index a4ced04215..87cbe25cdc 100644 --- a/pages/api/booking-references/index.ts +++ b/pages/api/booking-references/index.ts @@ -10,7 +10,7 @@ import { } from "@lib/validations/booking-reference"; async function createOrlistAllBookingReferences( - { method, userId }: NextApiRequest, + { method, userId, body }: NextApiRequest, res: NextApiResponse ) { const userWithBookings = await prisma.user.findUnique({ @@ -18,7 +18,7 @@ async function createOrlistAllBookingReferences( include: { bookings: true }, }); if (!userWithBookings) throw new Error("User not found"); - const userBookingIds = userWithBookings.bookings.map((booking: any) => booking.id).flat(); + const userBookingIds = userWithBookings.bookings.map((booking: { id: number }) => booking.id).flat(); if (method === "GET") { /** * @swagger @@ -62,13 +62,11 @@ async function createOrlistAllBookingReferences( * 401: * description: Authorization information is missing or invalid. */ - const safe = schemaBookingCreateBodyParams.safeParse(req.body); + const safe = schemaBookingCreateBodyParams.safeParse(body); if (!safe.success) { throw new Error("Invalid request body"); } - // const booking_reference = schemaBookingReferencePublic.parse(data); - const userId = req.userId; const userWithBookings = await prisma.user.findUnique({ where: { id: userId }, include: { bookings: true }, @@ -76,7 +74,7 @@ async function createOrlistAllBookingReferences( if (!userWithBookings) { throw new Error("User not found"); } - const userBookingIds = userWithBookings.bookings.map((booking: any) => booking.id).flat(); + const userBookingIds = userWithBookings.bookings.map((booking: { id: number }) => booking.id).flat(); if (!userBookingIds.includes(safe.data.bookingId)) res.status(401).json({ message: "Unauthorized" }); else { const booking_reference = await prisma.bookingReference.create({ diff --git a/pages/api/bookings/[id].ts b/pages/api/bookings/[id].ts index 0974bd55b1..21fdd9a937 100644 --- a/pages/api/bookings/[id].ts +++ b/pages/api/bookings/[id].ts @@ -10,17 +10,18 @@ import { withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -export async function bookingById(req: NextApiRequest, res: NextApiResponse) { - const { method, query, body } = req; +export async function bookingById( + { method, query, body, userId }: NextApiRequest, + res: NextApiResponse +) { const safeQuery = schemaQueryIdParseInt.safeParse(query); if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); - const userId = req.userId; const userWithBookings = await prisma.user.findUnique({ where: { id: userId }, include: { bookings: true }, }); if (!userWithBookings) throw new Error("User not found"); - const userBookingIds = userWithBookings.bookings.map((booking: any) => booking.id).flat(); + const userBookingIds = userWithBookings.bookings.map((booking: { id: number }) => booking.id).flat(); if (!userBookingIds.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" }); else { switch (method) { @@ -28,7 +29,7 @@ export async function bookingById(req: NextApiRequest, res: NextApiResponse ) { - const { method } = req; - const userId = req.userId; - if (method === "GET") { - -/** - * @swagger - * /bookings: - * get: - * summary: Find all bookings - * tags: - * - bookings - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: No bookings were found - */ + /** + * @swagger + * /bookings: + * get: + * summary: Find all bookings + * tags: + * - bookings + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: No bookings were found + */ const data = await prisma.booking.findMany({ where: { userId } }); const bookings = data.map((booking) => schemaBookingReadPublic.parse(booking)); if (bookings) res.status(200).json({ bookings }); @@ -40,22 +36,21 @@ async function createOrlistAllBookings( error, }); } else if (method === "POST") { - -/** - * @swagger - * /bookings: - * post: - * summary: Creates a new booking - * tags: - * - bookings - * responses: - * 201: - * description: OK, booking created - * 400: - * description: Bad request. Booking body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ + /** + * @swagger + * /bookings: + * post: + * summary: Creates a new booking + * tags: + * - bookings + * responses: + * 201: + * description: OK, booking created + * 400: + * description: Bad request. Booking body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ const safe = schemaBookingCreateBodyParams.safeParse(req.body); if (!safe.success) throw new Error("Invalid request body"); diff --git a/pages/api/custom-inputs/[id].ts b/pages/api/custom-inputs/[id].ts index 2860d9a525..3339f31027 100644 --- a/pages/api/custom-inputs/[id].ts +++ b/pages/api/custom-inputs/[id].ts @@ -17,7 +17,7 @@ import { * @swagger * /custom-inputs/{id}: * get: - * summary: Find a eventTypeCustomInput by ID + * summary: Find a eventTypeCustomInput * parameters: * - in: path * name: id @@ -71,12 +71,13 @@ import { * 401: * description: Authorization information is missing or invalid. */ -async function eventTypeById(req: NextApiRequest, res: NextApiResponse) { - const { method, query, body } = req; +async function eventTypeById( + { method, query, body, userId }: NextApiRequest, + res: NextApiResponse +) { const safeQuery = schemaQueryIdParseInt.safeParse(query); const safeBody = schemaEventTypeCustomInputBodyParams.safeParse(body); if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); - const userId = req.userId; const data = await prisma.eventType.findMany({ where: { userId } }); const userEventTypes = data.map((eventType) => eventType.id); const userEventTypeCustomInputs = await prisma.eventTypeCustomInput.findMany({ diff --git a/pages/api/custom-inputs/index.ts b/pages/api/custom-inputs/index.ts index ee1232d83c..c7d5f9df90 100644 --- a/pages/api/custom-inputs/index.ts +++ b/pages/api/custom-inputs/index.ts @@ -9,42 +9,28 @@ import { schemaEventTypeCustomInputPublic, } from "@lib/validations/event-type-custom-input"; -/** - * @swagger - * /custom-inputs: - * get: - * summary: Find all eventTypeCustomInputs - * tags: - * - custom-inputs - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: No eventTypeCustomInputs were found - * post: - * summary: Creates a new eventTypeCustomInput - * tags: - * - custom-inputs - * responses: - * 201: - * description: OK, eventTypeCustomInput created - * 400: - * description: Bad request. EventTypeCustomInput body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ async function createOrlistAllEventTypeCustomInputs( - req: NextApiRequest, + { userId, method, body }: NextApiRequest, res: NextApiResponse ) { - const { method } = req; - const userId = req.userId; const data = await prisma.eventType.findMany({ where: { userId } }); - const userEventTypes = data.map((eventType) => eventType.id); - + const userEventTypes: number[] = data.map((eventType) => eventType.id); if (method === "GET") { + /** + * @swagger + * /custom-inputs: + * get: + * summary: Find all eventTypeCustomInputs + * tags: + * - custom-inputs + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: No eventTypeCustomInputs were found + */ const data = await prisma.eventTypeCustomInput.findMany({ where: { eventType: userEventTypes } }); const event_type_custom_inputs = data.map((eventTypeCustomInput) => schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput) @@ -57,7 +43,22 @@ async function createOrlistAllEventTypeCustomInputs( error, }); } else if (method === "POST") { - const safe = schemaEventTypeCustomInputBodyParams.safeParse(req.body); + /** + * @swagger + * /custom-inputs: + * post: + * summary: Creates a new eventTypeCustomInput + * tags: + * - custom-inputs + * responses: + * 201: + * description: OK, eventTypeCustomInput created + * 400: + * description: Bad request. EventTypeCustomInput body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ + const safe = schemaEventTypeCustomInputBodyParams.safeParse(body); if (!safe.success) throw new Error("Invalid request body"); // Since we're supporting a create or connect relation on eventType, we need to treat them differently // When using connect on event type, check if userId is the owner of the event diff --git a/pages/api/destination-calendars/[id].ts b/pages/api/destination-calendars/[id].ts index 679dcd5d29..f5d187eb25 100644 --- a/pages/api/destination-calendars/[id].ts +++ b/pages/api/destination-calendars/[id].ts @@ -13,74 +13,13 @@ import { withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -/** - * @swagger - * /destination-calendars/{id}: - * get: - * summary: Find a destination calendar by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the destination calendar to get - * tags: - * - destination-calendars - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: DestinationCalendar was not found - * patch: - * summary: Edit an existing destination calendar - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the destination calendar to edit - * tags: - * - destination-calendars - * responses: - * 201: - * description: OK, destinationCalendar edited successfuly - * 400: - * description: Bad request. DestinationCalendar body is invalid. - * 401: - * description: Authorization information is missing or invalid. - * delete: - * summary: Remove an existing destination calendar - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the destination calendar to delete - - * tags: - * - destination-calendars - * responses: - * 201: - * description: OK, destinationCalendar removed successfuly - * 400: - * description: Bad request. DestinationCalendar id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ export async function destionationCalendarById( - req: NextApiRequest, + { method, query, body, userId }: NextApiRequest, res: NextApiResponse ) { - const { method, query, body } = req; const safeQuery = schemaQueryIdParseInt.safeParse(query); const safeBody = schemaDestinationCalendarEditBodyParams.safeParse(body); if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); - const userId = req.userId; const data = await prisma.destinationCalendar.findMany({ where: { userId } }); const userDestinationCalendars = data.map((destinationCalendar) => destinationCalendar.id); // FIXME: Should we also check ownership of bokingId and eventTypeId to avoid users cross-pollinating other users calendars. @@ -88,6 +27,64 @@ export async function destionationCalendarById( if (userDestinationCalendars.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" }); else { switch (method) { + /** + * @swagger + * /destination-calendars/{id}: + * get: + * summary: Find a destination calendar + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the destination calendar to get + * tags: + * - destination-calendars + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: DestinationCalendar was not found + * patch: + * summary: Edit an existing destination calendar + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the destination calendar to edit + * tags: + * - destination-calendars + * responses: + * 201: + * description: OK, destinationCalendar edited successfuly + * 400: + * description: Bad request. DestinationCalendar body is invalid. + * 401: + * description: Authorization information is missing or invalid. + * delete: + * summary: Remove an existing destination calendar + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the destination calendar to delete + * tags: + * - destination-calendars + * responses: + * 201: + * description: OK, destinationCalendar removed successfuly + * 400: + * description: Bad request. DestinationCalendar id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ case "GET": await prisma.destinationCalendar .findUnique({ where: { id: safeQuery.data.id } }) @@ -100,7 +97,28 @@ export async function destionationCalendarById( }) ); break; - + /** + * @swagger + * /destination-calendars/{id}: + * patch: + * summary: Edit an existing destination calendar + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the destination calendar to edit + * tags: + * - destination-calendars + * responses: + * 201: + * description: OK, destinationCalendar edited successfuly + * 400: + * description: Bad request. DestinationCalendar body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ case "PATCH": if (!safeBody.success) { throw new Error("Invalid request body"); @@ -116,7 +134,28 @@ export async function destionationCalendarById( }) ); break; - + /** + * @swagger + * /destination-calendars/{id}: + * delete: + * summary: Remove an existing destination calendar + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the destination calendar to delete + * tags: + * - destination-calendars + * responses: + * 201: + * description: OK, destinationCalendar removed successfuly + * 400: + * description: Bad request. DestinationCalendar id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ case "DELETE": await prisma.destinationCalendar .delete({ diff --git a/pages/api/destination-calendars/index.ts b/pages/api/destination-calendars/index.ts index 81a3d36c97..4006a67c72 100644 --- a/pages/api/destination-calendars/index.ts +++ b/pages/api/destination-calendars/index.ts @@ -9,40 +9,26 @@ import { schemaDestinationCalendarReadPublic, } from "@lib/validations/destination-calendar"; -/** - * @swagger - * /destination-calendars: - * get: - * summary: Find all destination calendars - * tags: - * - destination-calendars - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: No destination calendars were found - * post: - * summary: Creates a new destination calendar - * tags: - * - destination-calendars - * responses: - * 201: - * description: OK, destination calendar created - * 400: - * description: Bad request. DestinationCalendar body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ async function createOrlistAllDestinationCalendars( - req: NextApiRequest, + { method, body, userId }: NextApiRequest, res: NextApiResponse ) { - const { method } = req; - const userId = req.userId; - if (method === "GET") { + /** + * @swagger + * /destination-calendars: + * get: + * summary: Find all destination calendars + * tags: + * - destination-calendars + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: No destination calendars were found + */ const data = await prisma.destinationCalendar.findMany({ where: { userId } }); const destination_calendars = data.map((destinationCalendar) => schemaDestinationCalendarReadPublic.parse(destinationCalendar) @@ -55,7 +41,22 @@ async function createOrlistAllDestinationCalendars( error, }); } else if (method === "POST") { - const safe = schemaDestinationCalendarCreateBodyParams.safeParse(req.body); + /** + * @swagger + * /destination-calendars: + * post: + * summary: Creates a new destination calendar + * tags: + * - destination-calendars + * responses: + * 201: + * description: OK, destination calendar created + * 400: + * description: Bad request. DestinationCalendar body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ + const safe = schemaDestinationCalendarCreateBodyParams.safeParse(body); if (!safe.success) throw new Error("Invalid request body"); const data = await prisma.destinationCalendar.create({ data: { ...safe.data, userId } }); diff --git a/pages/api/event-references/[id].ts b/pages/api/event-references/[id].ts index d0a31fcf11..a98d5f74e9 100644 --- a/pages/api/event-references/[id].ts +++ b/pages/api/event-references/[id].ts @@ -14,16 +14,14 @@ import { } from "@lib/validations/shared/queryIdTransformParseInt"; export async function dailyEventReferenceById( - req: NextApiRequest, + { method, query, body, userId }: NextApiRequest, res: NextApiResponse ) { - const { method, query, body } = req; const safeQuery = schemaQueryIdParseInt.safeParse(query); const safeBody = schemaDailyEventReferenceEditBodyParams.safeParse(body); if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); - const userId = req.userId; const userBookings = await prisma.booking.findMany({ where: { userId } }); - const userBookingIds = userBookings.map((booking) => booking.id); + const userBookingIds: number[] = userBookings.map((booking) => booking.id); const userBookingDailyEventReferences = await prisma.dailyEventReference.findMany({ where: { bookingId: { in: userBookingIds } }, }); @@ -38,7 +36,7 @@ export async function dailyEventReferenceById( * @swagger * /event-references/{id}: * get: - * summary: Find a event reference by ID + * summary: Find a event reference * parameters: * - in: path * name: id diff --git a/pages/api/event-references/index.ts b/pages/api/event-references/index.ts index 1168ccbda6..bc19171de8 100644 --- a/pages/api/event-references/index.ts +++ b/pages/api/event-references/index.ts @@ -9,42 +9,29 @@ import { schemaDailyEventReferenceReadPublic, } from "@lib/validations/daily-event-reference"; -/** - * @swagger - * /event-references: - * get: - * summary: Find all daily event reference - * tags: - * - event-references - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: No daily event references were found - * post: - * summary: Creates a new daily event reference - * tags: - * - event-references - * responses: - * 201: - * description: OK, daily event reference created - * 400: - * description: Bad request. DailyEventReference body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ async function createOrlistAllDailyEventReferences( - req: NextApiRequest, + { method, body, userId }: NextApiRequest, res: NextApiResponse ) { - const { method } = req; - const userId = req.userId; const userBookings = await prisma.booking.findMany({ where: { userId } }); const userBookingIds = userBookings.map((booking) => booking.id); if (method === "GET") { + /** + * @swagger + * /event-references: + * get: + * summary: Find all daily event reference + * tags: + * - event-references + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: No daily event references were found + */ const data = await prisma.dailyEventReference.findMany({ where: { bookingId: { in: userBookingIds } }, }); @@ -59,7 +46,22 @@ async function createOrlistAllDailyEventReferences( error, }); } else if (method === "POST") { - const safe = schemaDailyEventReferenceCreateBodyParams.safeParse(req.body); + /** + * @swagger + * /event-references: + * post: + * summary: Creates a new daily event reference + * tags: + * - event-references + * responses: + * 201: + * description: OK, daily event reference created + * 400: + * description: Bad request. DailyEventReference body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ + const safe = schemaDailyEventReferenceCreateBodyParams.safeParse(body); if (!safe.success) throw new Error("Invalid request body"); const data = await prisma.dailyEventReference.create({ data: safe.data }); diff --git a/pages/api/event-types/[id].ts b/pages/api/event-types/[id].ts index ebb440bd37..0ece116d17 100644 --- a/pages/api/event-types/[id].ts +++ b/pages/api/event-types/[id].ts @@ -10,87 +10,43 @@ import { withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -/** - * @swagger - * /event-types/{id}: - * get: - * summary: Find a eventType by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the eventType to get - * security: - * - ApiKeyAuth: [] - * tags: - * - event-types - * externalDocs: - * url: https://docs.cal.com/event-types - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: EventType was not found - * patch: - * summary: Edit an existing eventType - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the eventType to edit - * security: - * - ApiKeyAuth: [] - * tags: - * - event-types - * externalDocs: - * url: https://docs.cal.com/event-types - * responses: - * 201: - * description: OK, eventType edited successfuly - * 400: - * description: Bad request. EventType body is invalid. - * 401: - * description: Authorization information is missing or invalid. - * delete: - * summary: Remove an existing eventType - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the eventType to delete - * security: - * - ApiKeyAuth: [] - * tags: - * - event-types - * externalDocs: - * url: https://docs.cal.com/event-types - * responses: - * 201: - * description: OK, eventType removed successfuly - * 400: - * description: Bad request. EventType id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function eventTypeById(req: NextApiRequest, res: NextApiResponse) { - const { method, query, body } = req; +export async function eventTypeById( + { method, query, body, userId }: NextApiRequest, + res: NextApiResponse +) { const safeQuery = schemaQueryIdParseInt.safeParse(query); - const safeBody = schemaEventTypeBodyParams.safeParse(body); if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); - const userId = req.userId; const data = await prisma.eventType.findMany({ where: { userId } }); const userEventTypes = data.map((eventType) => eventType.id); if (!userEventTypes.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" }); else { switch (method) { + /** + * @swagger + * /event-types/{id}: + * get: + * summary: Find a eventType + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the eventType to get + * security: + * - ApiKeyAuth: [] + * tags: + * - event-types + * externalDocs: + * url: https://docs.cal.com/event-types + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: EventType was not found + */ case "GET": await prisma.eventType .findUnique({ where: { id: safeQuery.data.id } }) @@ -103,8 +59,34 @@ export async function eventTypeById(req: NextApiRequest, res: NextApiResponse ) { - const { method } = req; - const userId = req.userId; - if (method === "GET") { + /** + * @swagger + * /event-types: + * get: + * summary: Find all event types + * tags: + * - event-types + * externalDocs: + * url: https://docs.cal.com/event-types + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: No event types were found + */ const data = await prisma.eventType.findMany({ where: { userId } }); const event_types = data.map((eventType) => schemaEventTypePublic.parse(eventType)); if (event_types) res.status(200).json({ event_types }); @@ -54,7 +38,24 @@ async function createOrlistAllEventTypes( error, }); } else if (method === "POST") { - const safe = schemaEventTypeBodyParams.safeParse(req.body); + /** + * @swagger + * /event-types: + * post: + * summary: Creates a new event type + * tags: + * - event-types + * externalDocs: + * url: https://docs.cal.com/event-types + * responses: + * 201: + * description: OK, event type created + * 400: + * description: Bad request. EventType body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ + const safe = schemaEventTypeBodyParams.safeParse(body); if (!safe.success) throw new Error("Invalid request body"); const data = await prisma.eventType.create({ data: { ...safe.data, userId } }); diff --git a/pages/api/memberships/[id].ts b/pages/api/memberships/[id].ts index fa4c298be4..0681a629a9 100644 --- a/pages/api/memberships/[id].ts +++ b/pages/api/memberships/[id].ts @@ -7,93 +7,45 @@ import type { MembershipResponse } from "@lib/types"; import { schemaMembershipBodyParams, schemaMembershipPublic } from "@lib/validations/membership"; import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString"; -/** - * @swagger - * /memberships/{userId}_{teamId}: - * get: - * summary: Find a membership by userID and teamID - * parameters: - * - in: path - * name: userId - * schema: - * type: integer - * required: true - * description: Numeric userId of the membership to get - * - in: path - * name: teamId - * schema: - * type: integer - * required: true - * description: Numeric teamId of the membership to get - * tags: - * - memberships - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: Membership was not found - * patch: - * summary: Edit an existing membership - * parameters: - * - in: path - * name: userId - * schema: - * type: integer - * required: true - * description: Numeric userId of the membership to get - * - in: path - * name: teamId - * schema: - * type: integer - * required: true - * description: Numeric teamId of the membership to get - * tags: - * - memberships - * responses: - * 201: - * description: OK, membership edited successfuly - * 400: - * description: Bad request. Membership body is invalid. - * 401: - * description: Authorization information is missing or invalid. - * delete: - * summary: Remove an existing membership - * parameters: - * - in: path - * name: userId - * schema: - * type: integer - * required: true - * description: Numeric userId of the membership to get - * - in: path - * name: teamId - * schema: - * type: integer - * required: true - * description: Numeric teamId of the membership to get - * tags: - * - memberships - * responses: - * 201: - * description: OK, membership removed successfuly - * 400: - * description: Bad request. Membership id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function membershipById(req: NextApiRequest, res: NextApiResponse) { - const { method, query, body } = req; +export async function membershipById( + { method, query, body, userId }: NextApiRequest, + res: NextApiResponse +) { const safeQuery = schemaQueryIdAsString.safeParse(query); - const safeBody = schemaMembershipBodyParams.safeParse(body); if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); // This is how we set the userId and teamId in the query for managing compoundId. const [paramUserId, teamId] = safeQuery.data.id.split("_"); - const userId = req.userId; if (parseInt(paramUserId) !== userId) res.status(401).json({ message: "Unauthorized" }); else { switch (method) { + /** + * @swagger + * /memberships/{userId}_{teamId}: + * get: + * summary: Find a membership by userID and teamID + * parameters: + * - in: path + * name: userId + * schema: + * type: integer + * required: true + * description: Numeric userId of the membership to get + * - in: path + * name: teamId + * schema: + * type: integer + * required: true + * description: Numeric teamId of the membership to get + * tags: + * - memberships + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: Membership was not found + */ case "GET": await prisma.membership .findUnique({ @@ -114,7 +66,36 @@ export async function membershipById(req: NextApiRequest, res: NextApiResponse ) { - const { method } = req; - const userId = req.userId; if (method === "GET") { + /** + * @swagger + * /memberships: + * get: + * summary: Find all memberships + * tags: + * - memberships + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: No memberships were found + */ const data = await prisma.membership.findMany({ where: { userId } }); const memberships = data.map((membership) => schemaMembershipPublic.parse(membership)); if (memberships) res.status(200).json({ memberships }); @@ -49,7 +36,22 @@ async function createOrlistAllMemberships( error, }); } else if (method === "POST") { - const safe = schemaMembershipBodyParams.safeParse(req.body); + /** + * @swagger + * /memberships: + * post: + * summary: Creates a new membership + * tags: + * - memberships + * responses: + * 201: + * description: OK, membership created + * 400: + * description: Bad request. Membership body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ + const safe = schemaMembershipBodyParams.safeParse(body); if (!safe.success) throw new Error("Invalid request body"); const data = await prisma.membership.create({ data: { ...safe.data, userId } }); diff --git a/pages/api/payments/[id].ts b/pages/api/payments/[id].ts index 90882e760c..ebcaa0c504 100644 --- a/pages/api/payments/[id].ts +++ b/pages/api/payments/[id].ts @@ -14,7 +14,7 @@ import { * @swagger * /payments/{id}: * get: - * summary: Find one of your own payments by ID + * summary: Find a payment * parameters: * - in: path * name: id @@ -32,11 +32,11 @@ import { * 404: * description: Payment was not found */ -export async function paymentById(req: NextApiRequest, res: NextApiResponse) { - const { method, query } = req; +export async function paymentById( + { method, query, userId }: NextApiRequest, + res: NextApiResponse +) { const safeQuery = schemaQueryIdParseInt.safeParse(query); - const userId = req.userId; - if (safeQuery.success && method === "GET") { const userWithBookings = await prisma.user.findUnique({ where: { id: userId }, diff --git a/pages/api/schedules/[id].ts b/pages/api/schedules/[id].ts index d6645597a6..af63d8af2b 100644 --- a/pages/api/schedules/[id].ts +++ b/pages/api/schedules/[id].ts @@ -10,64 +10,6 @@ import { withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -/** - * @swagger - * /schedules/{id}: - * get: - * summary: Find a schedule by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the schedule to get - * tags: - * - schedules - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: Schedule was not found - * patch: - * summary: Edit an existing schedule - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the schedule to edit - * tags: - * - schedules - * responses: - * 201: - * description: OK, schedule edited successfuly - * 400: - * description: Bad request. Schedule body is invalid. - * 401: - * description: Authorization information is missing or invalid. - * delete: - * summary: Remove an existing schedule - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the schedule to delete - * tags: - * - schedules - * responses: - * 201: - * description: OK, schedule removed successfuly - * 400: - * description: Bad request. Schedule id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ export async function scheduleById(req: NextApiRequest, res: NextApiResponse) { const { method, query, body } = req; const safeQuery = schemaQueryIdParseInt.safeParse(query); @@ -79,6 +21,28 @@ export async function scheduleById(req: NextApiRequest, res: NextApiResponse @@ -42,6 +14,21 @@ async function createOrlistAllSchedules( const userId = req.userId; if (method === "GET") { + /** + * @swagger + * /schedules: + * get: + * summary: Find all schedules + * tags: + * - schedules + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: No schedules were found + */ const data = await prisma.schedule.findMany({ where: { userId } }); const schedules = data.map((schedule) => schemaSchedulePublic.parse(schedule)); if (schedules) res.status(200).json({ schedules }); @@ -52,6 +39,21 @@ async function createOrlistAllSchedules( error, }); } else if (method === "POST") { + /** + * @swagger + * /schedules: + * post: + * summary: Creates a new schedule + * tags: + * - schedules + * responses: + * 201: + * description: OK, schedule created + * 400: + * description: Bad request. Schedule body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ const safe = schemaScheduleBodyParams.safeParse(req.body); if (!safe.success) throw new Error("Invalid request body"); const data = await prisma.schedule.create({ data: { ...safe.data, userId } }); diff --git a/pages/api/selected-calendars/[id].ts b/pages/api/selected-calendars/[id].ts index a8e1e7ab15..10fb737cf9 100644 --- a/pages/api/selected-calendars/[id].ts +++ b/pages/api/selected-calendars/[id].ts @@ -10,101 +10,6 @@ import { } from "@lib/validations/selected-calendar"; import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString"; -/** - * @swagger - * /selected-calendars/{userId}_{integration}_{externalId}: - * get: - * summary: Find a selected-calendar by userID and teamID - * parameters: - * - in: path - * name: userId - * schema: - * type: integer - * required: true - * description: userId of the selected calendar to get - * - in: path - * name: externalId - * schema: - * type: string - * required: true - * 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 - * tags: - * - selected-calendars - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: SelectedCalendar was not found - * patch: - * summary: Edit an existing selected calendar - * parameters: - * - in: path - * name: userId - * schema: - * type: integer - * required: true - * description: userId of the selected calendar to get - * - in: path - * name: externalId - * schema: - * type: string - * required: true - * 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 - * tags: - * - selected-calendars - * responses: - * 201: - * description: OK, selected-calendar edited successfuly - * 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 - * description: userId of the selected calendar to get - * - in: path - * name: externalId - * schema: - * type: integer - * required: true - * 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 - - * tags: - * - selected-calendars - * responses: - * 201: - * description: OK, selected-calendar removed successfuly - * 400: - * description: Bad request. SelectedCalendar id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ export async function selectedCalendarById( req: NextApiRequest, res: NextApiResponse @@ -119,6 +24,40 @@ export async function selectedCalendarById( if (userId !== parseInt(paramUserId)) res.status(401).json({ message: "Unauthorized" }); else { switch (method) { + /** + * @swagger + * /selected-calendars/{userId}_{integration}_{externalId}: + * get: + * summary: Find a selected calendar + * parameters: + * - in: path + * name: userId + * schema: + * type: integer + * required: true + * description: userId of the selected calendar to get + * - in: path + * name: externalId + * schema: + * type: string + * required: true + * 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 + * tags: + * - selected-calendars + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: SelectedCalendar was not found + */ case "GET": await prisma.selectedCalendar .findUnique({ @@ -140,6 +79,40 @@ export async function selectedCalendarById( ); break; + /** + * @swagger + * /selected-calendars/{userId}_{integration}_{externalId}: + * patch: + * summary: Edit a selected calendar + * parameters: + * - in: path + * name: userId + * schema: + * type: integer + * required: true + * description: userId of the selected calendar to get + * - in: path + * name: externalId + * schema: + * type: string + * required: true + * 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 + * tags: + * - selected-calendars + * responses: + * 201: + * description: OK, selected-calendar edited successfuly + * 400: + * description: Bad request. SelectedCalendar body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ case "PATCH": if (!safeBody.success) { throw new Error("Invalid request body"); @@ -164,7 +137,40 @@ export async function selectedCalendarById( }) ); break; - + /** + * @swagger + * /selected-calendars/{userId}_{integration}_{externalId}: + * delete: + * summary: Remove a selected calendar + * parameters: + * - in: path + * name: userId + * schema: + * type: integer + * required: true + * description: userId of the selected calendar to get + * - in: path + * name: externalId + * schema: + * type: integer + * required: true + * 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 + * tags: + * - selected-calendars + * responses: + * 201: + * description: OK, selected-calendar removed successfuly + * 400: + * description: Bad request. SelectedCalendar id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ case "DELETE": await prisma.selectedCalendar .delete({ diff --git a/pages/api/selected-calendars/index.ts b/pages/api/selected-calendars/index.ts index c6e1b122d9..758d663b8f 100644 --- a/pages/api/selected-calendars/index.ts +++ b/pages/api/selected-calendars/index.ts @@ -9,40 +9,26 @@ import { schemaSelectedCalendarPublic, } from "@lib/validations/selected-calendar"; -/** - * @swagger - * /selected-calendars: - * get: - * summary: Find all selected calendars - * tags: - * - selected-calendars - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: No selected calendars were found - * post: - * summary: Creates a new selected calendar - * tags: - * - selected-calendars - * responses: - * 201: - * description: OK, selected calendar created - * 400: - * description: Bad request. SelectedCalendar body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ async function createOrlistAllSelectedCalendars( - req: NextApiRequest, + { method, userId }: NextApiRequest, res: NextApiResponse ) { - const { method } = req; - const userId = req.userId; - if (method === "GET") { + /** + * @swagger + * /selected-calendars: + * get: + * summary: Find all selected calendars + * tags: + * - selected-calendars + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: No selected calendars were found + */ const data = await prisma.selectedCalendar.findMany({ where: { userId } }); const selected_calendars = data.map((selected_calendar) => schemaSelectedCalendarPublic.parse(selected_calendar) @@ -55,6 +41,32 @@ async function createOrlistAllSelectedCalendars( error, }); } else if (method === "POST") { + /** + * @swagger + * /selected-calendars: + * get: + * summary: Find all selected calendars + * tags: + * - selected-calendars + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: No selected calendars were found + * post: + * summary: Creates a new selected calendar + * tags: + * - selected-calendars + * responses: + * 201: + * description: OK, selected calendar created + * 400: + * description: Bad request. SelectedCalendar body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ const safe = schemaSelectedCalendarBodyParams.safeParse(req.body); if (!safe.success) throw new Error("Invalid request body"); // Create new selectedCalendar connecting it to current userId diff --git a/pages/api/teams/[id].ts b/pages/api/teams/[id].ts index 0368a69f5c..d3e971d487 100644 --- a/pages/api/teams/[id].ts +++ b/pages/api/teams/[id].ts @@ -14,7 +14,7 @@ import { schemaTeamBodyParams, schemaTeamPublic } from "@lib/validations/team"; * @swagger * /teams/{id}: * get: - * summary: Find a team by ID + * summary: Find a team * parameters: * - in: path * name: id diff --git a/pages/api/users/[id].ts b/pages/api/users/[id].ts index 4cea26e8e3..58d0b41d36 100644 --- a/pages/api/users/[id].ts +++ b/pages/api/users/[id].ts @@ -14,7 +14,7 @@ import { schemaUserEditBodyParams, schemaUserReadPublic } from "@lib/validations * @swagger * /users/{id}: * get: - * summary: Find a user by ID, returns your user if regular user. + * summary: Find a user, returns your user if regular user. * parameters: * - in: path * name: id diff --git a/templates/endpoints/[id]/index.ts b/templates/endpoints/[id]/index.ts index c3297459ac..c9b1d85152 100644 --- a/templates/endpoints/[id]/index.ts +++ b/templates/endpoints/[id]/index.ts @@ -14,7 +14,7 @@ import { * @swagger * /v1/resources/{id}: * get: - * summary: Find a resource by ID + * summary: Find a resource * parameters: * - in: path * name: id