Passed
Pull Request — master (#287)
by
unknown
01:50
created

server/src/Infrastructure/HumanResource/Leave/Action/GetLeavesCalendarAction.ts   A

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 43
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 39
mnd 1
bc 1
fnc 1
dl 0
loc 43
bpm 1
cpm 2
noi 0
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A GetLeavesCalendarAction.index 0 17 2
1
import {
2
  Controller,
3
  Inject,
4
  BadRequestException,
5
  Get,
6
  Res,
7
  UseGuards
8
} from '@nestjs/common';
9
import { Response } from 'express';
10
import { ApiTags, ApiOperation, ApiSecurity } from '@nestjs/swagger';
11
import { IQueryBus } from 'src/Application/IQueryBus';
12
import { CalendarSecretGuard } from 'src/Infrastructure/HumanResource/User/Security/CalendarSecretGuard';
13
import { GetLeavesCalendarQuery } from 'src/Application/HumanResource/Leave/Query/GetLeavesCalendarQuery';
14
15
@Controller('leaves')
16
@ApiTags('Human Resource')
17
@ApiSecurity('calendar_secret')
18
@UseGuards(CalendarSecretGuard)
19
export class GetLeavesCalendarAction {
20
  constructor(
21
    @Inject('IQueryBus')
22
    private readonly queryBus: IQueryBus
23
  ) {}
24
25
  @Get('calendar.ics')
26
  @ApiOperation({ summary: 'Export leaves to iCalendar format' })
27
  public async index(@Res() res: Response): Promise<Response> {
28
    res.header('Content-Type', 'text/calendar;charset=UTF-8');
29
    res.header('Cache-Control', 'no-store, no-cache, must-revalidate');
30
    res.attachment('leaves.ics');
31
32
    let ics: string;
33
34
    try {
35
      ics = await this.queryBus.execute(new GetLeavesCalendarQuery());
36
    } catch (e) {
37
      throw new BadRequestException(e.message);
38
    }
39
40
    return res.send(ics);
41
  }
42
}
43