feat(payments): automatic monthly charges with combined links per payer

- 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>
This commit is contained in:
Jose Selesan
2026-11-11 15:08:48 -03:00
parent 5b3a035dcf
commit ac98495a6e
87 changed files with 2648 additions and 106 deletions

View File

@@ -30,6 +30,8 @@ export const AttendeeDtoSchema = z.object({
guardianPhone: z.string().nullable(),
guardianEmail: z.string().nullable(),
notes: z.string().nullable(),
/** Off: pays by transfer, so automatic charges create the fee without a Mercado Pago link. */
payWithMercadoPago: z.boolean(),
notifyToken: z.string().nullable().optional(),
createdAt: isoDateTimeSchema,
updatedAt: isoDateTimeSchema,
@@ -93,6 +95,7 @@ const EditableAttendeeFieldsSchema = z.object({
.or(z.literal(''))
.nullable(),
notes: z.string().trim().nullable(),
payWithMercadoPago: z.boolean(),
});
export const UpdateAttendeeSchema = EditableAttendeeFieldsSchema.partial().refine(

View File

@@ -20,6 +20,11 @@ export const PaymentDtoSchema = z.object({
status: paymentStatusSchema,
description: z.string().nullable(),
checkoutUrl: z.string().nullable(),
// Combined link (automatic monthly charge): how many fees it pays at once.
checkoutId: z.string().nullable(),
checkoutItemCount: z.number().int().nullable(),
// Month of an automatic charge ("2026-12"); null for manual fees.
periodKey: z.string().nullable(),
dueDate: isoDateTimeSchema,
paidAt: isoDateTimeSchema.nullable(),
createdAt: isoDateTimeSchema,
@@ -71,4 +76,54 @@ export const CreatePaymentsBatchResultSchema = z.object({
failed: z.number().int().min(0),
items: z.array(PaymentBatchItemSchema),
});
export type CreatePaymentsBatchResult = z.output<typeof CreatePaymentsBatchResultSchema>;
export type CreatePaymentsBatchResult = z.output<typeof CreatePaymentsBatchResultSchema>;
// Automatic monthly charges: on `chargeDay` (1-28, teacher's time zone) a fee
// for every active member of the teacher's monthly groups, with one Mercado
// Pago link per payer (the adult in charge for kids).
export const AUTO_CHARGE_MAX_DAY = 28;
export const AUTO_CHARGE_DEFAULT_DUE_IN_DAYS = 10;
export const AUTO_CHARGE_MAX_DUE_IN_DAYS = 31;
export const UpdateAutoChargeSettingsSchema = z
.strictObject({
enabled: z.boolean().optional(),
chargeDay: z.number().int().min(1, 'payment.chargeDayRange').max(AUTO_CHARGE_MAX_DAY, 'payment.chargeDayRange').optional(),
dueInDays: z
.number()
.int()
.min(1, 'payment.dueInDaysRange')
.max(AUTO_CHARGE_MAX_DUE_IN_DAYS, 'payment.dueInDaysRange')
.optional(),
sendWhatsapp: z.boolean().optional(),
})
.refine((value) => Object.values(value).some((field) => field !== undefined), {
message: 'payment.autoChargeRequired',
});
export type UpdateAutoChargeSettings = z.output<typeof UpdateAutoChargeSettingsSchema>;
export const paymentRunStatusSchema = z.enum(['RUNNING', 'DONE', 'SKIPPED']);
export type PaymentRunStatus = z.output<typeof paymentRunStatusSchema>;
export const PaymentRunDtoSchema = z.object({
periodKey: z.string(),
status: paymentRunStatusSchema,
skipReason: z.string().nullable(),
fees: z.number().int(),
checkouts: z.number().int(),
linked: z.number().int(),
failed: z.number().int(),
sent: z.number().int(),
createdAt: isoDateTimeSchema,
});
export type PaymentRunDto = z.output<typeof PaymentRunDtoSchema>;
export const AutoChargeSettingsDtoSchema = z.object({
enabled: z.boolean(),
chargeDay: z.number().int(),
dueInDays: z.number().int(),
sendWhatsapp: z.boolean(),
/** Local date (YYYY-MM-DD) of the next run; null while it is off. */
nextRunDate: z.string().nullable(),
lastRun: PaymentRunDtoSchema.nullable(),
});
export type AutoChargeSettingsDto = z.output<typeof AutoChargeSettingsDtoSchema>;

View File

@@ -181,6 +181,16 @@ export const StudentFeeSchema = z.object({
dueDate: isoDateTimeSchema,
status: studentFeeStatusSchema,
checkoutUrl: z.string().nullable(),
// Month of an automatic charge ("2026-12"); null for fees created by hand.
periodKey: z.string().nullable(),
// The link pays this fee together with others of the same payer (siblings,
// other groups): its total and what it includes.
checkout: z
.object({
total: z.number(),
items: z.array(z.object({ groupName: z.string(), studentName: z.string() })),
})
.nullable(),
});
export type StudentFee = z.output<typeof StudentFeeSchema>;

View File

@@ -17,7 +17,7 @@ export const whatsappAccountStatusSchema = z.enum([
]);
export type WhatsappAccountStatus = z.output<typeof whatsappAccountStatusSchema>;
export const whatsappMessageKindSchema = z.enum(['PAYMENT_REMINDER', 'REENGAGEMENT']);
export const whatsappMessageKindSchema = z.enum(['PAYMENT_REMINDER', 'REENGAGEMENT', 'PAYMENT_LINK']);
export type WhatsappMessageKind = z.output<typeof whatsappMessageKindSchema>;
// Whether automated messages of one kind can go out right now.
@@ -75,6 +75,7 @@ export const WhatsappStatusDtoSchema = z.object({
readiness: z.object({
PAYMENT_REMINDER: whatsappReadinessSchema,
REENGAGEMENT: whatsappReadinessSchema,
PAYMENT_LINK: whatsappReadinessSchema,
}),
});
export type WhatsappStatusDto = z.output<typeof WhatsappStatusDtoSchema>;