diff --git a/.prettierignore b/.prettierignore index 28b23b7d57..745ffcee46 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,4 +1,5 @@ .next/ coverage/ node_modules/ -tests/ \ No newline at end of file +tests/ +templates/ \ No newline at end of file diff --git a/.prettierrc.js b/.prettierrc.js new file mode 100644 index 0000000000..246057aa0c --- /dev/null +++ b/.prettierrc.js @@ -0,0 +1 @@ +module.exports = require("../../packages/config/prettier-preset"); diff --git a/pages/api/api-keys/[id]/delete.ts b/pages/api/api-keys/[id]/delete.ts deleted file mode 100644 index dbe8250fe2..0000000000 --- a/pages/api/api-keys/[id]/delete.ts +++ /dev/null @@ -1,24 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString"; - -type ResponseData = { - message?: string; - error?: unknown; -}; - -export async function deleteApiKey(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdAsString.safeParse(req.query); - if (safe.success) { - const data = await prisma.apiKey.delete({ where: { id: safe.data.id } }); - // We only remove the apiKey type from the database if there's an existing resource. - if (data) res.status(200).json({ message: `ApiKey with id: ${safe.data.id} deleted successfully` }); - // This catches the error thrown by prisma.apiKey.delete() if the resource is not found. - else res.status(400).json({ message: `ApiKey with id: ${safe.data.id} was not able to be processed` }); - } -} - -export default withMiddleware("HTTP_DELETE", "addRequestId")(withValidQueryIdString(deleteApiKey)); diff --git a/pages/api/api-keys/[id]/edit.ts b/pages/api/api-keys/[id]/edit.ts deleted file mode 100644 index 66da3539bc..0000000000 --- a/pages/api/api-keys/[id]/edit.ts +++ /dev/null @@ -1,38 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; -import { ApiKey } from "@calcom/prisma/client"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import { schemaApiKeyBodyParams, withValidApiKey } from "@lib/validations/api-key"; -import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString"; - -type ResponseData = { - data?: ApiKey; - message?: string; - error?: unknown; -}; - -export async function editApiKey(req: NextApiRequest, res: NextApiResponse) { - const { query, body } = req; - const safeQuery = await schemaQueryIdAsString.safeParse(query); - const safeBody = await schemaApiKeyBodyParams.safeParse(body); - - if (safeQuery.success && safeBody.success) { - const data = await prisma.apiKey.update({ - where: { id: safeQuery.data.id }, - data: safeBody.data, - }); - if (data) res.status(200).json({ data }); - else - (error: unknown) => - res - .status(404) - .json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error }); - } -} - -export default withMiddleware( - "HTTP_PATCH", - "addRequestId" -)(withValidQueryIdString(withValidApiKey(editApiKey))); diff --git a/pages/api/api-keys/[id]/index.ts b/pages/api/api-keys/[id]/index.ts deleted file mode 100644 index 36b9203667..0000000000 --- a/pages/api/api-keys/[id]/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; -import { ApiKey } from "@calcom/prisma/client"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString"; - -type ResponseData = { - data?: ApiKey; - error?: unknown; -}; - -export async function apiKeyById(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdAsString.safeParse(req.query); - if (safe.success) { - const data = await prisma.apiKey.findUnique({ where: { id: safe.data.id } }); - - if (data) res.status(200).json({ data }); - else res.status(404).json({ error: { message: "ApiKey was not found" } }); - } -} - -export default withMiddleware("addRequestId", "HTTP_GET")(withValidQueryIdString(apiKeyById)); diff --git a/pages/api/api-keys/index.ts b/pages/api/api-keys/index.ts deleted file mode 100644 index 94074a940a..0000000000 --- a/pages/api/api-keys/index.ts +++ /dev/null @@ -1,20 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; -import { ApiKey } from "@calcom/prisma/client"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; - -type ResponseData = { - data?: ApiKey[]; - error?: unknown; -}; - -async function allApiKeys(req: NextApiRequest, res: NextApiResponse) { - const data = await prisma.apiKey.findMany(); - - if (data) res.status(200).json({ data }); - else res.status(400).json({ error: "No data found" }); -} - -export default withMiddleware("addRequestId", "HTTP_GET")(allApiKeys); diff --git a/pages/api/api-keys/new.ts b/pages/api/api-keys/new.ts deleted file mode 100644 index 160324c19c..0000000000 --- a/pages/api/api-keys/new.ts +++ /dev/null @@ -1,24 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; -import { ApiKey } from "@calcom/prisma/client"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import { schemaApiKeyBodyParams, withValidApiKey } from "@lib/validations/api-key"; - -type ResponseData = { - data?: ApiKey; - error?: unknown; -}; - -async function createApiKey(req: NextApiRequest, res: NextApiResponse) { - const safe = schemaApiKeyBodyParams.safeParse(req.body); - if (safe.success) { - const data = await prisma.apiKey.create({ data: safe.data }); - if (data) res.status(201).json({ data }); - else - (error: unknown) => res.status(400).json({ error: { message: "Could not create apiKey type", error } }); - } -} - -export default withMiddleware("addRequestId", "HTTP_POST")(withValidApiKey(createApiKey)); diff --git a/pages/api/attendees/[id].ts b/pages/api/attendees/[id].ts new file mode 100644 index 0000000000..502fb5d1c5 --- /dev/null +++ b/pages/api/attendees/[id].ts @@ -0,0 +1,130 @@ +import type { NextApiRequest, NextApiResponse } from "next"; + +import prisma from "@calcom/prisma"; + +import { withMiddleware } from "@lib/helpers/withMiddleware"; +import type { AttendeeResponse } from "@lib/types"; +import { schemaAttendeeBodyParams, schemaAttendeePublic } from "@lib/validations/attendee"; +import { + schemaQueryIdParseInt, + withValidQueryIdTransformParseInt, +} from "@lib/validations/shared/queryIdTransformParseInt"; + +/** + * @swagger + * /api/attendees/{id}: + * get: + * summary: Get an attendee by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the attendee to get + * tags: + * - attendees + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: Attendee was not found + * patch: + * summary: Edit an existing attendee + * consumes: + * - application/json + * parameters: + * - in: body + * name: attendee + * description: The attendee to edit + * schema: + * type: object + * $ref: '#/components/schemas/Attendee' + * required: true + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the attendee to edit + * tags: + * - attendees + * responses: + * 201: + * description: OK, attendee edited successfuly + * model: Attendee + * 400: + * description: Bad request. Attendee body is invalid. + * 401: + * description: Authorization information is missing or invalid. + * delete: + * summary: Remove an existing attendee + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the attendee to delete + * tags: + * - attendees + * responses: + * 201: + * description: OK, attendee removed successfuly + * model: Attendee + * 400: + * description: Bad request. Attendee id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function attendeeById(req: NextApiRequest, res: NextApiResponse) { + const { method, query, body } = req; + const safeQuery = await schemaQueryIdParseInt.safeParse(query); + const safeBody = await schemaAttendeeBodyParams.safeParse(body); + if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); + + switch (method) { + case "GET": + await prisma.attendee + .findUnique({ where: { id: safeQuery.data.id } }) + .then((attendee) => schemaAttendeePublic.parse(attendee)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `Attendee with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "PATCH": + if (!safeBody.success) throw new Error("Invalid request body"); + await prisma.attendee + .update({ + where: { id: safeQuery.data.id }, + data: safeBody.data, + }) + .then((attendee) => schemaAttendeePublic.parse(attendee)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `Attendee with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "DELETE": + await prisma.attendee + .delete({ where: { id: safeQuery.data.id } }) + .then(() => + res.status(200).json({ message: `Attendee with id: ${safeQuery.data.id} deleted successfully` }) + ) + .catch((error: Error) => + res.status(404).json({ message: `Attendee 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(attendeeById)); diff --git a/pages/api/attendees/[id]/delete.ts b/pages/api/attendees/[id]/delete.ts deleted file mode 100644 index 25e0818960..0000000000 --- a/pages/api/attendees/[id]/delete.ts +++ /dev/null @@ -1,50 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { BaseResponse } from "@lib/types"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/attendees/{id}/delete: - * delete: - * summary: Remove an existing attendee - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the attendee to delete - * tags: - * - attendees - * responses: - * 201: - * description: OK, attendee removed successfuly - * model: Attendee - * 400: - * description: Bad request. Attendee id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function deleteAttendee(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query", safe.error); - - const data = await prisma.attendee.delete({ where: { id: safe.data.id } }); - - if (data) res.status(200).json({ message: `Attendee with id: ${safe.data.id} deleted successfully` }); - else - (error: Error) => - res.status(400).json({ - message: `Attendee with id: ${safe.data.id} was not able to be processed`, - error, - }); -} - -export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteAttendee)); diff --git a/pages/api/attendees/[id]/edit.ts b/pages/api/attendees/[id]/edit.ts deleted file mode 100644 index dab8ed979c..0000000000 --- a/pages/api/attendees/[id]/edit.ts +++ /dev/null @@ -1,58 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { AttendeeResponse } from "@lib/types"; -import { schemaAttendeeBodyParams, schemaAttendeePublic, withValidAttendee } from "@lib/validations/attendee"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/attendees/{id}/edit: - * patch: - * summary: Edit an existing attendee - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the attendee to edit - * tags: - * - attendees - * responses: - * 201: - * description: OK, attendee edited successfuly - * model: Attendee - * 400: - * description: Bad request. Attendee body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function editAttendee(req: NextApiRequest, res: NextApiResponse) { - const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); - const safeBody = await schemaAttendeeBodyParams.safeParse(req.body); - - if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); - const attendee = await prisma.attendee.update({ - where: { id: safeQuery.data.id }, - data: safeBody.data, - }); - const data = schemaAttendeePublic.parse(attendee); - - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, - error, - }); -} - -export default withMiddleware("HTTP_PATCH")( - withValidQueryIdTransformParseInt(withValidAttendee(editAttendee)) -); diff --git a/pages/api/attendees/[id]/index.ts b/pages/api/attendees/[id]/index.ts deleted file mode 100644 index 81f9ead1bd..0000000000 --- a/pages/api/attendees/[id]/index.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 { AttendeeResponse } from "@lib/types"; -import { schemaAttendeePublic } from "@lib/validations/attendee"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/attendees/{id}: - * get: - * summary: Get a attendee by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the attendee to get - * tags: - * - attendees - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: Attendee was not found - */ -export async function attendeeById(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query"); - - const attendee = await prisma.attendee.findUnique({ where: { id: safe.data.id } }); - const data = schemaAttendeePublic.parse(attendee); - - if (attendee) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "Attendee was not found", - error, - }); -} - -export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(attendeeById)); diff --git a/pages/api/availabilities/[id].ts b/pages/api/availabilities/[id].ts new file mode 100644 index 0000000000..be9d9eb987 --- /dev/null +++ b/pages/api/availabilities/[id].ts @@ -0,0 +1,130 @@ +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 } from "@lib/validations/availability"; +import { + schemaQueryIdParseInt, + withValidQueryIdTransformParseInt, +} from "@lib/validations/shared/queryIdTransformParseInt"; + +/** + * @swagger + * /api/availabilities/{id}: + * get: + * summary: Get an availability by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the availability to get + * tags: + * - availabilities + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: Availability was not found + * patch: + * summary: Edit an existing availability + * consumes: + * - application/json + * parameters: + * - in: body + * name: availability + * description: The availability to edit + * schema: + * type: object + * $ref: '#/components/schemas/Availability' + * required: true + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the availability to edit + * tags: + * - availabilities + * responses: + * 201: + * description: OK, availability edited successfuly + * model: Availability + * 400: + * description: Bad request. Availability body is invalid. + * 401: + * description: Authorization information is missing or invalid. + * delete: + * summary: Remove an existing availability + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the availability to delete + * tags: + * - availabilities + * responses: + * 201: + * description: OK, availability removed successfuly + * model: Availability + * 400: + * description: Bad request. Availability id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function availabilityById(req: NextApiRequest, res: NextApiResponse) { + const { method, query, body } = req; + const safeQuery = await schemaQueryIdParseInt.safeParse(query); + const safeBody = await schemaAvailabilityBodyParams.safeParse(body); + if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); + + switch (method) { + case "GET": + await prisma.availability + .findUnique({ where: { id: safeQuery.data.id } }) + .then((availability) => schemaAvailabilityPublic.parse(availability)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `Availability with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "PATCH": + if (!safeBody.success) throw new Error("Invalid request body"); + await prisma.availability + .update({ + where: { id: safeQuery.data.id }, + data: safeBody.data, + }) + .then((availability) => schemaAvailabilityPublic.parse(availability)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `Availability with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "DELETE": + await prisma.availability + .delete({ where: { id: safeQuery.data.id } }) + .then(() => + res.status(200).json({ message: `Availability with id: ${safeQuery.data.id} deleted successfully` }) + ) + .catch((error: Error) => + res.status(404).json({ message: `Availability 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(availabilityById)); diff --git a/pages/api/availabilities/[id]/delete.ts b/pages/api/availabilities/[id]/delete.ts deleted file mode 100644 index a23f5e9792..0000000000 --- a/pages/api/availabilities/[id]/delete.ts +++ /dev/null @@ -1,50 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { BaseResponse } from "@lib/types"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/availabilites/{id}/delete: - * delete: - * summary: Remove an existing availability - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the availability to delete - * tags: - * - availabilites - * responses: - * 201: - * description: OK, availability removed successfuly - * model: Availability - * 400: - * description: Bad request. Availability id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function deleteAvailability(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query", safe.error); - - const data = await prisma.availability.delete({ where: { id: safe.data.id } }); - - if (data) res.status(200).json({ message: `Availability with id: ${safe.data.id} deleted successfully` }); - else - (error: Error) => - res.status(400).json({ - message: `Availability with id: ${safe.data.id} was not able to be processed`, - error, - }); -} - -export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteAvailability)); diff --git a/pages/api/availabilities/[id]/edit.ts b/pages/api/availabilities/[id]/edit.ts deleted file mode 100644 index 2aeb92dee2..0000000000 --- a/pages/api/availabilities/[id]/edit.ts +++ /dev/null @@ -1,62 +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"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/availabilites/{id}/edit: - * patch: - * summary: Edit an existing availability - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the availability to edit - * tags: - * - availabilites - * responses: - * 201: - * description: OK, availability edited successfuly - * model: Availability - * 400: - * description: Bad request. Availability body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function editAvailability(req: NextApiRequest, res: NextApiResponse) { - const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); - const safeBody = await schemaAvailabilityBodyParams.safeParse(req.body); - - if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); - const availability = await prisma.availability.update({ - where: { id: safeQuery.data.id }, - data: safeBody.data, - }); - const data = schemaAvailabilityPublic.parse(availability); - - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, - error, - }); -} - -export default withMiddleware("HTTP_PATCH")( - withValidQueryIdTransformParseInt(withValidAvailability(editAvailability)) -); diff --git a/pages/api/availabilities/[id]/index.ts b/pages/api/availabilities/[id]/index.ts deleted file mode 100644 index 054c32ee9f..0000000000 --- a/pages/api/availabilities/[id]/index.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 { schemaAvailabilityPublic } from "@lib/validations/availability"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/availabilites/{id}: - * get: - * summary: Get a availability by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the availability to get - * tags: - * - availabilites - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: Availability was not found - */ -export async function availabilityById(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query"); - - const availability = await prisma.availability.findUnique({ where: { id: safe.data.id } }); - const data = schemaAvailabilityPublic.parse(availability); - - if (availability) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "Availability was not found", - error, - }); -} - -export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(availabilityById)); diff --git a/pages/api/booking-references/[id].ts b/pages/api/booking-references/[id].ts new file mode 100644 index 0000000000..8c804321e0 --- /dev/null +++ b/pages/api/booking-references/[id].ts @@ -0,0 +1,138 @@ +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, +} from "@lib/validations/booking-reference"; +import { + schemaQueryIdParseInt, + withValidQueryIdTransformParseInt, +} from "@lib/validations/shared/queryIdTransformParseInt"; + +/** + * @swagger + * /api/booking-references/{id}: + * get: + * summary: Get a daily event reference by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the daily event reference to get + * tags: + * - booking-references + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: BookingReference was not found + * patch: + * summary: Edit an existing daily event reference + * consumes: + * - application/json + * parameters: + * - in: body + * name: bookingReference + * description: The bookingReference to edit + * schema: + * type: object + * $ref: '#/components/schemas/BookingReference' + * required: true + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the daily event reference to edit + * tags: + * - booking-references + * responses: + * 201: + * description: OK, bookingReference edited successfuly + * model: BookingReference + * 400: + * description: Bad request. BookingReference body is invalid. + * 401: + * description: Authorization information is missing or invalid. + * delete: + * summary: Remove an existing daily event reference + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the daily event reference to delete + * tags: + * - booking-references + * responses: + * 201: + * description: OK, bookingReference removed successfuly + * model: BookingReference + * 400: + * description: Bad request. BookingReference id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function bookingReferenceById( + req: NextApiRequest, + res: NextApiResponse +) { + const { method, query, body } = req; + const safeQuery = await schemaQueryIdParseInt.safeParse(query); + const safeBody = await schemaBookingReferenceBodyParams.safeParse(body); + if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); + + switch (method) { + case "GET": + await prisma.bookingReference + .findUnique({ where: { id: safeQuery.data.id } }) + .then((data) => schemaBookingReferencePublic.parse(data)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `BookingReference with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "PATCH": + if (!safeBody.success) throw new Error("Invalid request body"); + await prisma.bookingReference + .update({ + where: { id: safeQuery.data.id }, + data: safeBody.data, + }) + .then((bookingReference) => schemaBookingReferencePublic.parse(bookingReference)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `BookingReference with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "DELETE": + await prisma.bookingReference + .delete({ where: { id: safeQuery.data.id } }) + .then(() => + res.status(200).json({ message: `BookingReference with id: ${safeQuery.data.id} deleted` }) + ) + .catch((error: Error) => + res.status(404).json({ message: `BookingReference 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(bookingReferenceById) +); diff --git a/pages/api/booking-references/[id]/delete.ts b/pages/api/booking-references/[id]/delete.ts deleted file mode 100644 index 1fb0f32eb2..0000000000 --- a/pages/api/booking-references/[id]/delete.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 { BaseResponse } from "@lib/types"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/booking-references/{id}/delete: - * delete: - * summary: Remove an existing bookingReference - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the bookingReference to delete - * tags: - * - booking-references - * responses: - * 201: - * description: OK, bookingReference removed successfuly - * model: BookingReference - * 400: - * description: Bad request. BookingReference id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function deleteBookingReference(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query", safe.error); - - const data = await prisma.bookingReference.delete({ where: { id: safe.data.id } }); - - if (data) - res.status(200).json({ message: `BookingReference with id: ${safe.data.id} deleted successfully` }); - else - (error: Error) => - res.status(400).json({ - message: `BookingReference with id: ${safe.data.id} was not able to be processed`, - error, - }); -} - -export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteBookingReference)); diff --git a/pages/api/booking-references/[id]/edit.ts b/pages/api/booking-references/[id]/edit.ts deleted file mode 100644 index 60c5d53316..0000000000 --- a/pages/api/booking-references/[id]/edit.ts +++ /dev/null @@ -1,65 +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"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/booking-references/{id}/edit: - * patch: - * summary: Edit an existing bookingReference - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the bookingReference to edit - * tags: - * - booking-references - * responses: - * 201: - * description: OK, bookingReference edited successfuly - * model: BookingReference - * 400: - * description: Bad request. BookingReference body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function editBookingReference( - req: NextApiRequest, - res: NextApiResponse -) { - const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); - const safeBody = await schemaBookingReferenceBodyParams.safeParse(req.body); - - if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); - const bookingReference = await prisma.bookingReference.update({ - where: { id: safeQuery.data.id }, - data: safeBody.data, - }); - const data = schemaBookingReferencePublic.parse(bookingReference); - - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, - error, - }); -} - -export default withMiddleware("HTTP_PATCH")( - withValidQueryIdTransformParseInt(withValidBookingReference(editBookingReference)) -); diff --git a/pages/api/booking-references/[id]/index.ts b/pages/api/booking-references/[id]/index.ts deleted file mode 100644 index e71f6920d7..0000000000 --- a/pages/api/booking-references/[id]/index.ts +++ /dev/null @@ -1,54 +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 { schemaBookingReferencePublic } from "@lib/validations/booking-reference"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/booking-references/{id}: - * get: - * summary: Get a bookingReference by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the bookingReference to get - * tags: - * - booking-references - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: BookingReference was not found - */ -export async function bookingReferenceById( - req: NextApiRequest, - res: NextApiResponse -) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query"); - - const bookingReference = await prisma.bookingReference.findUnique({ where: { id: safe.data.id } }); - const data = schemaBookingReferencePublic.parse(bookingReference); - - if (bookingReference) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "BookingReference was not found", - error, - }); -} - -export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(bookingReferenceById)); diff --git a/pages/api/bookings/[id].ts b/pages/api/bookings/[id].ts new file mode 100644 index 0000000000..57b817464d --- /dev/null +++ b/pages/api/bookings/[id].ts @@ -0,0 +1,130 @@ +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 } from "@lib/validations/booking"; +import { + schemaQueryIdParseInt, + withValidQueryIdTransformParseInt, +} from "@lib/validations/shared/queryIdTransformParseInt"; + +/** + * @swagger + * /api/bookings/{id}: + * get: + * summary: Get a booking by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the booking to get + * tags: + * - bookings + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: Booking was not found + * patch: + * summary: Edit an existing booking + * consumes: + * - application/json + * parameters: + * - in: body + * name: booking + * description: The booking to edit + * schema: + * type: object + * $ref: '#/components/schemas/Booking' + * required: true + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the booking to edit + * tags: + * - bookings + * responses: + * 201: + * description: OK, booking edited successfuly + * model: Booking + * 400: + * description: Bad request. Booking body is invalid. + * 401: + * description: Authorization information is missing or invalid. + * delete: + * summary: Remove an existing booking + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the booking to delete + * tags: + * - bookings + * responses: + * 201: + * description: OK, booking removed successfuly + * model: Booking + * 400: + * description: Bad request. Booking id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function bookingById(req: NextApiRequest, res: NextApiResponse) { + const { method, query, body } = req; + const safeQuery = await schemaQueryIdParseInt.safeParse(query); + const safeBody = await schemaBookingBodyParams.safeParse(body); + if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); + + switch (method) { + case "GET": + await prisma.booking + .findUnique({ where: { id: safeQuery.data.id } }) + .then((booking) => schemaBookingPublic.parse(booking)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `Booking with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "PATCH": + if (!safeBody.success) throw new Error("Invalid request body"); + await prisma.booking + .update({ + where: { id: safeQuery.data.id }, + data: safeBody.data, + }) + .then((booking) => schemaBookingPublic.parse(booking)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `Booking with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "DELETE": + await prisma.booking + .delete({ where: { id: safeQuery.data.id } }) + .then(() => + res.status(200).json({ message: `Booking with id: ${safeQuery.data.id} deleted successfully` }) + ) + .catch((error: Error) => + res.status(404).json({ message: `Booking 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(bookingById)); diff --git a/pages/api/bookings/[id]/delete.ts b/pages/api/bookings/[id]/delete.ts deleted file mode 100644 index 3905a93b03..0000000000 --- a/pages/api/bookings/[id]/delete.ts +++ /dev/null @@ -1,50 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { BaseResponse } from "@lib/types"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/bookings/{id}/delete: - * delete: - * summary: Remove an existing booking - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the booking to delete - * tags: - * - bookings - * responses: - * 201: - * description: OK, booking removed successfuly - * model: Booking - * 400: - * description: Bad request. Booking id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function deleteBooking(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query", safe.error); - - const data = await prisma.booking.delete({ where: { id: safe.data.id } }); - - if (data) res.status(200).json({ message: `Booking with id: ${safe.data.id} deleted successfully` }); - else - (error: Error) => - res.status(400).json({ - message: `Booking with id: ${safe.data.id} was not able to be processed`, - error, - }); -} - -export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteBooking)); diff --git a/pages/api/bookings/[id]/edit.ts b/pages/api/bookings/[id]/edit.ts deleted file mode 100644 index fffc48c221..0000000000 --- a/pages/api/bookings/[id]/edit.ts +++ /dev/null @@ -1,56 +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"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/bookings/{id}/edit: - * patch: - * summary: Edit an existing booking - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the booking to edit - * tags: - * - bookings - * responses: - * 201: - * description: OK, booking edited successfuly - * model: Booking - * 400: - * description: Bad request. Booking body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function editBooking(req: NextApiRequest, res: NextApiResponse) { - const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); - const safeBody = await schemaBookingBodyParams.safeParse(req.body); - - if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); - const booking = await prisma.booking.update({ - where: { id: safeQuery.data.id }, - data: safeBody.data, - }); - const data = schemaBookingPublic.parse(booking); - - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, - error, - }); -} - -export default withMiddleware("HTTP_PATCH")(withValidQueryIdTransformParseInt(withValidBooking(editBooking))); diff --git a/pages/api/bookings/[id]/index.ts b/pages/api/bookings/[id]/index.ts deleted file mode 100644 index c2b47d737f..0000000000 --- a/pages/api/bookings/[id]/index.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 { BookingResponse } from "@lib/types"; -import { schemaBookingPublic } from "@lib/validations/booking"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/bookings/{id}: - * get: - * summary: Get a booking by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the booking to get - * tags: - * - bookings - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: Booking was not found - */ -export async function bookingById(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query"); - - const booking = await prisma.booking.findUnique({ where: { id: safe.data.id } }); - const data = schemaBookingPublic.parse(booking); - - if (booking) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "Booking was not found", - error, - }); -} - -export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(bookingById)); diff --git a/pages/api/credentials/[id].ts b/pages/api/credentials/[id].ts new file mode 100644 index 0000000000..cc48ffab6a --- /dev/null +++ b/pages/api/credentials/[id].ts @@ -0,0 +1,130 @@ +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 } from "@lib/validations/credential"; +import { + schemaQueryIdParseInt, + withValidQueryIdTransformParseInt, +} from "@lib/validations/shared/queryIdTransformParseInt"; + +/** + * @swagger + * /api/credentials/{id}: + * get: + * summary: Get a credential by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the credential to get + * tags: + * - credentials + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: Credential was not found + * patch: + * summary: Edit an existing credential + * consumes: + * - application/json + * parameters: + * - in: body + * name: credential + * description: The credential to edit + * schema: + * type: object + * $ref: '#/components/schemas/Credential' + * required: true + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the credential to edit + * tags: + * - credentials + * responses: + * 201: + * description: OK, credential edited successfuly + * model: Credential + * 400: + * description: Bad request. Credential body is invalid. + * 401: + * description: Authorization information is missing or invalid. + * delete: + * summary: Remove an existing credential + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the credential to delete + * tags: + * - credentials + * responses: + * 201: + * description: OK, credential removed successfuly + * model: Credential + * 400: + * description: Bad request. Credential id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function credentialById(req: NextApiRequest, res: NextApiResponse) { + const { method, query, body } = req; + const safeQuery = await schemaQueryIdParseInt.safeParse(query); + const safeBody = await schemaCredentialBodyParams.safeParse(body); + if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); + + switch (method) { + case "GET": + await prisma.credential + .findUnique({ where: { id: safeQuery.data.id } }) + .then((credential) => schemaCredentialPublic.parse(credential)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `Credential with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "PATCH": + if (!safeBody.success) throw new Error("Invalid request body"); + await prisma.credential + .update({ + where: { id: safeQuery.data.id }, + data: safeBody.data, + }) + .then((credential) => schemaCredentialPublic.parse(credential)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `Credential with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "DELETE": + await prisma.credential + .delete({ where: { id: safeQuery.data.id } }) + .then(() => + res.status(200).json({ message: `Credential with id: ${safeQuery.data.id} deleted successfully` }) + ) + .catch((error: Error) => + res.status(404).json({ message: `Credential 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(credentialById)); diff --git a/pages/api/credentials/[id]/delete.ts b/pages/api/credentials/[id]/delete.ts deleted file mode 100644 index 49e4918792..0000000000 --- a/pages/api/credentials/[id]/delete.ts +++ /dev/null @@ -1,50 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { BaseResponse } from "@lib/types"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/credentials/{id}/delete: - * delete: - * summary: Remove an existing credential - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the credential to delete - * tags: - * - credentials - * responses: - * 201: - * description: OK, credential removed successfuly - * model: Credential - * 400: - * description: Bad request. Credential id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function deleteCredential(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query", safe.error); - - const data = await prisma.credential.delete({ where: { id: safe.data.id } }); - - if (data) res.status(200).json({ message: `Credential with id: ${safe.data.id} deleted successfully` }); - else - (error: Error) => - res.status(400).json({ - message: `Credential with id: ${safe.data.id} was not able to be processed`, - error, - }); -} - -export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteCredential)); diff --git a/pages/api/credentials/[id]/edit.ts b/pages/api/credentials/[id]/edit.ts deleted file mode 100644 index 69883de41e..0000000000 --- a/pages/api/credentials/[id]/edit.ts +++ /dev/null @@ -1,62 +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"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/credentials/{id}/edit: - * patch: - * summary: Edit an existing credential - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the credential to edit - * tags: - * - credentials - * responses: - * 201: - * description: OK, credential edited successfuly - * model: Credential - * 400: - * description: Bad request. Credential body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function editCredential(req: NextApiRequest, res: NextApiResponse) { - const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); - const safeBody = await schemaCredentialBodyParams.safeParse(req.body); - - if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); - const credential = await prisma.credential.update({ - where: { id: safeQuery.data.id }, - data: safeBody.data, - }); - const data = schemaCredentialPublic.parse(credential); - - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, - error, - }); -} - -export default withMiddleware("HTTP_PATCH")( - withValidQueryIdTransformParseInt(withValidCredential(editCredential)) -); diff --git a/pages/api/credentials/[id]/index.ts b/pages/api/credentials/[id]/index.ts deleted file mode 100644 index f20fa4ba5c..0000000000 --- a/pages/api/credentials/[id]/index.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 { CredentialResponse } from "@lib/types"; -import { schemaCredentialPublic } from "@lib/validations/credential"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/credentials/{id}: - * get: - * summary: Get a credential by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the credential to get - * tags: - * - credentials - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: Credential was not found - */ -export async function credentialById(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query"); - - const credential = await prisma.credential.findUnique({ where: { id: safe.data.id } }); - const data = schemaCredentialPublic.parse(credential); - - if (credential) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "Credential was not found", - error, - }); -} - -export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(credentialById)); diff --git a/pages/api/daily-event-references/[id].ts b/pages/api/daily-event-references/[id].ts new file mode 100644 index 0000000000..21072a7a4e --- /dev/null +++ b/pages/api/daily-event-references/[id].ts @@ -0,0 +1,144 @@ +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, +} from "@lib/validations/daily-event-reference"; +import { + schemaQueryIdParseInt, + withValidQueryIdTransformParseInt, +} from "@lib/validations/shared/queryIdTransformParseInt"; + +/** + * @swagger + * /api/daily-event-references/{id}: + * get: + * summary: Get a daily event reference by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the daily event reference to get + * tags: + * - daily-event-references + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: DailyEventReference was not found + * patch: + * summary: Edit an existing daily event reference + * consumes: + * - application/json + * parameters: + * - in: body + * name: dailyEventReference + * description: The dailyEventReference to edit + * schema: + * type: object + * $ref: '#/components/schemas/DailyEventReference' + * required: true + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the daily event reference to edit + * tags: + * - daily-event-references + * responses: + * 201: + * description: OK, dailyEventReference edited successfuly + * model: DailyEventReference + * 400: + * description: Bad request. DailyEventReference body is invalid. + * 401: + * description: Authorization information is missing or invalid. + * delete: + * summary: Remove an existing daily event reference + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the daily event reference to delete + * tags: + * - daily-event-references + * responses: + * 201: + * description: OK, dailyEventReference removed successfuly + * model: DailyEventReference + * 400: + * description: Bad request. DailyEventReference id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function dailyEventReferenceById( + req: NextApiRequest, + res: NextApiResponse +) { + const { method, query, body } = req; + const safeQuery = await schemaQueryIdParseInt.safeParse(query); + const safeBody = await schemaDailyEventReferenceBodyParams.safeParse(body); + if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); + + switch (method) { + case "GET": + await prisma.dailyEventReference + .findUnique({ where: { id: safeQuery.data.id } }) + .then((data) => schemaDailyEventReferencePublic.parse(data)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res + .status(404) + .json({ message: `DailyEventReference with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "PATCH": + if (!safeBody.success) throw new Error("Invalid request body"); + await prisma.dailyEventReference + .update({ + where: { id: safeQuery.data.id }, + data: safeBody.data, + }) + .then((dailyEventReference) => schemaDailyEventReferencePublic.parse(dailyEventReference)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res + .status(404) + .json({ message: `DailyEventReference with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "DELETE": + await prisma.dailyEventReference + .delete({ where: { id: safeQuery.data.id } }) + .then(() => + res.status(200).json({ message: `DailyEventReference with id: ${safeQuery.data.id} deleted` }) + ) + .catch((error: Error) => + res + .status(404) + .json({ message: `DailyEventReference 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(dailyEventReferenceById) +); diff --git a/pages/api/daily-event-references/[id]/delete.ts b/pages/api/daily-event-references/[id]/delete.ts deleted file mode 100644 index 1e020c65ff..0000000000 --- a/pages/api/daily-event-references/[id]/delete.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 { BaseResponse } from "@lib/types"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/daily-event-references/{id}/delete: - * delete: - * summary: Remove an existing dailyEventReference - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the dailyEventReference to delete - * tags: - * - daily-event-references - * responses: - * 201: - * description: OK, dailyEventReference removed successfuly - * model: DailyEventReference - * 400: - * description: Bad request. DailyEventReference id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function deleteDailyEventReference(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query", safe.error); - - const data = await prisma.dailyEventReference.delete({ where: { id: safe.data.id } }); - - if (data) - res.status(200).json({ message: `DailyEventReference with id: ${safe.data.id} deleted successfully` }); - else - (error: Error) => - res.status(400).json({ - message: `DailyEventReference with id: ${safe.data.id} was not able to be processed`, - error, - }); -} - -export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteDailyEventReference)); diff --git a/pages/api/daily-event-references/[id]/edit.ts b/pages/api/daily-event-references/[id]/edit.ts deleted file mode 100644 index b090962717..0000000000 --- a/pages/api/daily-event-references/[id]/edit.ts +++ /dev/null @@ -1,65 +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"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/daily-event-references/{id}/edit: - * patch: - * summary: Edit an existing dailyEventReference - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the dailyEventReference to edit - * tags: - * - daily-event-references - * responses: - * 201: - * description: OK, dailyEventReference edited successfuly - * model: DailyEventReference - * 400: - * description: Bad request. DailyEventReference body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function editDailyEventReference( - req: NextApiRequest, - res: NextApiResponse -) { - const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); - const safeBody = await schemaDailyEventReferenceBodyParams.safeParse(req.body); - - if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); - const dailyEventReference = await prisma.dailyEventReference.update({ - where: { id: safeQuery.data.id }, - data: safeBody.data, - }); - const data = schemaDailyEventReferencePublic.parse(dailyEventReference); - - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, - error, - }); -} - -export default withMiddleware("HTTP_PATCH")( - withValidQueryIdTransformParseInt(withValidDailyEventReference(editDailyEventReference)) -); diff --git a/pages/api/daily-event-references/[id]/index.ts b/pages/api/daily-event-references/[id]/index.ts deleted file mode 100644 index 2ff523bc41..0000000000 --- a/pages/api/daily-event-references/[id]/index.ts +++ /dev/null @@ -1,54 +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 { schemaDailyEventReferencePublic } from "@lib/validations/daily-event-reference"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/daily-event-references/{id}: - * get: - * summary: Get a dailyEventReference by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the dailyEventReference to get - * tags: - * - daily-event-references - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: DailyEventReference was not found - */ -export async function dailyEventReferenceById( - req: NextApiRequest, - res: NextApiResponse -) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query"); - - const dailyEventReference = await prisma.dailyEventReference.findUnique({ where: { id: safe.data.id } }); - const data = schemaDailyEventReferencePublic.parse(dailyEventReference); - - if (dailyEventReference) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "DailyEventReference was not found", - error, - }); -} - -export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(dailyEventReferenceById)); diff --git a/pages/api/destination-calendars/[id].ts b/pages/api/destination-calendars/[id].ts new file mode 100644 index 0000000000..a08406f9fa --- /dev/null +++ b/pages/api/destination-calendars/[id].ts @@ -0,0 +1,144 @@ +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, +} from "@lib/validations/destination-calendar"; +import { + schemaQueryIdParseInt, + withValidQueryIdTransformParseInt, +} from "@lib/validations/shared/queryIdTransformParseInt"; + +/** + * @swagger + * /api/destination-calendars/{id}: + * get: + * summary: Get a destination calendar by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the destination calendar to get + * tags: + * - destination-calendars + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: DestinationCalendar was not found + * patch: + * summary: Edit an existing destination calendar + * consumes: + * - application/json + * parameters: + * - in: body + * name: destinationCalendar + * description: The destinationCalendar to edit + * schema: + * type: object + * $ref: '#/components/schemas/DestinationCalendar' + * required: true + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the destination calendar to edit + * tags: + * - destination-calendars + * responses: + * 201: + * description: OK, destinationCalendar edited successfuly + * model: DestinationCalendar + * 400: + * description: Bad request. DestinationCalendar body is invalid. + * 401: + * description: Authorization information is missing or invalid. + * delete: + * summary: Remove an existing destination calendar + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the destination calendar to delete + * tags: + * - destination-calendars + * responses: + * 201: + * description: OK, destinationCalendar removed successfuly + * model: DestinationCalendar + * 400: + * description: Bad request. DestinationCalendar id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function destionationCalendarById( + req: NextApiRequest, + res: NextApiResponse +) { + const { method, query, body } = req; + const safeQuery = await schemaQueryIdParseInt.safeParse(query); + const safeBody = await schemaDestinationCalendarBodyParams.safeParse(body); + if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); + + switch (method) { + case "GET": + await prisma.destinationCalendar + .findUnique({ where: { id: safeQuery.data.id } }) + .then((data) => schemaDestinationCalendarPublic.parse(data)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res + .status(404) + .json({ message: `DestinationCalendar with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "PATCH": + if (!safeBody.success) throw new Error("Invalid request body"); + await prisma.destinationCalendar + .update({ + where: { id: safeQuery.data.id }, + data: safeBody.data, + }) + .then((destinationCalendar) => schemaDestinationCalendarPublic.parse(destinationCalendar)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res + .status(404) + .json({ message: `DestinationCalendar with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "DELETE": + await prisma.destinationCalendar + .delete({ where: { id: safeQuery.data.id } }) + .then(() => + res.status(200).json({ message: `DestinationCalendar with id: ${safeQuery.data.id} deleted` }) + ) + .catch((error: Error) => + res + .status(404) + .json({ message: `DestinationCalendar 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(destionationCalendarById) +); diff --git a/pages/api/destination-calendars/[id]/delete.ts b/pages/api/destination-calendars/[id]/delete.ts deleted file mode 100644 index 1bf35128f9..0000000000 --- a/pages/api/destination-calendars/[id]/delete.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 { BaseResponse } from "@lib/types"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/destination-calendars/{id}/delete: - * delete: - * summary: Remove an existing destinationCalendar - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the destinationCalendar to delete - * tags: - * - destination-calendars - * responses: - * 201: - * description: OK, destinationCalendar removed successfuly - * model: DestinationCalendar - * 400: - * description: Bad request. DestinationCalendar id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function deleteDestinationCalendar(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query", safe.error); - - const data = await prisma.destinationCalendar.delete({ where: { id: safe.data.id } }); - - if (data) - res.status(200).json({ message: `DestinationCalendar with id: ${safe.data.id} deleted successfully` }); - else - (error: Error) => - res.status(400).json({ - message: `DestinationCalendar with id: ${safe.data.id} was not able to be processed`, - error, - }); -} - -export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteDestinationCalendar)); diff --git a/pages/api/destination-calendars/[id]/edit.ts b/pages/api/destination-calendars/[id]/edit.ts deleted file mode 100644 index ab09db2f4d..0000000000 --- a/pages/api/destination-calendars/[id]/edit.ts +++ /dev/null @@ -1,65 +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"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/destination-calendars/{id}/edit: - * patch: - * summary: Edit an existing destinationCalendar - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the destinationCalendar to edit - * tags: - * - destination-calendars - * responses: - * 201: - * description: OK, destinationCalendar edited successfuly - * model: DestinationCalendar - * 400: - * description: Bad request. DestinationCalendar body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function editDestinationCalendar( - req: NextApiRequest, - res: NextApiResponse -) { - const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); - const safeBody = await schemaDestinationCalendarBodyParams.safeParse(req.body); - - if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); - const destinationCalendar = await prisma.destinationCalendar.update({ - where: { id: safeQuery.data.id }, - data: safeBody.data, - }); - const data = schemaDestinationCalendarPublic.parse(destinationCalendar); - - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, - error, - }); -} - -export default withMiddleware("HTTP_PATCH")( - withValidQueryIdTransformParseInt(withValidDestinationCalendar(editDestinationCalendar)) -); diff --git a/pages/api/destination-calendars/[id]/index.ts b/pages/api/destination-calendars/[id]/index.ts deleted file mode 100644 index 4a7474ff7c..0000000000 --- a/pages/api/destination-calendars/[id]/index.ts +++ /dev/null @@ -1,54 +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 { schemaDestinationCalendarPublic } from "@lib/validations/destination-calendar"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/destination-calendars/{id}: - * get: - * summary: Get a destinationCalendar by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the destinationCalendar to get - * tags: - * - destination-calendars - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: DestinationCalendar was not found - */ -export async function destinationCalendarById( - req: NextApiRequest, - res: NextApiResponse -) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query"); - - const destinationCalendar = await prisma.destinationCalendar.findUnique({ where: { id: safe.data.id } }); - const data = schemaDestinationCalendarPublic.parse(destinationCalendar); - - if (destinationCalendar) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "DestinationCalendar was not found", - error, - }); -} - -export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(destinationCalendarById)); diff --git a/pages/api/destination-calendars/index.ts b/pages/api/destination-calendars/index.ts index fc3493bf28..b031b8a27e 100644 --- a/pages/api/destination-calendars/index.ts +++ b/pages/api/destination-calendars/index.ts @@ -10,7 +10,7 @@ import { schemaDestinationCalendarPublic } from "@lib/validations/destination-ca * @swagger * /api/destination-calendars: * get: - * summary: Get all destinationCalendars + * summary: Get all destination calendars * tags: * - destination-calendars * responses: diff --git a/pages/api/destination-calendars/new.ts b/pages/api/destination-calendars/new.ts index eac1e2e110..e98472b216 100644 --- a/pages/api/destination-calendars/new.ts +++ b/pages/api/destination-calendars/new.ts @@ -14,7 +14,7 @@ import { * @swagger * /api/destination-calendars/new: * post: - * summary: Creates a new destinationCalendar + * summary: Creates a new destination calendar * requestBody: * description: Optional description in *Markdown* * required: true diff --git a/pages/api/event-type-custom-inputs/[id].ts b/pages/api/event-type-custom-inputs/[id].ts new file mode 100644 index 0000000000..8b5d29ed8f --- /dev/null +++ b/pages/api/event-type-custom-inputs/[id].ts @@ -0,0 +1,133 @@ +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, +} from "@lib/validations/event-type-custom-input"; +import { + schemaQueryIdParseInt, + withValidQueryIdTransformParseInt, +} from "@lib/validations/shared/queryIdTransformParseInt"; + +/** + * @swagger + * /api/event-type-custom-inputs/{id}: + * get: + * summary: Get a eventTypeCustomInput by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the eventTypeCustomInput to get + * tags: + * - event-type-custom-inputs + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * deCustomInputscription: EventType was not found + * patch: + * summary: Edit an existing eventTypeCustomInput + * consumes: + * - application/json + * parameters: + * - in: body + * name: eventTypeCustomInput + * description: The eventTypeCustomInput to edit + * schema: + * type: object + * $ref: '#/comCustomInputponents/schemas/EventType' + * required: true + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the eventTypeCustomInput to edit + * tags: + * - event-type-custom-inputs + * responses: + * 201: + * description: OK, eventTypeCustomInput edited successfuly + * model: EventTypeCustomInput + * 400: + * desCustomInputcription: Bad request. EventType body is invalid. + * 401: + * description: Authorization information is missing or invalid. + * delete: + * summary: Remove an existing eventTypeCustomInput + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the eventTypeCustomInput to delete + * tags: + * - event-type-custom-inputs + * responses: + * 201: + * description: OK, eventTypeCustomInput removed successfuly + * model: EventTypeCustomInput + * 400: + * desCustomInputcription: Bad request. EventType id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +async function eventTypeById(req: NextApiRequest, res: NextApiResponse) { + const { method, query, body } = req; + const safeQuery = await schemaQueryIdParseInt.safeParse(query); + const safeBody = await schemaEventTypeCustomInputBodyParams.safeParse(body); + if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); + + switch (method) { + case "GET": + await prisma.eventTypeCustomInput + .findUnique({ where: { id: safeQuery.data.id } }) + .then((data) => schemaEventTypeCustomInputPublic.parse(data)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "PATCH": + if (!safeBody.success) throw new Error("Invalid request body"); + await prisma.eventTypeCustomInput + .update({ + where: { id: safeQuery.data.id }, + data: safeBody.data, + }) + .then((eventTypeCustomInput) => schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "DELETE": + await prisma.eventTypeCustomInput + .delete({ where: { id: safeQuery.data.id } }) + .then(() => + res.status(200).json({ message: `CustomInputEventType with id: ${safeQuery.data.id} deleted` }) + ) + .catch((error: Error) => + res.status(404).json({ message: `EventType 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(eventTypeById)); diff --git a/pages/api/event-type-custom-inputs/[id]/delete.ts b/pages/api/event-type-custom-inputs/[id]/delete.ts deleted file mode 100644 index e9064dd69d..0000000000 --- a/pages/api/event-type-custom-inputs/[id]/delete.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 { BaseResponse } from "@lib/types"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/event-type-custom-inputs/{id}/delete: - * delete: - * summary: Remove an existing eventTypeCustomInput - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the eventTypeCustomInput to delete - * tags: - * - event-type-custom-inputs - * responses: - * 201: - * description: OK, eventTypeCustomInput removed successfuly - * model: EventTypeCustomInput - * 400: - * description: Bad request. EventTypeCustomInput id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function deleteEventTypeCustomInput(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query", safe.error); - - const data = await prisma.eventTypeCustomInput.delete({ where: { id: safe.data.id } }); - - if (data) - res.status(200).json({ message: `EventTypeCustomInput with id: ${safe.data.id} deleted successfully` }); - else - (error: Error) => - res.status(400).json({ - message: `EventTypeCustomInput with id: ${safe.data.id} was not able to be processed`, - error, - }); -} - -export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteEventTypeCustomInput)); diff --git a/pages/api/event-type-custom-inputs/[id]/edit.ts b/pages/api/event-type-custom-inputs/[id]/edit.ts deleted file mode 100644 index 0357d22aff..0000000000 --- a/pages/api/event-type-custom-inputs/[id]/edit.ts +++ /dev/null @@ -1,65 +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"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/event-type-custom-inputs/{id}/edit: - * patch: - * summary: Edit an existing eventTypeCustomInput - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the eventTypeCustomInput to edit - * tags: - * - event-type-custom-inputs - * responses: - * 201: - * description: OK, eventTypeCustomInput edited successfuly - * model: EventTypeCustomInput - * 400: - * description: Bad request. EventTypeCustomInput body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function editEventTypeCustomInput( - req: NextApiRequest, - res: NextApiResponse -) { - const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); - const safeBody = await schemaEventTypeCustomInputBodyParams.safeParse(req.body); - - if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); - const eventTypeCustomInput = await prisma.eventTypeCustomInput.update({ - where: { id: safeQuery.data.id }, - data: safeBody.data, - }); - const data = schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput); - - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, - error, - }); -} - -export default withMiddleware("HTTP_PATCH")( - withValidQueryIdTransformParseInt(withValidEventTypeCustomInput(editEventTypeCustomInput)) -); diff --git a/pages/api/event-type-custom-inputs/[id]/index.ts b/pages/api/event-type-custom-inputs/[id]/index.ts deleted file mode 100644 index b99b067b9e..0000000000 --- a/pages/api/event-type-custom-inputs/[id]/index.ts +++ /dev/null @@ -1,54 +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 { schemaEventTypeCustomInputPublic } from "@lib/validations/event-type-custom-input"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/event-type-custom-inputs/{id}: - * get: - * summary: Get a eventTypeCustomInput by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the eventTypeCustomInput to get - * tags: - * - event-type-custom-inputs - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: EventTypeCustomInput was not found - */ -export async function eventTypeCustomInputById( - req: NextApiRequest, - res: NextApiResponse -) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query"); - - const eventTypeCustomInput = await prisma.eventTypeCustomInput.findUnique({ where: { id: safe.data.id } }); - const data = schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput); - - if (eventTypeCustomInput) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "EventTypeCustomInput was not found", - error, - }); -} - -export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(eventTypeCustomInputById)); diff --git a/pages/api/event-types/[id].ts b/pages/api/event-types/[id].ts new file mode 100644 index 0000000000..bf94bcdfa0 --- /dev/null +++ b/pages/api/event-types/[id].ts @@ -0,0 +1,128 @@ +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 } from "@lib/validations/event-type"; +import { + schemaQueryIdParseInt, + withValidQueryIdTransformParseInt, +} from "@lib/validations/shared/queryIdTransformParseInt"; + +/** + * @swagger + * /api/event-types/{id}: + * get: + * summary: Get a eventType by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the eventType to get + * tags: + * - event-types + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: EventType was not found + * patch: + * summary: Edit an existing eventType + * consumes: + * - application/json + * parameters: + * - in: body + * name: eventType + * description: The eventType to edit + * schema: + * type: object + * $ref: '#/components/schemas/EventType' + * required: true + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the eventType to edit + * tags: + * - event-types + * responses: + * 201: + * description: OK, eventType edited successfuly + * model: EventType + * 400: + * description: Bad request. EventType body is invalid. + * 401: + * description: Authorization information is missing or invalid. + * delete: + * summary: Remove an existing eventType + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the eventType to delete + * tags: + * - event-types + * responses: + * 201: + * description: OK, eventType removed successfuly + * model: EventType + * 400: + * description: Bad request. EventType id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function eventTypeById(req: NextApiRequest, res: NextApiResponse) { + const { method, query, body } = req; + const safeQuery = await schemaQueryIdParseInt.safeParse(query); + const safeBody = await schemaEventTypeBodyParams.safeParse(body); + if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); + + switch (method) { + case "GET": + await prisma.eventType + .findUnique({ where: { id: safeQuery.data.id } }) + .then((data) => schemaEventTypePublic.parse(data)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "PATCH": + if (!safeBody.success) throw new Error("Invalid request body"); + await prisma.eventType + .update({ + where: { id: safeQuery.data.id }, + data: safeBody.data, + }) + .then((eventType) => schemaEventTypePublic.parse(eventType)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "DELETE": + await prisma.eventType + .delete({ where: { id: safeQuery.data.id } }) + .then(() => res.status(200).json({ message: `EventType with id: ${safeQuery.data.id} deleted` })) + .catch((error: Error) => + res.status(404).json({ message: `EventType 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(eventTypeById)); diff --git a/pages/api/event-types/[id]/delete.ts b/pages/api/event-types/[id]/delete.ts deleted file mode 100644 index 1009a97e05..0000000000 --- a/pages/api/event-types/[id]/delete.ts +++ /dev/null @@ -1,50 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { BaseResponse } from "@lib/types"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/event-types/{id}/delete: - * delete: - * summary: Remove an existing eventType - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the eventType to delete - * tags: - * - event-types - * responses: - * 201: - * description: OK, eventType removed successfuly - * model: EventType - * 400: - * description: Bad request. EventType id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function deleteEventType(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query", safe.error); - - const data = await prisma.eventType.delete({ where: { id: safe.data.id } }); - - if (data) res.status(200).json({ message: `EventType with id: ${safe.data.id} deleted successfully` }); - else - (error: Error) => - res.status(400).json({ - message: `EventType with id: ${safe.data.id} was not able to be processed`, - error, - }); -} - -export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteEventType)); diff --git a/pages/api/event-types/[id]/edit.ts b/pages/api/event-types/[id]/edit.ts deleted file mode 100644 index 785774c0b6..0000000000 --- a/pages/api/event-types/[id]/edit.ts +++ /dev/null @@ -1,62 +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"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/event-types/{id}/edit: - * patch: - * summary: Edits an existing eventType - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the eventType to delete - * tags: - * - event-types - * responses: - * 201: - * description: OK, eventType edited successfuly - * model: EventType - * 400: - * description: Bad request. EventType body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function editEventType(req: NextApiRequest, res: NextApiResponse) { - const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); - const safeBody = await schemaEventTypeBodyParams.safeParse(req.body); - - if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); - const eventType = await prisma.eventType.update({ - where: { id: safeQuery.data.id }, - data: safeBody.data, - }); - const data = schemaEventTypePublic.parse(eventType); - - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, - error, - }); -} - -export default withMiddleware("HTTP_PATCH")( - withValidQueryIdTransformParseInt(withValidEventType(editEventType)) -); diff --git a/pages/api/event-types/[id]/index.ts b/pages/api/event-types/[id]/index.ts deleted file mode 100644 index 645708813d..0000000000 --- a/pages/api/event-types/[id]/index.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 { EventTypeResponse } from "@lib/types"; -import { schemaEventTypePublic } from "@lib/validations/event-type"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/event-types/{id}: - * get: - * summary: find eventType by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the eventType to get - * tags: - * - event-types - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: EventType was not found - */ -export async function eventTypeById(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query"); - - const eventType = await prisma.eventType.findUnique({ where: { id: safe.data.id } }); - const data = schemaEventTypePublic.parse(eventType); - - if (eventType) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "EventType was not found", - error, - }); -} - -export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(eventTypeById)); diff --git a/pages/api/payments/[id].ts b/pages/api/payments/[id].ts new file mode 100644 index 0000000000..a9ee3c78fe --- /dev/null +++ b/pages/api/payments/[id].ts @@ -0,0 +1,130 @@ +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 } from "@lib/validations/payment"; +import { + schemaQueryIdParseInt, + withValidQueryIdTransformParseInt, +} from "@lib/validations/shared/queryIdTransformParseInt"; + +/** + * @swagger + * /api/payments/{id}: + * get: + * summary: Get a payment by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the payment to get + * tags: + * - payments + * responses: + * 200: + * description: OK + * 401: + * 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) { + 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); + + 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, + }) + .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)); diff --git a/pages/api/payments/[id]/delete.ts b/pages/api/payments/[id]/delete.ts deleted file mode 100644 index 104fbb0da9..0000000000 --- a/pages/api/payments/[id]/delete.ts +++ /dev/null @@ -1,50 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { BaseResponse } from "@lib/types"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/payments/{id}/delete: - * 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 deletePayment(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query", safe.error); - - const data = await prisma.payment.delete({ where: { id: safe.data.id } }); - - if (data) res.status(200).json({ message: `Payment with id: ${safe.data.id} deleted successfully` }); - else - (error: Error) => - res.status(400).json({ - message: `Payment with id: ${safe.data.id} was not able to be processed`, - error, - }); -} - -export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deletePayment)); diff --git a/pages/api/payments/[id]/edit.ts b/pages/api/payments/[id]/edit.ts deleted file mode 100644 index 6b1549d66a..0000000000 --- a/pages/api/payments/[id]/edit.ts +++ /dev/null @@ -1,56 +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"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/payments/{id}/edit: - * patch: - * summary: Edit an existing payment - * parameters: - * - 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. - */ -export async function editPayment(req: NextApiRequest, res: NextApiResponse) { - const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); - const safeBody = await schemaPaymentBodyParams.safeParse(req.body); - - if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); - const payment = await prisma.payment.update({ - where: { id: safeQuery.data.id }, - data: safeBody.data, - }); - const data = schemaPaymentPublic.parse(payment); - - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, - error, - }); -} - -export default withMiddleware("HTTP_PATCH")(withValidQueryIdTransformParseInt(withValidPayment(editPayment))); diff --git a/pages/api/payments/[id]/index.ts b/pages/api/payments/[id]/index.ts deleted file mode 100644 index 5d4fa9547a..0000000000 --- a/pages/api/payments/[id]/index.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 { PaymentResponse } from "@lib/types"; -import { schemaPaymentPublic } from "@lib/validations/payment"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/payments/{id}: - * get: - * summary: Get a payment by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the payment to get - * tags: - * - payments - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: Payment was not found - */ -export async function paymentById(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query"); - - const payment = await prisma.payment.findUnique({ where: { id: safe.data.id } }); - const data = schemaPaymentPublic.parse(payment); - - if (payment) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "Payment was not found", - error, - }); -} - -export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(paymentById)); diff --git a/pages/api/reminder-mails/[id].ts b/pages/api/reminder-mails/[id].ts new file mode 100644 index 0000000000..5d101b4c6d --- /dev/null +++ b/pages/api/reminder-mails/[id].ts @@ -0,0 +1,130 @@ +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 } from "@lib/validations/reminder-mail"; +import { + schemaQueryIdParseInt, + withValidQueryIdTransformParseInt, +} from "@lib/validations/shared/queryIdTransformParseInt"; + +/** + * @swagger + * /api/reminder-mails/{id}: + * get: + * summary: Get a reminderMail by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the reminderMail to get + * tags: + * - reminder-mails + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: ReminderMail was not found + * patch: + * summary: Edit an existing reminderMail + * consumes: + * - application/json + * parameters: + * - in: body + * name: reminderMail + * description: The reminderMail to edit + * schema: + * type: object + * $ref: '#/components/schemas/ReminderMail' + * required: true + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the reminderMail to edit + * tags: + * - reminder-mails + * responses: + * 201: + * description: OK, reminderMail edited successfuly + * model: ReminderMail + * 400: + * description: Bad request. ReminderMail body is invalid. + * 401: + * description: Authorization information is missing or invalid. + * delete: + * summary: Remove an existing reminderMail + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the reminderMail to delete + * tags: + * - reminder-mails + * responses: + * 201: + * description: OK, reminderMail removed successfuly + * model: ReminderMail + * 400: + * description: Bad request. ReminderMail id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function reminderMailById(req: NextApiRequest, res: NextApiResponse) { + const { method, query, body } = req; + const safeQuery = await schemaQueryIdParseInt.safeParse(query); + const safeBody = await schemaReminderMailBodyParams.safeParse(body); + if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); + + switch (method) { + case "GET": + await prisma.reminderMail + .findUnique({ where: { id: safeQuery.data.id } }) + .then((data) => schemaReminderMailPublic.parse(data)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `ReminderMail with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "PATCH": + if (!safeBody.success) throw new Error("Invalid request body"); + await prisma.reminderMail + .update({ + where: { id: safeQuery.data.id }, + data: safeBody.data, + }) + .then((reminderMail) => schemaReminderMailPublic.parse(reminderMail)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `ReminderMail with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "DELETE": + await prisma.reminderMail + .delete({ where: { id: safeQuery.data.id } }) + .then(() => + res.status(200).json({ message: `ReminderMail with id: ${safeQuery.data.id} deleted successfully` }) + ) + .catch((error: Error) => + res.status(404).json({ message: `ReminderMail 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(reminderMailById)); diff --git a/pages/api/reminder-mails/[id]/delete.ts b/pages/api/reminder-mails/[id]/delete.ts deleted file mode 100644 index 6b3bad4fe9..0000000000 --- a/pages/api/reminder-mails/[id]/delete.ts +++ /dev/null @@ -1,50 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { BaseResponse } from "@lib/types"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/reminder-mails/{id}/delete: - * delete: - * summary: Remove an existing reminder mail - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the reminderMail to delete - * tags: - * - reminder-mails - * responses: - * 201: - * description: OK, reminderMail removed successfuly - * model: ReminderMail - * 400: - * description: Bad request. ReminderMail id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function deleteReminderMail(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query", safe.error); - - const data = await prisma.reminderMail.delete({ where: { id: safe.data.id } }); - - if (data) res.status(200).json({ message: `ReminderMail with id: ${safe.data.id} deleted successfully` }); - else - (error: Error) => - res.status(400).json({ - message: `ReminderMail with id: ${safe.data.id} was not able to be processed`, - error, - }); -} - -export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteReminderMail)); diff --git a/pages/api/reminder-mails/[id]/edit.ts b/pages/api/reminder-mails/[id]/edit.ts deleted file mode 100644 index 2fedbb8f86..0000000000 --- a/pages/api/reminder-mails/[id]/edit.ts +++ /dev/null @@ -1,62 +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"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/reminder-mails/{id}/edit: - * patch: - * summary: Edit an existing reminder mail - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the reminderMail to edit - * tags: - * - reminder-mails - * responses: - * 201: - * description: OK, reminderMail edited successfuly - * model: ReminderMail - * 400: - * description: Bad request. ReminderMail body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function editReminderMail(req: NextApiRequest, res: NextApiResponse) { - const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); - const safeBody = await schemaReminderMailBodyParams.safeParse(req.body); - - if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); - const reminderMail = await prisma.reminderMail.update({ - where: { id: safeQuery.data.id }, - data: safeBody.data, - }); - const data = schemaReminderMailPublic.parse(reminderMail); - - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, - error, - }); -} - -export default withMiddleware("HTTP_PATCH")( - withValidQueryIdTransformParseInt(withValidReminderMail(editReminderMail)) -); diff --git a/pages/api/reminder-mails/[id]/index.ts b/pages/api/reminder-mails/[id]/index.ts deleted file mode 100644 index 7b38d22cbe..0000000000 --- a/pages/api/reminder-mails/[id]/index.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 { ReminderMailResponse } from "@lib/types"; -import { schemaReminderMailPublic } from "@lib/validations/reminder-mail"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/reminder-mails/{id}: - * get: - * summary: Get a reminder mail by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the reminderMail to get - * tags: - * - reminder-mails - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: ReminderMail was not found - */ -export async function reminderMailById(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query"); - - const reminderMail = await prisma.reminderMail.findUnique({ where: { id: safe.data.id } }); - const data = schemaReminderMailPublic.parse(reminderMail); - - if (reminderMail) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "ReminderMail was not found", - error, - }); -} - -export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(reminderMailById)); diff --git a/pages/api/schedules/[id].ts b/pages/api/schedules/[id].ts new file mode 100644 index 0000000000..682dc334d2 --- /dev/null +++ b/pages/api/schedules/[id].ts @@ -0,0 +1,130 @@ +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 } from "@lib/validations/schedule"; +import { + schemaQueryIdParseInt, + withValidQueryIdTransformParseInt, +} from "@lib/validations/shared/queryIdTransformParseInt"; + +/** + * @swagger + * /api/schedules/{id}: + * get: + * summary: Get a schedule by ID + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the schedule to get + * tags: + * - schedules + * responses: + * 200: + * description: OK + * 401: + * description: Authorization information is missing or invalid. + * 404: + * description: Schedule was not found + * patch: + * summary: Edit an existing schedule + * consumes: + * - application/json + * parameters: + * - in: body + * name: schedule + * description: The schedule to edit + * schema: + * type: object + * $ref: '#/components/schemas/Schedule' + * required: true + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the schedule to edit + * tags: + * - schedules + * responses: + * 201: + * description: OK, schedule edited successfuly + * model: Schedule + * 400: + * description: Bad request. Schedule body is invalid. + * 401: + * description: Authorization information is missing or invalid. + * delete: + * summary: Remove an existing schedule + * parameters: + * - in: path + * name: id + * schema: + * type: integer + * required: true + * description: Numeric ID of the schedule to delete + * tags: + * - schedules + * responses: + * 201: + * description: OK, schedule removed successfuly + * model: Schedule + * 400: + * description: Bad request. Schedule id is invalid. + * 401: + * description: Authorization information is missing or invalid. + */ +export async function scheduleById(req: NextApiRequest, res: NextApiResponse) { + const { method, query, body } = req; + const safeQuery = await schemaQueryIdParseInt.safeParse(query); + const safeBody = await schemaScheduleBodyParams.safeParse(body); + if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); + + switch (method) { + case "GET": + await prisma.schedule + .findUnique({ where: { id: safeQuery.data.id } }) + .then((schedule) => schemaSchedulePublic.parse(schedule)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `Schedule with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "PATCH": + if (!safeBody.success) throw new Error("Invalid request body"); + await prisma.schedule + .update({ + where: { id: safeQuery.data.id }, + data: safeBody.data, + }) + .then((schedule) => schemaSchedulePublic.parse(schedule)) + .then((data) => res.status(200).json({ data })) + .catch((error: Error) => + res.status(404).json({ message: `Schedule with id: ${safeQuery.data.id} not found`, error }) + ); + break; + + case "DELETE": + await prisma.schedule + .delete({ where: { id: safeQuery.data.id } }) + .then(() => + res.status(200).json({ message: `Schedule with id: ${safeQuery.data.id} deleted successfully` }) + ) + .catch((error: Error) => + res.status(404).json({ message: `Schedule 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(scheduleById)); diff --git a/pages/api/schedules/[id]/delete.ts b/pages/api/schedules/[id]/delete.ts deleted file mode 100644 index 4a85704a21..0000000000 --- a/pages/api/schedules/[id]/delete.ts +++ /dev/null @@ -1,43 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { BaseResponse } from "@lib/types"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/schedules/{id}/delete: - * delete: - * summary: Remove an existing schedule - * tags: - * - schedules - * responses: - * 201: - * description: OK, schedule removed successfuly - * model: Schedule - * 400: - * description: Bad request. Schedule id is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function deleteSchedule(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query", safe.error); - - const data = await prisma.schedule.delete({ where: { id: safe.data.id } }); - - if (data) res.status(200).json({ message: `Schedule with id: ${safe.data.id} deleted successfully` }); - else - (error: Error) => - res.status(400).json({ - message: `Schedule with id: ${safe.data.id} was not able to be processed`, - error, - }); -} - -export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteSchedule)); diff --git a/pages/api/schedules/[id]/edit.ts b/pages/api/schedules/[id]/edit.ts deleted file mode 100644 index 97fad524a0..0000000000 --- a/pages/api/schedules/[id]/edit.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 { ScheduleResponse } from "@lib/types"; -import { schemaScheduleBodyParams, schemaSchedulePublic, withValidSchedule } from "@lib/validations/schedule"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/schedules/{id}/edit: - * patch: - * summary: Edits an existing schedule - * tags: - * - schedules - * responses: - * 201: - * description: OK, schedule edited successfuly - * model: Schedule - * 400: - * description: Bad request. Schedule body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -export async function editSchedule(req: NextApiRequest, res: NextApiResponse) { - const safeQuery = await schemaQueryIdParseInt.safeParse(req.query); - const safeBody = await schemaScheduleBodyParams.safeParse(req.body); - - if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request"); - const schedule = await prisma.schedule.update({ - where: { id: safeQuery.data.id }, - data: safeBody.data, - }); - const data = schemaSchedulePublic.parse(schedule); - - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, - error, - }); -} - -export default withMiddleware("HTTP_PATCH")( - withValidQueryIdTransformParseInt(withValidSchedule(editSchedule)) -); diff --git a/pages/api/schedules/[id]/index.ts b/pages/api/schedules/[id]/index.ts deleted file mode 100644 index 8bdab1fca0..0000000000 --- a/pages/api/schedules/[id]/index.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 { ScheduleResponse } from "@lib/types"; -import { schemaSchedulePublic } from "@lib/validations/schedule"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; - -/** - * @swagger - * /api/schedules/{id}: - * get: - * summary: Get schedule by ID - * parameters: - * - in: path - * name: id - * schema: - * type: integer - * required: true - * description: Numeric ID of the schedule to delete - * tags: - * - schedules - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: Schedule was not found - */ -export async function scheduleById(req: NextApiRequest, res: NextApiResponse) { - const safe = await schemaQueryIdParseInt.safeParse(req.query); - if (!safe.success) throw new Error("Invalid request query"); - - const schedule = await prisma.schedule.findUnique({ where: { id: safe.data.id } }); - const data = schemaSchedulePublic.parse(schedule); - - if (schedule) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "Schedule was not found", - error, - }); -} - -export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(scheduleById)); diff --git a/pages/api/teams/[id].ts b/pages/api/teams/[id].ts index 49895d6937..21defc76b8 100644 --- a/pages/api/teams/[id].ts +++ b/pages/api/teams/[id].ts @@ -90,7 +90,7 @@ export async function teamById(req: NextApiRequest, res: NextApiResponse schemaTeamPublic.parse(data)) - .then((data: TeamResponse) => res.status(200).json({ data })) + .then((data) => res.status(200).json({ data })) .catch((error: Error) => res.status(404).json({ message: `Team with id: ${safeQuery.data.id} not found`, error }) ); @@ -104,7 +104,7 @@ export async function teamById(req: NextApiRequest, res: NextApiResponse schemaTeamPublic.parse(team)) - .then((data: TeamResponse) => res.status(200).json({ data })) + .then((data) => res.status(200).json({ data })) .catch((error: Error) => res.status(404).json({ message: `Team with id: ${safeQuery.data.id} not found`, error }) ); diff --git a/pages/api/users/[id].ts b/pages/api/users/[id].ts index 35adf9fa08..fa0b4c7255 100644 --- a/pages/api/users/[id].ts +++ b/pages/api/users/[id].ts @@ -89,8 +89,8 @@ export async function userById(req: NextApiRequest, res: NextApiResponse schemaUserPublic.parse(data)) - .then((data: UserResponse) => res.status(200).json({ data })) + .then((user) => schemaUserPublic.parse(user)) + .then((data) => res.status(200).json({ data })) .catch((error: Error) => res.status(404).json({ message: `User with id: ${safeQuery.data.id} not found`, error }) ); @@ -104,7 +104,7 @@ export async function userById(req: NextApiRequest, res: NextApiResponse schemaUserPublic.parse(user)) - .then((data: UserResponse) => res.status(200).json({ data })) + .then((data) => res.status(200).json({ data })) .catch((error: Error) => res.status(404).json({ message: `User with id: ${safeQuery.data.id} not found`, error }) ); diff --git a/prettier.rc.js b/prettier.rc.js deleted file mode 100644 index aa497c0ac5..0000000000 --- a/prettier.rc.js +++ /dev/null @@ -1,7 +0,0 @@ -module.exports = { - semi: false, - singleQuote: true, - arrowParens: "avoid", - trailingComma: "none", - endOfLine: "auto", -};