cal.pub0.org/pages/api/payments/index.ts

43 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-04-02 00:38:46 +00:00
import type { NextApiRequest, NextApiResponse } from "next";
2022-11-25 13:56:58 +00:00
import { withMiddleware } from "~/lib/helpers/withMiddleware";
import { PaymentsResponse } from "~/lib/types";
import { schemaPaymentPublic } from "~/lib/validations/payment";
2022-04-02 00:38:46 +00:00
/**
* @swagger
2022-04-26 19:56:59 +00:00
* /payments:
2022-04-02 00:38:46 +00:00
* get:
* summary: Find all payments
2022-04-02 00:38:46 +00:00
* tags:
* - payments
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No payments were found
*/
2022-06-06 16:17:10 +00:00
async function allPayments({ userId, prisma }: NextApiRequest, res: NextApiResponse<PaymentsResponse>) {
const userWithBookings = await prisma.user.findUnique({
where: { id: userId },
include: { bookings: true },
});
if (!userWithBookings) throw new Error("No user found");
const bookings = userWithBookings.bookings;
const bookingIds = bookings.map((booking) => booking.id);
const data = await prisma.payment.findMany({ where: { bookingId: { in: bookingIds } } });
const payments = data.map((payment) => schemaPaymentPublic.parse(payment));
2022-04-02 00:38:46 +00:00
if (payments) res.status(200).json({ payments });
2022-04-02 00:38:46 +00:00
else
(error: Error) =>
res.status(404).json({
message: "No Payments were found",
error,
});
}
// NO POST FOR PAYMENTS FOR NOW
2022-04-02 00:38:46 +00:00
export default withMiddleware("HTTP_GET")(allPayments);