From 3f8dfc6514d0d0f34601c0cb66f7383d774eff8e Mon Sep 17 00:00:00 2001 From: Luna Simons Date: Wed, 15 Nov 2023 14:38:36 +0100 Subject: [PATCH] feat: allow changing the amount of fetched days --- .env.example | 4 ++++ src/lessons/lessons.controller.ts | 14 ++++++++++++-- src/lessons/lessons.service.ts | 20 +++++++++++++++----- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/.env.example b/.env.example index d798b03..7964f22 100644 --- a/.env.example +++ b/.env.example @@ -9,3 +9,7 @@ UNTIS_BASEURL= # Optionally set the cors origin to use with untis-ics-sync-ui #CORS_ORIGIN= + +# Optionally override the amount of days to fetch before / after today +#LESSONS_TIMETABLE_BEFORE=31 +#LESSONS_TIMETABLE_AFTER=31 diff --git a/src/lessons/lessons.controller.ts b/src/lessons/lessons.controller.ts index 5a80f09..77ad806 100644 --- a/src/lessons/lessons.controller.ts +++ b/src/lessons/lessons.controller.ts @@ -10,6 +10,7 @@ import { ParseIntPipe, Query, } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; import { ApiNotFoundResponse, ApiOkResponse, @@ -24,6 +25,7 @@ import { LessonsService } from './lessons.service'; @Controller('lessons') export class LessonsController { constructor( + private readonly configService: ConfigService, private readonly untisService: UntisService, private readonly lessonsService: LessonsService, ) {} @@ -37,7 +39,11 @@ export class LessonsController { }) @Get(':classId') async getLessonsForClass(@Param('classId', ParseIntPipe) classId: number) { - const lessons = await this.untisService.fetchTimetable(14, 31, classId); + const lessons = await this.untisService.fetchTimetable( + this.configService.get('LESSONS_TIMETABLE_BEFORE', 31), + this.configService.get('LESSONS_TIMETABLE_AFTER', 31), + classId, + ); if (!lessons) throw new HttpException( 'No lessons found, does class exist?', @@ -70,7 +76,11 @@ export class LessonsController { @Query('offset', new DefaultValuePipe(0), ParseIntPipe) offset?: number, ) { - const lessons = await this.untisService.fetchTimetable(14, 31, classId); + const lessons = await this.untisService.fetchTimetable( + this.configService.get('LESSONS_TIMETABLE_BEFORE', 31), + this.configService.get('LESSONS_TIMETABLE_AFTER', 31), + classId, + ); if (!lessons) throw new HttpException( 'No lessons found, does class exist?', diff --git a/src/lessons/lessons.service.ts b/src/lessons/lessons.service.ts index 36fb49d..c4ddf4c 100644 --- a/src/lessons/lessons.service.ts +++ b/src/lessons/lessons.service.ts @@ -1,4 +1,5 @@ import { Injectable, Logger } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; import { createEvents, EventAttributes } from 'ics'; import { Lesson, WebUntis } from 'webuntis'; @@ -13,6 +14,8 @@ export interface LessonsOptions { export class LessonsService { private readonly logger: Logger = new Logger(LessonsService.name); + constructor(private readonly configService: ConfigService) {} + convertDate(date: Date, offset = 0) { return [ date.getFullYear(), @@ -98,11 +101,18 @@ export class LessonsService { } createMaintenanceEvent() { - const { - MAINTENANCE_TITLE: title, - MAINTENANCE_DESCRIPTION: description, - MAINTENANCE_LOCATION: location, - } = process.env; + const title = this.configService.get( + 'MAINTENANCE_TITLE', + undefined, + ), + description = this.configService.get( + 'MAINTENANCE_DESCRIPTION', + undefined, + ), + location = this.configService.get( + 'MAINTENANCE_LOCATION', + undefined, + ); const today = new Date(); return title ? ({