diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2026-05-28 15:20:46 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2026-05-28 15:20:46 +0200 |
| commit | f9a9fa15d6d61eb9730183193ce8af29bdfc17e7 (patch) | |
| tree | c3b96a36623e52f22d281b518d7136795cf82b8a /cockpit/js/graduation-common.js | |
| download | bycs-sol-f9a9fa15d6d61eb9730183193ce8af29bdfc17e7.tar.gz bycs-sol-f9a9fa15d6d61eb9730183193ce8af29bdfc17e7.zip | |
cockpit: initial commit
Diffstat (limited to 'cockpit/js/graduation-common.js')
| -rw-r--r-- | cockpit/js/graduation-common.js | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/cockpit/js/graduation-common.js b/cockpit/js/graduation-common.js new file mode 100644 index 0000000..8d0e626 --- /dev/null +++ b/cockpit/js/graduation-common.js @@ -0,0 +1,134 @@ +import { GRADUATION_DB, GRADUATION_DB_PARAMS_MAP } from "./config.js"; +import { loadFromDatabase, writeToDatabase } from "./database.js" + +class GraduationChange { + constructor(date, time, id, name, group, change, reason, coach, json) { + this.date = date; + this.time = time; + this.id = id; + this.name = name; + this.group = group; + this.change = change; + this.reason = reason; + this.coach = coach; + this.json = json; + } + + static newGreenPointChange(student, reason, coach) { + const now = new Date(); + return new GraduationChange( + now, now.toTimeString(), + student.id, student.name, student.group, + "Grün", reason, coach, "" + ); + } + + static newOrangePointChange(student, reason, coach) { + const now = new Date(); + return new GraduationChange( + now, now.toTimeString(), + student.id, student.name, student.group, + "Orange", reason, coach, "" + ); + } + + static newRedPointChange(student, reason, coach) { + const now = new Date(); + return new GraduationChange( + now, now.toTimeString(), + student.id, student.name, student.group, + "Rot", reason, coach, "" + ); + } +} + +class Graduation { + constructor(student, green, orange, red, level, stickers, changes) { + this.student = student; + this.green = green; + this.orange = orange; + this.red = red; + this.level = level; + this.stickers = stickers; + this.changes = changes; + + student.graduation = this; + } + + static from(student, changes) { + let green = 0; + let orange = 0; + let red = 0; + let level = "Beginner"; + let stickers = []; + for (const change of changes) { + switch (change.change) { + case "Grün": + green++; + break; + case "Orange": + orange++; + break; + case "Rot": + red++; + break; + case "Gespräch": + orange = 0; + break; + case "Änderung": + // TODO: + break; + default: + alert(`Unexpected change: ${change}`); + } + } + return new Graduation(student, green, orange, red, level, stickers, changes); + } + + addChange(change) { + // TODO(muhq): Implement incremental addition + let changes = this.changes; + changes.push(change); + // This updates the student.graduation reference as well + Graduation.from(this.student, changes); + } +} + +async function fetchGraduationChanges(student) { + let params = {}; + + for (const key in student) { + if (GRADUATION_DB_PARAMS_MAP[key] instanceof Function) { + const res = GRADUATION_DB_PARAMS_MAP[key](student[key]); + Object.assign(res, params); + } else { + params[GRADUATION_DB_PARAMS_MAP[key]] = student[key]; + } + } + const data = await loadFromDatabase(GRADUATION_DB, params); + + let changes = []; + for (const e of data) { + const gradChange = new GraduationChange(e.date, e.time, e.id, e.name, e.group, e.change, e.reason, e.coach, e.json); + changes.push(gradChange); + } + return changes; +} + +async function fetchGraduation(student) { + const changes = await fetchGraduationChanges(student); + return Graduation.from(student, changes); +} + +async function submitGraduationChange(student, change) { + student.graduation.addChange(change); + writeToDatabase(change); +} + +export { + GraduationChange, + Graduation, + fetchGraduation, + fetchGraduationChanges, + submitGraduationChange +};
\ No newline at end of file |
