aboutsummaryrefslogtreecommitdiff
path: root/src/graduation-prod.ts
blob: f21aa5dbed01d879cfad2e8b09ec5df1a2f10998 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { getGraduationDb } from "./database-ENV.ts";
import { Graduation, GraduationChange } from "./graduation-common.ts";
import type { Student } from "./student-common.ts";

const _graduationChangesPromise = (async function () {
	let universalChanges = new Map();
	let studentChanges = new Map();

	const db = await getGraduationDb();

	let entries = await db.fetchEntries();
	for (const e of entries) {
		const gradChange = GraduationChange.fromDBEntry(e);
		const changes = gradChange.studentId > 0 ? studentChanges : universalChanges;

		if (!changes.has(gradChange.studentId)) {
			changes.set(gradChange.studentId, [gradChange]);
		} else {
			changes.get(gradChange.studentId).push(gradChange);
		}
	}
	return [studentChanges, universalChanges];
})();

const graduations: Map<number, Graduation> = new Map();

export async function fetchGraduation(student: Student): Promise<Graduation> {
	if (!graduations.has(student.id)) {
		const [studentChanges, universalChanges] = await _graduationChangesPromise;
		const specificChanges = studentChanges.get(student.id) ?? [];
		const graduation = Graduation.from(student, specificChanges.concat(universalChanges));
		graduations.set(student.id, graduation);
		return graduation;
	}
	return graduations.get(student.id)!;
}

export {
	LEVEL_NAMES,
	GraduationChange,
	Graduation,
	submitGraduationChange,
	deleteGraduationChange,
} from "./graduation-common.ts";