aboutsummaryrefslogtreecommitdiff
path: root/src/graduation-prod.ts
blob: 975fd0f24d7795793afe649a637a85053e6471be (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
import { getGraduationDb } from "./database-ENV.ts"
import { Graduation, GraduationChange } from "./graduation-common.ts";
import type { Student } from "./student-common.ts";

const _graduationChangesPromise = async function() {
	const db = await getGraduationDb();
	let changes = new Map();
	let entries = await db.fetchEntries();
	for (const e of entries) {
		const gradChange = GraduationChange.fromDBEntry(e);
		const studentChanges = changes.get(gradChange.studentId);
		if (studentChanges === null) {
			changes.set(gradChange.studentId, [gradChange]);
		} else {
			studentChanges.push(gradChange);
		}
	}
	return changes;
}();

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

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

export {
	LEVEL_NAMES,
	GraduationChange,
	Graduation,
	submitGraduationChange,
	submitObservation,

	OBSERVATION_TYPES,
	Observation,
	ObservationPeriod,
} from "./graduation-common.ts";