This repository has been archived on 2026-11-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
hope-events/frontend/src/lib/kids.ts
T

128 lines
6.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Kids check-in (1.45.0): shared types and the offline desk's local copy.
export type KidsChild = {
id: string; firstName: string; lastName: string; name: string; dateOfBirth: string | null; age: number | null;
gender: string | null; allergies: string; hasMedical: boolean; hasCustody: boolean;
medicalNotes?: string; custodyNotes?: string; lastCheckInAt: string | null;
};
export type KidsParent = { userId: string; name: string; phone: string | null; email: string | null };
export type KidsAdult = { id: string; name: string; phone: string | null; relationship: string | null };
export type KidsFamily = { id: string; name: string; parents: KidsParent[]; children: KidsChild[]; adults: KidsAdult[] };
export type KidsRoom = {
id: string; name: string; minAge: number | null; maxAge: number | null; capacity: number | null; ratio: number | null; order: number;
leaderNames: string[]; leaderIds: string[]; children: number; leaders: number; leadersNeeded: number; full: boolean; overRatio: boolean;
};
export type KidsCheckIn = {
id: string; childId: string | null; name: string; firstName: string; age: number | null; roomId: string | null; code: string;
checkedInAt: string; checkedInBy: string | null; checkedOutAt: string | null; checkedOutBy: string | null; collectedBy: string | null;
override: boolean; offline: boolean; allergies: string; medicalNotes: string; custodyNotes: string; sticker: string;
family: { id: string; name: string; parents: KidsParent[]; adults: KidsAdult[] } | null;
};
export type KidsAlert = { id: string; kind: "wrong_code" | "page_parent" | "override"; roomId: string | null; checkInId: string | null; message: string; by: string | null; at: string; resolvedAt: string | null };
export type KidsBoard = {
event: { id: string; title: string; scheduleType: string };
occasion: string; sessionId: string | null; sessionLabel: string | null;
rooms: KidsRoom[]; checkIns: KidsCheckIn[]; alerts: KidsAlert[];
};
export type KidsEvent = {
id: string; title: string; startDate: string; endDate: string; scheduleType: string; campusId: string | null;
sessions: { id: string; label: string | null; startsAt: string; endsAt: string }[];
};
export type CheckInResult = {
code: string; qr: string; occasion: string; family: { id: string; name: string };
checkIns: { id: string; name: string; firstName: string; room: { id: string; name: string } | null; already: boolean; sticker: string }[];
};
export const QR_PREFIX = "KIDS-";
export const FAMILY_QR_PREFIX = "FAMILY-";
/** "2–4 yrs", "4+ yrs", "up to 3 yrs", "" */
export function ageRange(r: { minAge: number | null; maxAge: number | null }) {
if (r.minAge != null && r.maxAge != null) return `${r.minAge}–${r.maxAge} yrs`;
if (r.minAge != null) return `${r.minAge}+ yrs`;
if (r.maxAge != null) return `up to ${r.maxAge} yrs`;
return "";
}
/** The same choice as the backend's suggestRoom: narrowest fitting room that isn't full. */
export function suggestRoom(rooms: KidsRoom[], age: number | null, extra: Record<string, number> = {}) {
const fits = rooms.filter(r =>
(age == null || ((r.minAge == null || age >= r.minAge) && (r.maxAge == null || age <= r.maxAge)))
&& !(r.capacity && r.children + (extra[r.id] || 0) >= r.capacity));
const span = (r: KidsRoom) => (r.maxAge ?? 25) - (r.minAge ?? 0);
return [...fits].sort((a, b) => (age == null ? 0 : span(a) - span(b)) || a.order - b.order)[0] || rooms[0] || null;
}
export function stickerAlert(allergies: string, medical: boolean) {
const parts: string[] = [];
const a = (allergies || "").trim();
if (a) parts.push(`ALLERGY: ${a.toUpperCase().slice(0, 24)}${a.length > 24 ? "…" : ""}`);
if (medical) parts.push("MEDICAL");
return parts.join(" · ");
}
// ─── Offline desk ─────────────────────────────────────────────────────────────
export type OfflineSnapshot = {
occasion: string; sessionId: string | null; sessionLabel: string | null;
event: { id: string; title: string; scheduleType: string };
rooms: KidsRoom[]; checkIns: KidsCheckIn[]; families: KidsFamily[]; codes: string[]; downloadedAt: string;
};
export type OfflineCheckIn = { clientId: string; familyId: string; childId: string; roomId: string; code: string; at: string; name: string; roomName: string };
export type OfflineCheckOut = { clientId: string; collectedBy: string; at: string };
export type OfflineState = {
snapshot: OfflineSnapshot;
// Codes handed out offline, per family (one code per family per occasion).
familyCodes: Record<string, string>;
checkIns: OfflineCheckIn[];
checkOuts: OfflineCheckOut[];
};
const key = (eventId: string) => `hope_events_kids_offline_${eventId}`;
export function deviceId(): string {
const k = "hope_events_kids_device";
try {
let id = localStorage.getItem(k);
if (!id) {
id = `kd-${crypto.randomUUID()}`;
localStorage.setItem(k, id);
}
return id;
} catch {
return "kd-unknown";
}
}
export function loadOffline(eventId: string): OfflineState | null {
try {
const raw = localStorage.getItem(key(eventId));
return raw ? (JSON.parse(raw) as OfflineState) : null;
} catch {
return null;
}
}
export function saveOffline(eventId: string, state: OfflineState | null) {
try {
if (state) localStorage.setItem(key(eventId), JSON.stringify(state));
else localStorage.removeItem(key(eventId));
} catch {
// Storage full or blocked: the desk keeps working online.
}
}
/** Check children in on the offline copy. Returns the code, or throws when the device has no codes left. */
export function offlineCheckIn(state: OfflineState, family: KidsFamily, picks: { child: KidsChild; room: KidsRoom }[]): { state: OfflineState; code: string } {
const used = new Set(Object.values(state.familyCodes));
const code = state.familyCodes[family.id] || state.snapshot.codes.find(c => !used.has(c));
if (!code) throw new Error("This device has no offline pickup codes left. Go back online to get more.");
const at = new Date().toISOString();
const already = new Set(state.checkIns.filter(c => !state.checkOuts.some(o => o.clientId === c.clientId)).map(c => c.childId));
const fresh = picks.filter(p => !already.has(p.child.id)).map(p => ({
clientId: `kc-${crypto.randomUUID()}`, familyId: family.id, childId: p.child.id, roomId: p.room.id, code, at, name: p.child.name, roomName: p.room.name,
}));
return { code, state: { ...state, familyCodes: { ...state.familyCodes, [family.id]: code }, checkIns: [...state.checkIns, ...fresh] } };
}