import { z } from 'zod'; const isoDateTimeSchema = z.string().datetime(); /* Student access (attendee or guardian), without passwords. The teacher's invitation link proves the identity once; the device then keeps a long session, and passkeys / a recovery email let the student back in. */ export const INVITATION_TTL_HOURS = 72; export const RECOVERY_TTL_MINUTES = 15; export const EMAIL_CONFIRMATION_TTL_HOURS = 24; export const STUDENT_SESSION_TTL_DAYS = 90; // — Link (GET shows, POST consumes) — export const studentLinkKindSchema = z.enum(['invitation', 'recovery']); export type StudentLinkKind = z.output; /** An unusable link (used, expired, revoked or unknown) answers the same * `invalid`, without telling whose it was. */ export const StudentLinkInfoSchema = z.discriminatedUnion('status', [ z.object({ status: z.literal('valid'), kind: studentLinkKindSchema, name: z.string(), teacherName: z.string().nullable(), groupName: z.string().nullable(), }), z.object({ status: z.literal('invalid') }), ]); export type StudentLinkInfo = z.output; export const ConsumeStudentLinkResultSchema = z.object({ /** First sign-in ever: the web shows the welcome screen. */ firstLogin: z.boolean(), }); export type ConsumeStudentLinkResult = z.output; // — Session ("me") — export const StudentProfileSchema = z.object({ attendeeId: z.string(), fullName: z.string(), groupId: z.string(), groupName: z.string(), teacherName: z.string(), }); export type StudentProfile = z.output; export const StudentMeSchema = z.object({ account: z.object({ id: z.string(), name: z.string(), recoveryEmail: z.string().nullable(), pendingRecoveryEmail: z.string().nullable(), hasPasskey: z.boolean(), welcomed: z.boolean(), }), students: z.array(StudentProfileSchema), }); export type StudentMe = z.output; // — Recovery email — const emailSchema = z.string().trim().toLowerCase().email().max(254); export const SetRecoveryEmailSchema = z.strictObject({ email: emailSchema }); export type SetRecoveryEmail = z.output; export const RequestRecoveryLinkSchema = z.strictObject({ email: emailSchema }); export type RequestRecoveryLink = z.output; export const ConfirmRecoveryEmailSchema = z.strictObject({ token: z.string().min(16).max(128) }); export type ConfirmRecoveryEmail = z.output; // — Passkeys (the WebAuthn JSON is validated by the library, not by zod) — export const PasskeyOptionsResultSchema = z.object({ challengeId: z.string(), options: z.record(z.unknown()), }); export type PasskeyOptionsResult = z.output; export const PasskeyVerifySchema = z.object({ challengeId: z.string().min(1).max(64), response: z.record(z.unknown()), }); export type PasskeyVerify = z.output; // — Teacher side: access state of an attendee — export const studentAccessStatusSchema = z.enum(['not_invited', 'invited', 'active', 'expired']); export type StudentAccessStatus = z.output; export const StudentAccessDeviceSchema = z.object({ device: z.string(), createdAt: isoDateTimeSchema, lastUsedAt: isoDateTimeSchema, }); export type StudentAccessDevice = z.output; export const StudentAccessDtoSchema = z.object({ status: studentAccessStatusSchema, invitationExpiresAt: isoDateTimeSchema.nullable(), /** Active sessions, most recently used first. */ devices: z.array(StudentAccessDeviceSchema), hasPasskey: z.boolean(), }); export type StudentAccessDto = z.output; export const StudentInvitationDtoSchema = z.object({ /** Absolute link to the web. Returned once: only its hash is stored. */ url: z.string().url(), expiresAt: isoDateTimeSchema, /** The attendee's phone (or the guardian's) in E.164 for `wa.me`; null without one. */ whatsappPhone: z.string().nullable(), access: StudentAccessDtoSchema, }); export type StudentInvitationDto = z.output; // — Student: classes and absence notices — export const absenceReasonSchema = z.enum(['health', 'travel', 'work_study', 'other']); export type AbsenceReasonValue = z.output; /** How far ahead a student can give notice. */ export const ABSENCE_NOTICE_HORIZON_DAYS = 30; export const StudentClassSchema = z.object({ /** The class is identified by its start: sessions are created on demand. */ startsAt: isoDateTimeSchema, groupName: z.string(), started: z.boolean(), notice: z .object({ id: z.string(), reason: absenceReasonSchema.nullable(), message: z.string().nullable(), createdAt: isoDateTimeSchema, }) .nullable(), }); export type StudentClass = z.output; export const StudentClassesDtoSchema = z.object({ attendeeId: z.string(), timeZone: z.string(), /** Next Monday 00:00 on the teacher's clock: classes before it are "Esta semana". */ weekEndsAt: isoDateTimeSchema, /** From the start of this week (Monday) to the notice horizon, ascending. */ classes: z.array(StudentClassSchema), }); export type StudentClassesDto = z.output; export const StudentAttendeeQuerySchema = z.object({ attendeeId: z.string().min(1) }); export type StudentAttendeeQuery = z.output; export const CreateAbsenceNoticesSchema = z.strictObject({ attendeeId: z.string().min(1), startsAt: z.array(isoDateTimeSchema).min(1).max(20), reason: absenceReasonSchema.nullish(), message: z.string().trim().max(500).nullish(), }); export type CreateAbsenceNotices = z.output; export const CreateAbsenceNoticesResultSchema = z.object({ notified: z.array(z.object({ id: z.string(), startsAt: isoDateTimeSchema })), }); export type CreateAbsenceNoticesResult = z.output; // — Student: fees and reported payments — export const studentFeeStatusSchema = z.enum(['pending', 'overdue', 'reported']); export type StudentFeeStatus = z.output; export const StudentFeeSchema = z.object({ id: z.string(), description: z.string().nullable(), amount: z.number(), currency: z.string(), dueDate: isoDateTimeSchema, status: studentFeeStatusSchema, checkoutUrl: z.string().nullable(), }); export type StudentFee = z.output; export const reportedPaymentStatusSchema = z.enum(['reported', 'confirmed', 'rejected']); export type ReportedPaymentStatusValue = z.output; export const StudentPaymentReportSchema = z.object({ id: z.string(), totalAmount: z.number(), currency: z.string(), status: reportedPaymentStatusSchema, createdAt: isoDateTimeSchema, reviewedAt: isoDateTimeSchema.nullable(), note: z.string().nullable(), rejectionReason: z.string().nullable(), fees: z.array(z.object({ id: z.string(), description: z.string().nullable(), dueDate: isoDateTimeSchema })), }); export type StudentPaymentReport = z.output; export const TransferDetailsSchema = z.object({ alias: z.string(), holder: z.string().nullable(), }); export type TransferDetails = z.output; export const StudentFeesDtoSchema = z.object({ attendeeId: z.string(), /** Unpaid fees (pending, overdue or already reported), oldest first. */ fees: z.array(StudentFeeSchema), transfer: TransferDetailsSchema.nullable(), /** Latest reports, newest first. */ reports: z.array(StudentPaymentReportSchema), }); export type StudentFeesDto = z.output; export const RECEIPT_MAX_MB = 10; /** Max length of the optional note the student adds to a reported payment. */ export const REPORTED_PAYMENT_NOTE_MAX = 500; export const RECEIPT_ACCEPT = 'image/jpeg,image/png,image/webp,image/heic,image/heif,application/pdf'; // — Teacher side: reported payments to verify — export const ReportedPaymentDtoSchema = z.object({ id: z.string(), status: reportedPaymentStatusSchema, attendee: z.object({ id: z.string(), fullName: z.string() }), group: z.object({ id: z.string(), name: z.string() }), createdAt: isoDateTimeSchema, totalAmount: z.number(), currency: z.string(), /** Optional message from the student. */ note: z.string().nullable(), fees: z.array( z.object({ id: z.string(), description: z.string().nullable(), dueDate: isoDateTimeSchema, amount: z.number(), }), ), receipt: z.object({ mime: z.string(), name: z.string(), size: z.number().int(), /** Signed 5-minute URL for the card's thumbnail (previewable images only). */ thumbnailUrl: z.string().nullable(), /** false once the retention policy deleted the file. */ available: z.boolean(), }), }); export type ReportedPaymentDto = z.output; export const ReportedPaymentListSchema = z.object({ data: z.array(ReportedPaymentDtoSchema) }); export type ReportedPaymentList = z.output; export const ReceiptUrlDtoSchema = z.object({ url: z.string().url(), mime: z.string(), name: z.string(), expiresAt: isoDateTimeSchema, }); export type ReceiptUrlDto = z.output; export const RejectReportedPaymentSchema = z.strictObject({ reason: z.string().trim().max(300).nullish(), }); export type RejectReportedPayment = z.output; /** Minutes after a review during which "Deshacer" still works server side. */ export const REVIEW_UNDO_MINUTES = 10; export const UpdateTransferDetailsSchema = z.strictObject({ alias: z.string().trim().max(60).nullable(), holder: z.string().trim().max(120).nullable(), }); export type UpdateTransferDetails = z.output; export const TransferDetailsDtoSchema = z.object({ alias: z.string().nullable(), holder: z.string().nullable(), }); export type TransferDetailsDto = z.output; // — Teacher side: absence notices of a group — export const TeacherAbsenceNoticeSchema = z.object({ id: z.string(), attendee: z.object({ id: z.string(), fullName: z.string() }), startsAt: isoDateTimeSchema, reason: absenceReasonSchema.nullable(), message: z.string().nullable(), createdAt: isoDateTimeSchema, }); export type TeacherAbsenceNotice = z.output; export const TeacherAbsenceNoticeListSchema = z.object({ /** Notices for classes of this week (Monday to Sunday, teacher's clock). */ data: z.array(TeacherAbsenceNoticeSchema), }); export type TeacherAbsenceNoticeList = z.output;