cal.pub0.org/pages/api/payments/[id].ts

62 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-04-04 00:02:11 +00:00
import type { NextApiRequest, NextApiResponse } from "next";
2022-11-25 13:56:58 +00:00
import { withMiddleware } from "~/lib/helpers/withMiddleware";
import type { PaymentResponse } from "~/lib/types";
import { schemaPaymentPublic } from "~/lib/validations/payment";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
2022-11-25 13:56:58 +00:00
} from "~/lib/validations/shared/queryIdTransformParseInt";
2022-04-04 00:02:11 +00:00
/**
* @swagger
2022-04-26 19:56:59 +00:00
* /payments/{id}:
2022-04-04 00:02:11 +00:00
* get:
2022-04-29 15:29:57 +00:00
* summary: Find a payment
2022-04-04 00:02:11 +00:00
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: ID of the payment to get
2022-04-04 00:02:11 +00:00
* tags:
* - payments
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: Payment was not found
*/
2022-04-29 15:29:57 +00:00
export async function paymentById(
2022-06-06 16:17:10 +00:00
{ method, query, userId, prisma }: NextApiRequest,
2022-04-29 15:29:57 +00:00
res: NextApiResponse<PaymentResponse>
) {
const safeQuery = schemaQueryIdParseInt.safeParse(query);
if (safeQuery.success && method === "GET") {
const userWithBookings = await prisma.user.findUnique({
where: { id: userId },
include: { bookings: true },
});
await prisma.payment
.findUnique({ where: { id: safeQuery.data.id } })
.then((data) => schemaPaymentPublic.parse(data))
.then((payment) => {
if (!userWithBookings?.bookings.map((b) => b.id).includes(payment.bookingId)) {
res.status(401).json({ message: "Unauthorized" });
} else {
res.status(200).json({ payment });
}
})
.catch((error: Error) =>
res.status(404).json({
message: `Payment with id: ${safeQuery.data.id} not found`,
error,
2022-04-04 00:02:11 +00:00
})
);
2022-04-04 00:02:11 +00:00
}
}
export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(paymentById));