Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 52 |
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 | } from '@nestjs/common'; |
||
9 | import { AuthGuard } from '@nestjs/passport'; |
||
10 | import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger'; |
||
11 | import { ICommandBus } from 'src/Application/ICommandBus'; |
||
12 | import { LoggedUser } from 'src/Infrastructure/HumanResource/User/Decorator/LoggedUser'; |
||
13 | import { User, UserRole } from 'src/Domain/HumanResource/User/User.entity'; |
||
14 | import { InvoiceDTO } from '../../DTO/InvoiceDTO'; |
||
15 | import { Roles } from 'src/Infrastructure/HumanResource/User/Decorator/Roles'; |
||
16 | import { RolesGuard } from 'src/Infrastructure/HumanResource/User/Security/RolesGuard'; |
||
17 | import { GenerateInvoiceCommand } from 'src/Application/Accounting/Command/Invoice/GenerateInvoiceCommand'; |
||
18 | import { InvoiceStatus } from 'src/Domain/Accounting/Invoice.entity'; |
||
19 | |||
20 | @Controller('invoices') |
||
21 | @ApiTags('Accounting') |
||
22 | @ApiBearerAuth() |
||
23 | @UseGuards(AuthGuard('bearer'), RolesGuard) |
||
24 | export class GenerateInvoiceAction { |
||
25 | constructor( |
||
26 | @Inject('ICommandBus') |
||
27 | private readonly commandBus: ICommandBus |
||
28 | ) {} |
||
29 | |||
30 | @Post() |
||
31 | @Roles(UserRole.COOPERATOR, UserRole.EMPLOYEE) |
||
32 | @ApiOperation({summary: 'Generate new invoice'}) |
||
33 | public async index(@Body() dto: InvoiceDTO, @LoggedUser() user: User) { |
||
34 | try { |
||
35 | const { projectId, date, expireInDays} = dto; |
||
36 | const id = await this.commandBus.execute( |
||
37 | new GenerateInvoiceCommand( |
||
38 | projectId, |
||
39 | InvoiceStatus.DRAFT, |
||
40 | expireInDays, |
||
41 | new Date(date), |
||
42 | user |
||
43 | ) |
||
44 | ); |
||
45 | |||
46 | return { id }; |
||
47 | } catch (e) { |
||
48 | throw new BadRequestException(e.message); |
||
49 | } |
||
50 | } |
||
51 | } |
||
52 |