87 lines
2.9 KiB
React
87 lines
2.9 KiB
React
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 <NotAuthorized />;
|
|
}
|
|
|
|
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 (
|
|
<MainSection>
|
|
<PageHeader
|
|
title="Pagamentos por Turma"
|
|
subtitle="Visualização agrupada de obrigações e movimentações financeiras"
|
|
actions={
|
|
<a
|
|
href="/admin/dashboard/payments"
|
|
className="inline-flex items-center gap-2 px-4 py-2 rounded-lg border border-gray-300 dark:border-neutral-600 bg-white dark:bg-neutral-700 text-gray-700 dark:text-gray-200 text-sm font-medium label-dark hover:bg-gray-50 dark:hover:bg-neutral-600 transition-colors"
|
|
>
|
|
← Voltar para Pagamentos
|
|
</a>
|
|
}
|
|
/>
|
|
|
|
<PaymentsByClass payments={serializedPayments} />
|
|
</MainSection>
|
|
);
|
|
}
|