import MainSection from "@/app/(protected)/components/shared/Main";
import PageHeader from "@/app/(protected)/components/shared/PageHeader";
import { getPaymentModel } from "@/app/models/Payment";
import { getUserModel } from "@/app/models/User";
import { getClassModel } from "@/app/models/Class";
import { auth } from "@/app/lib/utils/auth";
import NotAuthorized from "@/app/auth/components/NotAuthorized";
import { redirect } from "next/navigation";
import PaymentsByClass from "../components/PaymentsByClass";
function serializePayment(payment) {
return {
_id: payment._id?.toString(),
classId: payment.classId
? {
_id: payment.classId._id?.toString(),
classTitle: payment.classId.classTitle,
}
: null,
userId: payment.userId
? {
_id: payment.userId._id?.toString(),
fullName: payment.userId.fullName,
email: payment.userId.email,
}
: null,
type: payment.type,
amount: payment.amount,
status: payment.status,
paymentDate: payment.paymentDate,
dueDate: payment.dueDate,
description: payment.description,
paymentMethod: payment.paymentMethod,
receiptUrl: payment.receiptUrl,
notes: payment.notes,
createdBy: payment.createdBy?.toString(),
createdAt: payment.createdAt?.toISOString(),
updatedAt: payment.updatedAt?.toISOString(),
relatedObligationId: payment.relatedObligationId?.toString(),
};
}
export default async function AdminPaymentsByClassPage() {
const session = await auth();
if (!session?.user?.id) redirect("/auth/login");
const UserModel = await getUserModel();
const currentUser = await UserModel.findOne({ _id: session.user.id });
// Only admins can access this page
if (!currentUser.roles.includes("admin")) {
return ;
}
const Payment = await getPaymentModel();
const Class = await getClassModel();
// Fetch all payments and obligations
const payments = await Payment.find({})
.populate("classId", "classTitle")
.populate("userId", "fullName email")
.sort({ createdAt: -1 })
.lean();
// Serialize data for client components
const serializedPayments = payments.map(serializePayment);
return (
← Voltar para Pagamentos
}
/>
);
}