From e13ea234b84578a716bab7702060028b2386bb9d Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Fri, 29 Apr 2022 02:30:59 +0200 Subject: [PATCH] fix: improve docs in attendees availabilities and booking references --- pages/api/attendees/[id].ts | 180 ++++++++++++++------------ pages/api/attendees/index.ts | 114 ++++++++-------- pages/api/availabilities/[id].ts | 146 +++++++++++---------- pages/api/availabilities/index.ts | 70 +++++----- pages/api/booking-references/[id].ts | 132 +++++++++---------- pages/api/booking-references/index.ts | 60 ++++----- 6 files changed, 360 insertions(+), 342 deletions(-) diff --git a/pages/api/attendees/[id].ts b/pages/api/attendees/[id].ts index 3d6d16c9a1..01ae4f5449 100644 --- a/pages/api/attendees/[id].ts +++ b/pages/api/attendees/[id].ts @@ -10,90 +10,10 @@ import { withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -/** - * @swagger - * /attendees/{id}: - * get: - * summary: Find an attendee by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the attendee to get - * example: 3 - * tags: - * - attendees - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: Attendee was not found - * patch: - * summary: Edit an existing attendee - * requestBody: - * description: Edit an existing attendee related to one of your bookings - * required: true - * content: - * application/json: - * schema: - * type: object - * required: - * - bookingId - * - name - * - email - * - timeZone - * properties: - * email: - * type: string - * example: email@example.com - * name: - * type: string - * example: John Doe - * timeZone: - * type: string - * example: Europe/London - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * example: 3 - * required: true - * description: Numeric ID of the attendee to edit - * tags: - * - attendees - * responses: - * 201: - * description: OK, attendee edited successfuly - * 400: - * description: Bad request. Attendee body is invalid. - * 401: - * description: Authorization information is missing or invalid. - * delete: - * summary: Remove an existing attendee - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the attendee to delete - * tags: - * - attendees - * responses: - * 201: - * description: OK, attendee removed successfuly - * 400: - * description: Bad request. Attendee id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function attendeeById(req: NextApiRequest, res: NextApiResponse) { - const { method, query, body, userId } = req; +export async function attendeeById( + { method, query, body, userId }: NextApiRequest, + res: NextApiResponse +) { const safeQuery = schemaQueryIdParseInt.safeParse(query); if (!safeQuery.success) { res.status(400).json({ error: safeQuery.error }); @@ -109,6 +29,29 @@ export async function attendeeById(req: NextApiRequest, res: NextApiResponse ) { - const { method, userId } = req; - // Here we make sure to only return attendee's of the user's own bookings. const userBookings = await prisma.booking.findMany({ where: { userId, @@ -73,6 +20,21 @@ async function createOrlistAllAttendees( }); const attendees = userBookings.map((booking) => booking.attendees).flat(); if (method === "GET") { + /** + * @swagger + * /attendees: + * get: + * summary: Find all attendees + * tags: + * - attendees + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: No attendees were found + */ if (attendees) res.status(200).json({ attendees }); else (error: Error) => @@ -81,13 +43,51 @@ async function createOrlistAllAttendees( error, }); } else if (method === "POST") { - const safePost = schemaAttendeeCreateBodyParams.safeParse(req.body); + /** + * @swagger + * /attendees: + * post: + * summary: Creates a new attendee + * requestBody: + * description: Create a new attendee related to one of your bookings + * required: true + * content: + * application/json: + * schema: + * type: object + * required: + * - bookingId + * - name + * - email + * - timeZone + * properties: + * bookingId: + * type: number + * example: 1 + * email: + * type: string + * example: email@example.com + * name: + * type: string + * example: John Doe + * timeZone: + * type: string + * example: Europe/London + * tags: + * - attendees + * responses: + * 201: + * description: OK, attendee created + * 400: + * description: Bad request. Attendee body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ + const safePost = schemaAttendeeCreateBodyParams.safeParse(body); if (!safePost.success) { - console.log(safePost.error); res.status(400).json({ error: safePost.error }); throw new Error("Invalid request body", safePost.error); } - const userId = req.userId; const userWithBookings = await prisma.user.findUnique({ where: { id: userId }, include: { bookings: true }, diff --git a/pages/api/availabilities/[id].ts b/pages/api/availabilities/[id].ts index 21348a990c..de131eb0ff 100644 --- a/pages/api/availabilities/[id].ts +++ b/pages/api/availabilities/[id].ts @@ -13,80 +13,41 @@ import { withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -/** - * @swagger - * /availabilities/{id}: - * get: - * summary: Find an availability by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the availability to get - * tags: - * - availabilities - * externalDocs: - * url: https://docs.cal.com/availability - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: Availability was not found - * patch: - * summary: Edit an existing availability - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the availability to edit - * tags: - * - availabilities - * externalDocs: - * url: https://docs.cal.com/availability - * responses: - * 201: - * description: OK, availability edited successfuly - * 400: - * description: Bad request. Availability body is invalid. - * 401: - * description: Authorization information is missing or invalid. - * delete: - * summary: Remove an existing availability - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the availability to delete - * tags: - * - availabilities - * externalDocs: - * url: https://docs.cal.com/availability - * responses: - * 201: - * description: OK, availability removed successfuly - * 400: - * description: Bad request. Availability id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function availabilityById(req: NextApiRequest, res: NextApiResponse) { - const { method, query, body } = req; +export async function availabilityById( + { 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 data = await prisma.availability.findMany({ where: { userId } }); const availabiltiesIds = data.map((availability) => availability.id); if (!availabiltiesIds.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" }); else { switch (method) { + /** + * @swagger + * /availabilities/{id}: + * get: + * summary: Find an availability by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the availability to get + * tags: + * - availabilities + * externalDocs: + * url: https://docs.cal.com/availability + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: Availability was not found + */ case "GET": await prisma.availability .findUnique({ where: { id: safeQuery.data.id } }) @@ -96,7 +57,30 @@ export async function availabilityById(req: NextApiRequest, res: NextApiResponse res.status(404).json({ message: `Availability with id: ${safeQuery.data.id} not found`, error }) ); break; - + /** + * @swagger + * /availabilities/{id}: + * patch: + * summary: Edit an existing availability + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the availability to edit + * tags: + * - availabilities + * externalDocs: + * url: https://docs.cal.com/availability + * responses: + * 201: + * description: OK, availability edited successfuly + * 400: + * description: Bad request. Availability body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ case "PATCH": const safeBody = schemaAvailabilityEditBodyParams.safeParse(body); if (!safeBody.success) throw new Error("Invalid request body"); @@ -115,14 +99,36 @@ export async function availabilityById(req: NextApiRequest, res: NextApiResponse .then((data) => schemaAvailabilityReadPublic.parse(data)) .then((availability) => res.status(200).json({ availability })) .catch((error: Error) => { - console.log(error); res.status(404).json({ message: `Availability with id: ${safeQuery.data.id} not found`, error, }); }); break; - + /** + * @swagger + * /availabilities/{id}: + * delete: + * summary: Remove an existing availability + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the availability to delete + * tags: + * - availabilities + * externalDocs: + * url: https://docs.cal.com/availability + * responses: + * 201: + * description: OK, availability removed successfuly + * 400: + * description: Bad request. Availability id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ case "DELETE": await prisma.availability .delete({ where: { id: safeQuery.data.id } }) diff --git a/pages/api/availabilities/index.ts b/pages/api/availabilities/index.ts index 7ef5e65b8b..25b0866071 100644 --- a/pages/api/availabilities/index.ts +++ b/pages/api/availabilities/index.ts @@ -9,44 +9,28 @@ import { schemaAvailabilityReadPublic, } from "@lib/validations/availability"; -/** - * @swagger - * /availabilities: - * get: - * summary: Find all availabilities - * tags: - * - availabilities - * externalDocs: - * url: https://docs.cal.com/availability - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: No availabilities were found - * post: - * summary: Creates a new availability - * tags: - * - availabilities - * externalDocs: - * url: https://docs.cal.com/availability - * responses: - * 201: - * description: OK, availability created - * 400: - * description: Bad request. Availability body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ async function createOrlistAllAvailabilities( - req: NextApiRequest, + { method, userId }: NextApiRequest, res: NextApiResponse ) { - const { method } = req; - const userId = req.userId; - if (method === "GET") { + /** + * @swagger + * /availabilities: + * get: + * summary: Find all availabilities + * tags: + * - availabilities + * externalDocs: + * url: https://docs.cal.com/availability + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: No availabilities were found + */ const data = await prisma.availability.findMany({ where: { userId } }); const availabilities = data.map((availability) => schemaAvailabilityReadPublic.parse(availability)); if (availabilities) res.status(200).json({ availabilities }); @@ -57,11 +41,27 @@ async function createOrlistAllAvailabilities( error, }); } else if (method === "POST") { + /** + * @swagger + * /availabilities: + * post: + * summary: Creates a new availability + * tags: + * - availabilities + * externalDocs: + * url: https://docs.cal.com/availability + * responses: + * 201: + * description: OK, availability created + * 400: + * description: Bad request. Availability body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ const safe = schemaAvailabilityCreateBodyParams.safeParse(req.body); if (!safe.success) throw new Error("Invalid request body"); const data = await prisma.availability.create({ data: { ...safe.data, userId } }); - console.log(data); const availability = schemaAvailabilityReadPublic.parse(data); if (availability) res.status(201).json({ availability, message: "Availability created successfully" }); diff --git a/pages/api/booking-references/[id].ts b/pages/api/booking-references/[id].ts index cba249df5a..bbc2ea6fa2 100644 --- a/pages/api/booking-references/[id].ts +++ b/pages/api/booking-references/[id].ts @@ -14,73 +14,12 @@ import { withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -/** - * @swagger - * /booking-references/{id}: - * get: - * summary: Find a booking reference by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the booking reference to get - * tags: - * - booking-references - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: BookingReference was not found - * patch: - * summary: Edit an existing booking reference - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the booking reference to edit - * tags: - * - booking-references - * responses: - * 201: - * description: OK, bookingReference edited successfuly - * 400: - * description: Bad request. BookingReference body is invalid. - * 401: - * description: Authorization information is missing or invalid. - * delete: - * summary: Remove an existing booking reference - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the booking reference to delete - - * tags: - * - booking-references - * responses: - * 201: - * description: OK, bookingReference removed successfuly - * 400: - * description: Bad request. BookingReference id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ export async function bookingReferenceById( - req: NextApiRequest, + { method, query, body, userId }: NextApiRequest, res: NextApiResponse ) { - const { method, query, body } = req; 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 }, @@ -91,6 +30,28 @@ export async function bookingReferenceById( if (!bookingReference) throw new Error("BookingReference not found"); if (userBookingIds.includes(bookingReference.bookingId)) { switch (method) { + /** + * @swagger + * /booking-references/{id}: + * get: + * summary: Find a booking reference by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the booking reference to get + * tags: + * - booking-references + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: BookingReference was not found + */ case "GET": await prisma.bookingReference .findUnique({ where: { id: safeQuery.data.id } }) @@ -103,10 +64,30 @@ export async function bookingReferenceById( }) ); break; - + /** + * @swagger + * /booking-references/{id}: + * patch: + * summary: Edit an existing booking reference + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the booking reference to edit + * tags: + * - booking-references + * responses: + * 201: + * description: OK, bookingReference edited successfuly + * 400: + * description: Bad request. BookingReference body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ case "PATCH": const safeBody = schemaBookingEditBodyParams.safeParse(body); - if (!safeBody.success) { throw new Error("Invalid request body"); } @@ -121,7 +102,28 @@ export async function bookingReferenceById( }) ); break; - + /** + * @swagger + * /booking-references/{id}: + * delete: + * summary: Remove an existing booking reference + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the booking reference to delete + * tags: + * - booking-references + * responses: + * 201: + * description: OK, bookingReference removed successfuly + * 400: + * description: Bad request. BookingReference id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ case "DELETE": await prisma.bookingReference .delete({ diff --git a/pages/api/booking-references/index.ts b/pages/api/booking-references/index.ts index 7bd47a56f5..a4ced04215 100644 --- a/pages/api/booking-references/index.ts +++ b/pages/api/booking-references/index.ts @@ -9,38 +9,10 @@ import { schemaBookingReferenceReadPublic, } from "@lib/validations/booking-reference"; -/** - * @swagger - * /booking-references: - * get: - * summary: Find all booking references - * tags: - * - booking-references - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: No booking references were found - * post: - * summary: Creates a new booking reference - * tags: - * - booking-references - * responses: - * 201: - * description: OK, booking reference created - * 400: - * description: Bad request. BookingReference body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ async function createOrlistAllBookingReferences( - req: NextApiRequest, + { method, userId }: NextApiRequest, res: NextApiResponse ) { - const { method } = req; - const userId = req.userId; const userWithBookings = await prisma.user.findUnique({ where: { id: userId }, include: { bookings: true }, @@ -48,6 +20,21 @@ async function createOrlistAllBookingReferences( if (!userWithBookings) throw new Error("User not found"); const userBookingIds = userWithBookings.bookings.map((booking: any) => booking.id).flat(); if (method === "GET") { + /** + * @swagger + * /booking-references: + * get: + * summary: Find all booking references + * tags: + * - booking-references + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: No booking references were found + */ const data = await prisma.bookingReference.findMany({ where: { id: { in: userBookingIds } } }); const booking_references = data.map((bookingReference) => schemaBookingReferenceReadPublic.parse(bookingReference) @@ -60,6 +47,21 @@ async function createOrlistAllBookingReferences( error, }); } else if (method === "POST") { + /** + * @swagger + * /booking-references: + * post: + * summary: Creates a new booking reference + * tags: + * - booking-references + * responses: + * 201: + * description: OK, booking reference created + * 400: + * description: Bad request. BookingReference 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");