diff --git a/README.md b/README.md index 17082202cd..a9904ac3ff 100644 --- a/README.md +++ b/README.md @@ -127,9 +127,9 @@ We aim to provide a fully tested API for our peace of mind, this is accomplished | attendees | ✅ | ✅ | ✅ | ✅ | ✅ | | availabilities | ✅ | ✅ | ✅ | ✅ | ✅ | | booking-references | ✅ | ✅ | ✅ | ✅ | ✅ | -| daily-event-references | ✅ | ✅ | ✅ | ✅ | ✅ | +| event-references | ✅ | ✅ | ✅ | ✅ | ✅ | | destination-calendars | ✅ | ✅ | ✅ | ✅ | ✅ | -| event-type-custom-inputs | ✅ | ✅ | ✅ | ✅ | ✅ | +| custom-inputs | ✅ | ✅ | ✅ | ✅ | ✅ | | event-types | ✅ | ✅ | ✅ | ✅ | ✅ | | memberships | ✅ | ✅ | ✅ | ✅ | ✅ | | payments | ✅ | ✅ | ❌ | ❌ | ❌ | diff --git a/pages/api/attendees/[id].ts b/pages/api/attendees/[id].ts index 01ae4f5449..b0bf73796f 100644 --- a/pages/api/attendees/[id].ts +++ b/pages/api/attendees/[id].ts @@ -19,14 +19,22 @@ export async function attendeeById( res.status(400).json({ error: safeQuery.error }); throw new Error("Invalid request query", safeQuery.error); } - const userBookings = await prisma.booking.findMany({ - where: { userId }, - include: { attendees: true }, - }); - const attendees = userBookings.map((booking) => booking.attendees).flat(); - const attendeeIds = attendees.map((attendee) => attendee.id); - // Here we make sure to only return attendee's of the user's own bookings. - if (!attendeeIds.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" }); + const userBookingsAttendeeIds = await prisma.booking + // Find all user bookings, including attendees + .findMany({ + where: { userId }, + include: { attendees: true }, + }) + .then( + // Flatten and merge all the attendees in one array + (bookings) => + bookings + .map((bookings) => bookings.attendees) // Get the attendees IDs from user bookings + .flat() // Needed to flatten the array of arrays of all bookings attendees + .map((attendee) => attendee.id) // We only need the attendee IDs + ); + // @note: Here we make sure to only return attendee's of the user's own bookings. + if (!userBookingsAttendeeIds.includes(safeQuery.data.id)) res.status(401).json({ message: "Unauthorized" }); else { switch (method) { /** diff --git a/pages/api/bookings/[id].ts b/pages/api/bookings/[id].ts index ddfa7a34ce..0974bd55b1 100644 --- a/pages/api/bookings/[id].ts +++ b/pages/api/bookings/[id].ts @@ -10,64 +10,6 @@ import { withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -/** - * @swagger - * /bookings/{id}: - * get: - * summary: Find a booking by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the booking to get - * tags: - * - bookings - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: Booking was not found - * patch: - * summary: Edit an existing booking - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the booking to edit - * tags: - * - bookings - * responses: - * 201: - * description: OK, booking edited successfuly - * 400: - * description: Bad request. Booking body is invalid. - * 401: - * description: Authorization information is missing or invalid. - * delete: - * summary: Remove an existing booking - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the booking to delete - * tags: - * - bookings - * responses: - * 201: - * description: OK, booking removed successfuly - * 400: - * description: Bad request. Booking id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ export async function bookingById(req: NextApiRequest, res: NextApiResponse) { const { method, query, body } = req; const safeQuery = schemaQueryIdParseInt.safeParse(query); @@ -82,6 +24,28 @@ export async function bookingById(req: NextApiRequest, res: NextApiResponse +) { + const { method } = req; + const userId = req.userId; + + if (method === "GET") { + /** * @swagger * /bookings: @@ -20,6 +29,21 @@ import { schemaBookingCreateBodyParams, schemaBookingReadPublic } from "@lib/val * 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 }); + else + (error: Error) => + res.status(404).json({ + message: "No Bookings were found", + error, + }); + } else if (method === "POST") { + +/** + * @swagger + * /bookings: * post: * summary: Creates a new booking * tags: @@ -32,24 +56,6 @@ import { schemaBookingCreateBodyParams, schemaBookingReadPublic } from "@lib/val * 401: * description: Authorization information is missing or invalid. */ -async function createOrlistAllBookings( - req: NextApiRequest, - res: NextApiResponse -) { - const { method } = req; - const userId = req.userId; - - if (method === "GET") { - const data = await prisma.booking.findMany({ where: { userId } }); - const bookings = data.map((booking) => schemaBookingReadPublic.parse(booking)); - if (bookings) res.status(200).json({ bookings }); - else - (error: Error) => - res.status(404).json({ - message: "No Bookings were found", - error, - }); - } else if (method === "POST") { const safe = schemaBookingCreateBodyParams.safeParse(req.body); if (!safe.success) throw new Error("Invalid request body"); diff --git a/pages/api/event-type-custom-inputs/[id].ts b/pages/api/custom-inputs/[id].ts similarity index 97% rename from pages/api/event-type-custom-inputs/[id].ts rename to pages/api/custom-inputs/[id].ts index 0d84a2a6fb..2860d9a525 100644 --- a/pages/api/event-type-custom-inputs/[id].ts +++ b/pages/api/custom-inputs/[id].ts @@ -15,7 +15,7 @@ import { /** * @swagger - * /event-type-custom-inputs/{id}: + * /custom-inputs/{id}: * get: * summary: Find a eventTypeCustomInput by ID * parameters: @@ -26,7 +26,7 @@ import { * required: true * description: Numeric ID of the eventTypeCustomInput to get * tags: - * - event-type-custom-inputs + * - custom-inputs * responses: * 200: * description: OK @@ -44,7 +44,7 @@ import { * required: true * description: Numeric ID of the eventTypeCustomInput to edit * tags: - * - event-type-custom-inputs + * - custom-inputs * responses: * 201: * description: OK, eventTypeCustomInput edited successfuly @@ -62,7 +62,7 @@ import { * required: true * description: Numeric ID of the eventTypeCustomInput to delete * tags: - * - event-type-custom-inputs + * - custom-inputs * responses: * 201: * description: OK, eventTypeCustomInput removed successfuly diff --git a/pages/api/event-type-custom-inputs/index.ts b/pages/api/custom-inputs/index.ts similarity index 97% rename from pages/api/event-type-custom-inputs/index.ts rename to pages/api/custom-inputs/index.ts index ac6c0f94cb..ee1232d83c 100644 --- a/pages/api/event-type-custom-inputs/index.ts +++ b/pages/api/custom-inputs/index.ts @@ -11,11 +11,11 @@ import { /** * @swagger - * /event-type-custom-inputs: + * /custom-inputs: * get: * summary: Find all eventTypeCustomInputs * tags: - * - event-type-custom-inputs + * - custom-inputs * responses: * 200: * description: OK @@ -26,7 +26,7 @@ import { * post: * summary: Creates a new eventTypeCustomInput * tags: - * - event-type-custom-inputs + * - custom-inputs * responses: * 201: * description: OK, eventTypeCustomInput created diff --git a/pages/api/daily-event-references/[id].ts b/pages/api/event-references/[id].ts similarity index 61% rename from pages/api/daily-event-references/[id].ts rename to pages/api/event-references/[id].ts index 7f1b026e5c..d0a31fcf11 100644 --- a/pages/api/daily-event-references/[id].ts +++ b/pages/api/event-references/[id].ts @@ -13,64 +13,6 @@ import { withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -/** - * @swagger - * /daily-event-references/{id}: - * get: - * summary: Find a daily event reference by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the daily event reference to get - * tags: - * - daily-event-references - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: DailyEventReference was not found - * patch: - * summary: Edit an existing daily event reference - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the daily event reference to edit - * tags: - * - daily-event-references - * responses: - * 201: - * description: OK, dailyEventReference edited successfuly - * 400: - * description: Bad request. DailyEventReference body is invalid. - * 401: - * description: Authorization information is missing or invalid. - * delete: - * summary: Remove an existing daily event reference - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the daily event reference to delete - * tags: - * - daily-event-references - * responses: - * 201: - * description: OK, dailyEventReference removed successfuly - * 400: - * description: Bad request. DailyEventReference id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ export async function dailyEventReferenceById( req: NextApiRequest, res: NextApiResponse @@ -92,6 +34,28 @@ export async function dailyEventReferenceById( res.status(401).json({ message: "Unauthorized" }); else { switch (method) { + /** + * @swagger + * /event-references/{id}: + * get: + * summary: Find a event reference by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the event reference to get + * tags: + * - event-references + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: EventReference was not found + */ case "GET": await prisma.dailyEventReference .findUnique({ where: { id: safeQuery.data.id } }) @@ -105,6 +69,28 @@ export async function dailyEventReferenceById( ); break; + /** + * @swagger + * /event-references/{id}: + * patch: + * summary: Edit an existing event reference + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the event reference to edit + * tags: + * - event-references + * responses: + * 201: + * description: OK, EventReference edited successfuly + * 400: + * description: Bad request. EventReference body is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ case "PATCH": if (!safeBody.success) { throw new Error("Invalid request body"); @@ -121,6 +107,28 @@ export async function dailyEventReferenceById( ); break; + /** + * @swagger + * /event-references/{id}: + * delete: + * summary: Remove an existing event reference + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the event reference to delete + * tags: + * - event-references + * responses: + * 201: + * description: OK, EventReference removed successfuly + * 400: + * description: Bad request. EventReference id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ case "DELETE": await prisma.dailyEventReference .delete({ diff --git a/pages/api/daily-event-references/index.ts b/pages/api/event-references/index.ts similarity index 96% rename from pages/api/daily-event-references/index.ts rename to pages/api/event-references/index.ts index c129eef677..1168ccbda6 100644 --- a/pages/api/daily-event-references/index.ts +++ b/pages/api/event-references/index.ts @@ -11,11 +11,11 @@ import { /** * @swagger - * /daily-event-references: + * /event-references: * get: * summary: Find all daily event reference * tags: - * - daily-event-references + * - event-references * responses: * 200: * description: OK @@ -26,7 +26,7 @@ import { * post: * summary: Creates a new daily event reference * tags: - * - daily-event-references + * - event-references * responses: * 201: * description: OK, daily event reference created