Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 53 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |