feat: adds payments CRUD endpoints

pull/9078/head
Agusti Fernandez Pardo 2022-04-02 02:38:46 +02:00
parent 3411768da2
commit 3271e17317
5 changed files with 242 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

48
pages/api/payments/new.ts Normal file
View File

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