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; 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; export type AttendeeStatsDto = AttendeeStats; export const AttendeeQuerySchema = z.object({ page: pageSchema, pageSize: pageSizeSchema, groupId: z.string().optional(), }); export type AttendeeQuery = z.output; /** * 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; /** 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; 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; 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; /** * 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; export const AttendeeListSchema = z.object({ data: z.array(AttendeeListItemSchema), pagination: paginationSchema, }); export type AttendeeList = z.output; /* 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; 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; 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; 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; export const JoinGroupViaInviteResultSchema = z.object({ status: z.literal('pre_registered'), message: z.string(), attendee: AttendeeDtoSchema.nullable(), }); export type JoinGroupViaInviteResult = z.output; 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; export const BulkCreateAttendeesSchema = z.object({ attendees: z.array(BulkAttendeeItemSchema).min(1, 'bulk.empty'), }); export type BulkCreateAttendees = z.output; 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; /* 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; // 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; // 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; /* "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; 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; export const PromoteGroupWaitlistEntryResultSchema = z.object({ attendee: AttendeeDtoSchema, }); export type PromoteGroupWaitlistEntryResult = z.output; export const RemoveAttendeeQuerySchema = z.object({ promoteFromWaitlist: z.coerce.boolean().optional(), }); export type RemoveAttendeeQuery = z.output; export const RemoveAttendeeResultSchema = z.object({ removedAttendeeId: z.string(), promoted: GroupWaitlistEntryDtoSchema.nullable(), }); export type RemoveAttendeeResult = z.output;