Files
gruperly/packages/shared/src/schemas/analytics.ts
Jose Selesan 69275c37b4 chore: update comments and documentation for clarity and consistency across various files
- Translated comments from Spanish to English in vite.config.ts, docker-compose.yml, attendance-risk.ts, analytics.ts, attendance.ts, attendees.ts, enums.ts, groups.ts, and onboarding.ts.
- Improved descriptions for better understanding of code functionality and purpose.
- Ensured uniformity in terminology and phrasing throughout the codebase.
2026-10-01 16:06:54 -03:00

118 lines
4.6 KiB
TypeScript

import { z } from 'zod';
import { attendanceRiskLevelSchema, attendanceStatusSchema } from './attendance.js';
const isoDateTimeSchema = z.string().datetime();
export const attendeeStatusSchema = z.enum(['ACTIVE', 'PAUSED', 'DROPPED']);
export type AttendeeStatus = z.output<typeof attendeeStatusSchema>;
export const riskLevelSchema = z.enum(['HIGH', 'MEDIUM']);
export type RiskLevel = z.output<typeof riskLevelSchema>;
export const GroupAnalyticsSchema = z.object({
attendanceRate: z.number().min(0).max(100),
totalPresent: z.number().int().min(0),
totalClasses: z.number().int().min(0),
recoveredSlots: z.number().int().min(0),
});
export type GroupAnalytics = z.output<typeof GroupAnalyticsSchema>;
export type GroupAnalyticsDto = GroupAnalytics;
export const StudentAtRiskSchema = z.object({
attendeeId: z.string(),
fullName: z.string(),
phone: z.string().nullable(),
riskLevel: riskLevelSchema,
consecutiveAbsences: z.number().int().min(0),
lastAttendedAt: isoDateTimeSchema.nullable(),
monthlyAttendanceRate: z.number().min(0).max(100),
});
export type StudentAtRisk = z.output<typeof StudentAtRiskSchema>;
export type StudentAtRiskDto = StudentAtRisk;
export const StudentsAtRiskSchema = z.object({
groupName: z.string(),
data: z.array(StudentAtRiskSchema),
});
export type StudentsAtRisk = z.output<typeof StudentsAtRiskSchema>;
export type StudentsAtRiskDto = StudentsAtRisk;
export const AttendanceHistoryItemSchema = z.object({
classSessionId: z.string(),
startsAt: isoDateTimeSchema,
status: attendanceStatusSchema.nullable(),
});
export type AttendanceHistoryItem = z.output<typeof AttendanceHistoryItemSchema>;
export const AttendeeHistorySchema = z.object({
attendeeId: z.string(),
fullName: z.string(),
groupId: z.string(),
groupName: z.string(),
attendanceRate: z.number().min(0).max(100),
sessions: z.array(AttendanceHistoryItemSchema),
});
export type AttendeeHistory = z.output<typeof AttendeeHistorySchema>;
export type AttendeeHistoryDto = AttendeeHistory;
export const UpdateStudentStatusSchema = z.object({
status: attendeeStatusSchema,
});
export type UpdateStudentStatus = z.output<typeof UpdateStudentStatusSchema>;
export const UpdateStudentStatusResultSchema = z.object({
attendeeId: z.string(),
status: attendeeStatusSchema,
});
export type UpdateStudentStatusResult = z.output<typeof UpdateStudentStatusResultSchema>;
export const groupRiskLevelSchema = z.enum(['NONE', 'MEDIUM', 'HIGH']);
export type GroupRiskLevel = z.output<typeof groupRiskLevelSchema>;
/** How many members go in the summary's urgent list. The listing card only
* names the first one or two; the detail page has its own at-risk students
* screen, so the summary does not need to drag the whole group over the
* network. */
export const URGENT_MEMBERS_LIMIT = 5;
export const UrgentMemberSummarySchema = z.object({
attendeeId: z.string(),
fullName: z.string(),
level: attendanceRiskLevelSchema,
consecutiveAbsences: z.number().int().min(0),
daysSinceLastAttendance: z.number().int().min(0).nullable(),
attendanceRate: z.number().min(0).max(100).nullable(),
});
export type UrgentMemberSummary = z.output<typeof UrgentMemberSummarySchema>;
/* The level is the worst attendee in the group; the two counters say why it
fires, so the list can name the reason instead of a bare "risk". They are
disjoint: a member with an absence streak lands only in the first, and the
second groups those who only cross over for low attendance. */
export const GroupRiskSummarySchema = z.object({
groupId: z.string(),
groupName: z.string(),
riskLevel: groupRiskLevelSchema,
absenceStreakCount: z.number().int().min(0),
lowAttendanceCount: z.number().int().min(0),
/* What the listing card and the Home follow-up section consume. It comes
from `getGroupAttendanceSummary` (packages/shared/src/lib/attendance-risk.ts),
which already receives the whole history in memory: the per-level summary
cannot be derived from the two counters above, because "ok" is the
difference and the free spots come from the capacity, not from
attendance. */
riesgoAlto: z.number().int().min(0),
atencion: z.number().int().min(0),
ok: z.number().int().min(0),
libres: z.number().int().min(0),
enEspera: z.number().int().min(0),
asistencia: z.number().min(0).max(100).nullable(),
urgentes: z.array(UrgentMemberSummarySchema).max(URGENT_MEMBERS_LIMIT),
});
export type GroupRiskSummary = z.output<typeof GroupRiskSummarySchema>;
export const GroupsRiskOverviewSchema = z.object({
items: z.array(GroupRiskSummarySchema),
});
export type GroupsRiskOverview = z.output<typeof GroupsRiskOverviewSchema>;
export type GroupsRiskOverviewDto = GroupsRiskOverview;