Passed
Pull Request — master (#148)
by Mathieu
03:17
created

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

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 61
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A CreateLeaveAction.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 {ApiTags, ApiBearerAuth, ApiOperation} from '@nestjs/swagger';
11
import {ICommandBus} from 'src/Application/ICommandBus';
12
import {LeaveRequestDTO} from '../DTO/LeaveRequestDTO';
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 {CreateLeaveRequestCommand} from 'src/Application/HumanResource/Leave/Command/CreateLeaveRequestCommand';
17
import {LoggedUser} from '../../User/Decorator/LoggedUser';
18
19
@Controller('leaves')
20
@ApiTags('Human Resource')
21
@ApiBearerAuth()
22
@UseGuards(AuthGuard('bearer'), RolesGuard)
23
export class CreateLeaveAction {
24
  constructor(
25
    @Inject('ICommandBus')
26
    private readonly commandBus: ICommandBus
27
  ) {}
28
29
  @Post()
30
  @Roles(UserRole.COOPERATOR, UserRole.EMPLOYEE)
31
  @ApiOperation({summary: 'Create new leave'})
32
  public async index(@Body() dto: LeaveRequestDTO, @LoggedUser() user: User) {
33
    const {
34
      Type,
35
      startDate,
36
      startsAllDay,
37
      endDate,
38
      endsAllDay,
39
      comment
40
    } = dto;
41
42
    try {
43
      const id = await this.commandBus.execute(
44
        new CreateLeaveRequestCommand(
45
          user,
46
          Type,
47
          startDate,
48
          startsAllDay === 'true',
49
          endDate,
50
          endsAllDay === 'true',
51
          comment
52
        )
53
      );
54
55
      return {id};
56
    } catch (e) {
57
      throw new BadRequestException(e.message);
58
    }
59
  }
60
}
61