Passed
Pull Request — master (#165)
by Mathieu
03:17 queued 01:29
created

server/src/Infrastructure/Accounting/Action/Invoice/GenerateInvoiceAction.ts   A

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 52
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A GenerateInvoiceAction.index 0 20 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 { 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