Passed
Pull Request — master (#104)
by Mathieu
01:36
created

server/src/Infrastructure/HumanResource/PaySlip/Action/CreatePaySlipAction.ts   A

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 66
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A CreatePaySlipAction.index 0 24 3
1
import {
2
  Body,
3
  Post,
4
  Controller,
5
  Inject,
6
  BadRequestException,
7
  UseGuards,
8
  UploadedFile,
9
  UseInterceptors
10
} from '@nestjs/common';
11
import {
12
  ApiUseTags,
13
  ApiBearerAuth,
14
  ApiOperation,
15
  ApiImplicitFile,
16
  ApiConsumes
17
} from '@nestjs/swagger';
18
import {FileInterceptor} from '@nestjs/platform-express';
19
import {AuthGuard} from '@nestjs/passport';
20
import {ICommandBus} from 'src/Application/ICommandBus';
21
import {PaySlipDTO} from '../DTO/PaySlipDTO';
22
import {IUploadedFile} from 'src/Domain/File/IUploadedFile';
23
import {PDFValidator} from 'src/Domain/File/Validator/PDFValidator';
24
import {UploadFileCommand} from 'src/Application/File/Command/UploadFileCommand';
25
import {CreatePaySlipCommand} from 'src/Application/HumanResource/PaySlip/Command/CreatePaySlipCommand';
26
import {UserRole} from 'src/Domain/HumanResource/User/User.entity';
27
import {RolesGuard} from 'src/Infrastructure/HumanResource/User/Security/RolesGuard';
28
import {Roles} from 'src/Infrastructure/HumanResource/User/Decorator/Roles';
29
30
@Controller('pay_slips')
31
@ApiUseTags('Human Resource')
32
@ApiBearerAuth()
33
@UseGuards(AuthGuard('bearer'), RolesGuard)
34
export class CreatePaySlipAction {
35
  constructor(
36
    @Inject('ICommandBus')
37
    private readonly commandBus: ICommandBus
38
  ) {}
39
40
  @Post()
41
  @Roles(UserRole.COOPERATOR, UserRole.ACCOUNTANT)
42
  @UseInterceptors(FileInterceptor('file'))
43
  @ApiConsumes('multipart/form-data')
44
  @ApiOperation({title: 'Create new payslip'})
45
  @ApiImplicitFile({name: 'file', required: true})
46
  public async index(
47
    @UploadedFile() file: IUploadedFile,
48
    @Body() dto: PaySlipDTO
49
  ) {
50
    if (false === PDFValidator.isValid(file)) {
51
      throw new BadRequestException('file.erros.invalid_pdf');
52
    }
53
54
    try {
55
      const fileId = await this.commandBus.execute(new UploadFileCommand(file));
56
      const id = await this.commandBus.execute(
57
        new CreatePaySlipCommand(dto.date, dto.userId, fileId)
58
      );
59
60
      return {id};
61
    } catch (e) {
62
      throw new BadRequestException(e.message);
63
    }
64
  }
65
}
66