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 # Linting
lint-results 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 { addRequestId } from "./addRequestid";
import { captureErrors } from "./captureErrors"; import { captureErrors } from "./captureErrors";
import { verifyApiKey } from "./verifyApiKey"; import { verifyApiKey } from "./verifyApiKey";
import { postOnly, deleteOnly, patchOnly, getOnly } from "./httpMethods";
const withMiddleware = label( const withMiddleware = label(
{ {
getOnly,
patchOnly,
postOnly,
deleteOnly,
addRequestId, addRequestId,
verifyApiKey, verifyApiKey,
sentry: captureErrors, // <-- Optionally alias middleware sentry: captureErrors, // <-- Optionally alias middleware
@ -12,4 +16,4 @@ const withMiddleware = label(
["sentry","verifyApiKey"] // <-- Provide a list of middleware to call automatically ["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. // at different endpoints that require this validation.
const schemaQueryIdAsString = z const schemaQueryIdAsString = z
.object({ .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, // since nextjs parses query params as strings,
// we need to cast them to numbers using z.transform() and parseInt() // we need to cast them to numbers using z.transform() and parseInt()
id: z.string() id: z.string()

View File

@ -5,6 +5,9 @@ import { z } from "zod";
// at different endpoints that require this validation. // at different endpoints that require this validation.
const schemaQueryIdParseInt = z const schemaQueryIdParseInt = z
.object({ .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, // since nextjs parses query params as strings,
// we need to cast them to numbers using z.transform() and parseInt() // we need to cast them to numbers using z.transform() and parseInt()
id: z 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) // 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, // 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. // 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 response = NextResponse.next();
const apiKey = nextUrl.searchParams.get("apiKey"); const apiKey = nextUrl.searchParams.get("apiKey");

View File

@ -1,27 +1,29 @@
import prisma from "@calcom/prisma";
import type { NextApiRequest, NextApiResponse } from "next"; 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"; import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
type ResponseData = { type ResponseData = {
message?: string; message?: string;
error?: unknown; error?: unknown;
}; };
export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) { export async function deleteApiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req; const safe = await schemaQueryIdAsString.safeParse(req.query);
const safe = await schemaQueryIdAsString.safeParse(query); if (safe.success) {
if (method === "DELETE" && safe.success && safe.data) { const data = await prisma.apiKey
const apiKey = await prisma.apiKey
.delete({ where: { id: safe.data.id } }) .delete({ where: { id: safe.data.id } })
// We only remove the apiKey type from the database if there's an existing resource. // 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. // 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`}); else res.status(400).json({ message: `ApiKey with id: ${safe.data.id} was not able to be processed` });
// Reject any other HTTP method than POST }
} else res.status(405).json({ message: "Only DELETE Method allowed" });
} }
export default withValidQueryIdString(apiKey); export default withMiddleware("deleteOnly", "addRequestId")(
withValidQueryIdString(
deleteApiKey
)
);

View File

@ -1,9 +1,10 @@
import prisma from "@calcom/prisma"; import prisma from "@calcom/prisma";
import { ApiKey } from "@prisma/client"; import { ApiKey } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
import { schemaApiKey, withValidApiKey } from "@lib/validations/apiKey"; import { schemaApiKey, withValidApiKey } from "@lib/validations/apiKey";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString"; import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
type ResponseData = { type ResponseData = {
@ -17,17 +18,19 @@ export async function editApiKey(req: NextApiRequest, res: NextApiResponse<Respo
const safeQuery = await schemaQueryIdAsString.safeParse(query); const safeQuery = await schemaQueryIdAsString.safeParse(query);
const safeBody = await schemaApiKey.safeParse(body); const safeBody = await schemaApiKey.safeParse(body);
if (method === "PATCH" && safeQuery.success && safeBody.success) { if (safeQuery.success && safeBody.success) {
await prisma.apiKey.update({ const data = await prisma.apiKey.update({
where: { id: safeQuery.data.id }, where: { id: safeQuery.data.id },
data: safeBody.data, data: safeBody.data,
}).then(apiKey => { })
res.status(200).json({ data: apiKey }); if (data) res.status(200).json({ data });
}).catch(error => { else (error: unknown) => res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, 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" });
} }
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 prisma from "@calcom/prisma";
import { ApiKey } from "@prisma/client"; import { ApiKey } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString"; import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
import { withMiddleware } from "@lib/helpers/withMiddleware";
type ResponseData = { type ResponseData = {
data?: ApiKey; data?: ApiKey;
@ -11,16 +12,19 @@ type ResponseData = {
error?: unknown; error?: unknown;
}; };
export async function apiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) { export async function apiKeyById(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req; const safe = await schemaQueryIdAsString.safeParse(req.query);
const safe = await schemaQueryIdAsString.safeParse(query); if (safe.success) {
if (method === "GET" && safe.success) { const data = await prisma.apiKey.findUnique({ where: { id: safe.data.id } });
const apiKey = await prisma.apiKey.findUnique({ where: { id: safe.data.id } });
if (!apiKey) res.status(404).json({ message: "API key was not found" }); if (data) res.status(200).json({ data });
else res.status(200).json({ data: apiKey }); else res.status(404).json({ message: "ApiKey was not found" });
// Reject any other HTTP method than POST }
} else res.status(405).json({ message: "Only GET Method allowed" });
} }
export default withValidQueryIdString(apiKey); export default withMiddleware("addRequestId","getOnly")(
withValidQueryIdString(
apiKeyById
)
);

View File

@ -1,18 +1,19 @@
import prisma from "@calcom/prisma"; 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"; import type { NextApiRequest, NextApiResponse } from "next";
type ResponseData = { type ResponseData = {
data?: ApiKey[]; data?: ApiKey[];
error?: unknown; error?: unknown;
message?: string;
}; };
export default async function apiKeys(req: NextApiRequest, res: NextApiResponse<ResponseData>) { async function allApiKeys(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { method } = req; const data = await prisma.apiKey.findMany();
if (method === "GET") {
const data = await prisma.apiKey.findMany({}); if (data) res.status(200).json({ data });
res.status(200).json({ data }); else res.status(400).json({ error: "No data found" });
} else res.status(405).json({ message: "Only GET Method allowed" });
} }
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 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"; import { schemaApiKey, withValidApiKey } from "@lib/validations/apiKey";
type ResponseData = { type ResponseData = {
data?: ApiKey; data?: ApiKey;
message?: string; error?: object;
error?: string;
}; };
async function createApiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) { async function createApiKey(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req; const safe = schemaApiKey.safeParse(req.body);
const safe = schemaApiKey.safeParse(body); if (safe.success) {
if (method === "POST" && safe.success) { const data = await prisma.apiKey
const apiKey = await prisma.apiKey .create({ data: safe.data })
.create({ data: { ...safe.data, user: { connect: { id: 1 } } } }) if (data) res.status(201).json({ data })
else (error: unknown) => res.status(400).json({ error: { message: "Could not create apiKey type", error: error } });
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" });
} }
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 { BookingReference } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next"; 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"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = { 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 } }); const data = await prisma.bookingReference.findUnique({ where: { id: safe.data.id } });
if (data) res.status(200).json({ data }); 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 // Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" }); } 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 { BookingReference } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
import { schemaBookingReference, withValidBookingReference } from "@lib/validations/bookingReference"; import { schemaBookingReference, withValidBookingReference } from "@lib/validations/booking-reference";
type ResponseData = { type ResponseData = {
data?: BookingReference; 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 } }); const data = await prisma.credential.findUnique({ where: { id: safe.data.id } });
if (data) res.status(200).json({ data }); 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 // Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" }); } 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 type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
import { withMiddleware } from "@lib/helpers/withMiddleware";
type ResponseData = { type ResponseData = {
@ -11,17 +12,21 @@ type ResponseData = {
}; };
export async function deleteDailyEventReference(req: NextApiRequest, res: NextApiResponse<ResponseData>) { export async function deleteDailyEventReference(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req; const safe = await schemaQueryIdParseInt.safeParse(req.query);
const safe = await schemaQueryIdParseInt.safeParse(query); if (safe.success) {
if (method === "DELETE" && safe.success && safe.data) { const deletedDailyEventReference = await prisma.dailyEventReference
const dailyEventReference = await prisma.dailyEventReference
.delete({ where: { id: safe.data.id } }) .delete({ where: { id: safe.data.id } })
// We only remove the dailyEventReference type from the database if there's an existing resource. // 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. // 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`}); else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found`});
// Reject any other HTTP method than POST // Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only DELETE Method allowed" }); } 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 { DailyEventReference } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next"; 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"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = { 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 } }); const data = await prisma.dailyEventReference.findUnique({ where: { id: safe.data.id } });
if (data) res.status(200).json({ data }); 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 // Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" }); } 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 { DailyEventReference } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
import { schemaDailyEventReference, withValidDailyEventReference } from "@lib/validations/dailyEventReference"; import { schemaDailyEventReference, withValidDailyEventReference } from "@lib/validations/daily-event-reference";
type ResponseData = { type ResponseData = {
data?: DailyEventReference; data?: DailyEventReference;

View File

@ -3,7 +3,7 @@ import prisma from "@calcom/prisma";
import { DestinationCalendar } from "@calcom/prisma/client"; import { DestinationCalendar } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next"; 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"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = { 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 } }); const data = await prisma.destinationCalendar.findUnique({ where: { id: safe.data.id } });
if (data) res.status(200).json({ data }); 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 // Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" }); } 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 { DestinationCalendar } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
import { schemaDestinationCalendar, withValidDestinationCalendar } from "@lib/validations/destinationCalendar"; import { schemaDestinationCalendar, withValidDestinationCalendar } from "@lib/validations/destination-calendar";
type ResponseData = { type ResponseData = {
data?: DestinationCalendar; 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 } }); const data = await prisma.eventTypeCustomInput.findUnique({ where: { id: safe.data.id } });
if (data) res.status(200).json({ data }); 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 // Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" }); } 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 } }); const data = await prisma.membership.findUnique({ where: { id: safe.data.id } });
if (data) res.status(200).json({ data }); 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 // Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" }); } 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 } }); const data = await prisma.schedule.findUnique({ where: { id: safe.data.id } });
if (data) res.status(200).json({ data }); 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 // Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" }); } 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 { SelectedCalendar } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next"; 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"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = { 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 } }); const data = await prisma.selectedCalendar.findUnique({ where: { id: safe.data.id } });
if (data) res.status(200).json({ data }); 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 // Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" }); } 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 { SelectedCalendar } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
import { schemaSelectedCalendar, withValidSelectedCalendar } from "@lib/validations/selectedCalendar"; import { schemaSelectedCalendar, withValidSelectedCalendar } from "@lib/validations/selected-calendar";
type ResponseData = { type ResponseData = {
data?: SelectedCalendar; data?: SelectedCalendar;

View File

@ -1,27 +1,29 @@
import prisma from "@calcom/prisma";
import type { NextApiRequest, NextApiResponse } from "next"; 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"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = { type ResponseData = {
message?: string; message?: string;
error?: unknown; error?: unknown;
}; };
export async function deleteTeam(req: NextApiRequest, res: NextApiResponse<ResponseData>) { export async function deleteTeam(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req; const safe = await schemaQueryIdParseInt.safeParse(req.query);
const safe = await schemaQueryIdParseInt.safeParse(query); if (safe.success) {
if (method === "DELETE" && safe.success && safe.data) { const data = await prisma.team
const team = await prisma.team
.delete({ where: { id: safe.data.id } }) .delete({ where: { id: safe.data.id } })
// We only remove the team type from the database if there's an existing resource. // 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. // 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`}); else res.status(400).json({ message: `Team with id: ${safe.data.id} was not able to be processed` });
// Reject any other HTTP method than POST }
} else res.status(405).json({ message: "Only DELETE Method allowed" });
} }
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 type { NextApiRequest, NextApiResponse } from "next";
import { schemaTeam, withValidTeam } from "@lib/validations/team"; import { schemaTeam, withValidTeam } from "@lib/validations/team";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = { type ResponseData = {
@ -17,21 +18,19 @@ export async function editTeam(req: NextApiRequest, res: NextApiResponse<Respons
const safeQuery = await schemaQueryIdParseInt.safeParse(query); const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaTeam.safeParse(body); const safeBody = await schemaTeam.safeParse(body);
if (method === "PATCH") { if (safeQuery.success && safeBody.success) {
if (safeQuery.success && safeBody.success) { const data = await prisma.team.update({
await prisma.team.update({
where: { id: safeQuery.data.id }, where: { id: safeQuery.data.id },
data: safeBody.data, data: safeBody.data,
}).then(team => { })
res.status(200).json({ data: team }); if (data) res.status(200).json({ data });
}).catch(error => { else (error: unknown) => res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, 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" });
} }
} }
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 type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
import { withMiddleware } from "@lib/helpers/withMiddleware";
type ResponseData = { type ResponseData = {
data?: Team; data?: Team;
@ -11,20 +12,19 @@ type ResponseData = {
error?: unknown; error?: unknown;
}; };
export async function team(req: NextApiRequest, res: NextApiResponse<ResponseData>) { export async function teamById(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req; const safe = await schemaQueryIdParseInt.safeParse(req.query);
const safe = await schemaQueryIdParseInt.safeParse(query); if (safe.success) {
const data = await prisma.team.findUnique({ where: { id: safe.data.id } });
if (method === "GET" && safe.success) {
const team = await prisma.team.findUnique({ where: { id: safe.data.id } });
if (team) res.status(200).json({ data: team }); if (data) res.status(200).json({ data });
if (!team) res.status(404).json({ message: "Event type not found" }); else res.status(404).json({ message: "Team was not found" });
} else {
// Reject any other HTTP method than POST
res.status(405).json({ message: "Only GET Method allowed" });
} }
} }
export default withValidQueryIdTransformParseInt(team); export default withMiddleware("addRequestId","getOnly")(
withValidQueryIdTransformParseInt(
teamById
)
);

View File

@ -1,6 +1,7 @@
import prisma from "@calcom/prisma"; import prisma from "@calcom/prisma";
import { Team } from "@calcom/prisma/client"; import { Team } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
type ResponseData = { type ResponseData = {
@ -8,12 +9,11 @@ type ResponseData = {
error?: unknown; error?: unknown;
}; };
export default async function team(req: NextApiRequest, res: NextApiResponse<ResponseData>) { async function allTeams(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
try { const data = await prisma.team.findMany();
const data = await prisma.team.findMany();
res.status(200).json({ data }); if (data) res.status(200).json({ data });
} catch (error) { else res.status(400).json({ error: "No data found" });
// FIXME: Add zod for validation/error handling
res.status(400).json({ error: error });
}
} }
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 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"; import { schemaTeam, withValidTeam } from "@lib/validations/team";
type ResponseData = { type ResponseData = {
data?: Team; data?: Team;
message?: string; error?: object;
error?: string;
}; };
async function createTeam(req: NextApiRequest, res: NextApiResponse<ResponseData>) { async function createTeam(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req; const safe = schemaTeam.safeParse(req.body);
if (method === "POST") { if (safe.success) {
const safe = schemaTeam.safeParse(body); const data = await prisma.team
if (safe.success && safe.data) { .create({ data: safe.data })
await prisma.team if (data) res.status(201).json({ data })
.create({ data: safe.data }) else (error: unknown) => res.status(400).json({ error: { message: "Could not create team type", error: error } });
.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" });
} }
} }
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 type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = { type ResponseData = {
message?: string; message?: string;
error?: unknown; error?: unknown;
}; };
export async function deleteUser(req: NextApiRequest, res: NextApiResponse<ResponseData>) { export async function deleteUser(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req; const safe = await schemaQueryIdParseInt.safeParse(req.query);
const safe = await schemaQueryIdParseInt.safeParse(query); if (safe.success) {
if (method === "DELETE" && safe.success && safe.data) { const data = await prisma.user
const user = await prisma.user
.delete({ where: { id: safe.data.id } }) .delete({ where: { id: safe.data.id } })
// We only remove the user type from the database if there's an existing resource. // 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. // 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`}); else res.status(400).json({ message: `User with id: ${safe.data.id} was not able to be processed` });
// Reject any other HTTP method than POST }
} else res.status(405).json({ message: "Only DELETE Method allowed" });
} }
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 type { NextApiRequest, NextApiResponse } from "next";
import { schemaUser, withValidUser } from "@lib/validations/user"; import { schemaUser, withValidUser } from "@lib/validations/user";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = { type ResponseData = {
@ -17,16 +18,19 @@ export async function editUser(req: NextApiRequest, res: NextApiResponse<Respons
const safeQuery = await schemaQueryIdParseInt.safeParse(query); const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaUser.safeParse(body); const safeBody = await schemaUser.safeParse(body);
if (method === "PATCH" && safeQuery.success && safeBody.success) { if (safeQuery.success && safeBody.success) {
const data = await prisma.user.update({ const data = await prisma.user.update({
where: { id: safeQuery.data.id }, where: { id: safeQuery.data.id },
data: safeBody.data, data: safeBody.data,
}) })
if (data) res.status(200).json({ 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 }) else (error: unknown) => 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" });
} }
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 type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
import { withMiddleware } from "@lib/helpers/withMiddleware";
type ResponseData = { type ResponseData = {
data?: User; data?: User;
@ -11,17 +12,19 @@ type ResponseData = {
error?: unknown; error?: unknown;
}; };
export async function user(req: NextApiRequest, res: NextApiResponse<ResponseData>) { export async function userById(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req; const safe = await schemaQueryIdParseInt.safeParse(req.query);
const safe = await schemaQueryIdParseInt.safeParse(query); if (safe.success) {
if (method === "GET" && safe.success) {
const data = await prisma.user.findUnique({ where: { id: safe.data.id } }); const data = await prisma.user.findUnique({ where: { id: safe.data.id } });
if (data) res.status(200).json({ data }); if (data) res.status(200).json({ data });
if (!data) res.status(404).json({ message: "Event type not found" }); else res.status(404).json({ message: "User was not found" });
// Reject any other HTTP method than POST }
} else res.status(405).json({ message: "Only GET Method allowed" });
} }
export default withValidQueryIdTransformParseInt(user); export default withMiddleware("addRequestId","getOnly")(
withValidQueryIdTransformParseInt(
userById
)
);

View File

@ -1,7 +1,7 @@
import prisma from "@calcom/prisma"; import prisma from "@calcom/prisma";
import { User } from "@calcom/prisma/client"; import { User } from "@calcom/prisma/client";
import withMiddleware from "@lib/helpers/withMiddleware"; import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
type ResponseData = { type ResponseData = {
@ -9,9 +9,11 @@ type ResponseData = {
error?: unknown; error?: unknown;
}; };
async function user(req: NextApiRequest, res: NextApiResponse<ResponseData>) { async function allUsers(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const data = await prisma.user.findMany(); const data = await prisma.user.findMany();
if (data) res.status(200).json({ data }); if (data) res.status(200).json({ data });
else res.status(400).json({ error: "No data found" }); 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 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"; import { schemaUser, withValidUser } from "@lib/validations/user";
type ResponseData = { type ResponseData = {
data?: User; data?: User;
message?: string; error?: object;
error?: string;
}; };
async function createUser(req: NextApiRequest, res: NextApiResponse<ResponseData>) { async function createUser(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req; const safe = schemaUser.safeParse(req.body);
const safe = schemaUser.safeParse(body); if (safe.success) {
if (method === "POST" && safe.success) { const data = await prisma.user
await prisma.user .create({ data: safe.data })
.create({ data: safe.data }) if (data) res.status(201).json({ data })
.then((data) => res.status(201).json({ data })) else (error: unknown) => res.status(400).json({ error: { message: "Could not create user type", error: error } });
.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" });
} }
export default withValidUser(createUser); export default withMiddleware("addRequestId","postOnly")(
withValidUser(
createUser
)
);

File diff suppressed because it is too large Load Diff