Files
gruperly/packages/shared/src/schemas/attendees.ts
Jose Selesan f1829fb5a9 feat: cambiar a un miembro de grupo
- `POST /groups/:gid/attendees/:aid/move` mueve al mismo attendee a otro
  grupo del profesor: conserva historial, cuotas (siguen del grupo anterior)
  y acceso a la app de alumnos. Cancela las faltas avisadas y libera lo
  reservado en clases futuras del grupo anterior.
- Cupo y teléfono duplicado se validan con `checkCanJoinGroup`, compartido
  con el paso desde la lista de espera.
- Ficha del miembro: "Cambiar de grupo" abre un diálogo con el selector de
  grupo destino y navega a la ficha en el grupo nuevo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 16:21:32 -03:00

193 lines
7.4 KiB
TypeScript

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<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>;
export const AttendeeDetailDtoSchema = AttendeeDtoSchema.extend({
payment: AttendeePaymentSummarySchema.nullable(),
});
export type AttendeeDetailDto = z.output<typeof AttendeeDetailDtoSchema>;
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<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>;
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<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, '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<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, '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<typeof BulkAttendeeItemSchema>;
export const BulkCreateAttendeesSchema = z.object({
attendees: z.array(BulkAttendeeItemSchema).min(1, 'Debes enviar al menos un alumno'),
});
export type BulkCreateAttendees = z.output<typeof BulkCreateAttendeesSchema>;
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<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>;
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>;