| Total Complexity | 3 |
| Complexity/F | 3 |
| Lines of Code | 62 |
| Function Count | 1 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 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 |