Completed
Push — master ( 18b141...988e26 )
by Mathieu
14s queued 10s
created

CreateHolidayAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 32
dl 0
loc 40
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A index 0 30 2
1
import {
2
  Body,
3
  Post,
4
  Controller,
5
  Inject,
6
  BadRequestException,
7
  UseGuards
8
} from '@nestjs/common';
9
import {AuthGuard} from '@nestjs/passport';
10
import {ApiUseTags, ApiBearerAuth, ApiOperation} from '@nestjs/swagger';
11
import {ICommandBus} from 'src/Application/ICommandBus';
12
import {HolidayDTO} from '../DTO/HolidayDTO';
13
import {RolesGuard} from 'src/Infrastructure/HumanResource/User/Security/RolesGuard';
14
import {UserRole, User} from 'src/Domain/HumanResource/User/User.entity';
15
import {Roles} from 'src/Infrastructure/HumanResource/User/Decorator/Roles';
16
import {CreateHolidayCommand} from 'src/Application/HumanResource/Holiday/Command/CreateHolidayCommand';
17
import {LoggedUser} from '../../User/Decorator/LoggedUser';
18
19
@Controller('holidays')
20
@ApiUseTags('Human Resource')
21
@ApiBearerAuth()
22
@UseGuards(AuthGuard('bearer'), RolesGuard)
23
export class CreateHolidayAction {
24
  constructor(
25
    @Inject('ICommandBus')
26
    private readonly commandBus: ICommandBus
27
  ) {}
28
29
  @Post()
30
  @Roles(UserRole.COOPERATOR, UserRole.EMPLOYEE)
31
  @ApiOperation({title: 'Create new holiday'})
32
  public async index(@Body() dto: HolidayDTO, @LoggedUser() user: User) {
33
    const {
34
      leaveType,
35
      startDate,
36
      startsAllDay,
37
      endDate,
38
      endsAllDay,
39
      comment
40
    } = dto;
41
42
    try {
43
      const id = await this.commandBus.execute(
44
        new CreateHolidayCommand(
45
          user,
46
          leaveType,
47
          startDate,
48
          Boolean(startsAllDay),
49
          endDate,
50
          Boolean(endsAllDay),
51
          comment
52
        )
53
      );
54
55
      return {id};
56
    } catch (e) {
57
      throw new BadRequestException(e.message);
58
    }
59
  }
60
}
61