- 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>
56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import { logger } from '@/logger';
|
|
import { MarkOverduePayments } from './mark-overdue-payments';
|
|
import { ReconcilePayments } from './reconcile-payments';
|
|
import { RunAutoCharges } from './run-auto-charges';
|
|
|
|
const RECONCILE_CRON = '*/15 * * * *';
|
|
const OVERDUE_CRON = '4 4 * * *';
|
|
// Hourly: each teacher's day of the month is on their own clock.
|
|
const AUTO_CHARGE_CRON = '7 * * * *';
|
|
|
|
/**
|
|
* Registers the payment background jobs. Called once from `server.ts`, following
|
|
* the same guards as `startMpJobs`: Bun's in-process cron treats a rejected
|
|
* promise as an unhandledRejection (which would crash the process), and `unref`
|
|
* keeps the jobs from holding the process alive on their own.
|
|
*/
|
|
export function startPaymentJobs(): void {
|
|
Bun.cron(
|
|
RECONCILE_CRON,
|
|
async () => {
|
|
try {
|
|
const totals = await new ReconcilePayments().execute();
|
|
logger.info(totals, 'Mercado Pago payments reconciliation finished');
|
|
} catch (error) {
|
|
logger.error({ err: error }, 'Mercado Pago payments reconciliation failed');
|
|
}
|
|
},
|
|
{ tz: 'UTC' },
|
|
).unref();
|
|
|
|
Bun.cron(
|
|
OVERDUE_CRON,
|
|
async () => {
|
|
try {
|
|
const totals = await new MarkOverduePayments().execute();
|
|
logger.info(totals, 'Overdue payments marked');
|
|
} catch (error) {
|
|
logger.error({ err: error }, 'Marking overdue payments failed');
|
|
}
|
|
},
|
|
{ tz: 'UTC' },
|
|
).unref();
|
|
|
|
Bun.cron(
|
|
AUTO_CHARGE_CRON,
|
|
async () => {
|
|
try {
|
|
const totals = await new RunAutoCharges().execute();
|
|
if (totals.due > 0) logger.info(totals, 'Automatic monthly charges finished');
|
|
} catch (error) {
|
|
logger.error({ err: error }, 'Automatic monthly charges failed');
|
|
}
|
|
},
|
|
{ tz: 'UTC' },
|
|
).unref();
|
|
} |