This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
const API_BASE_URL = process.env.ANNOUNCEMENT_API || "http://localhost:3000";
|
||||
|
||||
export type Announcement = {
|
||||
id: number;
|
||||
text_content: string;
|
||||
author: string;
|
||||
flags: Flag[];
|
||||
start_date: string;
|
||||
end_date: string;
|
||||
};
|
||||
|
||||
export enum Flag {
|
||||
SHOW_ALL_ENTRIES
|
||||
}
|
||||
|
||||
export type AnnouncementResponse = {
|
||||
[date: string]: Announcement[];
|
||||
}
|
||||
|
||||
export default async function getAnnouncements(dates: string[]): Promise<AnnouncementResponse> {
|
||||
if (dates.length === 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const url = new URL(`/v1/announcements/${dates.join(",")}`, API_BASE_URL).toString();
|
||||
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return data;
|
||||
} catch (e) {
|
||||
console.error("Some random ahh error:", e);
|
||||
|
||||
return {};
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import fs from "fs"
|
||||
import fs from "fs/promises"
|
||||
|
||||
const CLASSES: string[] = [
|
||||
"A1a", "A1b", "A1c", "C1a", "C1b", "C1c", "A2", "C2a", "C2b", "C2c", "E2", "C3a", "C3b", "C3c", "E3"
|
||||
@@ -45,7 +45,7 @@ interface ScheduleData {
|
||||
status: { lastUpdated: string };
|
||||
}
|
||||
|
||||
export default function generateArchivedV1_V2(): void {
|
||||
export default async function generateArchivedV1_V2() {
|
||||
const dates = getCurrentWeekMondayToFriday();
|
||||
const currentDate = new Date();
|
||||
const lastUpdated = currentDate.getHours().toString().padStart(2, "0") + ":" + currentDate.getMinutes().toString().padStart(2, "0");
|
||||
@@ -73,6 +73,8 @@ export default function generateArchivedV1_V2(): void {
|
||||
data.schedule.push(d);
|
||||
}
|
||||
|
||||
fs.writeFileSync("volume/db/v1.json", JSON.stringify(data, null, 2));
|
||||
fs.writeFileSync("volume/db/v2.json", JSON.stringify(data, null, 2));
|
||||
await Promise.all([
|
||||
fs.writeFile("volume/db/v1.json", JSON.stringify(data, null, 2)),
|
||||
fs.writeFile("volume/db/v2.json", JSON.stringify(data, null, 2)),
|
||||
]);
|
||||
}
|
||||
|
||||
+14
-9
@@ -18,6 +18,7 @@ import parseTeachers from "../utils/parseTeachers.js"
|
||||
import ExcelJS, { Worksheet, Cell, Row, Workbook } from "exceljs"
|
||||
import JSZip from "jszip";
|
||||
import { parseStringPromise } from "xml2js";
|
||||
import getAnnouncements, { Flag } from "../api/annoucements.js";
|
||||
|
||||
interface ThemeColors {
|
||||
[key: number]: string | null;
|
||||
@@ -123,11 +124,13 @@ export default async function parseV3(workbook: Workbook, downloadedFilePath: st
|
||||
|
||||
const upcoming = getUpcomingSheets(workbook);
|
||||
const resolvedDays = groupSheetsByDate(upcoming);
|
||||
const getDates = resolvedDays.map((d) => d.dateKey);
|
||||
const annoucements = await getAnnouncements(getDates);
|
||||
|
||||
const schedule: any = {};
|
||||
|
||||
for (const { dateKey, sheet } of resolvedDays) {
|
||||
const { changes, absence, inWork, takesPlace, reservedRooms } = extractDaySchedule(sheet, teacherMap, themeColors);
|
||||
const { changes, absence, inWork, takesPlace, reservedRooms } = extractDaySchedule(sheet, teacherMap, themeColors, annoucements[dateKey].map(a => a.flags).flat());
|
||||
|
||||
schedule[dateKey] = {
|
||||
info: { inWork },
|
||||
@@ -140,6 +143,7 @@ export default async function parseV3(workbook: Workbook, downloadedFilePath: st
|
||||
|
||||
const data = {
|
||||
status: { lastUpdated: formatNowTime() },
|
||||
annoucements,
|
||||
schedule,
|
||||
};
|
||||
|
||||
@@ -202,9 +206,9 @@ function groupSheetsByDate(items: ResolvedDay[]) {
|
||||
// ────────────────────────────────────────────────────────────
|
||||
//
|
||||
|
||||
function extractDaySchedule(sheet: Worksheet, teacherMap: Record<string, string>, themeColors: ThemeColors | null) {
|
||||
function extractDaySchedule(sheet: Worksheet, teacherMap: Record<string, string>, themeColors: ThemeColors | null, flags: Flag[]) {
|
||||
return {
|
||||
changes: extractClassChanges(sheet, themeColors),
|
||||
changes: extractClassChanges(sheet, themeColors, flags),
|
||||
absence: extractAbsence(sheet, teacherMap),
|
||||
inWork: isPripravaSheet(sheet.name.toLowerCase()),
|
||||
takesPlace: extractTakesPlace(sheet),
|
||||
@@ -226,7 +230,8 @@ function isPripravaSheet(name: string) {
|
||||
// ────────────────────────────────────────────────────────────
|
||||
//
|
||||
|
||||
function extractClassChanges(sheet: Worksheet, themeColors: ThemeColors | null) {
|
||||
function extractClassChanges(sheet: Worksheet, themeColors: ThemeColors | null, flags: Flag[]) {
|
||||
const ignoreColors = flags.includes(Flag.SHOW_ALL_ENTRIES)
|
||||
const classRegex = /[AEC][0-4][a-c]?\s*\/.*/s;
|
||||
const prefixRegex = /[AEC][0-4][a-c]?/;
|
||||
|
||||
@@ -248,20 +253,20 @@ function extractClassChanges(sheet: Worksheet, themeColors: ThemeColors | null)
|
||||
|
||||
classCells.forEach((address, index) => {
|
||||
const row = sheet.getRow(Number(sheet.getCell(address).row));
|
||||
changes[classes[index]] = buildLessonArray(row, address, themeColors);
|
||||
changes[classes[index]] = buildLessonArray(row, address, themeColors, ignoreColors);
|
||||
});
|
||||
|
||||
return changes;
|
||||
}
|
||||
|
||||
function buildLessonArray(row: Row, ignoreAddress: string, themeColors: ThemeColors | null) {
|
||||
function buildLessonArray(row: Row, ignoreAddress: string, themeColors: ThemeColors | null, ignoreColors: boolean) {
|
||||
const lessons: (Lesson | null)[] = [];
|
||||
|
||||
row.eachCell((cell) => {
|
||||
if (cell.address === ignoreAddress) return;
|
||||
|
||||
const colIndex = letterToNumber(cell.address.replace(/[0-9]/g, ""));
|
||||
lessons[colIndex] = parseLessonCell(cell, themeColors);
|
||||
lessons[colIndex] = parseLessonCell(cell, themeColors, ignoreColors);
|
||||
});
|
||||
|
||||
const normalized = Array.from(lessons, (x) => (x === undefined ? null : x));
|
||||
@@ -270,13 +275,13 @@ function buildLessonArray(row: Row, ignoreAddress: string, themeColors: ThemeCol
|
||||
return normalized.slice(1, 11);
|
||||
}
|
||||
|
||||
function parseLessonCell(cell: Cell, themeColors: ThemeColors | null): Lesson | null {
|
||||
function parseLessonCell(cell: Cell, themeColors: ThemeColors | null, ignoreColors: boolean): Lesson | null {
|
||||
try {
|
||||
const text = (cell.text || "").trim();
|
||||
const cleanupRegex = /^úklid\s+(?:\d+\s+)?[A-Za-z]{2}$/;
|
||||
|
||||
// @ts-ignore
|
||||
if (!text || cleanupRegex.test(text) || !cell.fill?.fgColor) return null;
|
||||
if (!text || cleanupRegex.test(text) || (!cell.fill?.fgColor && !ignoreColors)) return null;
|
||||
|
||||
const backgroundColor = resolveCellColor(cell, themeColors);
|
||||
const foregroundColor = !backgroundColor ? undefined : (
|
||||
|
||||
Reference in New Issue
Block a user