| Total Complexity | 3 |
| Total Lines | 29 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import { CommandHandler } from '@nestjs/cqrs'; |
||
| 8 | |||
| 9 | @CommandHandler(UpdateShippingCostCommand) |
||
| 10 | export class UpdateShippingCostCommandHandler { |
||
| 11 | constructor( |
||
| 12 | @Inject('IShippingCostRepository') |
||
| 13 | private readonly shippingcostRepository: IShippingCostRepository, |
||
| 14 | private readonly isShippingCostAlreadyExist: IsShippingCostAlreadyExist |
||
| 15 | ) {} |
||
| 16 | |||
| 17 | public async execute(command: UpdateShippingCostCommand): Promise<string> { |
||
| 18 | const { id, grams, price } = command; |
||
| 19 | |||
| 20 | const shippingCost = await this.shippingcostRepository.findOneById(id); |
||
| 21 | if (!shippingCost) { |
||
| 22 | throw new ShippingCostNotFoundException(); |
||
| 23 | } |
||
| 24 | |||
| 25 | if ( |
||
| 26 | grams !== shippingCost.getGrams() && |
||
| 27 | true === (await this.isShippingCostAlreadyExist.isSatisfiedBy(grams)) |
||
| 28 | ) { |
||
| 29 | throw new ShippingCostAlreadyExistException(); |
||
| 30 | } |
||
| 31 | |||
| 32 | shippingCost.update(grams, Math.round(price * 100)); |
||
| 33 | |||
| 34 | await this.shippingcostRepository.save(shippingCost); |
||
| 35 | |||
| 36 | return shippingCost.getId(); |
||
| 37 | } |
||
| 39 |