Skip to content

Commit

Permalink
merge: pull request #25 from bddvlpr/feature/proper-env
Browse files Browse the repository at this point in the history
Allow changing the amount of fetched days
  • Loading branch information
bddvlpr authored Nov 15, 2023
2 parents 76d83ff + 3f8dfc6 commit 20d619b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 12 additions & 2 deletions src/lessons/lessons.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ParseIntPipe,
Query,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import {
ApiNotFoundResponse,
ApiOkResponse,
Expand All @@ -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,
) {}
Expand All @@ -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<number>('LESSONS_TIMETABLE_BEFORE', 31),
this.configService.get<number>('LESSONS_TIMETABLE_AFTER', 31),
classId,
);
if (!lessons)
throw new HttpException(
'No lessons found, does class exist?',
Expand Down Expand Up @@ -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<number>('LESSONS_TIMETABLE_BEFORE', 31),
this.configService.get<number>('LESSONS_TIMETABLE_AFTER', 31),
classId,
);
if (!lessons)
throw new HttpException(
'No lessons found, does class exist?',
Expand Down
20 changes: 15 additions & 5 deletions src/lessons/lessons.service.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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(),
Expand Down Expand Up @@ -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<string>(
'MAINTENANCE_TITLE',
undefined,
),
description = this.configService.get<string>(
'MAINTENANCE_DESCRIPTION',
undefined,
),
location = this.configService.get<string>(
'MAINTENANCE_LOCATION',
undefined,
);
const today = new Date();
return title
? ({
Expand Down

0 comments on commit 20d619b

Please sign in to comment.