Passed
Pull Request — master (#99)
by Mathieu
01:23
created

server/src/Infrastructure/PayStub/Action/PayStub/CreatePayStubAction.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
rs 10
bpm 2
cpm 3
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A CreatePayStubAction.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 {PayStubDTO} from '../../DTO/PayStubDTO';
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 {CreatePayStubCommand} from 'src/Application/PayStub/Command/CreatePayStubCommand';
26
import {UserRole} from 'src/Domain/User/User.entity';
27
import {RolesGuard} from 'src/Infrastructure/User/Security/RolesGuard';
28
import {Roles} from 'src/Infrastructure/User/Decorator/Roles';
29
30
@Controller('pay_stubs')
31
@ApiUseTags('Pay stub')
32
@ApiBearerAuth()
33
@UseGuards(AuthGuard('bearer'), RolesGuard)
34
export class CreatePayStubAction {
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 paystub'})
45
  @ApiImplicitFile({name: 'file', required: true})
46
  public async index(
47
    @UploadedFile() file: IUploadedFile,
48
    @Body() dto: PayStubDTO
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 CreatePayStubCommand(dto.date, dto.userId, fileId)
58
      );
59
60
      return {id};
61
    } catch (e) {
62
      throw new BadRequestException(e.message);
63
    }
64
  }
65
}
66