import { z } from 'zod'; 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; export const AttendeeDtoSchema = z.object({ id: z.string(), groupId: z.string(), fullName: z.string(), email: z.string().nullable(), phone: z.string().nullable(), guardianName: z.string().nullable(), guardianPhone: z.string().nullable(), notes: z.string().nullable(), 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; export const AttendeeDetailDtoSchema = AttendeeDtoSchema.extend({ payment: AttendeePaymentSummarySchema.nullable(), }); export type AttendeeDetailDto = z.output; const EditableAttendeeFieldsSchema = z.object({ phone: z.string().trim().nullable(), email: z .string() .trim() .email('Ingresá un email válido') .or(z.literal('')) .nullable(), notes: z.string().trim().nullable(), }); export const UpdateAttendeeSchema = EditableAttendeeFieldsSchema.partial().refine( (value) => Object.keys(value).length > 0, 'Enviá al menos un campo para actualizar', ); 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; export const CreateAttendeeSchema = z.object({ firstName: z.string().trim().min(1, 'El nombre es obligatorio'), lastName: z.string().trim().optional().default(''), phone: z.string().trim().min(6, 'Ingresa un número de teléfono válido'), email: z.string().trim().email('Email inválido').optional().or(z.literal('')), notes: z.string().trim().optional(), }); 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, 'El nombre es obligatorio'), lastName: z.string().trim().min(1, 'El apellido es obligatorio'), phone: z.string().trim().min(6, 'Ingresa un número de teléfono válido'), email: z.string().trim().email('Email inválido').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, 'El nombre es obligatorio'), lastName: z.string().trim().optional().default(''), fullName: z.string().trim().optional(), phone: z.string().trim().min(6, 'Ingresa un número de teléfono válido'), email: z.string().trim().email('Email inválido').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, 'Debes enviar al menos un alumno'), }); export type BulkCreateAttendees = z.output; export const BulkCreateAttendeesResultSchema = z.object({ totalProcessed: z.number().int(), createdCount: 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; 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;