Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 31 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { Inject } from '@nestjs/common'; |
||
2 | import { CommandHandler } from '@nestjs/cqrs'; |
||
3 | import { ShippingCostAlreadyExistException } from 'src/Domain/Order/Exception/ShippingCostAlreadyExistException'; |
||
4 | import { IShippingCostRepository } from 'src/Domain/Order/Repository/IShippingCostRepository'; |
||
5 | import { ShippingCost } from 'src/Domain/Order/ShippingCost.entity'; |
||
6 | import { IsShippingCostAlreadyExist } from 'src/Domain/Order/Specification/IsShippingCostAlreadyExist'; |
||
7 | import { CreateShippingCostCommand } from './CreateShippingCostCommand'; |
||
8 | |||
9 | @CommandHandler(CreateShippingCostCommand) |
||
10 | export class CreateShippingCostCommandHandler { |
||
11 | constructor( |
||
12 | @Inject('IShippingCostRepository') |
||
13 | private readonly shippingcostRepository: IShippingCostRepository, |
||
14 | private readonly isShippingCostAlreadyExist: IsShippingCostAlreadyExist |
||
15 | ) {} |
||
16 | |||
17 | public async execute(command: CreateShippingCostCommand): Promise<string> { |
||
18 | const { grams, price } = command; |
||
19 | |||
20 | if (true === (await this.isShippingCostAlreadyExist.isSatisfiedBy(grams))) { |
||
21 | throw new ShippingCostAlreadyExistException(); |
||
22 | } |
||
23 | |||
24 | const shippingCost = await this.shippingcostRepository.save( |
||
25 | new ShippingCost(grams, Math.round(price * 100)) |
||
26 | ); |
||
27 | |||
28 | return shippingCost.getId(); |
||
29 | } |
||
30 | } |
||
31 |