Skip to content

Commit

Permalink
feat: added holiday fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
bddvlpr committed Sep 18, 2023
1 parent 2841e4c commit e0fe5fc
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ClassesModule } from './classes/classes.module';
import { ConfigModule } from '@nestjs/config';
import { SubjectsModule } from './subjects/subjects.module';
import { LessonsModule } from './lessons/lessons.module';
import { HolidaysModule } from './holidays/holidays.module';

@Module({
imports: [
Expand All @@ -12,6 +13,7 @@ import { LessonsModule } from './lessons/lessons.module';
ClassesModule,
SubjectsModule,
LessonsModule,
HolidaysModule,
],
})
export class AppModule {}
18 changes: 18 additions & 0 deletions src/holidays/holidays.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { HolidaysController } from './holidays.controller';

describe('HolidaysController', () => {
let controller: HolidaysController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [HolidaysController],
}).compile();

controller = module.get<HolidaysController>(HolidaysController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
20 changes: 20 additions & 0 deletions src/holidays/holidays.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Controller, Get, Header } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { UntisService } from 'src/untis/untis.service';
import { HolidaysService } from './holidays.service';

@ApiTags('holidays')
@Controller('holidays')
export class HolidaysController {
constructor(
private readonly untisService: UntisService,
private readonly holidaysService: HolidaysService,
) {}

@Header('Content-Type', 'text/calendar')
@Get()
async getHolidays() {
const holidays = await this.untisService.fetchHolidays();
return await this.holidaysService.convertToEvents(holidays);
}
}
11 changes: 11 additions & 0 deletions src/holidays/holidays.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { UntisModule } from 'src/untis/untis.module';
import { HolidaysController } from './holidays.controller';
import { HolidaysService } from './holidays.service';

@Module({
controllers: [HolidaysController],
imports: [UntisModule],
providers: [HolidaysService],
})
export class HolidaysModule {}
18 changes: 18 additions & 0 deletions src/holidays/holidays.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { HolidaysService } from './holidays.service';

describe('HolidaysService', () => {
let service: HolidaysService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [HolidaysService],
}).compile();

service = module.get<HolidaysService>(HolidaysService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
45 changes: 45 additions & 0 deletions src/holidays/holidays.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Injectable, Logger } from '@nestjs/common';
import { createEvents, EventAttributes } from 'ics';
import { Holiday, WebUntis } from 'webuntis';

@Injectable()
export class HolidaysService {
private readonly logger: Logger = new Logger(HolidaysService.name);

async convertToEvents(holidays: Holiday[]) {
const { error, value } = createEvents(
holidays
.map((h) => ({
...h,
start: WebUntis.convertUntisDate(h.startDate.toString()),
end: WebUntis.convertUntisDate(h.endDate.toString()),
}))
.map(
(h) =>
({
title: h.name,
description: h.longName,

start: [
h.start.getFullYear(),
h.start.getMonth() + 1,
h.start.getDate(),
],
startInputType: 'local',
startOutputType: 'utc',

end: [h.end.getFullYear(), h.end.getMonth() + 1, h.end.getDate()],
endInputType: 'local',
endOutputType: 'utc',
} as EventAttributes),
),
);

if (error) {
this.logger.error(`Failed creating events: ${error.message}.`);
return null;
}

return value;
}
}
21 changes: 19 additions & 2 deletions src/untis/untis.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ import { Inject, Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Cache } from 'cache-manager';
import * as moment from 'moment';
import { Klasse, Lesson, SchoolYear, Subject, WebUntis } from 'webuntis';
import {
Holiday,
Klasse,
Lesson,
SchoolYear,
Subject,
WebUntis,
} from 'webuntis';

const CACHE_SCHOOLYEAR = 'SCHOOLYEAR',
CACHE_CLASSES = 'CLASSES',
CACHE_SUBJECTS = 'SUBJECTS',
CACHE_TIMETABLE = 'TIMETABLE';
CACHE_TIMETABLE = 'TIMETABLE',
CACHE_HOLIDAYS = 'HOLIDAYS';

@Injectable()
export class UntisService {
Expand Down Expand Up @@ -81,6 +89,15 @@ export class UntisService {
});
}

async fetchHolidays() {
return await this.retrieveCache<Holiday[]>(CACHE_HOLIDAYS, async () => {
await this.validateSession();

this.logger.log('Fetching holidays...');
return this.client.getHolidays(false);
});
}

async fetchTimetable(
before: number,
after: number,
Expand Down

0 comments on commit e0fe5fc

Please sign in to comment.