feat: update users/teams/apiKeys to use middleware
parent
3011e0ecf7
commit
20a93a13c9
|
@ -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");
|
|
@ -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 middleware
|
||||
|
|
|
@ -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
|
||||
)
|
||||
);
|
||||
|
|
|
@ -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<Respo
|
|||
const 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)
|
||||
)
|
||||
);
|
||||
|
|
|
@ -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
|
||||
)
|
||||
);
|
||||
|
|
|
@ -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);
|
|
@ -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
|
||||
)
|
||||
);
|
||||
|
|
|
@ -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 = {
|
||||
|
|
|
@ -18,7 +18,7 @@ export async function bookingReference(req: NextApiRequest, res: NextApiResponse
|
|||
const 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" });
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -18,7 +18,7 @@ export async function credential(req: NextApiRequest, res: NextApiResponse<Respo
|
|||
const 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" });
|
||||
}
|
||||
|
|
|
@ -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
|
||||
)
|
||||
);
|
||||
|
|
|
@ -18,7 +18,7 @@ export async function dailyEventReference(req: NextApiRequest, res: NextApiRespo
|
|||
const 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" });
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ export async function destinationCalendar(req: NextApiRequest, res: NextApiRespo
|
|||
const 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" });
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ export async function eventTypeCustomInput(req: NextApiRequest, res: NextApiResp
|
|||
const 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" });
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ export async function membership(req: NextApiRequest, res: NextApiResponse<Respo
|
|||
const 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" });
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ export async function schedule(req: NextApiRequest, res: NextApiResponse<Respons
|
|||
const 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" });
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ export async function selectedCalendar(req: NextApiRequest, res: NextApiResponse
|
|||
const 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" });
|
||||
}
|
||||
|
|
|
@ -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
|
||||
)
|
||||
);
|
||||
|
|
|
@ -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<Respons
|
|||
const 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)
|
||||
)
|
||||
);
|
||||
|
|
|
@ -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
|
||||
)
|
||||
);
|
||||
|
|
|
@ -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);
|
|
@ -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
|
||||
)
|
||||
);
|
||||
|
|
|
@ -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
|
||||
)
|
||||
);
|
||||
|
|
|
@ -18,16 +18,19 @@ export async function editUser(req: NextApiRequest, res: NextApiResponse<Respons
|
|||
const 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)
|
||||
)
|
||||
);
|
||||
|
|
|
@ -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
|
||||
)
|
||||
);
|
||||
|
|
|
@ -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);
|
||||
export default withMiddleware("addRequestId","getOnly")(allUsers);
|
|
@ -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
|
||||
)
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue