Skip to content

Commit edbe192

Browse files
committed
optimized school year management
1 parent 526b8dc commit edbe192

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

backend/untis.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export async function fetchWebUntis(
1717
credentials.server
1818
);
1919

20+
const logName = `${username}/${execution}`;
21+
2022
log(
2123
username,
2224
execution,
@@ -25,10 +27,6 @@ export async function fetchWebUntis(
2527
);
2628
await untis.login();
2729

28-
const currentSchoolyear = await untis.getCurrentSchoolyear();
29-
const endOfSchoolyear = new Date(currentSchoolyear.endDate);
30-
endOfSchoolyear.setHours(23, 59, 59, 999);
31-
3230
const now = new Date();
3331
let start = new Date(
3432
now.getFullYear(),
@@ -39,6 +37,38 @@ export async function fetchWebUntis(
3937
0,
4038
0
4139
);
40+
41+
let schoolyear = await untis.getCurrentSchoolyear();
42+
if (
43+
!schoolyear ||
44+
start > new Date(new Date(schoolyear.endDate).setHours(23, 59, 59, 999))
45+
) {
46+
console.info(
47+
`[${logName}]`,
48+
"Invalid current school year (fetching latest):",
49+
schoolyear
50+
);
51+
schoolyear = await untis.getLatestSchoolyear();
52+
if (
53+
!schoolyear ||
54+
start > new Date(new Date(schoolyear.endDate).setHours(23, 59, 59, 999))
55+
) {
56+
console.info(
57+
`[${logName}]`,
58+
"Invalid latest school year (error):",
59+
schoolyear
60+
);
61+
return {
62+
error:
63+
"No school year data found! Check WebUntis, when data is available there, contact support",
64+
};
65+
}
66+
start = new Date(new Date(schoolyear.startDate).setHours(0, 0, 0, 0));
67+
}
68+
const endOfSchoolyear = new Date(
69+
new Date(schoolyear.endDate).setHours(23, 59, 59, 999)
70+
);
71+
4272
let end = new Date(
4373
start.getFullYear(),
4474
start.getMonth(),
@@ -53,8 +83,6 @@ export async function fetchWebUntis(
5383
end = endOfSchoolyear;
5484
}
5585

56-
const logName = `${username}/${execution}`;
57-
5886
const data = {
5987
timetable: await fetchTimetable(logName, untis, start, end),
6088
holidays: await fetchHolidays(logName, untis),

0 commit comments

Comments
 (0)