diff --git a/pages/api/attendees/[id].ts b/pages/api/attendees/[id].ts index 6044c6cc74..093579a799 100644 --- a/pages/api/attendees/[id].ts +++ b/pages/api/attendees/[id].ts @@ -12,7 +12,7 @@ import { /** * @swagger - * /api/attendees/{id}: + * /v1/attendees/{id}: * get: * summary: Get an attendee by ID * parameters: diff --git a/pages/api/attendees/index.ts b/pages/api/attendees/index.ts index d9f7429325..509907bb8d 100644 --- a/pages/api/attendees/index.ts +++ b/pages/api/attendees/index.ts @@ -9,7 +9,7 @@ import { schemaAttendeeBodyParams, schemaAttendeePublic, withValidAttendee } fro /** * @swagger - * /api/attendees: + * /v1/attendees: * get: * summary: Get all attendees * tags: diff --git a/pages/api/availabilities/[id].ts b/pages/api/availabilities/[id].ts index 1c126ff2c2..34bab03008 100644 --- a/pages/api/availabilities/[id].ts +++ b/pages/api/availabilities/[id].ts @@ -12,7 +12,7 @@ import { /** * @swagger - * /api/availabilities/{id}: + * /v1/availabilities/{id}: * get: * summary: Get an availability by ID * parameters: diff --git a/pages/api/availabilities/index.ts b/pages/api/availabilities/index.ts index d0408a32aa..9778bfdbfa 100644 --- a/pages/api/availabilities/index.ts +++ b/pages/api/availabilities/index.ts @@ -8,7 +8,7 @@ import { schemaAvailabilityBodyParams, schemaAvailabilityPublic } from "@lib/val /** * @swagger - * /api/availabilities: + * /v1/availabilities: * get: * summary: Get all availabilities * tags: diff --git a/pages/api/booking-references/[id].ts b/pages/api/booking-references/[id].ts index 659c48f46d..2d48aa7413 100644 --- a/pages/api/booking-references/[id].ts +++ b/pages/api/booking-references/[id].ts @@ -15,7 +15,7 @@ import { /** * @swagger - * /api/booking-references/{id}: + * /v1/booking-references/{id}: * get: * summary: Get a daily event reference by ID * parameters: @@ -90,6 +90,7 @@ export async function bookingReferenceById( const safeQuery = schemaQueryIdParseInt.safeParse(query); const safeBody = schemaBookingReferenceBodyParams.safeParse(body); if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); + // FIXME: Allow only userId owner of booking ref to edit it switch (method) { case "GET": diff --git a/pages/api/booking-references/index.ts b/pages/api/booking-references/index.ts index df12d1c882..0ca58c2f8a 100644 --- a/pages/api/booking-references/index.ts +++ b/pages/api/booking-references/index.ts @@ -11,7 +11,7 @@ import { /** * @swagger - * /api/booking-references: + * /v1/booking-references: * get: * summary: Get all booking references * tags: diff --git a/pages/api/bookings/[id].ts b/pages/api/bookings/[id].ts index f3104fc44e..45f24774e6 100644 --- a/pages/api/bookings/[id].ts +++ b/pages/api/bookings/[id].ts @@ -12,7 +12,7 @@ import { /** * @swagger - * /api/bookings/{id}: + * /v1/bookings/{id}: * get: * summary: Get a booking by ID * parameters: @@ -84,7 +84,7 @@ export async function bookingById(req: NextApiRequest, res: NextApiResponse ) { const { method } = req; + // FIXME: List only bookings owner by userId + if (method === "GET") { const data = await prisma.booking.findMany(); const bookings = data.map((booking) => schemaBookingPublic.parse(booking)); diff --git a/pages/api/credentials/[id].ts b/pages/api/credentials/[id].ts index 16c60007a4..bb82d0f019 100644 --- a/pages/api/credentials/[id].ts +++ b/pages/api/credentials/[id].ts @@ -12,7 +12,7 @@ import { /** * @swagger - * /api/credentials/{id}: + * /v1/credentials/{id}: * get: * summary: Get a credential by ID * parameters: @@ -89,8 +89,8 @@ export async function credentialById(req: NextApiRequest, res: NextApiResponse schemaCredentialPublic.parse(credential)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaCredentialPublic.parse(data)) + .then((credential) => res.status(200).json({ credential })) .catch((error: Error) => res.status(404).json({ message: `Credential with id: ${safeQuery.data.id} not found`, error }) ); @@ -103,8 +103,8 @@ export async function credentialById(req: NextApiRequest, res: NextApiResponse schemaCredentialPublic.parse(credential)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaCredentialPublic.parse(data)) + .then((credential) => res.status(200).json({ credential })) .catch((error: Error) => res.status(404).json({ message: `Credential with id: ${safeQuery.data.id} not found`, error }) ); diff --git a/pages/api/credentials/index.ts b/pages/api/credentials/index.ts index 1ef98a95df..8ba9a5aeb5 100644 --- a/pages/api/credentials/index.ts +++ b/pages/api/credentials/index.ts @@ -3,8 +3,8 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; -import { CredentialsResponse } from "@lib/types"; -import { schemaCredentialPublic } from "@lib/validations/credential"; +import { CredentialResponse, CredentialsResponse } from "@lib/types"; +import { schemaCredentialBodyParams, schemaCredentialPublic } from "@lib/validations/credential"; /** * @swagger @@ -20,18 +20,49 @@ import { schemaCredentialPublic } from "@lib/validations/credential"; * description: Authorization information is missing or invalid. * 404: * description: No credentials were found + * post: + * summary: Creates a new credential + * tags: + * - credentials + * responses: + * 201: + * description: OK, credential created + * model: Credential + * 400: + * description: Bad request. Credential body is invalid. + * 401: + * description: Authorization information is missing or invalid. */ -async function allCredentials(_: NextApiRequest, res: NextApiResponse) { - const credentials = await prisma.credential.findMany(); - const data = credentials.map((credential) => schemaCredentialPublic.parse(credential)); +async function createOrlistAllCredentials( + req: NextApiRequest, + res: NextApiResponse +) { + const { method } = req; + if (method === "GET") { + const data = await prisma.credential.findMany(); + const credentials = data.map((credential) => schemaCredentialPublic.parse(credential)); + if (credentials) res.status(200).json({ credentials }); + else + (error: Error) => + res.status(404).json({ + message: "No Credentials were found", + error, + }); + } else if (method === "POST") { + const safe = schemaCredentialBodyParams.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 Credentials were found", - error, - }); + const data = await prisma.credential.create({ data: safe.data }); + const credential = schemaCredentialPublic.parse(data); + + if (credential) res.status(201).json({ credential, message: "Credential created successfully" }); + else + (error: Error) => + res.status(400).json({ + message: "Could not create new credential", + error, + }); + } else res.status(405).json({ message: `Method ${method} not allowed` }); } -export default withMiddleware("HTTP_GET")(allCredentials); +export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllCredentials); diff --git a/pages/api/credentials/new.ts b/pages/api/credentials/new.ts deleted file mode 100644 index 39302c7a1d..0000000000 --- a/pages/api/credentials/new.ts +++ /dev/null @@ -1,52 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { CredentialResponse } from "@lib/types"; -import { - schemaCredentialBodyParams, - schemaCredentialPublic, - withValidCredential, -} from "@lib/validations/credential"; - -/** - * @swagger - * /api/credentials/new: - * post: - * summary: Creates a new credential - * requestBody: - * description: Optional description in *Markdown* - * required: true - * content: - * application/json: - * schema: - * $ref: '#/components/schemas/Credential' - * tags: - * - credentials - * responses: - * 201: - * description: OK, credential created - * model: Credential - * 400: - * description: Bad request. Credential body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -async function createCredential(req: NextApiRequest, res: NextApiResponse) { - const safe = schemaCredentialBodyParams.safeParse(req.body); - if (!safe.success) throw new Error("Invalid request body", safe.error); - - const credential = await prisma.credential.create({ data: safe.data }); - const data = schemaCredentialPublic.parse(credential); - - if (data) res.status(201).json({ data, message: "Credential created successfully" }); - else - (error: Error) => - res.status(400).json({ - message: "Could not create new credential", - error, - }); -} - -export default withMiddleware("HTTP_POST")(withValidCredential(createCredential)); diff --git a/pages/api/daily-event-references/[id].ts b/pages/api/daily-event-references/[id].ts index d150cf69ce..482aab6ec1 100644 --- a/pages/api/daily-event-references/[id].ts +++ b/pages/api/daily-event-references/[id].ts @@ -15,7 +15,7 @@ import { /** * @swagger - * /api/daily-event-references/{id}: + * /v1/daily-event-references/{id}: * get: * summary: Get a daily event reference by ID * parameters: @@ -96,7 +96,7 @@ export async function dailyEventReferenceById( await prisma.dailyEventReference .findUnique({ where: { id: safeQuery.data.id } }) .then((data) => schemaDailyEventReferencePublic.parse(data)) - .then((data) => res.status(200).json({ data })) + .then((daily_event_reference) => res.status(200).json({ daily_event_reference })) .catch((error: Error) => res .status(404) @@ -111,8 +111,8 @@ export async function dailyEventReferenceById( where: { id: safeQuery.data.id }, data: safeBody.data, }) - .then((dailyEventReference) => schemaDailyEventReferencePublic.parse(dailyEventReference)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaDailyEventReferencePublic.parse(data)) + .then((daily_event_reference) => res.status(200).json({ daily_event_reference })) .catch((error: Error) => res .status(404) diff --git a/pages/api/daily-event-references/index.ts b/pages/api/daily-event-references/index.ts index d4495ef0f1..1a1762157d 100644 --- a/pages/api/daily-event-references/index.ts +++ b/pages/api/daily-event-references/index.ts @@ -3,40 +3,72 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; -import { DailyEventReferencesResponse } from "@lib/types"; -import { schemaDailyEventReferencePublic } from "@lib/validations/daily-event-reference"; +import { DailyEventReferenceResponse, DailyEventReferencesResponse } from "@lib/types"; +import { + schemaDailyEventReferenceBodyParams, + schemaDailyEventReferencePublic, +} from "@lib/validations/daily-event-reference"; /** * @swagger - * /api/daily-event-references: + * /v1/daily-event-references: * get: - * summary: Get all dailyEventReferences + * summary: Get all daily event reference * tags: - * - daily-event-references + * - daily-event-reference * responses: * 200: * description: OK * 401: * description: Authorization information is missing or invalid. * 404: - * description: No dailyEventReferences were found + * description: No daily event references were found + * post: + * summary: Creates a new daily event reference + * tags: + * - daily-event-reference + * responses: + * 201: + * description: OK, daily event reference created + * model: DailyEventReference + * 400: + * description: Bad request. DailyEventReference body is invalid. + * 401: + * description: Authorization information is missing or invalid. */ -async function allDailyEventReferences( - _: NextApiRequest, - res: NextApiResponse +async function createOrlistAllDailyEventReferences( + req: NextApiRequest, + res: NextApiResponse ) { - const dailyEventReferences = await prisma.dailyEventReference.findMany(); - const data = dailyEventReferences.map((dailyEventReference) => - schemaDailyEventReferencePublic.parse(dailyEventReference) - ); + const { method } = req; + if (method === "GET") { + const data = await prisma.dailyEventReference.findMany(); + const daily_event_references = data.map((dailyEventReference) => + schemaDailyEventReferencePublic.parse(dailyEventReference) + ); + if (daily_event_references) res.status(200).json({ daily_event_references }); + else + (error: Error) => + res.status(404).json({ + message: "No DailyEventReferences were found", + error, + }); + } else if (method === "POST") { + const safe = schemaDailyEventReferenceBodyParams.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 DailyEventReferences were found", - error, - }); + const data = await prisma.dailyEventReference.create({ data: safe.data }); + const daily_event_reference = schemaDailyEventReferencePublic.parse(data); + + if (daily_event_reference) + res.status(201).json({ daily_event_reference, message: "DailyEventReference created successfully" }); + else + (error: Error) => + res.status(400).json({ + message: "Could not create new daily event reference", + error, + }); + } else res.status(405).json({ message: `Method ${method} not allowed` }); } -export default withMiddleware("HTTP_GET")(allDailyEventReferences); +export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllDailyEventReferences); diff --git a/pages/api/daily-event-references/new.ts b/pages/api/daily-event-references/new.ts deleted file mode 100644 index 1e61a7fa59..0000000000 --- a/pages/api/daily-event-references/new.ts +++ /dev/null @@ -1,55 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { DailyEventReferenceResponse } from "@lib/types"; -import { - schemaDailyEventReferenceBodyParams, - schemaDailyEventReferencePublic, - withValidDailyEventReference, -} from "@lib/validations/daily-event-reference"; - -/** - * @swagger - * /api/daily-event-references/new: - * post: - * summary: Creates a new dailyEventReference - * requestBody: - * description: Optional description in *Markdown* - * required: true - * content: - * application/json: - * schema: - * $ref: '#/components/schemas/DailyEventReference' - * tags: - * - daily-event-references - * responses: - * 201: - * description: OK, dailyEventReference created - * model: DailyEventReference - * 400: - * description: Bad request. DailyEventReference body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -async function createDailyEventReference( - req: NextApiRequest, - res: NextApiResponse -) { - const safe = schemaDailyEventReferenceBodyParams.safeParse(req.body); - if (!safe.success) throw new Error("Invalid request body", safe.error); - - const dailyEventReference = await prisma.dailyEventReference.create({ data: safe.data }); - const data = schemaDailyEventReferencePublic.parse(dailyEventReference); - - if (data) res.status(201).json({ data, message: "DailyEventReference created successfully" }); - else - (error: Error) => - res.status(400).json({ - message: "Could not create new dailyEventReference", - error, - }); -} - -export default withMiddleware("HTTP_POST")(withValidDailyEventReference(createDailyEventReference)); diff --git a/pages/api/destination-calendars/[id].ts b/pages/api/destination-calendars/[id].ts index 16c5fba7be..a72bcecba1 100644 --- a/pages/api/destination-calendars/[id].ts +++ b/pages/api/destination-calendars/[id].ts @@ -15,7 +15,7 @@ import { /** * @swagger - * /api/destination-calendars/{id}: + * /v1/destination-calendars/{id}: * get: * summary: Get a destination calendar by ID * parameters: @@ -96,7 +96,7 @@ export async function destionationCalendarById( await prisma.destinationCalendar .findUnique({ where: { id: safeQuery.data.id } }) .then((data) => schemaDestinationCalendarPublic.parse(data)) - .then((data) => res.status(200).json({ data })) + .then((destination_calendar) => res.status(200).json({ destination_calendar })) .catch((error: Error) => res .status(404) @@ -111,8 +111,8 @@ export async function destionationCalendarById( where: { id: safeQuery.data.id }, data: safeBody.data, }) - .then((destinationCalendar) => schemaDestinationCalendarPublic.parse(destinationCalendar)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaDestinationCalendarPublic.parse(data)) + .then((destination_calendar) => res.status(200).json({ destination_calendar })) .catch((error: Error) => res .status(404) diff --git a/pages/api/destination-calendars/index.ts b/pages/api/destination-calendars/index.ts index b031b8a27e..3557272970 100644 --- a/pages/api/destination-calendars/index.ts +++ b/pages/api/destination-calendars/index.ts @@ -3,12 +3,15 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; -import { DestinationCalendarsResponse } from "@lib/types"; -import { schemaDestinationCalendarPublic } from "@lib/validations/destination-calendar"; +import { DestinationCalendarResponse, DestinationCalendarsResponse } from "@lib/types"; +import { + schemaDestinationCalendarBodyParams, + schemaDestinationCalendarPublic, +} from "@lib/validations/destination-calendar"; /** * @swagger - * /api/destination-calendars: + * /v1/destination-calendars: * get: * summary: Get all destination calendars * tags: @@ -19,24 +22,53 @@ import { schemaDestinationCalendarPublic } from "@lib/validations/destination-ca * 401: * description: Authorization information is missing or invalid. * 404: - * description: No destinationCalendars were found + * description: No destination calendars were found + * post: + * summary: Creates a new destination calendar + * tags: + * - destination-calendars + * responses: + * 201: + * description: OK, destination calendar created + * model: DestinationCalendar + * 400: + * description: Bad request. DestinationCalendar body is invalid. + * 401: + * description: Authorization information is missing or invalid. */ -async function allDestinationCalendars( - _: NextApiRequest, - res: NextApiResponse +async function createOrlistAllDestinationCalendars( + req: NextApiRequest, + res: NextApiResponse ) { - const destinationCalendars = await prisma.destinationCalendar.findMany(); - const data = destinationCalendars.map((destinationCalendar) => - schemaDestinationCalendarPublic.parse(destinationCalendar) - ); + const { method } = req; + if (method === "GET") { + const data = await prisma.destinationCalendar.findMany(); + const destination_calendars = data.map((destinationCalendar) => + schemaDestinationCalendarPublic.parse(destinationCalendar) + ); + if (data) res.status(200).json({ destination_calendars }); + else + (error: Error) => + res.status(404).json({ + message: "No DestinationCalendars were found", + error, + }); + } else if (method === "POST") { + const safe = schemaDestinationCalendarBodyParams.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 DestinationCalendars were found", - error, - }); + const data = await prisma.destinationCalendar.create({ data: safe.data }); + const destination_calendar = schemaDestinationCalendarPublic.parse(data); + + if (destination_calendar) + res.status(201).json({ destination_calendar, message: "DestinationCalendar created successfully" }); + else + (error: Error) => + res.status(400).json({ + message: "Could not create new destinationCalendar", + error, + }); + } else res.status(405).json({ message: `Method ${method} not allowed` }); } -export default withMiddleware("HTTP_GET")(allDestinationCalendars); +export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllDestinationCalendars); diff --git a/pages/api/destination-calendars/new.ts b/pages/api/destination-calendars/new.ts deleted file mode 100644 index e98472b216..0000000000 --- a/pages/api/destination-calendars/new.ts +++ /dev/null @@ -1,55 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { DestinationCalendarResponse } from "@lib/types"; -import { - schemaDestinationCalendarBodyParams, - schemaDestinationCalendarPublic, - withValidDestinationCalendar, -} from "@lib/validations/destination-calendar"; - -/** - * @swagger - * /api/destination-calendars/new: - * post: - * summary: Creates a new destination calendar - * requestBody: - * description: Optional description in *Markdown* - * required: true - * content: - * application/json: - * schema: - * $ref: '#/components/schemas/DestinationCalendar' - * tags: - * - destination-calendars - * responses: - * 201: - * description: OK, destinationCalendar created - * model: DestinationCalendar - * 400: - * description: Bad request. DestinationCalendar body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -async function createDestinationCalendar( - req: NextApiRequest, - res: NextApiResponse -) { - const safe = schemaDestinationCalendarBodyParams.safeParse(req.body); - if (!safe.success) throw new Error("Invalid request body", safe.error); - - const destinationCalendar = await prisma.destinationCalendar.create({ data: safe.data }); - const data = schemaDestinationCalendarPublic.parse(destinationCalendar); - - if (data) res.status(201).json({ data, message: "DestinationCalendar created successfully" }); - else - (error: Error) => - res.status(400).json({ - message: "Could not create new destinationCalendar", - error, - }); -} - -export default withMiddleware("HTTP_POST")(withValidDestinationCalendar(createDestinationCalendar)); diff --git a/pages/api/event-type-custom-inputs/[id].ts b/pages/api/event-type-custom-inputs/[id].ts index c737b5fb84..62b61d9c92 100644 --- a/pages/api/event-type-custom-inputs/[id].ts +++ b/pages/api/event-type-custom-inputs/[id].ts @@ -15,7 +15,7 @@ import { /** * @swagger - * /api/event-type-custom-inputs/{id}: + * /v1/event-type-custom-inputs/{id}: * get: * summary: Get a eventTypeCustomInput by ID * parameters: @@ -93,7 +93,7 @@ async function eventTypeById(req: NextApiRequest, res: NextApiResponse schemaEventTypeCustomInputPublic.parse(data)) - .then((data) => res.status(200).json({ data })) + .then((event_type_custom_input) => res.status(200).json({ event_type_custom_input })) .catch((error: Error) => res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error }) ); @@ -106,8 +106,8 @@ async function eventTypeById(req: NextApiRequest, res: NextApiResponse schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaEventTypeCustomInputPublic.parse(data)) + .then((event_type_custom_input) => res.status(200).json({ event_type_custom_input })) .catch((error: Error) => res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error }) ); diff --git a/pages/api/event-type-custom-inputs/index.ts b/pages/api/event-type-custom-inputs/index.ts index d1c8e3ad90..f988adc281 100644 --- a/pages/api/event-type-custom-inputs/index.ts +++ b/pages/api/event-type-custom-inputs/index.ts @@ -3,12 +3,15 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; -import { EventTypeCustomInputsResponse } from "@lib/types"; -import { schemaEventTypeCustomInputPublic } from "@lib/validations/event-type-custom-input"; +import { EventTypeCustomInputResponse, EventTypeCustomInputsResponse } from "@lib/types"; +import { + schemaEventTypeCustomInputBodyParams, + schemaEventTypeCustomInputPublic, +} from "@lib/validations/event-type-custom-input"; /** * @swagger - * /api/event-type-custom-inputs: + * /v1/event-type-custom-inputs: * get: * summary: Get all eventTypeCustomInputs * tags: @@ -20,23 +23,52 @@ import { schemaEventTypeCustomInputPublic } from "@lib/validations/event-type-cu * description: Authorization information is missing or invalid. * 404: * description: No eventTypeCustomInputs were found + * post: + * summary: Creates a new eventTypeCustomInput + * tags: + * - event-type-custom-inputs + * responses: + * 201: + * description: OK, eventTypeCustomInput created + * model: EventTypeCustomInput + * 400: + * description: Bad request. EventTypeCustomInput body is invalid. + * 401: + * description: Authorization information is missing or invalid. */ -async function allEventTypeCustomInputs( - _: NextApiRequest, - res: NextApiResponse +async function createOrlistAllEventTypeCustomInputs( + req: NextApiRequest, + res: NextApiResponse ) { - const eventTypeCustomInputs = await prisma.eventTypeCustomInput.findMany(); - const data = eventTypeCustomInputs.map((eventTypeCustomInput) => - schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput) - ); + const { method } = req; + if (method === "GET") { + const data = await prisma.eventTypeCustomInput.findMany(); + const event_type_custom_inputs = data.map((eventTypeCustomInput) => + schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput) + ); + if (event_type_custom_inputs) res.status(200).json({ event_type_custom_inputs }); + else + (error: Error) => + res.status(404).json({ + message: "No EventTypeCustomInputs were found", + error, + }); + } else if (method === "POST") { + const safe = schemaEventTypeCustomInputBodyParams.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 EventTypeCustomInputs were found", - error, - }); + const data = await prisma.eventTypeCustomInput.create({ data: safe.data }); + const event_type_custom_input = schemaEventTypeCustomInputPublic.parse(data); + + if (event_type_custom_input) + res.status(201).json({ event_type_custom_input, message: "EventTypeCustomInput created successfully" }); + else + (error: Error) => + res.status(400).json({ + message: "Could not create new eventTypeCustomInput", + error, + }); + } else res.status(405).json({ message: `Method ${method} not allowed` }); } -export default withMiddleware("HTTP_GET")(allEventTypeCustomInputs); +export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllEventTypeCustomInputs); diff --git a/pages/api/event-type-custom-inputs/new.ts b/pages/api/event-type-custom-inputs/new.ts deleted file mode 100644 index 3d64c296dd..0000000000 --- a/pages/api/event-type-custom-inputs/new.ts +++ /dev/null @@ -1,55 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { EventTypeCustomInputResponse } from "@lib/types"; -import { - schemaEventTypeCustomInputBodyParams, - schemaEventTypeCustomInputPublic, - withValidEventTypeCustomInput, -} from "@lib/validations/event-type-custom-input"; - -/** - * @swagger - * /api/event-type-custom-inputs/new: - * post: - * summary: Creates a new eventTypeCustomInput - * requestBody: - * description: Optional description in *Markdown* - * required: true - * content: - * application/json: - * schema: - * $ref: '#/components/schemas/EventTypeCustomInput' - * tags: - * - event-type-custom-inputs - * responses: - * 201: - * description: OK, eventTypeCustomInput created - * model: EventTypeCustomInput - * 400: - * description: Bad request. EventTypeCustomInput body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -async function createEventTypeCustomInput( - req: NextApiRequest, - res: NextApiResponse -) { - const safe = schemaEventTypeCustomInputBodyParams.safeParse(req.body); - if (!safe.success) throw new Error("Invalid request body", safe.error); - - const eventTypeCustomInput = await prisma.eventTypeCustomInput.create({ data: safe.data }); - const data = schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput); - - if (data) res.status(201).json({ data, message: "EventTypeCustomInput created successfully" }); - else - (error: Error) => - res.status(400).json({ - message: "Could not create new eventTypeCustomInput", - error, - }); -} - -export default withMiddleware("HTTP_POST")(withValidEventTypeCustomInput(createEventTypeCustomInput)); diff --git a/pages/api/event-types/[id].ts b/pages/api/event-types/[id].ts index dd441f5cd3..e3ffe87f9b 100644 --- a/pages/api/event-types/[id].ts +++ b/pages/api/event-types/[id].ts @@ -12,7 +12,7 @@ import { /** * @swagger - * /api/event-types/{id}: + * /v1/event-types/{id}: * get: * summary: Get a eventType by ID * parameters: @@ -90,7 +90,7 @@ export async function eventTypeById(req: NextApiRequest, res: NextApiResponse schemaEventTypePublic.parse(data)) - .then((data) => res.status(200).json({ data })) + .then((event_type) => res.status(200).json({ event_type })) .catch((error: Error) => res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error }) ); @@ -103,8 +103,8 @@ export async function eventTypeById(req: NextApiRequest, res: NextApiResponse schemaEventTypePublic.parse(eventType)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaEventTypePublic.parse(data)) + .then((event_type) => res.status(200).json({ event_type })) .catch((error: Error) => res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error }) ); diff --git a/pages/api/event-types/index.ts b/pages/api/event-types/index.ts index a626490919..724f0e9769 100644 --- a/pages/api/event-types/index.ts +++ b/pages/api/event-types/index.ts @@ -3,14 +3,14 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; -import { EventTypesResponse } from "@lib/types"; -import { schemaEventTypePublic } from "@lib/validations/event-type"; +import { EventTypeResponse, EventTypesResponse } from "@lib/types"; +import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validations/event-type"; /** * @swagger - * /api/eventTypes: + * /v1/event-types: * get: - * summary: Returns all eventTypes + * summary: Get all event types * tags: * - event-types * responses: @@ -19,19 +19,50 @@ import { schemaEventTypePublic } from "@lib/validations/event-type"; * 401: * description: Authorization information is missing or invalid. * 404: - * description: No eventTypes were found + * description: No event types were found + * post: + * summary: Creates a new event type + * tags: + * - event-types + * responses: + * 201: + * description: OK, event type created + * model: EventType + * 400: + * description: Bad request. EventType body is invalid. + * 401: + * description: Authorization information is missing or invalid. */ -async function allEventTypes(_: NextApiRequest, res: NextApiResponse) { - const eventTypes = await prisma.eventType.findMany(); - const data = eventTypes.map((eventType) => schemaEventTypePublic.parse(eventType)); +async function createOrlistAllEventTypes( + req: NextApiRequest, + res: NextApiResponse +) { + const { method } = req; + if (method === "GET") { + const data = await prisma.eventType.findMany(); + const event_types = data.map((eventType) => schemaEventTypePublic.parse(eventType)); + if (event_types) res.status(200).json({ event_types }); + else + (error: Error) => + res.status(404).json({ + message: "No EventTypes were found", + error, + }); + } else if (method === "POST") { + const safe = schemaEventTypeBodyParams.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 EventTypes were found", - error, - }); + const data = await prisma.eventType.create({ data: safe.data }); + const event_type = schemaEventTypePublic.parse(data); + + if (data) res.status(201).json({ event_type, message: "EventType created successfully" }); + else + (error: Error) => + res.status(400).json({ + message: "Could not create new event type", + error, + }); + } else res.status(405).json({ message: `Method ${method} not allowed` }); } -export default withMiddleware("HTTP_GET")(allEventTypes); +export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllEventTypes); diff --git a/pages/api/event-types/new.ts b/pages/api/event-types/new.ts deleted file mode 100644 index 491057c453..0000000000 --- a/pages/api/event-types/new.ts +++ /dev/null @@ -1,45 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { EventTypeResponse } from "@lib/types"; -import { - schemaEventTypeBodyParams, - schemaEventTypePublic, - withValidEventType, -} from "@lib/validations/event-type"; - -/** - * @swagger - * /api/event-types/new: - * post: - * summary: Creates a new eventType - * tags: - * - event-types - * responses: - * 201: - * description: OK, eventType created - * model: EventType - * 400: - * description: Bad request. EventType body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -async function createEventType(req: NextApiRequest, res: NextApiResponse) { - const safe = schemaEventTypeBodyParams.safeParse(req.body); - if (!safe.success) throw new Error("Invalid request body", safe.error); - - const eventType = await prisma.eventType.create({ data: safe.data }); - const data = schemaEventTypePublic.parse(eventType); - - if (data) res.status(201).json({ data, message: "EventType created successfully" }); - else - (error: Error) => - res.status(400).json({ - message: "Could not create new eventType", - error, - }); -} - -export default withMiddleware("HTTP_POST")(withValidEventType(createEventType)); diff --git a/pages/api/memberships/[id].ts b/pages/api/memberships/[id].ts index 61f78cc6e8..a963c023f5 100644 --- a/pages/api/memberships/[id].ts +++ b/pages/api/memberships/[id].ts @@ -9,7 +9,7 @@ import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/ /** * @swagger - * /api/memberships/{userId}_{teamId}: + * /v1/memberships/{userId}_{teamId}: * get: * summary: Get a membership by userID and teamID * parameters: @@ -106,8 +106,8 @@ export async function membershipById(req: NextApiRequest, res: NextApiResponse schemaMembershipPublic.parse(membership)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaMembershipPublic.parse(data)) + .then((membership) => res.status(200).json({ membership })) .catch((error: Error) => res.status(404).json({ message: `Membership with id: ${safeQuery.data.id} not found`, error }) ); @@ -120,8 +120,8 @@ export async function membershipById(req: NextApiRequest, res: NextApiResponse schemaMembershipPublic.parse(membership)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaMembershipPublic.parse(data)) + .then((membership) => res.status(200).json({ membership })) .catch((error: Error) => res.status(404).json({ message: `Membership with id: ${safeQuery.data.id} not found`, error }) ); diff --git a/pages/api/memberships/index.ts b/pages/api/memberships/index.ts index d53670ce92..54b6e70c16 100644 --- a/pages/api/memberships/index.ts +++ b/pages/api/memberships/index.ts @@ -3,14 +3,14 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; -import { MembershipsResponse } from "@lib/types"; -import { schemaMembershipPublic } from "@lib/validations/membership"; +import { MembershipResponse, MembershipsResponse } from "@lib/types"; +import { schemaMembershipBodyParams, schemaMembershipPublic } from "@lib/validations/membership"; /** * @swagger - * /api/memberships: + * /v1/memberships: * get: - * summary: Returns all memberships + * summary: Get all memberships * tags: * - memberships * responses: @@ -20,18 +20,49 @@ import { schemaMembershipPublic } from "@lib/validations/membership"; * description: Authorization information is missing or invalid. * 404: * description: No memberships were found + * post: + * summary: Creates a new membership + * tags: + * - memberships + * responses: + * 201: + * description: OK, membership created + * model: Membership + * 400: + * description: Bad request. Membership body is invalid. + * 401: + * description: Authorization information is missing or invalid. */ -async function allMemberships(_: NextApiRequest, res: NextApiResponse) { - const memberships = await prisma.membership.findMany(); - const data = memberships.map((membership) => schemaMembershipPublic.parse(membership)); +async function createOrlistAllMemberships( + req: NextApiRequest, + res: NextApiResponse +) { + const { method } = req; + if (method === "GET") { + const data = await prisma.membership.findMany(); + const memberships = data.map((membership) => schemaMembershipPublic.parse(membership)); + if (memberships) res.status(200).json({ memberships }); + else + (error: Error) => + res.status(404).json({ + message: "No Memberships were found", + error, + }); + } else if (method === "POST") { + const safe = schemaMembershipBodyParams.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 Memberships were found", - error, - }); + const data = await prisma.membership.create({ data: safe.data }); + const membership = schemaMembershipPublic.parse(data); + + if (membership) res.status(201).json({ membership, message: "Membership created successfully" }); + else + (error: Error) => + res.status(400).json({ + message: "Could not create new membership", + error, + }); + } else res.status(405).json({ message: `Method ${method} not allowed` }); } -export default withMiddleware("HTTP_GET")(allMemberships); +export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllMemberships); diff --git a/pages/api/memberships/new.ts b/pages/api/memberships/new.ts deleted file mode 100644 index e3b9dbe02f..0000000000 --- a/pages/api/memberships/new.ts +++ /dev/null @@ -1,45 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { MembershipResponse } from "@lib/types"; -import { - schemaMembershipBodyParams, - schemaMembershipPublic, - withValidMembership, -} from "@lib/validations/membership"; - -/** - * @swagger - * /api/memberships/new: - * post: - * summary: Creates a new membership - * tags: - * - memberships - * responses: - * 201: - * description: OK, membership created - * model: Membership - * 400: - * description: Bad request. Membership body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -async function createMembership(req: NextApiRequest, res: NextApiResponse) { - const safe = schemaMembershipBodyParams.safeParse(req.body); - if (!safe.success) throw new Error("Invalid request body", safe.error); - - const membership = await prisma.membership.create({ data: safe.data }); - const data = schemaMembershipPublic.parse(membership); - - if (data) res.status(201).json({ data, message: "Membership created successfully" }); - else - (error: Error) => - res.status(400).json({ - message: "Could not create new membership", - error, - }); -} - -export default withMiddleware("HTTP_POST")(withValidMembership(createMembership)); diff --git a/pages/api/payments/[id].ts b/pages/api/payments/[id].ts index bdd8766c69..cdedf473ec 100644 --- a/pages/api/payments/[id].ts +++ b/pages/api/payments/[id].ts @@ -13,7 +13,7 @@ import { /** * @swagger - * /api/payments/{id}: + * /v1/payments/{id}: * get: * summary: Get one of your own payments by ID * parameters: @@ -45,10 +45,10 @@ export async function paymentById(req: NextApiRequest, res: NextApiResponse schemaPaymentPublic.parse(payment)) - .then((data) => { - if (userWithBookings?.bookings.map((b) => b.id).includes(data.bookingId)) { - res.status(200).json({ data }); + .then((data) => schemaPaymentPublic.parse(data)) + .then((payment) => { + if (userWithBookings?.bookings.map((b) => b.id).includes(payment.bookingId)) { + res.status(200).json({ payment }); } else { res.status(401).json({ message: "Unauthorized" }); } diff --git a/pages/api/payments/index.ts b/pages/api/payments/index.ts index 73b32a7d1c..955cff0725 100644 --- a/pages/api/payments/index.ts +++ b/pages/api/payments/index.ts @@ -9,7 +9,7 @@ import { schemaPaymentPublic } from "@lib/validations/payment"; /** * @swagger - * /api/payments: + * /v1/payments: * get: * summary: Get all payments * tags: @@ -32,10 +32,10 @@ async function allPayments(_: NextApiRequest, res: NextApiResponse booking.id); - const payments = await prisma.payment.findMany({ where: { bookingId: { in: bookingIds } } }); - const data = payments.map((payment) => schemaPaymentPublic.parse(payment)); + const data = await prisma.payment.findMany({ where: { bookingId: { in: bookingIds } } }); + const payments = data.map((payment) => schemaPaymentPublic.parse(payment)); - if (data) res.status(200).json({ data }); + if (payments) res.status(200).json({ payments }); else (error: Error) => res.status(404).json({ @@ -43,5 +43,5 @@ async function allPayments(_: NextApiRequest, res: NextApiResponse schemaReminderMailPublic.parse(data)) - .then((data) => res.status(200).json({ data })) + .then((reminder_mail) => res.status(200).json({ reminder_mail })) .catch((error: Error) => res.status(404).json({ message: `ReminderMail with id: ${safeQuery.data.id} not found`, error }) ); @@ -104,7 +104,7 @@ export async function reminderMailById(req: NextApiRequest, res: NextApiResponse data: safeBody.data, }) .then((reminderMail) => schemaReminderMailPublic.parse(reminderMail)) - .then((data) => res.status(200).json({ data })) + .then((reminder_mail) => res.status(200).json({ reminder_mail })) .catch((error: Error) => res.status(404).json({ message: `ReminderMail with id: ${safeQuery.data.id} not found`, error }) ); diff --git a/pages/api/reminder-mails/index.ts b/pages/api/reminder-mails/index.ts index be7025ba21..2c743c0474 100644 --- a/pages/api/reminder-mails/index.ts +++ b/pages/api/reminder-mails/index.ts @@ -43,9 +43,9 @@ async function createOrlistAllReminderMails( ) { const { method } = req; if (method === "GET") { - const reminderMails = await prisma.reminderMail.findMany(); - const data = reminderMails.map((reminderMail) => schemaReminderMailPublic.parse(reminderMail)); - if (data) res.status(200).json({ data }); + const data = await prisma.reminderMail.findMany(); + const reminder_mails = data.map((reminderMail) => schemaReminderMailPublic.parse(reminderMail)); + if (reminder_mails) res.status(200).json({ reminder_mails }); else (error: Error) => res.status(404).json({ @@ -56,10 +56,10 @@ async function createOrlistAllReminderMails( const safe = schemaReminderMailBodyParams.safeParse(req.body); if (!safe.success) throw new Error("Invalid request body"); - const reminderMail = await prisma.reminderMail.create({ data: safe.data }); - const data = schemaReminderMailPublic.parse(reminderMail); + const data = await prisma.reminderMail.create({ data: safe.data }); + const reminder_mail = schemaReminderMailPublic.parse(data); - if (data) res.status(201).json({ data, message: "reminder mail created successfully" }); + if (reminder_mail) res.status(201).json({ reminder_mail, message: "reminder mail created successfully" }); else (error: Error) => res.status(400).json({ diff --git a/pages/api/schedules/[id].ts b/pages/api/schedules/[id].ts index 162f7f9542..c75034e5d1 100644 --- a/pages/api/schedules/[id].ts +++ b/pages/api/schedules/[id].ts @@ -89,8 +89,8 @@ export async function scheduleById(req: NextApiRequest, res: NextApiResponse schemaSchedulePublic.parse(schedule)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaSchedulePublic.parse(data)) + .then((schedule) => res.status(200).json({ schedule })) .catch((error: Error) => res.status(404).json({ message: `Schedule with id: ${safeQuery.data.id} not found`, error }) ); @@ -103,8 +103,8 @@ export async function scheduleById(req: NextApiRequest, res: NextApiResponse schemaSchedulePublic.parse(schedule)) - .then((data) => res.status(200).json({ data })) + .then((data) => schemaSchedulePublic.parse(data)) + .then((schedule) => res.status(200).json({ schedule })) .catch((error: Error) => res.status(404).json({ message: `Schedule with id: ${safeQuery.data.id} not found`, error }) ); diff --git a/pages/api/schedules/index.ts b/pages/api/schedules/index.ts index e3b178d216..f8eedf599f 100644 --- a/pages/api/schedules/index.ts +++ b/pages/api/schedules/index.ts @@ -39,9 +39,9 @@ async function createOrlistAllSchedules( ) { const { method } = req; if (method === "GET") { - const schedules = await prisma.schedule.findMany(); - const data = schedules.map((schedule) => schemaSchedulePublic.parse(schedule)); - if (data) res.status(200).json({ data }); + const data = await prisma.schedule.findMany(); + const schedules = data.map((schedule) => schemaSchedulePublic.parse(schedule)); + if (schedules) res.status(200).json({ schedules }); else (error: Error) => res.status(404).json({ @@ -52,10 +52,10 @@ async function createOrlistAllSchedules( const safe = schemaScheduleBodyParams.safeParse(req.body); if (!safe.success) throw new Error("Invalid request body"); - const schedule = await prisma.schedule.create({ data: safe.data }); - const data = schemaSchedulePublic.parse(schedule); + const data = await prisma.schedule.create({ data: safe.data }); + const schedule = schemaSchedulePublic.parse(data); - if (data) res.status(201).json({ data, message: "Schedule created successfully" }); + if (schedule) res.status(201).json({ schedule, message: "Schedule created successfully" }); else (error: Error) => res.status(400).json({ diff --git a/templates/endpoints/[id]/delete.ts b/templates/endpoints/[id]/delete.ts index a9816a3ab6..2b59baee90 100644 --- a/templates/endpoints/[id]/delete.ts +++ b/templates/endpoints/[id]/delete.ts @@ -11,7 +11,7 @@ import { /** * @swagger - * /api/resources/{id}/delete: + * /v1/resources/{id}/delete: * delete: * summary: Remove an existing resource * parameters: diff --git a/templates/endpoints/[id]/edit.ts b/templates/endpoints/[id]/edit.ts index 2be7bd06c1..0e73604fb4 100644 --- a/templates/endpoints/[id]/edit.ts +++ b/templates/endpoints/[id]/edit.ts @@ -4,15 +4,15 @@ import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; import type { ResourceResponse } from "@lib/types"; +import { schemaResourceBodyParams, schemaResourcePublic, withValidResource } from "@lib/validations/resource"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt, } from "@lib/validations/shared/queryIdTransformParseInt"; -import { schemaResourceBodyParams, schemaResourcePublic, withValidResource } from "@lib/validations/resource"; /** * @swagger - * /api/resources/{id}/edit: + * /v1/resources/{id}/edit: * patch: * summary: Edit an existing resource * parameters: @@ -53,4 +53,6 @@ export async function editResource(req: NextApiRequest, res: NextApiResponse