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:
|
2022-04-27 17:25:36 +00:00
|
|
|
* 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>) {
|
2022-04-10 00:10:34 +00:00
|
|
|
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);
|
2022-04-11 13:10:16 +00:00
|
|
|
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
|
|
|
|
2022-04-11 13:10:16 +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,
|
|
|
|
});
|
|
|
|
}
|
2022-04-11 13:10:16 +00:00
|
|
|
// NO POST FOR PAYMENTS FOR NOW
|
2022-04-02 00:38:46 +00:00
|
|
|
export default withMiddleware("HTTP_GET")(allPayments);
|