From 963c8937272dc3ce50bded645f81a195cc6c61b4 Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Mon, 11 Apr 2022 12:03:15 +0200 Subject: [PATCH] feat: unify new/index of attendees, availabilities, booking references, bookings, selected calendars, update response types --- lib/types.ts | 76 ++++++++++++--------------- pages/api/attendees/[id].ts | 8 +-- pages/api/attendees/index.ts | 10 ++-- pages/api/availabilities/[id].ts | 8 +-- pages/api/availabilities/index.ts | 57 +++++++++++++++----- pages/api/availabilities/new.ts | 51 ------------------ pages/api/booking-references/[id].ts | 6 +-- pages/api/booking-references/index.ts | 68 ++++++++++++++++++------ pages/api/booking-references/new.ts | 51 ------------------ pages/api/bookings/[id].ts | 8 +-- pages/api/bookings/index.ts | 57 +++++++++++++++----- pages/api/bookings/new.ts | 48 ----------------- pages/api/payments/[id].ts | 2 +- pages/api/schedules/index.ts | 2 +- pages/api/selected-calendars/[id].ts | 4 +- pages/api/selected-calendars/index.ts | 17 +++--- pages/api/teams/[id].ts | 11 ++-- pages/api/teams/index.ts | 21 +++++--- pages/api/users/[id].ts | 8 +-- pages/api/users/index.ts | 6 +-- templates/endpoints/newIndex.ts | 2 +- 21 files changed, 237 insertions(+), 284 deletions(-) delete mode 100644 pages/api/availabilities/new.ts delete mode 100644 pages/api/booking-references/new.ts delete mode 100644 pages/api/bookings/new.ts diff --git a/lib/types.ts b/lib/types.ts index cffd7c9841..91af310a62 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -27,147 +27,139 @@ export type BaseResponse = { // User export type UserResponse = BaseResponse & { - data?: Partial; + user?: Partial; }; export type UsersResponse = BaseResponse & { - data?: Partial[]; -}; - -// API Key -export type ApiKeyResponse = BaseResponse & { - data?: Partial; -}; -export type ApiKeysResponse = BaseResponse & { - data?: Partial[]; + users?: Partial[]; }; // Team export type TeamResponse = BaseResponse & { - data?: Partial; + team?: Partial; }; export type TeamsResponse = BaseResponse & { - data?: Partial[]; + teams?: Partial[]; }; // SelectedCalendar export type SelectedCalendarResponse = BaseResponse & { - data?: Partial; + selected_calendar?: Partial; }; export type SelectedCalendarsResponse = BaseResponse & { - data?: Partial[]; + selected_calendars?: Partial[]; }; // Attendee export type AttendeeResponse = BaseResponse & { - data?: Partial; + attendee?: Partial; }; // Grouping attendees in booking arrays for now, // later might remove endpoint and move to booking endpoint altogether. export type AttendeesResponse = BaseResponse & { - data?: Partial[]; + attendees?: Partial[]; }; // Availability export type AvailabilityResponse = BaseResponse & { - data?: Partial; + availability?: Partial; }; export type AvailabilitiesResponse = BaseResponse & { - data?: Partial[]; + availabilities?: Partial[]; }; // BookingReference export type BookingReferenceResponse = BaseResponse & { - data?: Partial; + booking_reference?: Partial; }; export type BookingReferencesResponse = BaseResponse & { - data?: Partial[]; + booking_references?: Partial[]; }; // Booking export type BookingResponse = BaseResponse & { - data?: Partial; + booking?: Partial; }; export type BookingsResponse = BaseResponse & { - data?: Partial[]; + bookings?: Partial[]; }; // Credential export type CredentialResponse = BaseResponse & { - data?: Partial; + credential?: Partial; }; export type CredentialsResponse = BaseResponse & { - data?: Partial[]; + credentials?: Partial[]; }; // DailyEventReference export type DailyEventReferenceResponse = BaseResponse & { - data?: Partial; + daily_event_reference?: Partial; }; export type DailyEventReferencesResponse = BaseResponse & { - data?: Partial[]; + daily_event_references?: Partial[]; }; // DestinationCalendar export type DestinationCalendarResponse = BaseResponse & { - data?: Partial; + destination_calendar?: Partial; }; export type DestinationCalendarsResponse = BaseResponse & { - data?: Partial[]; + destination_calendars?: Partial[]; }; // Membership export type MembershipResponse = BaseResponse & { - data?: Partial; + membership?: Partial; }; export type MembershipsResponse = BaseResponse & { - data?: Partial[]; + memberships?: Partial[]; }; // EventTypeCustomInput export type EventTypeCustomInputResponse = BaseResponse & { - data?: Partial; + event_type_custom_input?: Partial; }; export type EventTypeCustomInputsResponse = BaseResponse & { - data?: Partial[]; + event_type_custom_inputs?: Partial[]; }; // EventType export type EventTypeResponse = BaseResponse & { - data?: Partial; + event_type?: Partial; }; export type EventTypesResponse = BaseResponse & { - data?: Partial[]; + event_types?: Partial[]; }; // Payment export type PaymentResponse = BaseResponse & { - data?: Partial; + payment?: Partial; }; export type PaymentsResponse = BaseResponse & { - data?: Partial[]; + payments?: Partial[]; }; // Schedule export type ScheduleResponse = BaseResponse & { - data?: Partial; + schedule?: Partial; }; export type SchedulesResponse = BaseResponse & { - data?: Partial[]; + schedules?: Partial[]; }; // Webhook export type WebhookResponse = BaseResponse & { - data?: Partial; + webhook?: Partial; }; export type WebhooksResponse = BaseResponse & { - data?: Partial[]; + webhooks?: Partial[]; }; // ReminderMail export type ReminderMailResponse = BaseResponse & { - data?: Partial; + reminder_mail?: Partial; }; export type ReminderMailsResponse = BaseResponse & { - data?: Partial[]; + reminder_mails?: Partial[]; }; diff --git a/pages/api/attendees/[id].ts b/pages/api/attendees/[id].ts index 8152512d5c..6044c6cc74 100644 --- a/pages/api/attendees/[id].ts +++ b/pages/api/attendees/[id].ts @@ -89,8 +89,8 @@ export async function attendeeById(req: NextApiRequest, res: NextApiResponse schemaAttendeePublic.parse(attendee)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaAttendeePublic.parse(data)) + .then((attendee) => res.status(200).json({ attendee })) .catch((error: Error) => res.status(404).json({ message: `Attendee with id: ${safeQuery.data.id} not found`, error }) ); @@ -103,8 +103,8 @@ export async function attendeeById(req: NextApiRequest, res: NextApiResponse schemaAttendeePublic.parse(attendee)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaAttendeePublic.parse(data)) + .then((attendee) => res.status(200).json({ attendee })) .catch((error: Error) => res.status(404).json({ message: `Attendee with id: ${safeQuery.data.id} not found`, error }) ); diff --git a/pages/api/attendees/index.ts b/pages/api/attendees/index.ts index e15a3af0e0..d9f7429325 100644 --- a/pages/api/attendees/index.ts +++ b/pages/api/attendees/index.ts @@ -49,9 +49,9 @@ async function createOrlistAllAttendees( attendees: true, }, }); - const userBookingsAttendees = userBookings.map((booking) => booking.attendees).flat(); + const attendees = userBookings.map((booking) => booking.attendees).flat(); if (method === "GET") { - if (userBookingsAttendees) res.status(200).json({ data: userBookingsAttendees }); + if (attendees) res.status(200).json({ attendees }); else (error: Error) => res.status(404).json({ @@ -67,12 +67,12 @@ async function createOrlistAllAttendees( const bookingId = safe.data.bookingId; delete safe.data.bookingId; const noBookingId = safe.data; - const attendee = await prisma.attendee.create({ + const data = await prisma.attendee.create({ data: { ...noBookingId, booking: { connect: { id: parseInt(bookingId as string) } } }, }); - const data = schemaAttendeePublic.parse(attendee); + const attendee = schemaAttendeePublic.parse(data); - if (data) res.status(201).json({ data, message: "Attendee created successfully" }); + if (attendee) res.status(201).json({ attendee, message: "Attendee created successfully" }); else (error: Error) => res.status(400).json({ diff --git a/pages/api/availabilities/[id].ts b/pages/api/availabilities/[id].ts index 5e9e243e54..1c126ff2c2 100644 --- a/pages/api/availabilities/[id].ts +++ b/pages/api/availabilities/[id].ts @@ -89,8 +89,8 @@ export async function availabilityById(req: NextApiRequest, res: NextApiResponse case "GET": await prisma.availability .findUnique({ where: { id: safeQuery.data.id } }) - .then((availability) => schemaAvailabilityPublic.parse(availability)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaAvailabilityPublic.parse(data)) + .then((availability) => res.status(200).json({ availability })) .catch((error: Error) => res.status(404).json({ message: `Availability with id: ${safeQuery.data.id} not found`, error }) ); @@ -103,8 +103,8 @@ export async function availabilityById(req: NextApiRequest, res: NextApiResponse where: { id: safeQuery.data.id }, data: safeBody.data, }) - .then((availability) => schemaAvailabilityPublic.parse(availability)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaAvailabilityPublic.parse(data)) + .then((availability) => res.status(200).json({ availability })) .catch((error: Error) => res.status(404).json({ message: `Availability with id: ${safeQuery.data.id} not found`, error }) ); diff --git a/pages/api/availabilities/index.ts b/pages/api/availabilities/index.ts index 5449162d95..d0408a32aa 100644 --- a/pages/api/availabilities/index.ts +++ b/pages/api/availabilities/index.ts @@ -3,8 +3,8 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; -import { AvailabilitiesResponse } from "@lib/types"; -import { schemaAvailabilityPublic } from "@lib/validations/availability"; +import { AvailabilityResponse, AvailabilitiesResponse } from "@lib/types"; +import { schemaAvailabilityBodyParams, schemaAvailabilityPublic } from "@lib/validations/availability"; /** * @swagger @@ -20,18 +20,49 @@ import { schemaAvailabilityPublic } from "@lib/validations/availability"; * description: Authorization information is missing or invalid. * 404: * description: No availabilities were found + * post: + * summary: Creates a new availability + * tags: + * - availabilities + * responses: + * 201: + * description: OK, availability created + * model: Availability + * 400: + * description: Bad request. Availability body is invalid. + * 401: + * description: Authorization information is missing or invalid. */ -async function allAvailabilities(_: NextApiRequest, res: NextApiResponse) { - const availabilities = await prisma.availability.findMany(); - const data = availabilities.map((availability) => schemaAvailabilityPublic.parse(availability)); +async function createOrlistAllAvailabilities( + req: NextApiRequest, + res: NextApiResponse +) { + const { method } = req; + if (method === "GET") { + const data = await prisma.availability.findMany(); + const availabilities = data.map((availability) => schemaAvailabilityPublic.parse(availability)); + if (availabilities) res.status(200).json({ availabilities }); + else + (error: Error) => + res.status(404).json({ + message: "No Availabilities were found", + error, + }); + } else if (method === "POST") { + const safe = schemaAvailabilityBodyParams.safeParse(req.body); + if (!safe.success) throw new Error("Invalid request body"); - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "No Availabilities were found", - error, - }); + const data = await prisma.availability.create({ data: safe.data }); + const availability = schemaAvailabilityPublic.parse(data); + + if (availability) res.status(201).json({ availability, message: "Availability created successfully" }); + else + (error: Error) => + res.status(400).json({ + message: "Could not create new availability", + error, + }); + } else res.status(405).json({ message: `Method ${method} not allowed` }); } -export default withMiddleware("HTTP_GET")(allAvailabilities); +export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllAvailabilities); diff --git a/pages/api/availabilities/new.ts b/pages/api/availabilities/new.ts deleted file mode 100644 index d7461ba803..0000000000 --- a/pages/api/availabilities/new.ts +++ /dev/null @@ -1,51 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { AvailabilityResponse } from "@lib/types"; -import { - schemaAvailabilityBodyParams, - schemaAvailabilityPublic, - withValidAvailability, -} from "@lib/validations/availability"; - -/** - * @swagger - * /api/availabilities/new: - * post: - * summary: Creates a new availability - * requestBody: - * description: Optional description in *Markdown* - * required: true - * content: - * application/json: - * schema: - * $ref: '#/components/schemas/Availability' - * tags: - * - availabilities - * responses: - * 201: - * description: OK, availability created - * 400: - * description: Bad request. Availability body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -async function createAvailability(req: NextApiRequest, res: NextApiResponse) { - const safe = schemaAvailabilityBodyParams.safeParse(req.body); - if (!safe.success) throw new Error("Invalid request body", safe.error); - - const availability = await prisma.availability.create({ data: safe.data }); - const data = schemaAvailabilityPublic.parse(availability); - - if (data) res.status(201).json({ data, message: "Availability created successfully" }); - else - (error: Error) => - res.status(400).json({ - message: "Could not create new availability", - error, - }); -} - -export default withMiddleware("HTTP_POST")(withValidAvailability(createAvailability)); diff --git a/pages/api/booking-references/[id].ts b/pages/api/booking-references/[id].ts index 216d1282ff..659c48f46d 100644 --- a/pages/api/booking-references/[id].ts +++ b/pages/api/booking-references/[id].ts @@ -96,7 +96,7 @@ export async function bookingReferenceById( await prisma.bookingReference .findUnique({ where: { id: safeQuery.data.id } }) .then((data) => schemaBookingReferencePublic.parse(data)) - .then((data) => res.status(200).json({ data })) + .then((booking_reference) => res.status(200).json({ booking_reference })) .catch((error: Error) => res.status(404).json({ message: `BookingReference with id: ${safeQuery.data.id} not found`, error }) ); @@ -109,8 +109,8 @@ export async function bookingReferenceById( where: { id: safeQuery.data.id }, data: safeBody.data, }) - .then((bookingReference) => schemaBookingReferencePublic.parse(bookingReference)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaBookingReferencePublic.parse(data)) + .then((booking_reference) => res.status(200).json({ booking_reference })) .catch((error: Error) => res.status(404).json({ message: `BookingReference with id: ${safeQuery.data.id} not found`, error }) ); diff --git a/pages/api/booking-references/index.ts b/pages/api/booking-references/index.ts index 312ccf4b78..df12d1c882 100644 --- a/pages/api/booking-references/index.ts +++ b/pages/api/booking-references/index.ts @@ -3,14 +3,17 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; -import { BookingReferencesResponse } from "@lib/types"; -import { schemaBookingReferencePublic } from "@lib/validations/booking-reference"; +import { BookingReferenceResponse, BookingReferencesResponse } from "@lib/types"; +import { + schemaBookingReferenceBodyParams, + schemaBookingReferencePublic, +} from "@lib/validations/booking-reference"; /** * @swagger * /api/booking-references: * get: - * summary: Get all bookingReferences + * summary: Get all booking references * tags: * - booking-references * responses: @@ -19,21 +22,52 @@ import { schemaBookingReferencePublic } from "@lib/validations/booking-reference * 401: * description: Authorization information is missing or invalid. * 404: - * description: No bookingReferences were found + * description: No booking references were found + * post: + * summary: Creates a new booking reference + * tags: + * - booking-references + * responses: + * 201: + * description: OK, booking reference created + * model: BookingReference + * 400: + * description: Bad request. BookingReference body is invalid. + * 401: + * description: Authorization information is missing or invalid. */ -async function allBookingReferences(_: NextApiRequest, res: NextApiResponse) { - const bookingReferences = await prisma.bookingReference.findMany(); - const data = bookingReferences.map((bookingReference) => - schemaBookingReferencePublic.parse(bookingReference) - ); +async function createOrlistAllBookingReferences( + req: NextApiRequest, + res: NextApiResponse +) { + const { method } = req; + if (method === "GET") { + const data = await prisma.bookingReference.findMany(); + const booking_references = data.map((bookingReference) => + schemaBookingReferencePublic.parse(bookingReference) + ); + if (booking_references) res.status(200).json({ booking_references }); + else + (error: Error) => + res.status(404).json({ + message: "No BookingReferences were found", + error, + }); + } else if (method === "POST") { + const safe = schemaBookingReferenceBodyParams.safeParse(req.body); + if (!safe.success) throw new Error("Invalid request body"); - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "No BookingReferences were found", - error, - }); + const data = await prisma.bookingReference.create({ data: safe.data }); + const booking_reference = schemaBookingReferencePublic.parse(data); + + if (data) res.status(201).json({ booking_reference, message: "BookingReference created successfully" }); + else + (error: Error) => + res.status(400).json({ + message: "Could not create new booking reference", + error, + }); + } else res.status(405).json({ message: `Method ${method} not allowed` }); } -export default withMiddleware("HTTP_GET")(allBookingReferences); +export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllBookingReferences); diff --git a/pages/api/booking-references/new.ts b/pages/api/booking-references/new.ts deleted file mode 100644 index 737156ae3f..0000000000 --- a/pages/api/booking-references/new.ts +++ /dev/null @@ -1,51 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { BookingReferenceResponse } from "@lib/types"; -import { - schemaBookingReferenceBodyParams, - schemaBookingReferencePublic, - withValidBookingReference, -} from "@lib/validations/booking-reference"; - -/** - * @swagger - * /api/booking-references/new: - * post: - * summary: Creates a new bookingReference - * requestBody: - * description: Optional description in *Markdown* - * required: true - * content: - * application/json: - * schema: - * $ref: '#/components/schemas/BookingReference' - * tags: - * - booking-references - * responses: - * 201: - * description: OK, bookingReference created - * 400: - * description: Bad request. BookingReference body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -async function createBookingReference(req: NextApiRequest, res: NextApiResponse) { - const safe = schemaBookingReferenceBodyParams.safeParse(req.body); - if (!safe.success) throw new Error("Invalid request body", safe.error); - - const bookingReference = await prisma.bookingReference.create({ data: safe.data }); - const data = schemaBookingReferencePublic.parse(bookingReference); - - if (data) res.status(201).json({ data, message: "BookingReference created successfully" }); - else - (error: Error) => - res.status(400).json({ - message: "Could not create new bookingReference", - error, - }); -} - -export default withMiddleware("HTTP_POST")(withValidBookingReference(createBookingReference)); diff --git a/pages/api/bookings/[id].ts b/pages/api/bookings/[id].ts index 852b32b983..f3104fc44e 100644 --- a/pages/api/bookings/[id].ts +++ b/pages/api/bookings/[id].ts @@ -89,8 +89,8 @@ export async function bookingById(req: NextApiRequest, res: NextApiResponse schemaBookingPublic.parse(booking)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaBookingPublic.parse(data)) + .then((booking) => res.status(200).json({ booking })) .catch((error: Error) => res.status(404).json({ message: `Booking with id: ${safeQuery.data.id} not found`, error }) ); @@ -103,8 +103,8 @@ export async function bookingById(req: NextApiRequest, res: NextApiResponse schemaBookingPublic.parse(booking)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaBookingPublic.parse(data)) + .then((booking) => res.status(200).json({ booking })) .catch((error: Error) => res.status(404).json({ message: `Booking with id: ${safeQuery.data.id} not found`, error }) ); diff --git a/pages/api/bookings/index.ts b/pages/api/bookings/index.ts index f2d724071c..dabf90f245 100644 --- a/pages/api/bookings/index.ts +++ b/pages/api/bookings/index.ts @@ -3,8 +3,8 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; -import { BookingsResponse } from "@lib/types"; -import { schemaBookingPublic } from "@lib/validations/booking"; +import { BookingResponse, BookingsResponse } from "@lib/types"; +import { schemaBookingBodyParams, schemaBookingPublic, withValidBooking } from "@lib/validations/booking"; /** * @swagger @@ -20,18 +20,49 @@ import { schemaBookingPublic } from "@lib/validations/booking"; * description: Authorization information is missing or invalid. * 404: * description: No bookings were found + * post: + * summary: Creates a new booking + * tags: + * - bookings + * responses: + * 201: + * description: OK, booking created + * model: Booking + * 400: + * description: Bad request. Booking body is invalid. + * 401: + * description: Authorization information is missing or invalid. */ -async function allBookings(_: NextApiRequest, res: NextApiResponse) { - const bookings = await prisma.booking.findMany(); - const data = bookings.map((booking) => schemaBookingPublic.parse(booking)); +async function createOrlistAllBookings( + req: NextApiRequest, + res: NextApiResponse +) { + const { method } = req; + if (method === "GET") { + const data = await prisma.booking.findMany(); + const bookings = data.map((booking) => schemaBookingPublic.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 = schemaBookingBodyParams.safeParse(req.body); + if (!safe.success) throw new Error("Invalid request body"); - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "No Bookings were found", - error, - }); + const data = await prisma.booking.create({ data: safe.data }); + const booking = schemaBookingPublic.parse(data); + + if (booking) res.status(201).json({ booking, message: "Booking created successfully" }); + else + (error: Error) => + res.status(400).json({ + message: "Could not create new booking", + error, + }); + } else res.status(405).json({ message: `Method ${method} not allowed` }); } -export default withMiddleware("HTTP_GET")(allBookings); +export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllBookings); diff --git a/pages/api/bookings/new.ts b/pages/api/bookings/new.ts deleted file mode 100644 index 3e10e1c143..0000000000 --- a/pages/api/bookings/new.ts +++ /dev/null @@ -1,48 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { BookingResponse } from "@lib/types"; -import { schemaBookingBodyParams, schemaBookingPublic, withValidBooking } from "@lib/validations/booking"; - -/** - * @swagger - * /api/bookings/new: - * post: - * summary: Creates a new booking - * requestBody: - * description: Optional description in *Markdown* - * required: true - * content: - * application/json: - * schema: - * $ref: '#/components/schemas/Booking' - * tags: - * - bookings - * responses: - * 201: - * description: OK, booking created - * model: Booking - * 400: - * description: Bad request. Booking body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -async function createBooking(req: NextApiRequest, res: NextApiResponse) { - const safe = schemaBookingBodyParams.safeParse(req.body); - if (!safe.success) throw new Error("Invalid request body", safe.error); - - const booking = await prisma.booking.create({ data: safe.data }); - const data = schemaBookingPublic.parse(booking); - - if (data) res.status(201).json({ data, message: "Booking created successfully" }); - else - (error: Error) => - res.status(400).json({ - message: "Could not create new booking", - error, - }); -} - -export default withMiddleware("HTTP_POST")(withValidBooking(createBooking)); diff --git a/pages/api/payments/[id].ts b/pages/api/payments/[id].ts index e026e62dc5..bdd8766c69 100644 --- a/pages/api/payments/[id].ts +++ b/pages/api/payments/[id].ts @@ -15,7 +15,7 @@ import { * @swagger * /api/payments/{id}: * get: - * summary: Get a payment by ID + * summary: Get one of your own payments by ID * parameters: * - in: path * name: id diff --git a/pages/api/schedules/index.ts b/pages/api/schedules/index.ts index 3458e4896b..e3b178d216 100644 --- a/pages/api/schedules/index.ts +++ b/pages/api/schedules/index.ts @@ -8,7 +8,7 @@ import { schemaScheduleBodyParams, schemaSchedulePublic, withValidSchedule } fro /** * @swagger - * /api/schedules: + * /v1/schedules: * get: * summary: Get all schedules * tags: diff --git a/pages/api/selected-calendars/[id].ts b/pages/api/selected-calendars/[id].ts index 900c2295db..b66fc5a888 100644 --- a/pages/api/selected-calendars/[id].ts +++ b/pages/api/selected-calendars/[id].ts @@ -139,7 +139,7 @@ export async function selectedCalendarById( }, }) .then((selectedCalendar) => schemaSelectedCalendarPublic.parse(selectedCalendar)) - .then((data) => res.status(200).json({ data })) + .then((selected_calendar) => res.status(200).json({ selected_calendar })) .catch((error: Error) => res.status(404).json({ message: `SelectedCalendar with id: ${safeQuery.data.id} not found`, error }) ); @@ -159,7 +159,7 @@ export async function selectedCalendarById( data: safeBody.data, }) .then((selectedCalendar) => schemaSelectedCalendarPublic.parse(selectedCalendar)) - .then((data) => res.status(200).json({ data })) + .then((selected_calendar) => res.status(200).json({ selected_calendar })) .catch((error: Error) => res.status(404).json({ message: `SelectedCalendar with id: ${safeQuery.data.id} not found`, error }) ); diff --git a/pages/api/selected-calendars/index.ts b/pages/api/selected-calendars/index.ts index faa2eb0e5c..a6a8a39c16 100644 --- a/pages/api/selected-calendars/index.ts +++ b/pages/api/selected-calendars/index.ts @@ -43,11 +43,11 @@ async function createOrlistAllSelectedCalendars( ) { const { method } = req; if (method === "GET") { - const selectedCalendars = await prisma.selectedCalendar.findMany(); - const data = selectedCalendars.map((selectedCalendar) => - schemaSelectedCalendarPublic.parse(selectedCalendar) + const data = await prisma.selectedCalendar.findMany(); + const selected_calendars = data.map((selected_calendar) => + schemaSelectedCalendarPublic.parse(selected_calendar) ); - if (data) res.status(200).json({ data }); + if (selected_calendars) res.status(200).json({ selected_calendars }); else (error: Error) => res.status(404).json({ @@ -58,14 +58,15 @@ async function createOrlistAllSelectedCalendars( const safe = schemaSelectedCalendarBodyParams.safeParse(req.body); if (!safe.success) throw new Error("Invalid request body"); - const selectedCalendar = await prisma.selectedCalendar.create({ data: safe.data }); - const data = schemaSelectedCalendarPublic.parse(selectedCalendar); + const data = await prisma.selectedCalendar.create({ data: safe.data }); + const selected_calendar = schemaSelectedCalendarPublic.parse(data); - if (data) res.status(201).json({ data, message: "SelectedCalendar created successfully" }); + if (selected_calendar) + res.status(201).json({ selected_calendar, message: "SelectedCalendar created successfully" }); else (error: Error) => res.status(400).json({ - message: "Could not create new selectedCalendar", + message: "Could not create new selected calendar", error, }); } else res.status(405).json({ message: `Method ${method} not allowed` }); diff --git a/pages/api/teams/[id].ts b/pages/api/teams/[id].ts index ef9f26845c..5435b18dc5 100644 --- a/pages/api/teams/[id].ts +++ b/pages/api/teams/[id].ts @@ -4,6 +4,7 @@ import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; import type { TeamResponse } from "@lib/types"; +import { getCalcomUserId } from "@lib/utils/getCalcomUserId"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt, @@ -84,13 +85,17 @@ export async function teamById(req: NextApiRequest, res: NextApiResponse schemaTeamPublic.parse(data)) - .then((data) => res.status(200).json({ data })) + .then((team) => res.status(200).json({ team })) .catch((error: Error) => res.status(404).json({ message: `Team with id: ${safeQuery.data.id} not found`, error }) ); @@ -104,7 +109,7 @@ export async function teamById(req: NextApiRequest, res: NextApiResponse schemaTeamPublic.parse(team)) - .then((data) => res.status(200).json({ data })) + .then((team) => res.status(200).json({ team })) .catch((error: Error) => res.status(404).json({ message: `Team with id: ${safeQuery.data.id} not found`, error }) ); diff --git a/pages/api/teams/index.ts b/pages/api/teams/index.ts index 6184a5e1bd..5e68236e2e 100644 --- a/pages/api/teams/index.ts +++ b/pages/api/teams/index.ts @@ -4,6 +4,7 @@ import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; import { TeamResponse, TeamsResponse } from "@lib/types"; +import { getCalcomUserId } from "@lib/utils/getCalcomUserId"; import { schemaTeamBodyParams, schemaTeamPublic, withValidTeam } from "@lib/validations/team"; /** @@ -35,10 +36,14 @@ import { schemaTeamBodyParams, schemaTeamPublic, withValidTeam } from "@lib/vali */ async function createOrlistAllTeams(req: NextApiRequest, res: NextApiResponse) { const { method } = req; + const userId = getCalcomUserId(res); if (method === "GET") { - const teams = await prisma.team.findMany(); - const data = teams.map((team) => schemaTeamPublic.parse(team)); - if (data) res.status(200).json({ data }); + const userWithMemberships = await prisma.membership.findMany({ + where: { userId: userId }, + }); + const teamIds = userWithMemberships.map((membership) => membership.teamId); + const teams = await prisma.team.findMany({ where: { id: { in: teamIds } } }); + if (teams) res.status(200).json({ teams }); else (error: Error) => res.status(404).json({ @@ -46,13 +51,17 @@ async function createOrlistAllTeams(req: NextApiRequest, res: NextApiResponse res.status(400).json({ @@ -62,4 +71,4 @@ async function createOrlistAllTeams(req: NextApiRequest, res: NextApiResponse schemaUserPublic.parse(user)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaUserPublic.parse(data)) + .then((user) => res.status(200).json({ user })) .catch((error: Error) => res.status(404).json({ message: `User with id: ${safeQuery.data.id} not found`, error }) ); @@ -105,8 +105,8 @@ export async function userById(req: NextApiRequest, res: NextApiResponse schemaUserPublic.parse(user)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaUserPublic.parse(data)) + .then((user) => res.status(200).json({ user })) .catch((error: Error) => res.status(404).json({ message: `User with id: ${safeQuery.data.id} not found`, error }) ); diff --git a/pages/api/users/index.ts b/pages/api/users/index.ts index 1d11d751d5..138395adba 100644 --- a/pages/api/users/index.ts +++ b/pages/api/users/index.ts @@ -24,13 +24,13 @@ import { schemaUserPublic } from "@lib/validations/user"; */ async function allUsers(req: NextApiRequest, res: NextApiResponse) { const userId = getCalcomUserId(res); - const users = await prisma.user.findMany({ + const data = await prisma.user.findMany({ where: { id: userId, }, }); - const data = users.map((user) => schemaUserPublic.parse(user)); - if (data) res.status(200).json({ data }); + const users = data.map((user) => schemaUserPublic.parse(user)); + if (users) res.status(200).json({ users }); else (error: Error) => res.status(404).json({ diff --git a/templates/endpoints/newIndex.ts b/templates/endpoints/newIndex.ts index 732eb013b2..5d608ee0cb 100644 --- a/templates/endpoints/newIndex.ts +++ b/templates/endpoints/newIndex.ts @@ -65,4 +65,4 @@ async function createOrlistAllPayments( } else res.status(405).json({ message: `Method ${method} not allowed` }); } -export default withMiddleware("HTTP_GET_OR_POST")(withValidPayment(createOrlistAllPayments)); +export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllPayments);