Completed
Push — master ( 509540...176e41 )
by Mathieu
25s queued 12s
created

server/src/Infrastructure/Accounting/Action/PayStub/CreatePayStubAction.ts   A

Complexity

Total Complexity 3
Complexity/F 3

Size

Lines of Code 62
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 56
mnd 2
bc 2
fnc 1
dl 0
loc 62
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 23 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/Accounting/Command/PayStub/CreatePayStubCommand';
26
27
@Controller('pay_stubs')
28
@ApiUseTags('Accounting')
29
@ApiBearerAuth()
30
@UseGuards(AuthGuard('bearer'))
31
export class CreatePayStubAction {
32
  constructor(
33
    @Inject('ICommandBus')
34
    private readonly commandBus: ICommandBus
35
  ) {}
36
37
  @Post()
38
  @UseInterceptors(FileInterceptor('file'))
39
  @ApiConsumes('multipart/form-data')
40
  @ApiOperation({title: 'Create new paystub'})
41
  @ApiImplicitFile({name: 'file', required: true})
42
  public async index(
43
    @UploadedFile() file: IUploadedFile,
44
    @Body() dto: PayStubDTO
45
  ) {
46
    if (false === PDFValidator.isValid(file)) {
47
      throw new BadRequestException('file.erros.invalid_pdf');
48
    }
49
50
    try {
51
      const fileId = await this.commandBus.execute(new UploadFileCommand(file));
52
      const id = await this.commandBus.execute(
53
        new CreatePayStubCommand(dto.date, dto.userId, fileId)
54
      );
55
56
      return {id};
57
    } catch (e) {
58
      throw new BadRequestException(e.message);
59
    }
60
  }
61
}
62