Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 48 |
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 {ApiUseTags, ApiBearerAuth, ApiOperation} from '@nestjs/swagger'; |
||
11 | import {ICommandBus} from 'src/Application/ICommandBus'; |
||
12 | import {LoggedUser} from 'src/Infrastructure/User/Decorator/LoggedUser'; |
||
13 | import {User} from 'src/Domain/User/User.entity'; |
||
14 | import {CreateQuoteCommand} from 'src/Application/Billing/Command/CreateQuoteCommand'; |
||
15 | import {CreateQuoteDTO} from './DTO/CreateQuoteDTO'; |
||
16 | import {CreateQuoteItemsCommand} from 'src/Application/Billing/Command/CreateQuoteItemsCommand'; |
||
17 | |||
18 | @Controller('quotes') |
||
19 | @ApiUseTags('Billing') |
||
20 | @ApiBearerAuth() |
||
21 | @UseGuards(AuthGuard('bearer')) |
||
22 | export class CreateQuoteAction { |
||
23 | constructor( |
||
24 | @Inject('ICommandBus') |
||
25 | private readonly commandBus: ICommandBus |
||
26 | ) {} |
||
27 | |||
28 | @Post() |
||
29 | @ApiOperation({title: 'Create new quote'}) |
||
30 | public async index( |
||
31 | @Body() quoteDTO: CreateQuoteDTO, |
||
32 | @LoggedUser() user: User |
||
33 | ) { |
||
34 | try { |
||
35 | const {projectId, customerId, status, items} = quoteDTO; |
||
36 | const id = await this.commandBus.execute( |
||
37 | new CreateQuoteCommand(user, status, customerId, projectId) |
||
38 | ); |
||
39 | |||
40 | await this.commandBus.execute(new CreateQuoteItemsCommand(id, items)); |
||
41 | |||
42 | return {id}; |
||
43 | } catch (e) { |
||
44 | throw new BadRequestException(e.message); |
||
45 | } |
||
46 | } |
||
47 | } |
||
48 |