hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import type { CourseSession, Instance } from "@/api/client"; |
| 2 | |
| 3 | export type RollCallState = "not-taken" | "partial" | "done" | "upcoming" | "in-progress"; |
| 4 | |
| 5 | export type TodayLesson = { |
| 6 | session: CourseSession; |
| 7 | instance: Instance; |
| 8 | /** Roll-call rows saved for the session; null when the read failed. */ |
| 9 | marked: number | null; |
| 10 | state: RollCallState | null; |
| 11 | }; |
| 12 | |
| 13 | /** Local-day bounds [start, end) around `now`, in unix milliseconds. */ |
| 14 | export function dayBounds(now: number): [number, number] { |
| 15 | const start = new Date(now); |
| 16 | start.setHours(0, 0, 0, 0); |
| 17 | const end = new Date(start); |
| 18 | end.setDate(end.getDate() + 1); |
| 19 | return [start.getTime(), end.getTime()]; |
| 20 | } |
| 21 | |
| 22 | /** Sessions that start today, earliest first. */ |
| 23 | export function sessionsToday<T extends { starts_at: number }>(sessions: T[], now: number): T[] { |
| 24 | const [start, end] = dayBounds(now); |
| 25 | return sessions.filter((session) => session.starts_at >= start && session.starts_at < end).sort((a, b) => a.starts_at - b.starts_at); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * What the teacher still owes a lesson. A lesson that has not started yet is |
| 30 | * "upcoming" whatever its rows say; after the start, no rows is "not taken", |
| 31 | * fewer rows than enrolled students is "partial". Rows can include the |
| 32 | * teacher's own presence, so "done" is at least the roster size. |
| 33 | */ |
| 34 | export function rollCallState(startsAt: number, now: number, marked: number | null, enrolled: number): RollCallState | null { |
| 35 | if (startsAt > now) return "upcoming"; |
| 36 | if (marked == null) return null; |
| 37 | if (marked === 0) return "not-taken"; |
| 38 | return marked >= enrolled ? "done" : "partial"; |
| 39 | } |
| 40 | |
| 41 | /** A student's view of a lesson: only whether it is on right now. */ |
| 42 | export function lessonNow(startsAt: number, endsAt: number | null, now: number): "in-progress" | null { |
| 43 | return startsAt <= now && endsAt != null && now < endsAt ? "in-progress" : null; |
| 44 | } |