Passed
Pull Request — master (#87)
by Mathieu
01:49
created

server/src/Infrastructure/Accounting/Action/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 21 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 {FileInterceptor} from '@nestjs/platform-express';
12
import {AuthGuard} from '@nestjs/passport';
13
import {
14
  ApiUseTags,
15
  ApiBearerAuth,
16
  ApiOperation,
17
  ApiImplicitFile,
18
  ApiConsumes
19
} from '@nestjs/swagger';
20
import {ICommandBus} from 'src/Application/ICommandBus';
21
import {Roles} from 'src/Infrastructure/User/Decorator/Roles';
22
import {RolesGuard} from 'src/Infrastructure/User/Security/RolesGuard';
23
import {UserRole} from 'src/Domain/User/User.entity';
24
import {PayStubDTO} from '../DTO/PayStubDTO';
25
import {IUploadedFile} from 'src/Domain/File/IUploadedFile';
26
import {PDFValidator} from 'src/Domain/File/Validator/PDFValidator';
27
import {UploadFileCommand} from 'src/Application/File/Command/UploadFileCommand';
28
29
@Controller('pay_stubs')
30
@ApiUseTags('Accounting')
31
@ApiBearerAuth()
32
@UseGuards(AuthGuard('bearer'), RolesGuard)
33
export class CreatePayStubAction {
34
  constructor(
35
    @Inject('ICommandBus')
36
    private readonly commandBus: ICommandBus
37
  ) {}
38
39
  @Post()
40
  @Roles(UserRole.COOPERATOR, UserRole.EMPLOYEE)
41
  @UseInterceptors(FileInterceptor('file'))
42
  @ApiConsumes('multipart/form-data')
43
  @ApiOperation({title: 'Create new paystub'})
44
  @ApiImplicitFile({name: 'file', required: true})
45
  public async index(
46
    @UploadedFile() file: IUploadedFile,
47
    @Body() dto: PayStubDTO
48
  ) {
49
    if (false === PDFValidator.isValid(file)) {
50
      throw new BadRequestException(
51
        `Unsupported mime type ${file.mimetype}, application/pdf required`
52
      );
53
    }
54
55
    try {
56
      const {id} = await this.commandBus.execute(new UploadFileCommand(file));
57
    } catch (e) {
58
      throw new BadRequestException(e.message);
59
    }
60
  }
61
}
62