- 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>
268 lines
11 KiB
TypeScript
268 lines
11 KiB
TypeScript
import { z } from 'zod';
|
|
import { attendeeStatusSchema } from './analytics.js';
|
|
import { attendanceStatusSchema } from './attendance.js';
|
|
import { createPageSchema, createPageSizeSchema, createPaginationSchema } from './pagination.js';
|
|
import { paymentStatusSchema } from './payments.js';
|
|
import { GroupWaitlistEntryDtoSchema } from './waitlist.js';
|
|
|
|
const isoDateTimeSchema = z.string().datetime();
|
|
const pageSchema = createPageSchema();
|
|
const pageSizeSchema = createPageSizeSchema(10);
|
|
const paginationSchema = createPaginationSchema();
|
|
|
|
/* How many recent class sessions travel per member in the listing. This is the
|
|
size of the roster's dot strip, not an analysis window: analytics asks for
|
|
HISTORY_SESSIONS_LIMIT classes separately. */
|
|
export const RECENT_ATTENDANCE_LIMIT = 5;
|
|
|
|
/* An attendee is the membership of a person in one group. Contact data belongs
|
|
to the person (shared by all their groups) and travels flattened here. */
|
|
export const AttendeeDtoSchema = z.object({
|
|
id: z.string(),
|
|
groupId: z.string(),
|
|
personId: z.string(),
|
|
fullName: z.string(),
|
|
email: z.string().nullable(),
|
|
phone: z.string().nullable(),
|
|
/** The adult in charge (kids groups): a person of their own, flattened here. */
|
|
guardianPersonId: z.string().nullable(),
|
|
guardianName: z.string().nullable(),
|
|
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,
|
|
});
|
|
export type AttendeeDto = z.output<typeof AttendeeDtoSchema>;
|
|
|
|
export const AttendeeStatsSchema = z.object({
|
|
attendeeId: z.string(),
|
|
groupId: z.string(),
|
|
presentCount: z.number().int().min(0),
|
|
absentCount: z.number().int().min(0),
|
|
excusedCount: z.number().int().min(0),
|
|
totalMarked: z.number().int().min(0),
|
|
attendanceRate: z.number().min(0).max(100),
|
|
});
|
|
export type AttendeeStats = z.output<typeof AttendeeStatsSchema>;
|
|
export type AttendeeStatsDto = AttendeeStats;
|
|
|
|
export const AttendeeQuerySchema = z.object({
|
|
page: pageSchema,
|
|
pageSize: pageSizeSchema,
|
|
groupId: z.string().optional(),
|
|
});
|
|
export type AttendeeQuery = z.output<typeof AttendeeQuerySchema>;
|
|
|
|
/**
|
|
* Charge for the current period, as stored in the database: no denormalized
|
|
* column and no computed verdict. The backend returns the most recent unpaid
|
|
* payment; the client decides how to present it.
|
|
*/
|
|
export const AttendeePaymentSummarySchema = z.object({
|
|
status: paymentStatusSchema,
|
|
dueDate: isoDateTimeSchema,
|
|
});
|
|
export type AttendeePaymentSummary = z.output<typeof AttendeePaymentSummarySchema>;
|
|
|
|
/** Another group of the teacher the same person is in. */
|
|
export const PersonMembershipSchema = z.object({
|
|
attendeeId: z.string(),
|
|
groupId: z.string(),
|
|
groupName: z.string(),
|
|
status: attendeeStatusSchema,
|
|
});
|
|
export type PersonMembership = z.output<typeof PersonMembershipSchema>;
|
|
|
|
export const AttendeeDetailDtoSchema = AttendeeDtoSchema.extend({
|
|
payment: AttendeePaymentSummarySchema.nullable(),
|
|
/** The person's memberships in the teacher's other groups. */
|
|
otherGroups: z.array(PersonMembershipSchema),
|
|
/** Other kids of the same adult in charge (kids groups). */
|
|
siblings: z.array(z.object({ personId: z.string(), fullName: z.string() })),
|
|
});
|
|
export type AttendeeDetailDto = z.output<typeof AttendeeDetailDtoSchema>;
|
|
|
|
const EditableAttendeeFieldsSchema = z.object({
|
|
phone: z.string().trim().nullable(),
|
|
email: z
|
|
.string()
|
|
.trim()
|
|
.email('email.invalid')
|
|
.or(z.literal(''))
|
|
.nullable(),
|
|
notes: z.string().trim().nullable(),
|
|
payWithMercadoPago: z.boolean(),
|
|
});
|
|
|
|
export const UpdateAttendeeSchema = EditableAttendeeFieldsSchema.partial().refine(
|
|
(value) => Object.keys(value).length > 0,
|
|
'update.empty',
|
|
);
|
|
export type UpdateAttendee = z.output<typeof UpdateAttendeeSchema>;
|
|
|
|
/**
|
|
* Member listing item: `AttendeeDto` plus the summary the row needs so it does
|
|
* not run dry of data. It is a superset, so every consumer of the plain DTO
|
|
* keeps working unchanged; only the listings see these two extra fields.
|
|
*/
|
|
export const AttendeeListItemSchema = AttendeeDtoSchema.extend({
|
|
/** Most recent class sessions, oldest to newest. Empty if none was ever recorded. */
|
|
recentAttendance: z.array(attendanceStatusSchema).max(RECENT_ATTENDANCE_LIMIT),
|
|
/** `null` if there is no unpaid charge on record. */
|
|
payment: AttendeePaymentSummarySchema.nullable(),
|
|
});
|
|
export type AttendeeListItem = z.output<typeof AttendeeListItemSchema>;
|
|
|
|
export const AttendeeListSchema = z.object({
|
|
data: z.array(AttendeeListItemSchema),
|
|
pagination: paginationSchema,
|
|
});
|
|
export type AttendeeList = z.output<typeof AttendeeListSchema>;
|
|
|
|
/* The adult in charge of a kid (kids groups). Found by phone among the
|
|
teacher's people, so siblings share the same adult. */
|
|
export const GuardianInputSchema = z.object({
|
|
fullName: z.string().trim().min(1, 'guardian.nameRequired'),
|
|
phone: z.string().trim().min(6, 'phone.invalid'),
|
|
email: z.string().trim().email('email.invalid').optional().or(z.literal('')),
|
|
});
|
|
export type GuardianInput = z.output<typeof GuardianInputSchema>;
|
|
|
|
export const CreateAttendeeSchema = z
|
|
.object({
|
|
firstName: z.string().trim().min(1, 'name.required'),
|
|
lastName: z.string().trim().optional().default(''),
|
|
/* Optional only for a kid with an adult in charge (`guardian`) or when
|
|
adding an existing person (`personId`). */
|
|
phone: z.string().trim().min(6, 'phone.invalid').optional().or(z.literal('')),
|
|
email: z.string().trim().email('email.invalid').optional().or(z.literal('')),
|
|
notes: z.string().trim().optional(),
|
|
/* Adds an existing person of the teacher (found by phone) instead of creating
|
|
a new one: the name/email fields are ignored then. */
|
|
personId: z.string().min(1).optional(),
|
|
/* Kids groups: the adult in charge of a new kid. */
|
|
guardian: GuardianInputSchema.optional(),
|
|
})
|
|
.superRefine((value, ctx) => {
|
|
if (!value.phone && !value.guardian && !value.personId) {
|
|
ctx.addIssue({ code: z.ZodIssueCode.custom, path: ['phone'], message: 'phone.invalid' });
|
|
}
|
|
});
|
|
export type CreateAttendee = z.output<typeof CreateAttendeeSchema>;
|
|
|
|
export const CreateAttendeeResultSchema = z.discriminatedUnion('outcome', [
|
|
z.object({
|
|
outcome: z.literal('created'),
|
|
attendee: AttendeeDtoSchema,
|
|
}),
|
|
z.object({
|
|
outcome: z.literal('waitlisted'),
|
|
message: z.string(),
|
|
}),
|
|
]);
|
|
export type CreateAttendeeResult = z.output<typeof CreateAttendeeResultSchema>;
|
|
|
|
export const JoinGroupViaInviteSchema = z.object({
|
|
firstName: z.string().trim().min(1, 'name.required'),
|
|
lastName: z.string().trim().min(1, 'lastName.required'),
|
|
phone: z.string().trim().min(6, 'phone.invalid'),
|
|
email: z.string().trim().email('email.invalid').optional().or(z.literal('')),
|
|
});
|
|
export type JoinGroupViaInvite = z.output<typeof JoinGroupViaInviteSchema>;
|
|
|
|
export const JoinGroupViaInviteResultSchema = z.object({
|
|
status: z.literal('pre_registered'),
|
|
message: z.string(),
|
|
attendee: AttendeeDtoSchema.nullable(),
|
|
});
|
|
export type JoinGroupViaInviteResult = z.output<typeof JoinGroupViaInviteResultSchema>;
|
|
|
|
export const BulkAttendeeItemSchema = z.object({
|
|
firstName: z.string().trim().min(1, 'name.required'),
|
|
lastName: z.string().trim().optional().default(''),
|
|
fullName: z.string().trim().optional(),
|
|
phone: z.string().trim().min(6, 'phone.invalid'),
|
|
email: z.string().trim().email('email.invalid').optional().or(z.literal('')),
|
|
notes: z.string().trim().optional(),
|
|
});
|
|
export type BulkAttendeeItem = z.output<typeof BulkAttendeeItemSchema>;
|
|
|
|
export const BulkCreateAttendeesSchema = z.object({
|
|
attendees: z.array(BulkAttendeeItemSchema).min(1, 'bulk.empty'),
|
|
});
|
|
export type BulkCreateAttendees = z.output<typeof BulkCreateAttendeesSchema>;
|
|
|
|
export const BulkCreateAttendeesResultSchema = z.object({
|
|
totalProcessed: z.number().int(),
|
|
createdCount: z.number().int(),
|
|
/** Rows whose phone was already a person of the teacher: added to this group as that person. */
|
|
linkedCount: z.number().int(),
|
|
duplicatesCount: z.number().int(),
|
|
invalidCount: z.number().int(),
|
|
message: z.string(),
|
|
created: z.array(AttendeeDtoSchema),
|
|
});
|
|
export type BulkCreateAttendeesResult = z.output<typeof BulkCreateAttendeesResultSchema>;
|
|
|
|
/* Accepting someone from the waitlist defaults to the group they signed up
|
|
for; `targetGroupId` lets the teacher place them in another of their groups. */
|
|
export const PromoteGroupWaitlistEntrySchema = z.object({
|
|
targetGroupId: z.string().min(1).optional(),
|
|
});
|
|
export type PromoteGroupWaitlistEntry = z.output<typeof PromoteGroupWaitlistEntrySchema>;
|
|
|
|
// Moves a member to another group of the same teacher. History, fees and the
|
|
// student app access go with them (it is the same attendee).
|
|
export const MoveAttendeeSchema = z.object({
|
|
targetGroupId: z.string().min(1),
|
|
});
|
|
export type MoveAttendee = z.output<typeof MoveAttendeeSchema>;
|
|
|
|
// Adds the same person to another group of the teacher, keeping this one.
|
|
export const AddAttendeeToGroupSchema = z.object({
|
|
targetGroupId: z.string().min(1),
|
|
});
|
|
export type AddAttendeeToGroup = z.output<typeof AddAttendeeToGroupSchema>;
|
|
|
|
/* "Ya existe esta persona": what the add form shows when the phone is already
|
|
one of the teacher's people (`GET /people/lookup?groupId=&phone=`). The
|
|
group says whose people: its owner's. */
|
|
export const PersonLookupQuerySchema = z.object({
|
|
groupId: z.string().min(1),
|
|
phone: z.string().trim().min(6),
|
|
});
|
|
export type PersonLookupQuery = z.output<typeof PersonLookupQuerySchema>;
|
|
|
|
export const PersonSummarySchema = z.object({
|
|
id: z.string(),
|
|
fullName: z.string(),
|
|
email: z.string().nullable(),
|
|
phone: z.string().nullable(),
|
|
groups: z.array(PersonMembershipSchema),
|
|
/** When the phone is an adult in charge: their kids, to add one of them or a sibling. */
|
|
children: z.array(
|
|
z.object({ id: z.string(), fullName: z.string(), groups: z.array(PersonMembershipSchema) }),
|
|
),
|
|
});
|
|
export type PersonSummary = z.output<typeof PersonSummarySchema>;
|
|
|
|
export const PromoteGroupWaitlistEntryResultSchema = z.object({
|
|
attendee: AttendeeDtoSchema,
|
|
});
|
|
export type PromoteGroupWaitlistEntryResult = z.output<typeof PromoteGroupWaitlistEntryResultSchema>;
|
|
|
|
export const RemoveAttendeeQuerySchema = z.object({
|
|
promoteFromWaitlist: z.coerce.boolean().optional(),
|
|
});
|
|
export type RemoveAttendeeQuery = z.output<typeof RemoveAttendeeQuerySchema>;
|
|
|
|
export const RemoveAttendeeResultSchema = z.object({
|
|
removedAttendeeId: z.string(),
|
|
promoted: GroupWaitlistEntryDtoSchema.nullable(),
|
|
});
|
|
export type RemoveAttendeeResult = z.output<typeof RemoveAttendeeResultSchema>;
|