hezarfen_landing
Clone
https://git.tahinli.com/archive/hezarfen_landing.git
0 watching0 forksTypeScript581.6 KiB94b1804Burak Boduroğlu2 months ago15 commits
| 1 | /** |
| 2 | * Answers the frontend's `/api` requests from the fixture school. |
| 3 | * |
| 4 | * Only reads are served. Anything unmatched returns an empty page and is |
| 5 | * logged, so a new screen that needs data shows up as a line in the output |
| 6 | * instead of a silently blank panel. |
| 7 | */ |
| 8 | const page = (items, url) => { |
| 9 | const limit = Number(url.searchParams.get("limit")) || null; |
| 10 | const offset = Number(url.searchParams.get("offset")) || 0; |
| 11 | const slice = limit ? items.slice(offset, offset + limit) : items.slice(offset); |
| 12 | return { items: slice, total: items.length, limit, offset }; |
| 13 | }; |
| 14 | |
| 15 | export function createApi(school, { as }) { |
| 16 | const s = school; |
| 17 | const me = as; |
| 18 | const strip = ({ key, ...rest }) => rest; |
| 19 | const find = (list, id) => list.find(x => x.id === id); |
| 20 | |
| 21 | const marksFor = user => { |
| 22 | const courses = s.courses.map(c => { |
| 23 | const res = s.exams |
| 24 | .filter(e => e.course === c.id) |
| 25 | .map(e => ({ e, r: s.results.find(r => r.exam === e.id && r.user.id === user) })) |
| 26 | .filter(x => x.r) |
| 27 | .map(({ e, r }) => ({ exam: e.id, title: e.title, kind: e.kind, weight: e.kind === "midterm" ? 2 : 1, mark: r.mark, grade: null, graded_by: s.teachers[c.key].id })); |
| 28 | const avg = res.length ? Math.round((res.reduce((a, b) => a + b.mark * b.weight, 0) / res.reduce((a, b) => a + b.weight, 0)) * 10) / 10 : null; |
| 29 | return { course: strip(c), results: res, average: avg, average_grade: null }; |
| 30 | }); |
| 31 | const avgs = courses.map(c => c.average).filter(a => a != null); |
| 32 | return { user, courses, overall_average: avgs.length ? Math.round((avgs.reduce((a, b) => a + b, 0) / avgs.length) * 10) / 10 : null, overall_grade: null }; |
| 33 | }; |
| 34 | |
| 35 | const counts = (present, absent, late, excused) => ({ present, absent, late, excused, custom: {}, total: present + absent + late + excused, rate: present / (present + absent + late + excused) }); |
| 36 | |
| 37 | const routes = [ |
| 38 | [/^\/auth\/me$/, () => me], |
| 39 | [/^\/time$/, () => ({ now: s.now })], |
| 40 | [/^\/users\/me\/profile$/, () => ({ id: me.id, username: me.username, display_name: me.display_name, role: me.role, bio: null, avatar: null, classes: [], courses: [], badges: [], stats: {} })], |
| 41 | [/^\/users\/search$/, u => page(s.users, u)], |
| 42 | [/^\/users$/, u => page(s.users, u)], |
| 43 | [/^\/users\/([^/]+)$/, (u, [id]) => find(s.users, id)], |
| 44 | [/^\/courses(\/me)?$/, u => page(s.courses.map(strip), u)], |
| 45 | [/^\/courses\/([^/]+)$/, (u, [id]) => strip(find(s.courses, id))], |
| 46 | [/^\/courses\/([^/]+)\/subjects$/, (u, [id]) => page(s.subjectRows.filter(x => x.course === id).map(strip), u)], |
| 47 | [/^\/courses\/([^/]+)\/exams$/, (u, [id]) => page(s.exams.filter(x => x.course === id), u)], |
| 48 | [/^\/courses\/([^/]+)\/enrollments$/, (u, [id]) => page(s.students.map(st => ({ id: `en-${id}-${st.id}`, course: id, user: s.ref(st), enrolled_by: s.ref(s.manager), source: s.classOf(st).id })), u)], |
| 49 | [/^\/classes(\/me)?$/, u => page(s.classes, u)], |
| 50 | [/^\/classes\/([^/]+)$/, (u, [id]) => find(s.classes, id)], |
| 51 | [/^\/classes\/([^/]+)\/members$/, (u, [id]) => page(s.students.filter(st => s.classOf(st).id === id).map(st => ({ id: `cm-${st.id}`, class: id, user: s.ref(st), added_by: s.ref(s.manager) })), u)], |
| 52 | [/^\/classes\/user\/([^/]+)$/, (u, [id]) => page([s.classOf(find(s.students, id) ?? s.students[0])], u)], |
| 53 | [/^\/terms$/, u => page([s.term], u)], |
| 54 | [/^\/exams$/, u => { |
| 55 | const after = Number(u.searchParams.get("ends_after")); |
| 56 | const list = after ? s.exams.filter(e => e.ends_at > after).sort((a, b) => a.starts_at - b.starts_at) : [...s.exams].reverse(); |
| 57 | return page(list, u); |
| 58 | }], |
| 59 | [/^\/exams\/([^/]+)$/, (u, [id]) => find(s.exams, id)], |
| 60 | [/^\/exams\/([^/]+)\/statistics$/, (u, [id]) => s.examStats.get(id) ?? { exam: id, graded: 0, average: null, min: null, max: null }], |
| 61 | [/^\/exams\/([^/]+)\/results$/, (u, [id]) => page(s.results.filter(r => r.exam === id), u)], |
| 62 | [/^\/exams\/([^/]+)\/questions$/, (u, [id]) => page(s.questions.map(q => ({ ...q, exam: id })), u)], |
| 63 | [/^\/bank-questions$/, u => page(s.bank, u)], |
| 64 | [/^\/homework$/, u => page(s.homework, u)], |
| 65 | [/^\/events$/, u => { |
| 66 | const after = Number(u.searchParams.get("ends_after")); |
| 67 | const list = after ? s.events.filter(e => e.ends_at > after).sort((a, b) => a.starts_at - b.starts_at) : s.events; |
| 68 | return page(list, u); |
| 69 | }], |
| 70 | [/^\/appointments$/, u => page(s.appointments, u)], |
| 71 | [/^\/messages$/, u => page(u.searchParams.get("read") === "false" ? s.messages.filter(m => !m.read) : s.messages, u)], |
| 72 | [/^\/meals\/menus$/, u => page([s.menu], u)], |
| 73 | [/^\/marks\/me$/, () => marksFor(me.id)], |
| 74 | [/^\/marks\/([^/]+)$/, (u, [id]) => marksFor(decodeURIComponent(id))], |
| 75 | [/^\/attendance\/(me|[^/]+)$/, (u, [id]) => ({ user: id, events: counts(11, 1, 0, 0), sessions: counts(142, 4, 3, 2), courses: s.courses.map(c => ({ course: strip(c), counts: counts(24, 1, 1, 0) })) })], |
| 76 | [/^\/modules$/, () => ({ enabled: ["appointments", "attendance", "bank_questions", "boards", "chatbot", "classes", "course_notes", "courses", "events", "exams", "homework", "marks", "meals", "messages", "notes", "payments", "pomodoro", "questions", "sessions", "subjects", "work"] })], |
| 77 | [/^\/notes$/, u => page([], u)], |
| 78 | [/^\/chatbot\/threads$/, u => page(s.chat?.threads ?? [], u)], |
| 79 | [/^\/chatbot\/threads\/([^/]+)\/messages$/, (u, [id]) => page(s.chat?.messages.filter(m => m.thread_id === id) ?? [], u)], |
| 80 | ]; |
| 81 | |
| 82 | /** Metadata identical for every school: safe to read from the real backend. */ |
| 83 | const passthrough = [/^\/limits$/, /^\/modules\/catalog$/, /^\/settings$/]; |
| 84 | |
| 85 | return async function handle(route) { |
| 86 | const req = route.request(); |
| 87 | const url = new URL(req.url()); |
| 88 | const path = url.pathname.replace(/^\/api/, ""); |
| 89 | if (req.method() !== "GET") return route.fulfill({ status: 204, body: "" }); |
| 90 | if (passthrough.some(re => re.test(path))) return route.continue(); |
| 91 | for (const [re, fn] of routes) { |
| 92 | const m = path.match(re); |
| 93 | if (m) { |
| 94 | const body = fn(url, m.slice(1)); |
| 95 | if (body === undefined) break; |
| 96 | return route.fulfill({ status: 200, contentType: "application/json", body: JSON.stringify(body) }); |
| 97 | } |
| 98 | } |
| 99 | console.log(" [api] unmocked", path + url.search); |
| 100 | return route.fulfill({ status: 200, contentType: "application/json", body: JSON.stringify({ items: [], total: 0, limit: null, offset: 0 }) }); |
| 101 | }; |
| 102 | } |