Scheduling infrastructure for absolutely everyone.
payments safe, unify endpoints for remindermails schedules
Agusti Fernandez Pardod95325c06c75
diff --git a/pages/api/attendees/[id].ts b/pages/api/attendees/[id].tsindex 502fb5d1c57..8152512d5c6 100644--- a/pages/api/attendees/[id].ts+++ b/pages/api/attendees/[id].ts@@ -81,8 +81,8 @@ import {*/export async function attendeeById(req: NextApiRequest, res: NextApiResponse<AttendeeResponse>) {const { method, query, body } = req;- const safeQuery = await schemaQueryIdParseInt.safeParse(query);- const safeBody = await schemaAttendeeBodyParams.safeParse(body);+ const safeQuery = schemaQueryIdParseInt.safeParse(query);+ const safeBody = schemaAttendeeBodyParams.safeParse(body);if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);switch (method) {diff --git a/pages/api/availabilities/[id].ts b/pages/api/availabilities/[id].tsindex be9d9eb9870..5e9e243e543 100644--- a/pages/api/availabilities/[id].ts+++ b/pages/api/availabilities/[id].ts@@ -81,8 +81,8 @@ import {*/export async function availabilityById(req: NextApiRequest, res: NextApiResponse<AvailabilityResponse>) {const { method, query, body } = req;- const safeQuery = await schemaQueryIdParseInt.safeParse(query);- const safeBody = await schemaAvailabilityBodyParams.safeParse(body);+ const safeQuery = schemaQueryIdParseInt.safeParse(query);+ const safeBody = schemaAvailabilityBodyParams.safeParse(body);if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);switch (method) {diff --git a/pages/api/booking-references/[id].ts b/pages/api/booking-references/[id].tsindex 8c804321e02..216d1282ff1 100644--- a/pages/api/booking-references/[id].ts+++ b/pages/api/booking-references/[id].ts@@ -87,8 +87,8 @@ export async function bookingReferenceById(res: NextApiResponse<BookingReferenceResponse>) {const { method, query, body } = req;- const safeQuery = await schemaQueryIdParseInt.safeParse(query);- const safeBody = await schemaBookingReferenceBodyParams.safeParse(body);+ const safeQuery = schemaQueryIdParseInt.safeParse(query);+ const safeBody = schemaBookingReferenceBodyParams.safeParse(body);if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);switch (method) {diff --git a/pages/api/bookings/[id].ts b/pages/api/bookings/[id].tsindex 57b817464d5..852b32b9838 100644--- a/pages/api/bookings/[id].ts+++ b/pages/api/bookings/[id].ts@@ -81,8 +81,8 @@ import {*/export async function bookingById(req: NextApiRequest, res: NextApiResponse<BookingResponse>) {const { method, query, body } = req;- const safeQuery = await schemaQueryIdParseInt.safeParse(query);- const safeBody = await schemaBookingBodyParams.safeParse(body);+ const safeQuery = schemaQueryIdParseInt.safeParse(query);+ const safeBody = schemaBookingBodyParams.safeParse(body);if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);switch (method) {diff --git a/pages/api/credentials/[id].ts b/pages/api/credentials/[id].tsindex cc48ffab6ab..16c60007a47 100644--- a/pages/api/credentials/[id].ts+++ b/pages/api/credentials/[id].ts@@ -81,8 +81,8 @@ import {*/export async function credentialById(req: NextApiRequest, res: NextApiResponse<CredentialResponse>) {const { method, query, body } = req;- const safeQuery = await schemaQueryIdParseInt.safeParse(query);- const safeBody = await schemaCredentialBodyParams.safeParse(body);+ const safeQuery = schemaQueryIdParseInt.safeParse(query);+ const safeBody = schemaCredentialBodyParams.safeParse(body);if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);switch (method) {diff --git a/pages/api/daily-event-references/[id].ts b/pages/api/daily-event-references/[id].tsindex 21072a7a4e8..d150cf69ce2 100644--- a/pages/api/daily-event-references/[id].ts+++ b/pages/api/daily-event-references/[id].ts@@ -87,8 +87,8 @@ export async function dailyEventReferenceById(res: NextApiResponse<DailyEventReferenceResponse>) {const { method, query, body } = req;- const safeQuery = await schemaQueryIdParseInt.safeParse(query);- const safeBody = await schemaDailyEventReferenceBodyParams.safeParse(body);+ const safeQuery = schemaQueryIdParseInt.safeParse(query);+ const safeBody = schemaDailyEventReferenceBodyParams.safeParse(body);if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);switch (method) {diff --git a/pages/api/destination-calendars/[id].ts b/pages/api/destination-calendars/[id].tsindex a08406f9fa1..16c5fba7bea 100644--- a/pages/api/destination-calendars/[id].ts+++ b/pages/api/destination-calendars/[id].ts@@ -87,8 +87,8 @@ export async function destionationCalendarById(res: NextApiResponse<DestinationCalendarResponse>) {const { method, query, body } = req;- const safeQuery = await schemaQueryIdParseInt.safeParse(query);- const safeBody = await schemaDestinationCalendarBodyParams.safeParse(body);+ const safeQuery = schemaQueryIdParseInt.safeParse(query);+ const safeBody = schemaDestinationCalendarBodyParams.safeParse(body);if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);switch (method) {diff --git a/pages/api/event-type-custom-inputs/[id].ts b/pages/api/event-type-custom-inputs/[id].tsindex 8b5d29ed8f7..85b2df0ccd5 100644--- a/pages/api/event-type-custom-inputs/[id].ts+++ b/pages/api/event-type-custom-inputs/[id].ts@@ -84,8 +84,8 @@ import {*/async function eventTypeById(req: NextApiRequest, res: NextApiResponse<EventTypeCustomInputResponse>) {const { method, query, body } = req;- const safeQuery = await schemaQueryIdParseInt.safeParse(query);- const safeBody = await schemaEventTypeCustomInputBodyParams.safeParse(body);+ const safeQuery = schemaQueryIdParseInt.safeParse(query);+ const safeBody = schemaEventTypeCustomInputBodyParams.safeParse(body);if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);switch (method) {diff --git a/pages/api/event-types/[id].ts b/pages/api/event-types/[id].tsindex bf94bcdfa00..dd441f5cd32 100644--- a/pages/api/event-types/[id].ts+++ b/pages/api/event-types/[id].ts@@ -81,8 +81,8 @@ import {*/export async function eventTypeById(req: NextApiRequest, res: NextApiResponse<EventTypeResponse>) {const { method, query, body } = req;- const safeQuery = await schemaQueryIdParseInt.safeParse(query);- const safeBody = await schemaEventTypeBodyParams.safeParse(body);+ const safeQuery = schemaQueryIdParseInt.safeParse(query);+ const safeBody = schemaEventTypeBodyParams.safeParse(body);if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);switch (method) {diff --git a/pages/api/memberships/[id].ts b/pages/api/memberships/[id].tsindex d78d0f1c883..61f78cc6e8f 100644--- a/pages/api/memberships/[id].ts+++ b/pages/api/memberships/[id].ts@@ -96,8 +96,8 @@ import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/*/export async function membershipById(req: NextApiRequest, res: NextApiResponse<MembershipResponse>) {const { method, query, body } = req;- const safeQuery = await schemaQueryIdAsString.safeParse(query);- const safeBody = await schemaMembershipBodyParams.safeParse(body);+ const safeQuery = schemaQueryIdAsString.safeParse(query);+ const safeBody = schemaMembershipBodyParams.safeParse(body);if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);// This is how we set the userId and teamId in the query for managing compoundId.const [userId, teamId] = safeQuery.data.id.split("_");diff --git a/pages/api/payments/[id].ts b/pages/api/payments/[id].tsindex 7637bb724d4..e026e62dc52 100644--- a/pages/api/payments/[id].ts+++ b/pages/api/payments/[id].ts@@ -4,7 +4,8 @@ import prisma from "@calcom/prisma";import { withMiddleware } from "@lib/helpers/withMiddleware";import type { PaymentResponse } from "@lib/types";-import { schemaPaymentBodyParams, schemaPaymentPublic } from "@lib/validations/payment";+import { getCalcomUserId } from "@lib/utils/getCalcomUserId";+import { schemaPaymentPublic } from "@lib/validations/payment";import {schemaQueryIdParseInt,withValidQueryIdTransformParseInt,@@ -14,7 +15,7 @@ import {* @swagger* /api/payments/{id}:* get:- * summary: Get an payment by ID+ * summary: Get a payment by ID* parameters:* - in: path* name: id@@ -31,100 +32,33 @@ import {* description: Authorization information is missing or invalid.* 404:* description: Payment was not found- * patch:- * summary: Edit an existing payment- * consumes:- * - application/json- * parameters:- * - in: body- * name: payment- * description: The payment to edit- * schema:- * type: object- * $ref: '#/components/schemas/Payment'- * required: true- * - in: path- * name: id- * schema:- * type: integer- * required: true- * description: Numeric ID of the payment to edit- * tags:- * - payments- * responses:- * 201:- * description: OK, payment edited successfuly- * model: Payment- * 400:- * description: Bad request. Payment body is invalid.- * 401:- * description: Authorization information is missing or invalid.- * delete:- * summary: Remove an existing payment- * parameters:- * - in: path- * name: id- * schema:- * type: integer- * required: true- * description: Numeric ID of the payment to delete- * tags:- * - payments- * responses:- * 201:- * description: OK, payment removed successfuly- * model: Payment- * 400:- * description: Bad request. Payment id is invalid.- * 401:- * description: Authorization information is missing or invalid.*/export async function paymentById(req: NextApiRequest, res: NextApiResponse<PaymentResponse>) {- const { method, query, body } = req;- const safeQuery = await schemaQueryIdParseInt.safeParse(query);- const safeBody = await schemaPaymentBodyParams.safeParse(body);- if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);+ const { method, query } = req;+ const safeQuery = schemaQueryIdParseInt.safeParse(query);+ const userId = await getCalcomUserId(res);- switch (method) {- case "GET":- await prisma.payment- .findUnique({ where: { id: safeQuery.data.id } })- .then((payment) => schemaPaymentPublic.parse(payment))- .then((data) => res.status(200).json({ data }))- .catch((error: Error) =>- res.status(404).json({ message: `Payment with id: ${safeQuery.data.id} not found`, error })- );- break;-- case "PATCH":- if (!safeBody.success) throw new Error("Invalid request body");- await prisma.payment- .update({- where: { id: safeQuery.data.id },- data: safeBody.data,+ if (safeQuery.success && method === "GET") {+ const userWithBookings = await prisma.user.findUnique({+ where: { id: userId },+ include: { bookings: true },+ });+ await prisma.payment+ .findUnique({ where: { id: safeQuery.data.id } })+ .then((payment) => schemaPaymentPublic.parse(payment))+ .then((data) => {+ if (userWithBookings?.bookings.map((b) => b.id).includes(data.bookingId)) {+ res.status(200).json({ data });+ } else {+ res.status(401).json({ message: "Unauthorized" });+ }+ })+ .catch((error: Error) =>+ res.status(404).json({+ message: `Payment with id: ${safeQuery.data.id} not found`,+ error,})- .then((payment) => schemaPaymentPublic.parse(payment))- .then((data) => res.status(200).json({ data }))- .catch((error: Error) =>- res.status(404).json({ message: `Payment with id: ${safeQuery.data.id} not found`, error })- );- break;-- case "DELETE":- await prisma.payment- .delete({ where: { id: safeQuery.data.id } })- .then(() =>- res.status(200).json({ message: `Payment with id: ${safeQuery.data.id} deleted successfully` })- )- .catch((error: Error) =>- res.status(404).json({ message: `Payment with id: ${safeQuery.data.id} not found`, error })- );- break;-- default:- res.status(405).json({ message: "Method not allowed" });- break;+ );}}--export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(paymentById));+export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(paymentById));diff --git a/pages/api/payments/index.ts b/pages/api/payments/index.tsindex ccffac11616..73b32a7d1cb 100644--- a/pages/api/payments/index.ts+++ b/pages/api/payments/index.ts@@ -4,6 +4,7 @@ import prisma from "@calcom/prisma";import { withMiddleware } from "@lib/helpers/withMiddleware";import { PaymentsResponse } from "@lib/types";+import { getCalcomUserId } from "@lib/utils/getCalcomUserId";import { schemaPaymentPublic } from "@lib/validations/payment";/**@@ -22,7 +23,16 @@ import { schemaPaymentPublic } from "@lib/validations/payment";* description: No payments were found*/async function allPayments(_: NextApiRequest, res: NextApiResponse<PaymentsResponse>) {- const payments = await prisma.payment.findMany();+ const userId = await getCalcomUserId(res);++ const userWithBookings = await prisma.user.findUnique({+ where: { id: userId },+ include: { bookings: true },+ });+ if (!userWithBookings) throw new Error("No user found");+ const bookings = userWithBookings.bookings;+ const bookingIds = bookings.map((booking) => booking.id);+ const payments = await prisma.payment.findMany({ where: { bookingId: { in: bookingIds } } });const data = payments.map((payment) => schemaPaymentPublic.parse(payment));if (data) res.status(200).json({ data });diff --git a/pages/api/payments/new.ts b/pages/api/payments/new.tsdeleted file mode 100644index 1886270fe2b..00000000000--- a/pages/api/payments/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 { PaymentResponse } from "@lib/types";-import { schemaPaymentBodyParams, schemaPaymentPublic, withValidPayment } from "@lib/validations/payment";--/**- * @swagger- * /api/payments/new:- * post:- * summary: Creates a new payment- * requestBody:- * description: Optional description in *Markdown*- * required: true- * content:- * application/json:- * schema:- * $ref: '#/components/schemas/Payment'- * tags:- * - payments- * responses:- * 201:- * description: OK, payment created- * model: Payment- * 400:- * description: Bad request. Payment body is invalid.- * 401:- * description: Authorization information is missing or invalid.- */-async function createPayment(req: NextApiRequest, res: NextApiResponse<PaymentResponse>) {- const safe = schemaPaymentBodyParams.safeParse(req.body);- if (!safe.success) throw new Error("Invalid request body", safe.error);-- const payment = await prisma.payment.create({ data: safe.data });- const data = schemaPaymentPublic.parse(payment);-- if (data) res.status(201).json({ data, message: "Payment created successfully" });- else- (error: Error) =>- res.status(400).json({- message: "Could not create new payment",- error,- });-}--export default withMiddleware("HTTP_POST")(withValidPayment(createPayment));diff --git a/pages/api/reminder-mails/[id].ts b/pages/api/reminder-mails/[id].tsindex 5d101b4c6d9..543b84de9bc 100644--- a/pages/api/reminder-mails/[id].ts+++ b/pages/api/reminder-mails/[id].ts@@ -81,8 +81,8 @@ import {*/export async function reminderMailById(req: NextApiRequest, res: NextApiResponse<ReminderMailResponse>) {const { method, query, body } = req;- const safeQuery = await schemaQueryIdParseInt.safeParse(query);- const safeBody = await schemaReminderMailBodyParams.safeParse(body);+ const safeQuery = schemaQueryIdParseInt.safeParse(query);+ const safeBody = schemaReminderMailBodyParams.safeParse(body);if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);switch (method) {diff --git a/pages/api/reminder-mails/index.ts b/pages/api/reminder-mails/index.tsindex 3a847def6b4..be7025ba216 100644--- a/pages/api/reminder-mails/index.ts+++ b/pages/api/reminder-mails/index.ts@@ -3,12 +3,16 @@ import type { NextApiRequest, NextApiResponse } from "next";import prisma from "@calcom/prisma";import { withMiddleware } from "@lib/helpers/withMiddleware";-import { ReminderMailsResponse } from "@lib/types";-import { schemaReminderMailPublic } from "@lib/validations/reminder-mail";+import { ReminderMailResponse, ReminderMailsResponse } from "@lib/types";+import {+ schemaReminderMailBodyParams,+ schemaReminderMailPublic,+ withValidReminderMail,+} from "@lib/validations/reminder-mail";/*** @swagger- * /api/reminder-mails:+ * /v1/reminder-mails:* get:* summary: Get all reminder mails* tags:@@ -19,19 +23,50 @@ import { schemaReminderMailPublic } from "@lib/validations/reminder-mail";* 401:* description: Authorization information is missing or invalid.* 404:- * description: No reminderMails were found+ * description: No reminder mails were found+ * post:+ * summary: Creates a new reminder mail+ * tags:+ * - reminder-mails+ * responses:+ * 201:+ * description: OK, reminder mail created+ * model: ReminderMail+ * 400:+ * description: Bad request. ReminderMail body is invalid.+ * 401:+ * description: Authorization information is missing or invalid.*/-async function allReminderMails(_: NextApiRequest, res: NextApiResponse<ReminderMailsResponse>) {- const reminderMails = await prisma.reminderMail.findMany();- const data = reminderMails.map((reminderMail) => schemaReminderMailPublic.parse(reminderMail));+async function createOrlistAllReminderMails(+ req: NextApiRequest,+ res: NextApiResponse<ReminderMailsResponse | ReminderMailResponse>+) {+ 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 });+ else+ (error: Error) =>+ res.status(404).json({+ message: "No ReminderMails were found",+ error,+ });+ } else if (method === "POST") {+ 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);- if (data) res.status(200).json({ data });- else- (error: Error) =>- res.status(404).json({- message: "No ReminderMails were found",- error,- });+ if (data) res.status(201).json({ data, message: "reminder mail created successfully" });+ else+ (error: Error) =>+ res.status(400).json({+ message: "could not create new reminder mail",+ error,+ });+ } else res.status(405).json({ message: `Method ${method} not allowed` });}-export default withMiddleware("HTTP_GET")(allReminderMails);+export default withMiddleware("HTTP_GET_OR_POST")(withValidReminderMail(createOrlistAllReminderMails));diff --git a/pages/api/reminder-mails/new.ts b/pages/api/reminder-mails/new.tsdeleted file mode 100644index dd82b7a78e6..00000000000--- a/pages/api/reminder-mails/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 { ReminderMailResponse } from "@lib/types";-import {- schemaReminderMailBodyParams,- schemaReminderMailPublic,- withValidReminderMail,-} from "@lib/validations/reminder-mail";--/**- * @swagger- * /api/reminder-mails/new:- * post:- * summary: Creates a new reminder mail- * requestBody:- * description: Optional description in *Markdown*- * required: true- * content:- * application/json:- * schema:- * $ref: '#/components/schemas/ReminderMail'- * tags:- * - reminder-mails- * responses:- * 201:- * description: OK, reminderMail created- * model: ReminderMail- * 400:- * description: Bad request. ReminderMail body is invalid.- * 401:- * description: Authorization information is missing or invalid.- */-async function createReminderMail(req: NextApiRequest, res: NextApiResponse<ReminderMailResponse>) {- const safe = schemaReminderMailBodyParams.safeParse(req.body);- if (!safe.success) throw new Error("Invalid request body", safe.error);-- const reminderMail = await prisma.reminderMail.create({ data: safe.data });- const data = schemaReminderMailPublic.parse(reminderMail);-- if (data) res.status(201).json({ data, message: "ReminderMail created successfully" });- else- (error: Error) =>- res.status(400).json({- message: "Could not create new reminderMail",- error,- });-}--export default withMiddleware("HTTP_POST")(withValidReminderMail(createReminderMail));diff --git a/pages/api/reminder-mails/newIndex.ts b/pages/api/reminder-mails/newIndex.tsdeleted file mode 100644index 732eb013b27..00000000000--- a/pages/api/reminder-mails/newIndex.ts+++ /dev/null@@ -1,68 +0,0 @@-import type { NextApiRequest, NextApiResponse } from "next";--import prisma from "@calcom/prisma";--import { withMiddleware } from "@lib/helpers/withMiddleware";-import { PaymentResponse, PaymentsResponse } from "@lib/types";-import { schemaPaymentBodyParams, schemaPaymentPublic, withValidPayment } from "@lib/validations/payment";--/**- * @swagger- * /api/payments:- * get:- * summary: Get all payments- * tags:- * - payments- * responses:- * 200:- * description: OK- * 401:- * description: Authorization information is missing or invalid.- * 404:- * description: No payments were found- * post:- * summary: Creates a new payment- * tags:- * - payments- * responses:- * 201:- * description: OK, payment created- * model: Payment- * 400:- * description: Bad request. Payment body is invalid.- * 401:- * description: Authorization information is missing or invalid.- */-async function createOrlistAllPayments(- req: NextApiRequest,- res: NextApiResponse<PaymentsResponse | PaymentResponse>-) {- const { method } = req;- if (method === "GET") {- const payments = await prisma.payment.findMany();- const data = payments.map((payment) => schemaPaymentPublic.parse(payment));- if (data) res.status(200).json({ data });- else- (error: Error) =>- res.status(404).json({- message: "No Payments were found",- error,- });- } else if (method === "POST") {- const safe = schemaPaymentBodyParams.safeParse(req.body);- if (!safe.success) throw new Error("Invalid request body");-- const payment = await prisma.payment.create({ data: safe.data });- const data = schemaPaymentPublic.parse(payment);-- if (data) res.status(201).json({ data, message: "Payment created successfully" });- else- (error: Error) =>- res.status(400).json({- message: "Could not create new payment",- error,- });- } else res.status(405).json({ message: `Method ${method} not allowed` });-}--export default withMiddleware("HTTP_GET_OR_POST")(withValidPayment(createOrlistAllPayments));diff --git a/pages/api/schedules/[id].ts b/pages/api/schedules/[id].tsindex 682dc334d21..162f7f95422 100644--- a/pages/api/schedules/[id].ts+++ b/pages/api/schedules/[id].ts@@ -12,7 +12,7 @@ import {/*** @swagger- * /api/schedules/{id}:+ * /v1/schedules/{id}:* get:* summary: Get a schedule by ID* parameters:@@ -81,8 +81,8 @@ import {*/export async function scheduleById(req: NextApiRequest, res: NextApiResponse<ScheduleResponse>) {const { method, query, body } = req;- const safeQuery = await schemaQueryIdParseInt.safeParse(query);- const safeBody = await schemaScheduleBodyParams.safeParse(body);+ const safeQuery = schemaQueryIdParseInt.safeParse(query);+ const safeBody = schemaScheduleBodyParams.safeParse(body);if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);switch (method) {diff --git a/pages/api/schedules/index.ts b/pages/api/schedules/index.tsindex cd09534c999..3458e4896b9 100644--- a/pages/api/schedules/index.ts+++ b/pages/api/schedules/index.ts@@ -3,14 +3,14 @@ import type { NextApiRequest, NextApiResponse } from "next";import prisma from "@calcom/prisma";import { withMiddleware } from "@lib/helpers/withMiddleware";-import { SchedulesResponse } from "@lib/types";-import { schemaSchedulePublic } from "@lib/validations/schedule";+import { ScheduleResponse, SchedulesResponse } from "@lib/types";+import { schemaScheduleBodyParams, schemaSchedulePublic, withValidSchedule } from "@lib/validations/schedule";/*** @swagger* /api/schedules:* get:- * summary: Returns all schedules+ * summary: Get all schedules* tags:* - schedules* responses:@@ -20,18 +20,49 @@ import { schemaSchedulePublic } from "@lib/validations/schedule";* description: Authorization information is missing or invalid.* 404:* description: No schedules were found+ * post:+ * summary: Creates a new schedule+ * tags:+ * - schedules+ * responses:+ * 201:+ * description: OK, schedule created+ * model: Schedule+ * 400:+ * description: Bad request. Schedule body is invalid.+ * 401:+ * description: Authorization information is missing or invalid.*/-async function allSchedules(_: NextApiRequest, res: NextApiResponse<SchedulesResponse>) {- const schedules = await prisma.schedule.findMany();- const data = schedules.map((schedule) => schemaSchedulePublic.parse(schedule));+async function createOrlistAllSchedules(+ req: NextApiRequest,+ res: NextApiResponse<SchedulesResponse | ScheduleResponse>+) {+ 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 });+ else+ (error: Error) =>+ res.status(404).json({+ message: "No Schedules were found",+ error,+ });+ } else if (method === "POST") {+ 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);- if (data) res.status(200).json({ data });- else- (error: Error) =>- res.status(404).json({- message: "No Schedules were found",- error,- });+ if (data) res.status(201).json({ data, message: "Schedule created successfully" });+ else+ (error: Error) =>+ res.status(400).json({+ message: "Could not create new schedule",+ error,+ });+ } else res.status(405).json({ message: `Method ${method} not allowed` });}-export default withMiddleware("HTTP_GET")(allSchedules);+export default withMiddleware("HTTP_GET_OR_POST")(withValidSchedule(createOrlistAllSchedules));diff --git a/pages/api/schedules/new.ts b/pages/api/schedules/new.tsdeleted file mode 100644index d4a3ea44166..00000000000--- a/pages/api/schedules/new.ts+++ /dev/null@@ -1,41 +0,0 @@-import type { NextApiRequest, NextApiResponse } from "next";--import prisma from "@calcom/prisma";--import { withMiddleware } from "@lib/helpers/withMiddleware";-import type { ScheduleResponse } from "@lib/types";-import { schemaScheduleBodyParams, schemaSchedulePublic, withValidSchedule } from "@lib/validations/schedule";--/**- * @swagger- * /api/schedules/new:- * post:- * summary: Creates a new schedule- * tags:- * - schedules- * responses:- * 201:- * description: OK, schedule created- * model: Schedule- * 400:- * description: Bad request. Schedule body is invalid.- * 401:- * description: Authorization information is missing or invalid.- */-async function createSchedule(req: NextApiRequest, res: NextApiResponse<ScheduleResponse>) {- const safe = schemaScheduleBodyParams.safeParse(req.body);- if (!safe.success) throw new Error("Invalid request body", safe.error);-- const schedule = await prisma.schedule.create({ data: safe.data });- const data = schemaSchedulePublic.parse(schedule);-- if (data) res.status(201).json({ data, message: "Schedule created successfully" });- else- (error: Error) =>- res.status(400).json({- message: "Could not create new schedule",- error,- });-}--export default withMiddleware("HTTP_POST")(withValidSchedule(createSchedule));diff --git a/pages/api/schedules/newIndex.ts b/pages/api/schedules/newIndex.tsdeleted file mode 100644index 732eb013b27..00000000000--- a/pages/api/schedules/newIndex.ts+++ /dev/null@@ -1,68 +0,0 @@-import type { NextApiRequest, NextApiResponse } from "next";--import prisma from "@calcom/prisma";--import { withMiddleware } from "@lib/helpers/withMiddleware";-import { PaymentResponse, PaymentsResponse } from "@lib/types";-import { schemaPaymentBodyParams, schemaPaymentPublic, withValidPayment } from "@lib/validations/payment";--/**- * @swagger- * /api/payments:- * get:- * summary: Get all payments- * tags:- * - payments- * responses:- * 200:- * description: OK- * 401:- * description: Authorization information is missing or invalid.- * 404:- * description: No payments were found- * post:- * summary: Creates a new payment- * tags:- * - payments- * responses:- * 201:- * description: OK, payment created- * model: Payment- * 400:- * description: Bad request. Payment body is invalid.- * 401:- * description: Authorization information is missing or invalid.- */-async function createOrlistAllPayments(- req: NextApiRequest,- res: NextApiResponse<PaymentsResponse | PaymentResponse>-) {- const { method } = req;- if (method === "GET") {- const payments = await prisma.payment.findMany();- const data = payments.map((payment) => schemaPaymentPublic.parse(payment));- if (data) res.status(200).json({ data });- else- (error: Error) =>- res.status(404).json({- message: "No Payments were found",- error,- });- } else if (method === "POST") {- const safe = schemaPaymentBodyParams.safeParse(req.body);- if (!safe.success) throw new Error("Invalid request body");-- const payment = await prisma.payment.create({ data: safe.data });- const data = schemaPaymentPublic.parse(payment);-- if (data) res.status(201).json({ data, message: "Payment created successfully" });- else- (error: Error) =>- res.status(400).json({- message: "Could not create new payment",- error,- });- } else res.status(405).json({ message: `Method ${method} not allowed` });-}--export default withMiddleware("HTTP_GET_OR_POST")(withValidPayment(createOrlistAllPayments));diff --git a/pages/api/selected-calendars/[id].ts b/pages/api/selected-calendars/[id].tsindex ed3af1e7b78..900c2295dbd 100644--- a/pages/api/selected-calendars/[id].ts+++ b/pages/api/selected-calendars/[id].ts@@ -120,8 +120,8 @@ export async function selectedCalendarById(res: NextApiResponse<SelectedCalendarResponse>) {const { method, query, body } = req;- const safeQuery = await schemaQueryIdAsString.safeParse(query);- const safeBody = await schemaSelectedCalendarBodyParams.safeParse(body);+ const safeQuery = schemaQueryIdAsString.safeParse(query);+ const safeBody = schemaSelectedCalendarBodyParams.safeParse(body);if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);// This is how we set the userId and externalId in the query for managing compoundId.const [userId, integration, externalId] = safeQuery.data.id.split("_");diff --git a/pages/api/users/[id].ts b/pages/api/users/[id].tsindex 3802a4fb901..a264051726e 100644--- a/pages/api/users/[id].ts+++ b/pages/api/users/[id].ts@@ -13,7 +13,7 @@ import { schemaUserBodyParams, schemaUserPublic } from "@lib/validations/user";/*** @swagger- * /api/users/{id}:+ * /v1/users/{id}:* get:* summary: Get a user by ID, returns your user if regular user.* parameters:@@ -82,8 +82,8 @@ import { schemaUserBodyParams, schemaUserPublic } from "@lib/validations/user";*/export async function userById(req: NextApiRequest, res: NextApiResponse<UserResponse>) {const { method, query, body } = req;- const safeQuery = await schemaQueryIdParseInt.safeParse(query);- const safeBody = await schemaUserBodyParams.safeParse(body);+ const safeQuery = schemaQueryIdParseInt.safeParse(query);+ const safeBody = schemaUserBodyParams.safeParse(body);if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);const userId = getCalcomUserId(res);if (safeQuery.data.id === userId) {diff --git a/templates/endpoints/[id]/delete.ts b/templates/endpoints/[id]/delete.tsindex 95049ae7992..a9816a3ab6d 100644--- a/templates/endpoints/[id]/delete.ts+++ b/templates/endpoints/[id]/delete.ts@@ -33,7 +33,7 @@ import {* description: Authorization information is missing or invalid.*/export async function deleteResource(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {- const safe = await schemaQueryIdParseInt.safeParse(req.query);+ const safe = schemaQueryIdParseInt.safeParse(req.query);if (!safe.success) throw new Error("Invalid request query", safe.error);const data = await prisma.resource.delete({ where: { id: safe.data.id } });diff --git a/templates/endpoints/[id]/edit.ts b/templates/endpoints/[id]/edit.tsindex e4d2f382ecc..2be7bd06c1c 100644--- a/templates/endpoints/[id]/edit.ts+++ b/templates/endpoints/[id]/edit.ts@@ -34,8 +34,8 @@ import { schemaResourceBodyParams, schemaResourcePublic, withValidResource } fro* description: Authorization information is missing or invalid.*/export async function editResource(req: NextApiRequest, res: NextApiResponse<ResourceResponse>) {- const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);- const safeBody = await schemaResourceBodyParams.safeParse(req.body);+ const safeQuery = schemaQueryIdParseInt.safeParse(req.query);+ const safeBody = schemaResourceBodyParams.safeParse(req.body);if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");const resource = await prisma.resource.update({diff --git a/templates/endpoints/[id]/index.ts b/templates/endpoints/[id]/index.tsindex 0ba07d382f2..2ef4d18a674 100644--- a/templates/endpoints/[id]/index.ts+++ b/templates/endpoints/[id]/index.ts@@ -33,7 +33,7 @@ import { schemaResourcePublic } from "@lib/validations/resource";* description: Resource was not found*/export async function resourceById(req: NextApiRequest, res: NextApiResponse<ResourceResponse>) {- const safe = await schemaQueryIdParseInt.safeParse(req.query);+ const safe = schemaQueryIdParseInt.safeParse(req.query);if (!safe.success) throw new Error("Invalid request query");const resource = await prisma.resource.findUnique({ where: { id: safe.data.id } });