Merge pull request #11 from calcom/chores/ignore-yarn-error

pull/9078/head
Agusti Fernandez 2022-03-29 04:04:57 +02:00 committed by GitHub
commit 39ae2aed37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
39 changed files with 248 additions and 16163 deletions

3
.gitignore vendored
View File

@ -70,3 +70,6 @@ dist
# Linting
lint-results
# Yarn
yarn-error.log

View File

@ -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");

View File

@ -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
@ -12,4 +16,4 @@ const withMiddleware = label(
["sentry","verifyApiKey"] // <-- Provide a list of middleware to call automatically
);
export default withMiddleware;
export { withMiddleware };

View File

@ -5,6 +5,9 @@ import { z } from "zod";
// at different endpoints that require this validation.
const schemaQueryIdAsString = z
.object({
// since we added apiKey as query param this is required by next-validations helper
// for query params to work properly and not fail.
apiKey: z.string().cuid(),
// since nextjs parses query params as strings,
// we need to cast them to numbers using z.transform() and parseInt()
id: z.string()

View File

@ -5,6 +5,9 @@ import { z } from "zod";
// at different endpoints that require this validation.
const schemaQueryIdParseInt = z
.object({
// since we added apiKey as query param this is required by next-validations helper
// for query params to work properly and not fail.
apiKey: z.string().cuid(),
// since nextjs parses query params as strings,
// we need to cast them to numbers using z.transform() and parseInt()
id: z

View File

@ -3,7 +3,7 @@ import { NextRequest, NextResponse } from "next/server";
// Not much useful yet as prisma.client can't be used in the middlewares (client is not available)
// For now we just throw early if no apiKey is passed,
// but we could also check if the apiKey is valid if we had prisma here.
export async function middleware({ nextUrl }: NextRequest) {
export async function requireApiKeyAsQueryParams({ nextUrl }: NextRequest) {
const response = NextResponse.next();
const apiKey = nextUrl.searchParams.get("apiKey");

View File

@ -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
)
);

View File

@ -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)
)
);

View File

@ -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
)
);

View File

@ -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);

View File

@ -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
)
);

View File

@ -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 = {

View File

@ -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" });
}

View File

@ -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;

View File

@ -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" });
}

View File

@ -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
)
);

View File

@ -3,7 +3,7 @@ import prisma from "@calcom/prisma";
import { DailyEventReference } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaDailyEventReference, withValidDailyEventReference } from "@lib/validations/dailyEventReference";
import { schemaDailyEventReference, withValidDailyEventReference } from "@lib/validations/daily-event-reference";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {

View File

@ -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" });
}

View File

@ -3,7 +3,7 @@ import prisma from "@calcom/prisma";
import { DailyEventReference } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaDailyEventReference, withValidDailyEventReference } from "@lib/validations/dailyEventReference";
import { schemaDailyEventReference, withValidDailyEventReference } from "@lib/validations/daily-event-reference";
type ResponseData = {
data?: DailyEventReference;

View File

@ -3,7 +3,7 @@ import prisma from "@calcom/prisma";
import { DestinationCalendar } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaDestinationCalendar, withValidDestinationCalendar } from "@lib/validations/destinationCalendar";
import { schemaDestinationCalendar, withValidDestinationCalendar } from "@lib/validations/destination-calendar";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {

View File

@ -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" });
}

View File

@ -3,7 +3,7 @@ import prisma from "@calcom/prisma";
import { DestinationCalendar } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaDestinationCalendar, withValidDestinationCalendar } from "@lib/validations/destinationCalendar";
import { schemaDestinationCalendar, withValidDestinationCalendar } from "@lib/validations/destination-calendar";
type ResponseData = {
data?: DestinationCalendar;

View File

@ -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" });
}

View File

@ -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" });
}

View File

@ -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" });
}

View File

@ -3,7 +3,7 @@ import prisma from "@calcom/prisma";
import { SelectedCalendar } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaSelectedCalendar, withValidSelectedCalendar } from "@lib/validations/selectedCalendar";
import { schemaSelectedCalendar, withValidSelectedCalendar } from "@lib/validations/selected-calendar";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {

View File

@ -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" });
}

View File

@ -3,7 +3,7 @@ import prisma from "@calcom/prisma";
import { SelectedCalendar } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaSelectedCalendar, withValidSelectedCalendar } from "@lib/validations/selectedCalendar";
import { schemaSelectedCalendar, withValidSelectedCalendar } from "@lib/validations/selected-calendar";
type ResponseData = {
data?: SelectedCalendar;

View File

@ -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
)
);

View File

@ -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)
)
);

View File

@ -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
)
);

View File

@ -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);

View File

@ -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
)
);

View File

@ -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 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 withValidQueryIdTransformParseInt(deleteUser);
export default withMiddleware("deleteOnly", "addRequestId")(
withValidQueryIdTransformParseInt(
deleteUser
)
);

View File

@ -4,6 +4,7 @@ import { User } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaUser, withValidUser } from "@lib/validations/user";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
@ -17,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 withValidQueryIdTransformParseInt(withValidUser(editUser));
export default withMiddleware("patchOnly","addRequestId")(
withValidQueryIdTransformParseInt(
withValidUser(
editUser)
)
);

View File

@ -4,6 +4,7 @@ import { User } 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?: User;
@ -11,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 withValidQueryIdTransformParseInt(user);
export default withMiddleware("addRequestId","getOnly")(
withValidQueryIdTransformParseInt(
userById
)
);

View File

@ -1,7 +1,7 @@
import prisma from "@calcom/prisma";
import { User } from "@calcom/prisma/client";
import withMiddleware from "@lib/helpers/withMiddleware";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { NextApiRequest, NextApiResponse } from "next";
type ResponseData = {
@ -9,9 +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);

View File

@ -1,26 +1,28 @@
import prisma from "@calcom/prisma";
import { User } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { User } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { schemaUser, withValidUser } from "@lib/validations/user";
type ResponseData = {
data?: User;
message?: string;
error?: string;
error?: object;
};
async function createUser(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req;
const safe = schemaUser.safeParse(body);
if (method === "POST" && safe.success) {
await prisma.user
.create({ data: safe.data })
.then((data) => res.status(201).json({ data }))
.catch((error) => res.status(400).json({ message: "Could not create user type", error: error }));
// Reject any other HTTP method than POST
} else res.status(405).json({ error: "Only POST Method allowed" });
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 } });
}
}
export default withValidUser(createUser);
export default withMiddleware("addRequestId","postOnly")(
withValidUser(
createUser
)
);

File diff suppressed because it is too large Load Diff