Skip to content

Scheduling infrastructure for absolutely everyone.

feat: adds payments CRUD endpoints

Agusti Fernandez Pardo3271e1731796
diff --git a/pages/api/payments/[id]/delete.ts b/pages/api/payments/[id]/delete.ts
new file mode 100644
index 00000000000..2367f463b0b
--- /dev/null
+++ b/pages/api/payments/[id]/delete.ts
@@ -0,0 +1,50 @@
+import type { NextApiRequest, NextApiResponse } from "next";
+
+import prisma from "@calcom/prisma";
+
+import { withMiddleware } from "@lib/helpers/withMiddleware";
+import type { BaseResponse } from "@lib/types";
+import {
+ schemaQueryIdParseInt,
+ withValidQueryIdTransformParseInt,
+} from "@lib/validations/shared/queryIdTransformParseInt";
+
+/**
+ * @swagger
+ * /api/payments/{id}/delete:
+ * delete:
+ * summary: Remove an existing payment
+ * parameters:
+ * - in: path
+ * name: id
+ * schema:
+ * type: integer
+ * required: true
+ * description: Numeric ID of the payment to delete
+ * tags:
+ * - payments
+ * responses:
+ * 201:
+ * description: OK, payment removed successfuly
+ * model: Payment
+ * 400:
+ * description: Bad request. Payment id is invalid.
+ * 401:
+ * description: Authorization information is missing or invalid.
+ */
+export async function deletePayment(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
+ const safe = await schemaQueryIdParseInt.safeParse(req.query);
+ if (!safe.success) throw new Error("Invalid request query", safe.error);
+
+ const data = await prisma.payment.delete({ where: { id: safe.data.id } });
+
+ if (data) res.status(200).json({ message: `Payment with id: ${safe.data.id} deleted successfully` });
+ else
+ (error: Error) =>
+ res.status(400).json({
+ message: `Payment with id: ${safe.data.id} was not able to be processed`,
+ error,
+ });
+}
+
+export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deletePayment));
diff --git a/pages/api/payments/[id]/edit.ts b/pages/api/payments/[id]/edit.ts
new file mode 100644
index 00000000000..f0f38b5efdb
--- /dev/null
+++ b/pages/api/payments/[id]/edit.ts
@@ -0,0 +1,56 @@
+import type { NextApiRequest, NextApiResponse } from "next";
+
+import prisma from "@calcom/prisma";
+
+import { withMiddleware } from "@lib/helpers/withMiddleware";
+import type { PaymentResponse } from "@lib/types";
+import { schemaPaymentBodyParams, schemaPaymentPublic, withValidPayment } from "@lib/validations/payment";
+import {
+ schemaQueryIdParseInt,
+ withValidQueryIdTransformParseInt,
+} from "@lib/validations/shared/queryIdTransformParseInt";
+
+/**
+ * @swagger
+ * /api/payments/{id}/edit:
+ * patch:
+ * summary: Edit an existing payment
+ * parameters:
+ * - in: path
+ * name: id
+ * schema:
+ * type: integer
+ * required: true
+ * description: Numeric ID of the payment to edit
+ * tags:
+ * - payments
+ * responses:
+ * 201:
+ * description: OK, payment edited successfuly
+ * model: Payment
+ * 400:
+ * description: Bad request. Payment body is invalid.
+ * 401:
+ * description: Authorization information is missing or invalid.
+ */
+export async function editPayment(req: NextApiRequest, res: NextApiResponse<PaymentResponse>) {
+ const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
+ const safeBody = await schemaPaymentBodyParams.safeParse(req.body);
+
+ if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
+ const payment = await prisma.payment.update({
+ where: { id: safeQuery.data.id },
+ data: safeBody.data,
+ });
+ const data = schemaPaymentPublic.parse(payment);
+
+ if (data) res.status(200).json({ data });
+ else
+ (error: Error) =>
+ res.status(404).json({
+ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
+ error,
+ });
+}
+
+export default withMiddleware("HTTP_PATCH")(withValidQueryIdTransformParseInt(withValidPayment(editPayment)));
diff --git a/pages/api/payments/[id]/index.ts b/pages/api/payments/[id]/index.ts
new file mode 100644
index 00000000000..ea5039cf431
--- /dev/null
+++ b/pages/api/payments/[id]/index.ts
@@ -0,0 +1,51 @@
+import type { NextApiRequest, NextApiResponse } from "next";
+
+import prisma from "@calcom/prisma";
+
+import { withMiddleware } from "@lib/helpers/withMiddleware";
+import type { PaymentResponse } from "@lib/types";
+import { schemaPaymentPublic } from "@lib/validations/payment";
+import {
+ schemaQueryIdParseInt,
+ withValidQueryIdTransformParseInt,
+} from "@lib/validations/shared/queryIdTransformParseInt";
+
+/**
+ * @swagger
+ * /api/payments/{id}:
+ * get:
+ * summary: Get a payment by ID
+ * parameters:
+ * - in: path
+ * name: id
+ * schema:
+ * type: integer
+ * required: true
+ * description: Numeric ID of the payment to get
+ * tags:
+ * - payments
+ * responses:
+ * 200:
+ * description: OK
+ * 401:
+ * description: Authorization information is missing or invalid.
+ * 404:
+ * description: Payment was not found
+ */
+export async function paymentById(req: NextApiRequest, res: NextApiResponse<PaymentResponse>) {
+ const safe = await schemaQueryIdParseInt.safeParse(req.query);
+ if (!safe.success) throw new Error("Invalid request query");
+
+ const payment = await prisma.payment.findUnique({ where: { id: safe.data.id } });
+ const data = schemaPaymentPublic.parse(payment);
+
+ if (payment) res.status(200).json({ data });
+ else
+ (error: Error) =>
+ res.status(404).json({
+ message: "Payment was not found",
+ error,
+ });
+}
+
+export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(paymentById));
diff --git a/pages/api/payments/index.ts b/pages/api/payments/index.ts
new file mode 100644
index 00000000000..ccffac11616
--- /dev/null
+++ b/pages/api/payments/index.ts
@@ -0,0 +1,37 @@
+import type { NextApiRequest, NextApiResponse } from "next";
+
+import prisma from "@calcom/prisma";
+
+import { withMiddleware } from "@lib/helpers/withMiddleware";
+import { PaymentsResponse } from "@lib/types";
+import { schemaPaymentPublic } 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
+ */
+async function allPayments(_: NextApiRequest, res: NextApiResponse<PaymentsResponse>) {
+ 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,
+ });
+}
+
+export default withMiddleware("HTTP_GET")(allPayments);
diff --git a/pages/api/payments/new.ts b/pages/api/payments/new.ts
new file mode 100644
index 00000000000..dca0099e482
--- /dev/null
+++ b/pages/api/payments/new.ts
@@ -0,0 +1,48 @@
+import type { NextApiRequest, NextApiResponse } from "next";
+
+import prisma from "@calcom/prisma";
+
+import { withMiddleware } from "@lib/helpers/withMiddleware";
+import type { PaymentResponse } from "@lib/types";
+import { schemaPaymentBodyParams, schemaPaymentPublic, withValidPayment } from "@lib/validations/payment";
+
+/**
+ * @swagger
+ * /api/payments/new:
+ * post:
+ * summary: Creates a new payment
+ * requestBody:
+ * description: Optional description in *Markdown*
+ * required: true
+ * content:
+ * application/json:
+ * schema:
+ * $ref: '#/components/schemas/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 createPayment(req: NextApiRequest, res: NextApiResponse<PaymentResponse>) {
+ const safe = schemaPaymentBodyParams.safeParse(req.body);
+ if (!safe.success) throw new Error("Invalid request body", safe.error);
+
+ 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,
+ });
+}
+
+export default withMiddleware("HTTP_POST")(withValidPayment(createPayment));