//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format. //Copyright (C) 2024 HTWKalender support@htwkalender.de //This program is free software: you can redistribute it and/or modify //it under the terms of the GNU Affero General Public License as published by //the Free Software Foundation, either version 3 of the License, or //(at your option) any later version. //This program is distributed in the hope that it will be useful, //but WITHOUT ANY WARRANTY; without even the implied warranty of //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //GNU Affero General Public License for more details. //You should have received a copy of the GNU Affero General Public License //along with this program. If not, see . import { BSON } from "bson"; import { RoomOccupancyList } from "@/model/roomOccupancyList.ts"; import { Duration, NormalizedInterval, add, addDays, addMinutes, addMonths, clamp, differenceInMinutes, eachDayOfInterval, endOfDay, interval, isAfter, isBefore, isEqual, max, min, startOfDay, subDays } from "date-fns"; import { formatYearMonthDay } from "@/helpers/dates"; const END_OF_SUMMER_SEMESTER = "0930"; const END_OF_WINTER_SEMESTER = "0331"; /** * check if date is in winter semester before summer semester * @param date - The date to check * @returns boolean - true if date is in winter semester */ export function isBeforeSummer(date: Date): boolean { const formattedDate = formatYearMonthDay(date).slice(4); return formattedDate <= END_OF_WINTER_SEMESTER; } /** * check if date is in winter semester after summer semester * @param date - The date to check * @returns boolean - true if date is in winter semester */ export function isAfterSummer(date: Date): boolean { const formattedDate = formatYearMonthDay(date).slice(4); return formattedDate > END_OF_SUMMER_SEMESTER; } /** * Gets the start date of the current semester * @param date - The date to check * @returns Date - The start date of the current semester */ export function getSemesterStart(date: Date): Date { if (isBeforeSummer(date)) { return new Date(date.getFullYear()-1, 9, 1); } else if (isAfterSummer(date)) { return new Date(date.getFullYear(), 9, 1); } else { return new Date(date.getFullYear(), 3, 1); } } /** * Fetches the room occupancy for a given date range. * @param from_date the start date of the date range * @param to_date the end date of the date range * @returns RoomOccupancyList - the room occupancy list */ export async function fetchRoomOccupancy( from_date?: string, to_date?: string ): Promise { if (from_date == undefined) { let new_from_date = getSemesterStart(new Date()); from_date = new_from_date.toISOString(); } if (to_date == undefined) { let new_to_date = getSemesterStart(addMonths(new Date(), 6)); to_date = new_to_date.toISOString(); } var roomOccupancyList: RoomOccupancyList = new RoomOccupancyList( new Date(), 0, 0, [], ); await fetch("/api/schedule/rooms?from=" + from_date + "&to=" + to_date) .then((response) => { return response.arrayBuffer(); }) .then((roomsResponse: ArrayBuffer | null) => { if (roomsResponse == null) { return null; } const data = new Uint8Array(roomsResponse); roomOccupancyList = RoomOccupancyList.fromJSON(BSON.deserialize(data)); return roomOccupancyList; }); return roomOccupancyList; }