| Total Complexity | 2 |
| Complexity/F | 2 |
| Lines of Code | 48 |
| Function Count | 1 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 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 {CreateCustomerCommand} from 'src/Application/Customer/Command/CreateCustomerCommand'; |
||
| 12 | import {ICommandBus} from 'src/Application/ICommandBus'; |
||
| 13 | import {CustomerDTO} from '../DTO/CustomerDTO'; |
||
| 14 | import {RolesGuard} from 'src/Infrastructure/HumanResource/User/Security/RolesGuard'; |
||
| 15 | import {UserRole} from 'src/Domain/HumanResource/User/User.entity'; |
||
| 16 | import {Roles} from 'src/Infrastructure/HumanResource/User/Decorator/Roles'; |
||
| 17 | |||
| 18 | @Controller('customers') |
||
| 19 | @ApiTags('Customer') |
||
| 20 | @ApiBearerAuth() |
||
| 21 | @UseGuards(AuthGuard('bearer'), RolesGuard) |
||
| 22 | export class CreateCustomerAction { |
||
| 23 | constructor( |
||
| 24 | @Inject('ICommandBus') |
||
| 25 | private readonly commandBus: ICommandBus |
||
| 26 | ) {} |
||
| 27 | |||
| 28 | @Post() |
||
| 29 | @Roles(UserRole.COOPERATOR, UserRole.EMPLOYEE) |
||
| 30 | @ApiOperation({summary: 'Create new customer'}) |
||
| 31 | public async index(@Body() customerDto: CustomerDTO) { |
||
| 32 | const { |
||
| 33 | address: {street, city, zipCode, country}, |
||
| 34 | name |
||
| 35 | } = customerDto; |
||
| 36 | |||
| 37 | try { |
||
| 38 | const id = await this.commandBus.execute( |
||
| 39 | new CreateCustomerCommand(name, street, city, zipCode, country) |
||
| 40 | ); |
||
| 41 | |||
| 42 | return {id}; |
||
| 43 | } catch (e) { |
||
| 44 | throw new BadRequestException(e.message); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | } |
||
| 48 |