| Total Complexity | 3 | 
| Complexity/F | 3 | 
| Lines of Code | 36 | 
| Function Count | 1 | 
| Duplicated Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
| 1 | import {CommandHandler} from '@nestjs/cqrs'; | ||
| 2 | import {Inject} from '@nestjs/common'; | ||
| 3 | import {UpdateTaskCommand} from './UpdateTaskCommand'; | ||
| 4 | import {ITaskRepository} from 'src/Domain/Task/Repository/ITaskRepository'; | ||
| 5 | import {TaskNotFoundException} from 'src/Domain/Task/Exception/TaskNotFoundException'; | ||
| 6 | import {IsTaskAlreadyExist} from 'src/Domain/Task/Specification/IsTaskAlreadyExist'; | ||
| 7 | import {TaskAlreadyExistException} from 'src/Domain/Task/Exception/TaskAlreadyExistException'; | ||
| 8 | |||
| 9 | @CommandHandler(UpdateTaskCommand) | ||
| 10 | export class UpdateTaskCommandHandler { | ||
| 11 | constructor( | ||
| 12 |     @Inject('ITaskRepository') | ||
| 13 | private readonly taskRepository: ITaskRepository, | ||
| 14 | private readonly isTaskAlreadyExist: IsTaskAlreadyExist | ||
| 15 |   ) {} | ||
| 16 | |||
| 17 |   public async execute(command: UpdateTaskCommand): Promise<void> { | ||
| 18 |     const {id, name} = command; | ||
| 19 | |||
| 20 | const task = await this.taskRepository.findOneById(id); | ||
| 21 |     if (!task) { | ||
| 22 | throw new TaskNotFoundException(); | ||
| 23 | } | ||
| 24 | |||
| 25 | if ( | ||
| 26 | name !== task.getName() && | ||
| 27 | true === (await this.isTaskAlreadyExist.isSatisfiedBy(name)) | ||
| 28 |     ) { | ||
| 29 | throw new TaskAlreadyExistException(); | ||
| 30 | } | ||
| 31 | |||
| 32 | task.updateName(name); | ||
| 33 | await this.taskRepository.save(task); | ||
| 34 | } | ||
| 35 | } | ||
| 36 |