Skip to content

Scheduling infrastructure for absolutely everyone.

feat: initial work unifying new endpoint and generating api key

Agusti Fernandez Pardofc2978a61bdb
diff --git a/.github/workflows/type-check.yml b/.github/workflows/type-check.yml
deleted file mode 100644
index a8c933d87eb..00000000000
--- a/.github/workflows/type-check.yml
+++ /dev/null
@@ -1,26 +0,0 @@
-name: Check types
-on:
- pull_request:
- branches:
- - main
-jobs:
- types:
- name: Check types
- strategy:
- matrix:
- node: ["14.x"]
- os: [ubuntu-latest]
- runs-on: ${{ matrix.os }}
-
- steps:
- - name: Checkout repo
- uses: actions/checkout@v2
- with:
- fetch-depth: 0
-
- - name: Use Node ${{ matrix.node }}
- uses: actions/setup-node@v2
- with:
- node-version: ${{ matrix.node }}
- - run: yarn
- - run: yarn type-check
diff --git a/lib/helpers/httpMethods.ts b/lib/helpers/httpMethods.ts
index 1454355f3a3..0617208a903 100644
--- a/lib/helpers/httpMethods.ts
+++ b/lib/helpers/httpMethods.ts
@@ -10,7 +10,9 @@ export const httpMethod = (allowedHttpMethod: "GET" | "POST" | "PATCH" | "DELETE
}
};
};
-
+// Made this so we can support several HTTP Methods in one route and use it there.
+// Could be further extracted into a third function or refactored into one.
+// that checks if it's just a string or an array and apply the correct logic to both cases.
export const httpMethods = (allowedHttpMethod: string[]): NextMiddleware => {
return async function (req, res, next) {
if (allowedHttpMethod.map((method) => method === req.method)) {
@@ -27,3 +29,4 @@ export const HTTP_GET = httpMethod("GET");
export const HTTP_PATCH = httpMethod("PATCH");
export const HTTP_DELETE = httpMethod("DELETE");
export const HTTP_GET_DELETE_PATCH = httpMethods(["GET", "DELETE", "PATCH"]);
+export const HTTP_GET_OR_POST = httpMethods(["GET", "POST"]);
diff --git a/lib/helpers/withMiddleware.ts b/lib/helpers/withMiddleware.ts
index 4171fa774f6..c926eff4ecd 100644
--- a/lib/helpers/withMiddleware.ts
+++ b/lib/helpers/withMiddleware.ts
@@ -2,11 +2,19 @@ import { label } from "next-api-middleware";
import { addRequestId } from "./addRequestid";
import { captureErrors } from "./captureErrors";
-import { HTTP_POST, HTTP_DELETE, HTTP_PATCH, HTTP_GET, HTTP_GET_DELETE_PATCH } from "./httpMethods";
+import {
+ HTTP_POST,
+ HTTP_DELETE,
+ HTTP_PATCH,
+ HTTP_GET,
+ HTTP_GET_OR_POST,
+ HTTP_GET_DELETE_PATCH,
+} from "./httpMethods";
import { verifyApiKey } from "./verifyApiKey";
const withMiddleware = label(
{
+ HTTP_GET_OR_POST,
HTTP_GET_DELETE_PATCH,
HTTP_GET,
HTTP_PATCH,
diff --git a/lib/validations/payment.ts b/lib/validations/payment.ts
index 7a9a138a13c..5789d4a092c 100644
--- a/lib/validations/payment.ts
+++ b/lib/validations/payment.ts
@@ -2,9 +2,9 @@ import { withValidation } from "next-validations";
import { _PaymentModel as Payment } from "@calcom/prisma/zod";
+// FIXME: Payment seems a delicate endpoint, do we need to remove anything here?
export const schemaPaymentBodyParams = Payment.omit({ id: true });
-
-export const schemaPaymentPublic = Payment.omit({});
+export const schemaPaymentPublic = Payment.omit({ externalId: true });
export const withValidPayment = withValidation({
schema: schemaPaymentBodyParams,
diff --git a/pages/api/attendees/index.ts b/pages/api/attendees/index.ts
index 4c94c6ed7df..60f6a35ebcb 100644
--- a/pages/api/attendees/index.ts
+++ b/pages/api/attendees/index.ts
@@ -3,8 +3,8 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
-import { AttendeesResponse } from "@lib/types";
-import { schemaAttendeePublic } from "@lib/validations/attendee";
+import { AttendeeResponse, AttendeesResponse } from "@lib/types";
+import { schemaAttendeeBodyParams, schemaAttendeePublic, withValidAttendee } from "@lib/validations/attendee";
/**
* @swagger
@@ -20,18 +20,49 @@ import { schemaAttendeePublic } from "@lib/validations/attendee";
* description: Authorization information is missing or invalid.
* 404:
* description: No attendees were found
+ * post:
+ * summary: Creates a new attendee
+ * tags:
+ * - attendees
+ * responses:
+ * 201:
+ * description: OK, attendee created
+ * model: Attendee
+ * 400:
+ * description: Bad request. Attendee body is invalid.
+ * 401:
+ * description: Authorization information is missing or invalid.
*/
-async function allAttendees(_: NextApiRequest, res: NextApiResponse<AttendeesResponse>) {
- const attendees = await prisma.attendee.findMany();
- const data = attendees.map((attendee) => schemaAttendeePublic.parse(attendee));
+async function createOrlistAllAttendees(
+ req: NextApiRequest,
+ res: NextApiResponse<AttendeesResponse | AttendeeResponse>
+) {
+ const { method } = req;
+ if (method === "GET") {
+ const attendees = await prisma.attendee.findMany();
+ const data = attendees.map((attendee) => schemaAttendeePublic.parse(attendee));
+ if (data) res.status(200).json({ data });
+ else
+ (error: Error) =>
+ res.status(404).json({
+ message: "No Attendees were found",
+ error,
+ });
+ } else if (method === "POST") {
+ const safe = schemaAttendeeBodyParams.safeParse(req.body);
+ if (!safe.success) throw new Error("Invalid request body", safe.error);
+
+ const attendee = await prisma.attendee.create({ data: safe.data });
+ const data = schemaAttendeePublic.parse(attendee);
- if (data) res.status(200).json({ data });
- else
- (error: Error) =>
- res.status(404).json({
- message: "No Attendees were found",
- error,
- });
+ if (data) res.status(201).json({ data, message: "Attendee created successfully" });
+ else
+ (error: Error) =>
+ res.status(400).json({
+ message: "Could not create new attendee",
+ error,
+ });
+ } else res.status(405).json({ message: `Method ${method} not allowed` });
}
-export default withMiddleware("HTTP_GET")(allAttendees);
+export default withMiddleware("HTTP_GET_OR_POST")(withValidAttendee(createOrlistAllAttendees));
diff --git a/pages/api/attendees/new.ts b/pages/api/attendees/new.ts
deleted file mode 100644
index b934e5b62de..00000000000
--- a/pages/api/attendees/new.ts
+++ /dev/null
@@ -1,41 +0,0 @@
-import type { NextApiRequest, NextApiResponse } from "next";
-
-import prisma from "@calcom/prisma";
-
-import { withMiddleware } from "@lib/helpers/withMiddleware";
-import type { AttendeeResponse } from "@lib/types";
-import { schemaAttendeeBodyParams, schemaAttendeePublic, withValidAttendee } from "@lib/validations/attendee";
-
-/**
- * @swagger
- * /api/attendees/new:
- * post:
- * summary: Creates a new attendee
- * tags:
- * - attendees
- * responses:
- * 201:
- * description: OK, attendee created
- * model: Attendee
- * 400:
- * description: Bad request. Attendee body is invalid.
- * 401:
- * description: Authorization information is missing or invalid.
- */
-async function createAttendee(req: NextApiRequest, res: NextApiResponse<AttendeeResponse>) {
- const safe = schemaAttendeeBodyParams.safeParse(req.body);
- if (!safe.success) throw new Error("Invalid request body", safe.error);
-
- const attendee = await prisma.attendee.create({ data: safe.data });
- const data = schemaAttendeePublic.parse(attendee);
-
- if (data) res.status(201).json({ data, message: "Attendee created successfully" });
- else
- (error: Error) =>
- res.status(400).json({
- message: "Could not create new attendee",
- error,
- });
-}
-
-export default withMiddleware("HTTP_POST")(withValidAttendee(createAttendee));
diff --git a/pages/api/payments/[id].ts b/pages/api/payments/[id].ts
index 14b106ca43f..7637bb724d4 100644
--- a/pages/api/payments/[id].ts
+++ b/pages/api/payments/[id].ts
@@ -5,13 +5,16 @@ import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { PaymentResponse } from "@lib/types";
import { schemaPaymentBodyParams, schemaPaymentPublic } from "@lib/validations/payment";
-import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
+import {
+ schemaQueryIdParseInt,
+ withValidQueryIdTransformParseInt,
+} from "@lib/validations/shared/queryIdTransformParseInt";
/**
* @swagger
* /api/payments/{id}:
* get:
- * summary: Get a payment by ID
+ * summary: Get an payment by ID
* parameters:
* - in: path
* name: id
@@ -78,17 +81,14 @@ import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/
*/
export async function paymentById(req: NextApiRequest, res: NextApiResponse<PaymentResponse>) {
const { method, query, body } = req;
- const safeQuery = await schemaQueryIdAsString.safeParse(query);
+ const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaPaymentBodyParams.safeParse(body);
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
- const [userId, teamId] = safeQuery.data.id.split("_");
switch (method) {
case "GET":
await prisma.payment
- .findUnique({
- where: { userId_teamId: { userId: parseInt(userId), teamId: parseInt(teamId) } },
- })
+ .findUnique({ where: { id: safeQuery.data.id } })
.then((payment) => schemaPaymentPublic.parse(payment))
.then((data) => res.status(200).json({ data }))
.catch((error: Error) =>
@@ -127,4 +127,4 @@ export async function paymentById(req: NextApiRequest, res: NextApiResponse<Paym
}
}
-export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdString(paymentById));
+export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(paymentById));
diff --git a/templates/newIndex.ts b/templates/newIndex.ts
new file mode 100644
index 00000000000..732eb013b27
--- /dev/null
+++ b/templates/newIndex.ts
@@ -0,0 +1,68 @@
+import type { NextApiRequest, NextApiResponse } from "next";
+
+import prisma from "@calcom/prisma";
+
+import { withMiddleware } from "@lib/helpers/withMiddleware";
+import { PaymentResponse, PaymentsResponse } from "@lib/types";
+import { schemaPaymentBodyParams, schemaPaymentPublic, withValidPayment } from "@lib/validations/payment";
+
+/**
+ * @swagger
+ * /api/payments:
+ * get:
+ * summary: Get all payments
+ * tags:
+ * - payments
+ * responses:
+ * 200:
+ * description: OK
+ * 401:
+ * description: Authorization information is missing or invalid.
+ * 404:
+ * description: No payments were found
+ * post:
+ * summary: Creates a new payment
+ * tags:
+ * - payments
+ * responses:
+ * 201:
+ * description: OK, payment created
+ * model: Payment
+ * 400:
+ * description: Bad request. Payment body is invalid.
+ * 401:
+ * description: Authorization information is missing or invalid.
+ */
+async function createOrlistAllPayments(
+ req: NextApiRequest,
+ res: NextApiResponse<PaymentsResponse | PaymentResponse>
+) {
+ const { method } = req;
+ if (method === "GET") {
+ const payments = await prisma.payment.findMany();
+ const data = payments.map((payment) => schemaPaymentPublic.parse(payment));
+ if (data) res.status(200).json({ data });
+ else
+ (error: Error) =>
+ res.status(404).json({
+ message: "No Payments were found",
+ error,
+ });
+ } else if (method === "POST") {
+ const safe = schemaPaymentBodyParams.safeParse(req.body);
+ if (!safe.success) throw new Error("Invalid request body");
+
+ const payment = await prisma.payment.create({ data: safe.data });
+ const data = schemaPaymentPublic.parse(payment);
+
+ if (data) res.status(201).json({ data, message: "Payment created successfully" });
+ else
+ (error: Error) =>
+ res.status(400).json({
+ message: "Could not create new payment",
+ error,
+ });
+ } else res.status(405).json({ message: `Method ${method} not allowed` });
+}
+
+export default withMiddleware("HTTP_GET_OR_POST")(withValidPayment(createOrlistAllPayments));