All files / application/useCases ReopenTaskUseCase.ts

100% Statements 12/12
100% Branches 2/2
100% Functions 2/2
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32              8x 8x       8x 8x   8x   8x 1x     7x   7x   4x 4x 8x        
import { TaskId } from '../../domain/valueObjects/TaskId';
import { UserId } from '../../domain/valueObjects/UserId';
import { ITaskRepository } from '../../domain/repositories/ITaskRepository';
import { DomainEventPublisher } from '../services/DomainEventPublisher';
 
export class ReopenTaskUseCase {
  constructor(
    private taskRepository: ITaskRepository,
    private eventPublisher: DomainEventPublisher
  ) {}
 
  async execute(taskId: string, userId: string): Promise<void> {
    const id = TaskId.fromString(taskId);
    const user = UserId.create(userId);
 
    const task = await this.taskRepository.findById(id);
 
    if (!task) {
      throw new Error('Task not found');
    }
 
    task.reopen(user);
 
    await this.taskRepository.save(task);
 
    const events = task.pullDomainEvents();
    for (const event of events) {
      await this.eventPublisher.publish(event);
    }
  }
}