Completed
Push — master ( fa76b5...b46f0e )
by Mathieu
21s queued 11s
created

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

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 50
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A AcceptLeaveRequestAction.index 0 17 2
1
import {
2
  Controller,
3
  Inject,
4
  BadRequestException,
5
  UseGuards,
6
  Param,
7
  Put,
8
  Body
9
} from '@nestjs/common';
10
import { AuthGuard } from '@nestjs/passport';
11
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
12
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO';
13
import { Roles } from 'src/Infrastructure/HumanResource/User/Decorator/Roles';
14
import { UserRole, User } from 'src/Domain/HumanResource/User/User.entity';
15
import { RolesGuard } from 'src/Infrastructure/HumanResource/User/Security/RolesGuard';
16
import { LoggedUser } from '../../User/Decorator/LoggedUser';
17
import { ICommandBus } from 'src/Application/ICommandBus';
18
import { ModerationDTO } from '../DTO/ModerationDTO';
19
import { AcceptLeaveRequestCommand } from 'src/Application/HumanResource/Leave/Command/AcceptLeaveRequestCommand';
20
21
@Controller('leave-requests')
22
@ApiTags('Human Resource')
23
@ApiBearerAuth()
24
@UseGuards(AuthGuard('bearer'), RolesGuard)
25
export class AcceptLeaveRequestAction {
26
  constructor(
27
    @Inject('ICommandBus')
28
    private readonly commandBus: ICommandBus
29
  ) {}
30
31
  @Put(':id/accept')
32
  @Roles(UserRole.COOPERATOR)
33
  @ApiOperation({summary: 'Accept leave request'})
34
  public async index(
35
    @Param() dto: IdDTO,
36
    @Body() moderation: ModerationDTO,
37
    @LoggedUser() user: User
38
  ) {
39
    try {
40
      const id = await this.commandBus.execute(
41
        new AcceptLeaveRequestCommand(user, dto.id, moderation.comment)
42
      );
43
44
      return {id};
45
    } catch (e) {
46
      throw new BadRequestException(e.message);
47
    }
48
  }
49
}
50