Passed
Push — master ( 829953...28db6b )
by Mathieu
01:39
created

CreateShippingCostAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 24
dl 0
loc 25
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A index 0 15 2
1
import {
2
  Controller,
3
  Inject,
4
  Post,
5
  Body,
6
  BadRequestException,
7
  UseGuards
8
} from '@nestjs/common';
9
import { AuthGuard } from '@nestjs/passport';
10
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
11
import { ICommandBus } from 'src/Application/ICommandBus';
12
import { CreateShippingCostCommand } from 'src/Application/Order/Command/ShippingCost/CreateShippingCostCommand';
13
import { UserRole } from 'src/Domain/User/User.entity';
14
import { Roles } from 'src/Infrastructure/User/Decorator/Roles';
15
import { RolesGuard } from 'src/Infrastructure/User/Security/RolesGuard';
16
import { ShippingCostDTO } from '../../DTO/ShippingCostDTO';
17
18
@Controller('shipping-costs')
19
@ApiTags('Order')
20
@ApiBearerAuth()
21
@UseGuards(AuthGuard('bearer'), RolesGuard)
22
export class CreateShippingCostAction {
23
  constructor(
24
    @Inject('ICommandBus')
25
    private readonly commandBus: ICommandBus
26
  ) {}
27
28
  @Post()
29
  @Roles(UserRole.PHOTOGRAPHER)
30
  @ApiOperation({ summary: 'Create new shipping cost' })
31
  public async index(@Body() dto: ShippingCostDTO) {
32
    const { grams, price } = dto;
33
34
    try {
35
      const id = await this.commandBus.execute(
36
        new CreateShippingCostCommand(grams, price)
37
      );
38
39
      return { id };
40
    } catch (e) {
41
      throw new BadRequestException(e.message);
42
    }
43
  }
44
}
45