Passed
Pull Request — master (#70)
by Mathieu
01:22
created

server/src/Infrastructure/Billing/Action/CreateQuoteAction.ts   A

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 51
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 46
mnd 1
bc 1
fnc 1
dl 0
loc 51
bpm 1
cpm 2
noi 0
c 0
b 0
f 0
rs 10

1 Function

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