Total Complexity | 3 |
Complexity/F | 3 |
Lines of Code | 39 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { CommandHandler } from '@nestjs/cqrs'; |
||
2 | import { Inject } from '@nestjs/common'; |
||
3 | import { UpdateShippingCostCommand } from './UpdateShippingCostCommand'; |
||
4 | import { IShippingCostRepository } from 'src/Domain/Order/Repository/IShippingCostRepository'; |
||
5 | import { ShippingCostNotFoundException } from 'src/Domain/Order/Exception/ShippingCostNotFoundException'; |
||
6 | import { IsShippingCostAlreadyExist } from 'src/Domain/Order/Specification/IsShippingCostAlreadyExist'; |
||
7 | import { ShippingCostAlreadyExistException } from 'src/Domain/Order/Exception/ShippingCostAlreadyExistException'; |
||
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 | } |
||
38 | } |
||
39 |