Scheduling infrastructure for absolutely everyone.
feat: update users/teams/apiKeys to use middleware
Agusti Fernandez Pardo20a93a13c949
diff --git a/lib/helpers/httpMethods.ts b/lib/helpers/httpMethods.tsnew file mode 100644index 00000000000..4bfeb1b1fee--- /dev/null+++ b/lib/helpers/httpMethods.ts@@ -0,0 +1,19 @@+import { NextMiddleware } from "next-api-middleware";++export const httpMethod = (+ allowedHttpMethod: "GET" | "POST" | "PATCH" | "DELETE"+): NextMiddleware => {+ return async function (req, res, next) {+ if (req.method === allowedHttpMethod || req.method == "OPTIONS") {+ await next();+ } else {+ res.status(404);+ res.end();+ }+ };+};++export const postOnly = httpMethod("POST");+export const getOnly = httpMethod("GET");+export const patchOnly = httpMethod("PATCH");+export const deleteOnly = httpMethod("DELETE");\ No newline at end of filediff --git a/lib/helpers/withMiddleware.ts b/lib/helpers/withMiddleware.tsindex d333439df87..7d94e5da7a9 100644--- a/lib/helpers/withMiddleware.ts+++ b/lib/helpers/withMiddleware.ts@@ -2,9 +2,13 @@ import { label } from "next-api-middleware";import { addRequestId } from "./addRequestid";import { captureErrors } from "./captureErrors";import { verifyApiKey } from "./verifyApiKey";-+import { postOnly, deleteOnly, patchOnly, getOnly } from "./httpMethods";const withMiddleware = label({+ getOnly,+ patchOnly,+ postOnly,+ deleteOnly,addRequestId,verifyApiKey,sentry: captureErrors, // <-- Optionally alias middlewarediff --git a/pages/api/api-keys/[id]/delete.ts b/pages/api/api-keys/[id]/delete.tsindex 10e6aaef983..722705a303f 100644--- a/pages/api/api-keys/[id]/delete.ts+++ b/pages/api/api-keys/[id]/delete.ts@@ -1,27 +1,29 @@-import prisma from "@calcom/prisma";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 apiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {- const { query, method } = req;- const safe = await schemaQueryIdAsString.safeParse(query);- if (method === "DELETE" && safe.success && safe.data) {- const apiKey = await prisma.apiKey+export async function deleteApiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {+ 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 (apiKey) res.status(200).json({ message: `apiKey with id: ${safe.data.id} deleted successfully` });+ 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: `Resource with id:${safe.data.id} was not found`});- // Reject any other HTTP method than POST- } else res.status(405).json({ message: "Only DELETE Method allowed" });+ else res.status(400).json({ message: `ApiKey with id: ${safe.data.id} was not able to be processed` });+ }}-export default withValidQueryIdString(apiKey);+export default withMiddleware("deleteOnly", "addRequestId")(+ withValidQueryIdString(+ deleteApiKey+ )+);diff --git a/pages/api/api-keys/[id]/edit.ts b/pages/api/api-keys/[id]/edit.tsindex 4fc45724444..d436faf9677 100644--- a/pages/api/api-keys/[id]/edit.ts+++ b/pages/api/api-keys/[id]/edit.ts@@ -1,9 +1,10 @@import prisma from "@calcom/prisma";-import { ApiKey } from "@prisma/client";+import { ApiKey } from "@calcom/prisma/client";import type { NextApiRequest, NextApiResponse } from "next";import { schemaApiKey, withValidApiKey } from "@lib/validations/apiKey";+import { withMiddleware } from "@lib/helpers/withMiddleware";import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";type ResponseData = {@@ -17,17 +18,19 @@ export async function editApiKey(req: NextApiRequest, res: NextApiResponse<Respoconst safeQuery = await schemaQueryIdAsString.safeParse(query);const safeBody = await schemaApiKey.safeParse(body);- if (method === "PATCH" && safeQuery.success && safeBody.success) {- await prisma.apiKey.update({+ if (safeQuery.success && safeBody.success) {+ const data = await prisma.apiKey.update({where: { id: safeQuery.data.id },data: safeBody.data,- }).then(apiKey => {- res.status(200).json({ data: apiKey });- }).catch(error => {- res.status(404).json({ message: `apiKey with ID ${safeQuery.data.id} not found and wasn't updated`, error })- });- // Reject any other HTTP method than POST- } else res.status(405).json({ message: "Only PATCH Method allowed for updating API keys" });+ })+ 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 withValidQueryIdString(withValidApiKey(editApiKey));+export default withMiddleware("patchOnly","addRequestId")(+ withValidQueryIdString(+ withValidApiKey(+ editApiKey)+ )+);diff --git a/pages/api/api-keys/[id]/index.ts b/pages/api/api-keys/[id]/index.tsindex 49b3a7f6a2b..18d6dbcc69a 100644--- a/pages/api/api-keys/[id]/index.ts+++ b/pages/api/api-keys/[id]/index.ts@@ -1,9 +1,10 @@import prisma from "@calcom/prisma";-import { ApiKey } from "@prisma/client";+import { ApiKey } from "@calcom/prisma/client";import type { NextApiRequest, NextApiResponse } from "next";import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";+import { withMiddleware } from "@lib/helpers/withMiddleware";type ResponseData = {data?: ApiKey;@@ -11,16 +12,19 @@ type ResponseData = {error?: unknown;};-export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {- const { query, method } = req;- const safe = await schemaQueryIdAsString.safeParse(query);- if (method === "GET" && safe.success) {- const apiKey = await prisma.apiKey.findUnique({ where: { id: safe.data.id } });- if (!apiKey) res.status(404).json({ message: "API key was not found" });- else res.status(200).json({ data: apiKey });- // Reject any other HTTP method than POST- } else res.status(405).json({ message: "Only GET Method allowed" });+export async function apiKeyById(req: NextApiRequest, res: NextApiResponse<ResponseData>) {+ 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({ message: "ApiKey was not found" });+ }}-export default withValidQueryIdString(apiKey);+export default withMiddleware("addRequestId","getOnly")(+ withValidQueryIdString(+ apiKeyById+ )+);diff --git a/pages/api/api-keys/index.ts b/pages/api/api-keys/index.tsindex 5155c36c596..eca77dbefe5 100644--- a/pages/api/api-keys/index.ts+++ b/pages/api/api-keys/index.ts@@ -1,18 +1,19 @@import prisma from "@calcom/prisma";-import { ApiKey } from "@prisma/client";+import { ApiKey } from "@calcom/prisma/client";+import { withMiddleware } from "@lib/helpers/withMiddleware";import type { NextApiRequest, NextApiResponse } from "next";type ResponseData = {data?: ApiKey[];error?: unknown;- message?: string;};-export default async function apiKeys(req: NextApiRequest, res: NextApiResponse<ResponseData>) {- const { method } = req;- if (method === "GET") {- const data = await prisma.apiKey.findMany({});- res.status(200).json({ data });- } else res.status(405).json({ message: "Only GET Method allowed" });+async function allApiKeys(req: NextApiRequest, res: NextApiResponse<ResponseData>) {+ 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","getOnly")(allApiKeys);\ No newline at end of filediff --git a/pages/api/api-keys/new.ts b/pages/api/api-keys/new.tsindex 8787824fbd0..b49b8a70497 100644--- a/pages/api/api-keys/new.ts+++ b/pages/api/api-keys/new.ts@@ -1,27 +1,28 @@-import prisma from "@calcom/prisma";--import { ApiKey } from "@prisma/client";import type { NextApiRequest, NextApiResponse } from "next";+import prisma from "@calcom/prisma";+import { ApiKey } from "@calcom/prisma/client";+import { withMiddleware } from "@lib/helpers/withMiddleware";import { schemaApiKey, withValidApiKey } from "@lib/validations/apiKey";+type ResponseData = {data?: ApiKey;- message?: string;- error?: string;+ error?: object;};async function createApiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {- const { body, method } = req;- const safe = schemaApiKey.safeParse(body);- if (method === "POST" && safe.success) {- const apiKey = await prisma.apiKey- .create({ data: { ...safe.data, user: { connect: { id: 1 } } } })-- if (apiKey) res.status(201).json({ data: apiKey });- else res.status(404).json({ message: "API Key not created" });-- } else res.status(405).json({ error: "Only POST Method allowed" });+ const safe = schemaApiKey.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: error } });+ }}-export default withValidApiKey(createApiKey);+export default withMiddleware("addRequestId","postOnly")(+ withValidApiKey(+ createApiKey+ )+);diff --git a/pages/api/booking-references/[id]/edit.ts b/pages/api/booking-references/[id]/edit.tsindex cdfe7a739ba..834574a3a1c 100644--- a/pages/api/booking-references/[id]/edit.ts+++ b/pages/api/booking-references/[id]/edit.ts@@ -3,7 +3,7 @@ import prisma from "@calcom/prisma";import { BookingReference } from "@calcom/prisma/client";import type { NextApiRequest, NextApiResponse } from "next";-import { schemaBookingReference, withValidBookingReference } from "@lib/validations/bookingReference";+import { schemaBookingReference, withValidBookingReference } from "@lib/validations/booking-reference";import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";type ResponseData = {diff --git a/pages/api/booking-references/[id]/index.ts b/pages/api/booking-references/[id]/index.tsindex ec973f50165..8a649fb4d98 100644--- a/pages/api/booking-references/[id]/index.ts+++ b/pages/api/booking-references/[id]/index.ts@@ -18,7 +18,7 @@ export async function bookingReference(req: NextApiRequest, res: NextApiResponseconst data = await prisma.bookingReference.findUnique({ where: { id: safe.data.id } });if (data) res.status(200).json({ data });- if (!data) res.status(404).json({ message: "Event type not found" });+ else res.status(404).json({ message: "Event type not found" });// Reject any other HTTP method than POST} else res.status(405).json({ message: "Only GET Method allowed" });}diff --git a/pages/api/booking-references/new.ts b/pages/api/booking-references/new.tsindex ba21befec81..e792e3f1c77 100644--- a/pages/api/booking-references/new.ts+++ b/pages/api/booking-references/new.ts@@ -3,7 +3,7 @@ import prisma from "@calcom/prisma";import { BookingReference } from "@calcom/prisma/client";import type { NextApiRequest, NextApiResponse } from "next";-import { schemaBookingReference, withValidBookingReference } from "@lib/validations/bookingReference";+import { schemaBookingReference, withValidBookingReference } from "@lib/validations/booking-reference";type ResponseData = {data?: BookingReference;diff --git a/pages/api/credentials/[id]/index.ts b/pages/api/credentials/[id]/index.tsindex 14ca8518670..a15e0463bcc 100644--- a/pages/api/credentials/[id]/index.ts+++ b/pages/api/credentials/[id]/index.ts@@ -18,7 +18,7 @@ export async function credential(req: NextApiRequest, res: NextApiResponse<Respoconst data = await prisma.credential.findUnique({ where: { id: safe.data.id } });if (data) res.status(200).json({ data });- if (!data) res.status(404).json({ message: "Event type not found" });+ else res.status(404).json({ message: "Event type not found" });// Reject any other HTTP method than POST} else res.status(405).json({ message: "Only GET Method allowed" });}diff --git a/pages/api/daily-event-references/[id]/delete.ts b/pages/api/daily-event-references/[id]/delete.tsindex 6b7a2a55416..2a43ab2d7e7 100644--- a/pages/api/daily-event-references/[id]/delete.ts+++ b/pages/api/daily-event-references/[id]/delete.ts@@ -3,6 +3,7 @@ import prisma from "@calcom/prisma";import type { NextApiRequest, NextApiResponse } from "next";import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";+import { withMiddleware } from "@lib/helpers/withMiddleware";type ResponseData = {@@ -11,17 +12,21 @@ type ResponseData = {};export async function deleteDailyEventReference(req: NextApiRequest, res: NextApiResponse<ResponseData>) {- const { query, method } = req;- const safe = await schemaQueryIdParseInt.safeParse(query);- if (method === "DELETE" && safe.success && safe.data) {- const dailyEventReference = await prisma.dailyEventReference+ const safe = await schemaQueryIdParseInt.safeParse(req.query);+ if (safe.success) {+ const deletedDailyEventReference = await prisma.dailyEventReference.delete({ where: { id: safe.data.id } })// We only remove the dailyEventReference type from the database if there's an existing resource.- if (dailyEventReference) res.status(200).json({ message: `dailyEventReference with id: ${safe.data.id} deleted successfully` });+ if (deletedDailyEventReference) res.status(200).json({ message: `dailyEventReference with id: ${safe.data.id} deleted successfully` });// This catches the error thrown by prisma.dailyEventReference.delete() if the resource is not found.else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`});// Reject any other HTTP method than POST} else res.status(405).json({ message: "Only DELETE Method allowed" });}-export default withValidQueryIdTransformParseInt(deleteDailyEventReference);+// export default withValidQueryIdTransformParseInt(deleteDailyEventReference);+export default withMiddleware("deleteOnly")(+ withValidQueryIdTransformParseInt(+ deleteDailyEventReference+ )+);diff --git a/pages/api/daily-event-references/[id]/index.ts b/pages/api/daily-event-references/[id]/index.tsindex c9b243f7072..166a661dcc4 100644--- a/pages/api/daily-event-references/[id]/index.ts+++ b/pages/api/daily-event-references/[id]/index.ts@@ -18,7 +18,7 @@ export async function dailyEventReference(req: NextApiRequest, res: NextApiRespoconst data = await prisma.dailyEventReference.findUnique({ where: { id: safe.data.id } });if (data) res.status(200).json({ data });- if (!data) res.status(404).json({ message: "Event type not found" });+ else res.status(404).json({ message: "Event type not found" });// Reject any other HTTP method than POST} else res.status(405).json({ message: "Only GET Method allowed" });}diff --git a/pages/api/destination-calendars/[id]/index.ts b/pages/api/destination-calendars/[id]/index.tsindex 95a14093720..95a44a5edff 100644--- a/pages/api/destination-calendars/[id]/index.ts+++ b/pages/api/destination-calendars/[id]/index.ts@@ -18,7 +18,7 @@ export async function destinationCalendar(req: NextApiRequest, res: NextApiRespoconst data = await prisma.destinationCalendar.findUnique({ where: { id: safe.data.id } });if (data) res.status(200).json({ data });- if (!data) res.status(404).json({ message: "Event type not found" });+ else res.status(404).json({ message: "Event type not found" });// Reject any other HTTP method than POST} else res.status(405).json({ message: "Only GET Method allowed" });}diff --git a/pages/api/event-type-custom-inputs/[id]/index.ts b/pages/api/event-type-custom-inputs/[id]/index.tsindex 3b7a926dcd5..c53b2e4c060 100644--- a/pages/api/event-type-custom-inputs/[id]/index.ts+++ b/pages/api/event-type-custom-inputs/[id]/index.ts@@ -18,7 +18,7 @@ export async function eventTypeCustomInput(req: NextApiRequest, res: NextApiRespconst data = await prisma.eventTypeCustomInput.findUnique({ where: { id: safe.data.id } });if (data) res.status(200).json({ data });- if (!data) res.status(404).json({ message: "Event type not found" });+ else res.status(404).json({ message: "Event type not found" });// Reject any other HTTP method than POST} else res.status(405).json({ message: "Only GET Method allowed" });}diff --git a/pages/api/memberships/[id]/index.ts b/pages/api/memberships/[id]/index.tsindex 7f56aa3889c..8f1496ddc59 100644--- a/pages/api/memberships/[id]/index.ts+++ b/pages/api/memberships/[id]/index.ts@@ -18,7 +18,7 @@ export async function membership(req: NextApiRequest, res: NextApiResponse<Respoconst data = await prisma.membership.findUnique({ where: { id: safe.data.id } });if (data) res.status(200).json({ data });- if (!data) res.status(404).json({ message: "Event type not found" });+ else res.status(404).json({ message: "Event type not found" });// Reject any other HTTP method than POST} else res.status(405).json({ message: "Only GET Method allowed" });}diff --git a/pages/api/schedules/[id]/index.ts b/pages/api/schedules/[id]/index.tsindex f6d3621a809..64ecb6c4ca3 100644--- a/pages/api/schedules/[id]/index.ts+++ b/pages/api/schedules/[id]/index.ts@@ -18,7 +18,7 @@ export async function schedule(req: NextApiRequest, res: NextApiResponse<Responsconst data = await prisma.schedule.findUnique({ where: { id: safe.data.id } });if (data) res.status(200).json({ data });- if (!data) res.status(404).json({ message: "Event type not found" });+ else res.status(404).json({ message: "Event type not found" });// Reject any other HTTP method than POST} else res.status(405).json({ message: "Only GET Method allowed" });}diff --git a/pages/api/selected-calendars/[id]/index.ts b/pages/api/selected-calendars/[id]/index.tsindex d89fb31398e..6afe9d3135c 100644--- a/pages/api/selected-calendars/[id]/index.ts+++ b/pages/api/selected-calendars/[id]/index.ts@@ -18,7 +18,7 @@ export async function selectedCalendar(req: NextApiRequest, res: NextApiResponseconst data = await prisma.selectedCalendar.findUnique({ where: { id: safe.data.id } });if (data) res.status(200).json({ data });- if (!data) res.status(404).json({ message: "Event type not found" });+ else res.status(404).json({ message: "Event type not found" });// Reject any other HTTP method than POST} else res.status(405).json({ message: "Only GET Method allowed" });}diff --git a/pages/api/teams/[id]/delete.ts b/pages/api/teams/[id]/delete.tsindex 9ca57af0a4a..05933134406 100644--- a/pages/api/teams/[id]/delete.ts+++ b/pages/api/teams/[id]/delete.ts@@ -1,27 +1,29 @@-import prisma from "@calcom/prisma";import type { NextApiRequest, NextApiResponse } from "next";+import prisma from "@calcom/prisma";+import { withMiddleware } from "@lib/helpers/withMiddleware";import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";-type ResponseData = {message?: string;error?: unknown;};export async function deleteTeam(req: NextApiRequest, res: NextApiResponse<ResponseData>) {- const { query, method } = req;- const safe = await schemaQueryIdParseInt.safeParse(query);- if (method === "DELETE" && safe.success && safe.data) {- const team = await prisma.team+ const safe = await schemaQueryIdParseInt.safeParse(req.query);+ if (safe.success) {+ const data = await prisma.team.delete({ where: { id: safe.data.id } })// We only remove the team type from the database if there's an existing resource.- if (team) res.status(200).json({ message: `team with id: ${safe.data.id} deleted successfully` });+ if (data) res.status(200).json({ message: `Team with id: ${safe.data.id} deleted successfully` });// This catches the error thrown by prisma.team.delete() if the resource is not found.- else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`});- // Reject any other HTTP method than POST- } else res.status(405).json({ message: "Only DELETE Method allowed" });+ else res.status(400).json({ message: `Team with id: ${safe.data.id} was not able to be processed` });+ }}-export default withValidQueryIdTransformParseInt(deleteTeam);+export default withMiddleware("deleteOnly", "addRequestId")(+ withValidQueryIdTransformParseInt(+ deleteTeam+ )+);diff --git a/pages/api/teams/[id]/edit.ts b/pages/api/teams/[id]/edit.tsindex c046c97da4e..96e2fa5d3ad 100644--- a/pages/api/teams/[id]/edit.ts+++ b/pages/api/teams/[id]/edit.ts@@ -4,6 +4,7 @@ import { Team } from "@calcom/prisma/client";import type { NextApiRequest, NextApiResponse } from "next";import { schemaTeam, withValidTeam } from "@lib/validations/team";+import { withMiddleware } from "@lib/helpers/withMiddleware";import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";type ResponseData = {@@ -17,21 +18,19 @@ export async function editTeam(req: NextApiRequest, res: NextApiResponse<Responsconst safeQuery = await schemaQueryIdParseInt.safeParse(query);const safeBody = await schemaTeam.safeParse(body);- if (method === "PATCH") {- if (safeQuery.success && safeBody.success) {- await prisma.team.update({+ if (safeQuery.success && safeBody.success) {+ const data = await prisma.team.update({where: { id: safeQuery.data.id },data: safeBody.data,- }).then(team => {- res.status(200).json({ data: team });- }).catch(error => {- res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error })- });- }- } else {- // Reject any other HTTP method than POST- res.status(405).json({ message: "Only PATCH Method allowed for updating teams" });+ })+ 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 withValidQueryIdTransformParseInt(withValidTeam(editTeam));+export default withMiddleware("patchOnly","addRequestId")(+ withValidQueryIdTransformParseInt(+ withValidTeam(+ editTeam)+ )+);diff --git a/pages/api/teams/[id]/index.ts b/pages/api/teams/[id]/index.tsindex cd9cd660d46..9868c043fde 100644--- a/pages/api/teams/[id]/index.ts+++ b/pages/api/teams/[id]/index.ts@@ -4,6 +4,7 @@ import { Team } from "@calcom/prisma/client";import type { NextApiRequest, NextApiResponse } from "next";import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";+import { withMiddleware } from "@lib/helpers/withMiddleware";type ResponseData = {data?: Team;@@ -11,20 +12,19 @@ type ResponseData = {error?: unknown;};-export async function team(req: NextApiRequest, res: NextApiResponse<ResponseData>) {- const { query, method } = req;- const safe = await schemaQueryIdParseInt.safeParse(query);-- if (method === "GET" && safe.success) {- const team = await prisma.team.findUnique({ where: { id: safe.data.id } });+export async function teamById(req: NextApiRequest, res: NextApiResponse<ResponseData>) {+ const safe = await schemaQueryIdParseInt.safeParse(req.query);+ if (safe.success) {+ const data = await prisma.team.findUnique({ where: { id: safe.data.id } });- if (team) res.status(200).json({ data: team });- if (!team) res.status(404).json({ message: "Event type not found" });- } else {- // Reject any other HTTP method than POST- res.status(405).json({ message: "Only GET Method allowed" });+ if (data) res.status(200).json({ data });+ else res.status(404).json({ message: "Team was not found" });}}-export default withValidQueryIdTransformParseInt(team);+export default withMiddleware("addRequestId","getOnly")(+ withValidQueryIdTransformParseInt(+ teamById+ )+);diff --git a/pages/api/teams/index.ts b/pages/api/teams/index.tsindex 0b57f216045..1fd88d492f5 100644--- a/pages/api/teams/index.ts+++ b/pages/api/teams/index.ts@@ -1,6 +1,7 @@import prisma from "@calcom/prisma";import { Team } from "@calcom/prisma/client";+import { withMiddleware } from "@lib/helpers/withMiddleware";import type { NextApiRequest, NextApiResponse } from "next";type ResponseData = {@@ -8,12 +9,11 @@ type ResponseData = {error?: unknown;};-export default async function team(req: NextApiRequest, res: NextApiResponse<ResponseData>) {- try {- const data = await prisma.team.findMany();- res.status(200).json({ data });- } catch (error) {- // FIXME: Add zod for validation/error handling- res.status(400).json({ error: error });- }+async function allTeams(req: NextApiRequest, res: NextApiResponse<ResponseData>) {+ const data = await prisma.team.findMany();++ if (data) res.status(200).json({ data });+ else res.status(400).json({ error: "No data found" });}++export default withMiddleware("addRequestId","getOnly")(allTeams);\ No newline at end of filediff --git a/pages/api/teams/new.ts b/pages/api/teams/new.tsindex ebcb5137771..7ae4d0034e2 100644--- a/pages/api/teams/new.ts+++ b/pages/api/teams/new.ts@@ -1,30 +1,28 @@-import prisma from "@calcom/prisma";--import { Team } from "@calcom/prisma/client";import type { NextApiRequest, NextApiResponse } from "next";+import prisma from "@calcom/prisma";+import { Team } from "@calcom/prisma/client";+import { withMiddleware } from "@lib/helpers/withMiddleware";import { schemaTeam, withValidTeam } from "@lib/validations/team";+type ResponseData = {data?: Team;- message?: string;- error?: string;+ error?: object;};async function createTeam(req: NextApiRequest, res: NextApiResponse<ResponseData>) {- const { body, method } = req;- if (method === "POST") {- const safe = schemaTeam.safeParse(body);- if (safe.success && safe.data) {- await prisma.team- .create({ data: safe.data })- .then((team) => res.status(201).json({ data: team }))- .catch((error) => res.status(400).json({ message: "Could not create team", error: error }));- }- } else {- // Reject any other HTTP method than POST- res.status(405).json({ error: "Only POST Method allowed" });+ const safe = schemaTeam.safeParse(req.body);+ if (safe.success) {+ const data = await prisma.team+ .create({ data: safe.data })+ if (data) res.status(201).json({ data })+ else (error: unknown) => res.status(400).json({ error: { message: "Could not create team type", error: error } });}}-export default withValidTeam(createTeam);+export default withMiddleware("addRequestId","postOnly")(+ withValidTeam(+ createTeam+ )+);diff --git a/pages/api/users/[id]/delete.ts b/pages/api/users/[id]/delete.tsindex 1a2a2b240e8..6542b991348 100644--- a/pages/api/users/[id]/delete.ts+++ b/pages/api/users/[id]/delete.ts@@ -5,24 +5,25 @@ import prisma from "@calcom/prisma";import { withMiddleware } from "@lib/helpers/withMiddleware";import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";-type ResponseData = {message?: string;error?: unknown;};export async function deleteUser(req: NextApiRequest, res: NextApiResponse<ResponseData>) {- const { query, method } = req;- const safe = await schemaQueryIdParseInt.safeParse(query);- if (method === "DELETE" && safe.success && safe.data) {- const user = await prisma.user+ const safe = await schemaQueryIdParseInt.safeParse(req.query);+ if (safe.success) {+ const data = await prisma.user.delete({ where: { id: safe.data.id } })// We only remove the user type from the database if there's an existing resource.- if (user) res.status(200).json({ message: `user with id: ${safe.data.id} deleted successfully` });+ if (data) res.status(200).json({ message: `User with id: ${safe.data.id} deleted successfully` });// This catches the error thrown by prisma.user.delete() if the resource is not found.- else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`});- // Reject any other HTTP method than POST- } else res.status(405).json({ message: "Only DELETE Method allowed" });+ else res.status(400).json({ message: `User with id: ${safe.data.id} was not able to be processed` });+ }}-export default withMiddleware("addRequestId")((withValidQueryIdTransformParseInt(deleteUser)));+export default withMiddleware("deleteOnly", "addRequestId")(+ withValidQueryIdTransformParseInt(+ deleteUser+ )+);diff --git a/pages/api/users/[id]/edit.ts b/pages/api/users/[id]/edit.tsindex a59a0df9401..3e5425b3d96 100644--- a/pages/api/users/[id]/edit.ts+++ b/pages/api/users/[id]/edit.ts@@ -18,16 +18,19 @@ export async function editUser(req: NextApiRequest, res: NextApiResponse<Responsconst safeQuery = await schemaQueryIdParseInt.safeParse(query);const safeBody = await schemaUser.safeParse(body);- if (method === "PATCH" && safeQuery.success && safeBody.success) {+ if (safeQuery.success && safeBody.success) {const data = await prisma.user.update({where: { id: safeQuery.data.id },data: safeBody.data,})if (data) res.status(200).json({ data });- else res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error })-- // Reject any other HTTP method than POST- } else res.status(405).json({ message: "Only PATCH Method allowed for updating users" });+ 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("addRequestId")(withValidQueryIdTransformParseInt(withValidUser(editUser)));+export default withMiddleware("patchOnly","addRequestId")(+ withValidQueryIdTransformParseInt(+ withValidUser(+ editUser)+ )+);diff --git a/pages/api/users/[id]/index.ts b/pages/api/users/[id]/index.tsindex cbd088e9dfc..6264acd115b 100644--- a/pages/api/users/[id]/index.ts+++ b/pages/api/users/[id]/index.ts@@ -12,17 +12,19 @@ type ResponseData = {error?: unknown;};-export async function user(req: NextApiRequest, res: NextApiResponse<ResponseData>) {- const { query, method } = req;- const safe = await schemaQueryIdParseInt.safeParse(query);- if (method === "GET" && safe.success) {+export async function userById(req: NextApiRequest, res: NextApiResponse<ResponseData>) {+ const safe = await schemaQueryIdParseInt.safeParse(req.query);+ if (safe.success) {const data = await prisma.user.findUnique({ where: { id: safe.data.id } });if (data) res.status(200).json({ data });- if (!data) res.status(404).json({ message: "Event type not found" });- // Reject any other HTTP method than POST- } else res.status(405).json({ message: "Only GET Method allowed" });+ else res.status(404).json({ message: "User was not found" });+ }}-export default withMiddleware("addRequestId")(withValidQueryIdTransformParseInt(user));+export default withMiddleware("addRequestId","getOnly")(+ withValidQueryIdTransformParseInt(+ userById+ )+);diff --git a/pages/api/users/index.ts b/pages/api/users/index.tsindex 9c7473252fc..ef75e93cc7a 100644--- a/pages/api/users/index.ts+++ b/pages/api/users/index.ts@@ -9,10 +9,11 @@ type ResponseData = {error?: unknown;};-async function user(req: NextApiRequest, res: NextApiResponse<ResponseData>) {+async function allUsers(req: NextApiRequest, res: NextApiResponse<ResponseData>) {const data = await prisma.user.findMany();+if (data) res.status(200).json({ data });else res.status(400).json({ error: "No data found" });}-export default withMiddleware("addRequestId")(user);\ No newline at end of file+export default withMiddleware("addRequestId","getOnly")(allUsers);\ No newline at end of filediff --git a/pages/api/users/new.ts b/pages/api/users/new.tsindex 1ad079ca446..6ce94b5df0a 100644--- a/pages/api/users/new.ts+++ b/pages/api/users/new.ts@@ -12,15 +12,17 @@ type ResponseData = {};async function createUser(req: NextApiRequest, res: NextApiResponse<ResponseData>) {- const { body, method } = req;- const safe = schemaUser.safeParse(body);- if (method === "POST" && safe.success) {+ const safe = schemaUser.safeParse(req.body);+ if (safe.success) {const data = await prisma.user.create({ data: safe.data })if (data) res.status(201).json({ data })else (error: unknown) => res.status(400).json({ error: { message: "Could not create user type", error: error } });- // Reject any other HTTP method than POST- } else res.status(405).json({ error: { message: "Only POST Method allowed" } });+ }}-export default withMiddleware("addRequestId")(withValidUser(createUser));+export default withMiddleware("addRequestId","postOnly")(+ withValidUser(+ createUser+ )+);