-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters