Passed
Push — master ( 1029c0...f58cd4 )
by Mathieu
01:51 queued 22s
created

CreateDiscountAction.index   A

Complexity

Conditions 2

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 21
rs 9.45
c 0
b 0
f 0
cc 2
1
import {
2
  Controller,
3
  Inject,
4
  Post,
5
  Body,
6
  BadRequestException,
7
  UseGuards,
8
  Param
9
} from '@nestjs/common';
10
import { AuthGuard } from '@nestjs/passport';
11
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
12
import { ICommandBus } from 'src/Application/ICommandBus';
13
import { CreateDiscountCommand } from 'src/Application/School/Command/Discount/CreateDiscountCommand';
14
import { UserRole } from 'src/Domain/User/User.entity';
15
import { IdDTO } from 'src/Infrastructure/Common/DTO/IdDTO';
16
import { Roles } from 'src/Infrastructure/User/Decorator/Roles';
17
import { RolesGuard } from 'src/Infrastructure/User/Security/RolesGuard';
18
import { DiscountDTO } from '../../DTO/DiscountDTO';
19
20
@Controller('schools')
21
@ApiTags('School discount')
22
@ApiBearerAuth()
23
@UseGuards(AuthGuard('bearer'), RolesGuard)
24
export class CreateDiscountAction {
25
  constructor(
26
    @Inject('ICommandBus')
27
    private readonly commandBus: ICommandBus
28
  ) {}
29
30
  @Post(':id/discounts')
31
  @Roles(UserRole.PHOTOGRAPHER)
32
  @ApiOperation({ summary: 'Create a discount' })
33
  public async index(
34
    @Param() idDto: IdDTO,
35
    @Body() { amount, value, type }: DiscountDTO
36
  ) {
37
    try {
38
      const id = await this.commandBus.execute(
39
        new CreateDiscountCommand(
40
          type,
41
          amount,
42
          value,
43
          idDto.id
44
        )
45
      );
46
47
      return { id };
48
    } catch (e) {
49
      throw new BadRequestException(e.message);
50
    }
51
  }
52
}
53