- Teacher picks a day of the month in Settings; an hourly job creates the fees of all monthly groups on that day in the teacher's time zone (one run per teacher and month, partial unique index per attendee/period). - One Mercado Pago link per payer (PaymentCheckout): the adult in charge bundles their kids and their own fees; external_reference chk_<id>, dedicated webhook and reconciliation mark every fee PAID at once. - Person.payWithMercadoPago toggle (default on): off creates the fee without a link so the student pays by transfer. - A fee paid some other way supersedes its combined link and expires the preference; renewing rebuilds the combined link or a single one. - Optional WhatsApp delivery (PAYMENT_LINK kind, new Kapso template); weekly reminders now go once per combined link. - Web: auto charge settings card, per-student toggle, combined link label in Cobros and "Pay all" in the student app, in the four locales. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
126 lines
4.6 KiB
TypeScript
126 lines
4.6 KiB
TypeScript
import type { PrismaClient } from '@generated/prisma/client';
|
|
import type { ProblemDetails, Result, StudentFeeStatus, StudentFeesDto } from '@gruperly/shared';
|
|
import { err, ok } from '@gruperly/shared';
|
|
import prisma from '@/lib/prisma';
|
|
import { startOfDayInTimeZone } from '@/modules/home/lib/scheduler';
|
|
import { findLinkedAttendee, studentAttendeeNotFoundProblem, teacherTimeZone } from '../../lib';
|
|
|
|
type GetStudentFeesDeps = {
|
|
db?: Pick<PrismaClient, 'attendee' | 'payment' | 'reportedPayment'>;
|
|
now?: Date;
|
|
};
|
|
|
|
const REPORTS_SHOWN = 10;
|
|
|
|
const REPORT_STATUS = { REPORTED: 'reported', CONFIRMED: 'confirmed', REJECTED: 'rejected' } as const;
|
|
|
|
/* Unpaid fees of the attendee and their latest reports. A fee inside a report
|
|
the teacher hasn't reviewed shows as "reported" (the Payment row itself is
|
|
not touched until the teacher confirms). */
|
|
export class GetStudentFees {
|
|
constructor(private readonly deps: GetStudentFeesDeps = {}) {}
|
|
|
|
async execute(accountId: string, attendeeId: string): Promise<Result<StudentFeesDto, ProblemDetails>> {
|
|
const db = this.deps.db ?? prisma;
|
|
const now = this.deps.now ?? new Date();
|
|
|
|
const attendee = await findLinkedAttendee(db, accountId, attendeeId);
|
|
if (!attendee) return err(studentAttendeeNotFoundProblem());
|
|
const startOfToday = startOfDayInTimeZone(now, teacherTimeZone(attendee.group.owner.country));
|
|
|
|
const [payments, reports] = await Promise.all([
|
|
db.payment.findMany({
|
|
where: { attendeeId, status: { in: ['PENDING', 'OVERDUE'] } },
|
|
orderBy: { dueDate: 'asc' },
|
|
select: {
|
|
id: true,
|
|
description: true,
|
|
amount: true,
|
|
currency: true,
|
|
dueDate: true,
|
|
status: true,
|
|
checkoutUrl: true,
|
|
periodKey: true,
|
|
checkout: {
|
|
select: {
|
|
status: true,
|
|
amount: true,
|
|
payments: {
|
|
where: { status: { in: ['PENDING', 'OVERDUE'] } },
|
|
orderBy: { createdAt: 'asc' },
|
|
select: {
|
|
group: { select: { name: true } },
|
|
attendee: { select: { person: { select: { fullName: true } } } },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
reportedItems: { where: { reportedPayment: { status: 'REPORTED' } }, select: { paymentId: true } },
|
|
},
|
|
}),
|
|
db.reportedPayment.findMany({
|
|
where: { attendeeId },
|
|
orderBy: { createdAt: 'desc' },
|
|
take: REPORTS_SHOWN,
|
|
select: {
|
|
id: true,
|
|
totalAmount: true,
|
|
currency: true,
|
|
status: true,
|
|
createdAt: true,
|
|
reviewedAt: true,
|
|
note: true,
|
|
rejectionReason: true,
|
|
items: { select: { payment: { select: { id: true, description: true, dueDate: true } } } },
|
|
},
|
|
}),
|
|
]);
|
|
|
|
const { transferAlias, transferHolder } = attendee.group.owner;
|
|
return ok({
|
|
attendeeId,
|
|
fees: payments.map((payment) => {
|
|
let status: StudentFeeStatus = 'pending';
|
|
if (payment.reportedItems.length > 0) status = 'reported';
|
|
else if (payment.status === 'OVERDUE' || payment.dueDate < startOfToday) status = 'overdue';
|
|
return {
|
|
id: payment.id,
|
|
description: payment.description,
|
|
amount: payment.amount.toNumber(),
|
|
currency: payment.currency,
|
|
dueDate: payment.dueDate.toISOString(),
|
|
status,
|
|
checkoutUrl: payment.checkoutUrl,
|
|
periodKey: payment.periodKey,
|
|
checkout:
|
|
payment.checkout?.status === 'OPEN' && payment.checkout.payments.length > 1
|
|
? {
|
|
total: payment.checkout.amount.toNumber(),
|
|
items: payment.checkout.payments.map((line) => ({
|
|
groupName: line.group.name,
|
|
studentName: line.attendee.person.fullName,
|
|
})),
|
|
}
|
|
: null,
|
|
};
|
|
}),
|
|
transfer: transferAlias ? { alias: transferAlias, holder: transferHolder } : null,
|
|
reports: reports.map((report) => ({
|
|
id: report.id,
|
|
totalAmount: report.totalAmount.toNumber(),
|
|
currency: report.currency,
|
|
status: REPORT_STATUS[report.status],
|
|
createdAt: report.createdAt.toISOString(),
|
|
reviewedAt: report.reviewedAt?.toISOString() ?? null,
|
|
note: report.note,
|
|
rejectionReason: report.rejectionReason,
|
|
fees: report.items.map(({ payment }) => ({
|
|
id: payment.id,
|
|
description: payment.description,
|
|
dueDate: payment.dueDate.toISOString(),
|
|
})),
|
|
})),
|
|
});
|
|
}
|
|
}
|