hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import type { AttendanceStatus } from "@/api/client"; |
| 2 | import type { MessageKey } from "@/i18n/messages"; |
| 3 | |
| 4 | /** |
| 5 | * Event wording for the core attendance statuses. The backend knows the same |
| 6 | * status values for lessons and events; only the label changes ("Var" reads |
| 7 | * as "in class" — at an event the student "attended"). Unknown school-defined |
| 8 | * statuses have no entry and fall back to the shared lesson label or the raw |
| 9 | * value. |
| 10 | */ |
| 11 | const EVENT_STATUS_KEYS: Record<string, MessageKey> = { |
| 12 | present: "events.status.present", |
| 13 | absent: "events.status.absent", |
| 14 | late: "events.status.late", |
| 15 | excused: "events.status.excused", |
| 16 | }; |
| 17 | |
| 18 | export function eventStatusLabelKey(status: AttendanceStatus): MessageKey | null { |
| 19 | return EVENT_STATUS_KEYS[status] ?? null; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * The school's statuses in its own order, except "present" leads — it is the |
| 24 | * status taken most, so it sits first on every row (as in lesson roll call). |
| 25 | */ |
| 26 | export function orderRollCallStatuses(list: readonly AttendanceStatus[]): AttendanceStatus[] { |
| 27 | return list.includes("present") ? ["present", ...list.filter((status) => status !== "present")] : [...list]; |
| 28 | } |
| 29 | |
| 30 | export type RollCallChange = { userId: string; status: AttendanceStatus }; |
| 31 | |
| 32 | /** |
| 33 | * The rows a save has to send: every drafted status that differs from what |
| 34 | * the server already holds. A draft equal to the saved mark is a no-op. |
| 35 | */ |
| 36 | export function rollCallChanges( |
| 37 | saved: Readonly<Record<string, AttendanceStatus | null | undefined>>, |
| 38 | draft: Readonly<Record<string, AttendanceStatus | undefined>>, |
| 39 | ): RollCallChange[] { |
| 40 | const changes: RollCallChange[] = []; |
| 41 | for (const [userId, status] of Object.entries(draft)) { |
| 42 | if (status && saved[userId] !== status) changes.push({ userId, status }); |
| 43 | } |
| 44 | return changes; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Roll call opens once the event has started. An event with no start time |
| 49 | * has nothing to wait for and is always open. |
| 50 | */ |
| 51 | export function isRollCallOpen(startsAt: number | null | undefined, now: number): boolean { |
| 52 | return startsAt == null || startsAt <= now; |
| 53 | } |
| 54 | |
| 55 | export type RollCallSaveResult = { |
| 56 | saved: RollCallChange[]; |
| 57 | failed: { change: RollCallChange; error: unknown }[]; |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * Sends the changes one at a time — the API marks a single user per call and |
| 62 | * has no bulk endpoint. A failed row does not stop the rest; each outcome is |
| 63 | * reported so the caller can show per-row errors. |
| 64 | */ |
| 65 | export async function saveRollCallChanges( |
| 66 | changes: readonly RollCallChange[], |
| 67 | send: (change: RollCallChange) => Promise<unknown>, |
| 68 | onProgress?: (done: number, total: number) => void, |
| 69 | ): Promise<RollCallSaveResult> { |
| 70 | const result: RollCallSaveResult = { saved: [], failed: [] }; |
| 71 | let done = 0; |
| 72 | for (const change of changes) { |
| 73 | try { |
| 74 | await send(change); |
| 75 | result.saved.push(change); |
| 76 | } catch (error) { |
| 77 | result.failed.push({ change, error }); |
| 78 | } |
| 79 | done += 1; |
| 80 | onProgress?.(done, changes.length); |
| 81 | } |
| 82 | return result; |
| 83 | } |