All files / main/services taskDeletionService.ts

88.88% Statements 8/9
50% Branches 1/2
100% Functions 4/4
87.5% Lines 7/8

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                      4x     4x 5x 4x       4x 4x 4x        
import type { TaskRepository, TimerSessionRepository, TransactionRunner } from '@main/ports/repositories'
import type { TimerService } from './timerService'
 
export interface TaskDeletionServiceDependencies {
  tasks: Pick<TaskRepository, 'list' | 'delete'>
  sessions: Pick<TimerSessionRepository, 'deleteHistoricalFocusByTaskId'>
  timer: Pick<TimerService, 'clearDeletedTaskBinding'>
  transactions: TransactionRunner
}
 
export class TaskDeletionService {
  constructor(private readonly dependencies: TaskDeletionServiceDependencies) {}
 
  async delete(taskId: string): Promise<void> {
    await this.dependencies.transactions.transaction(async () => {
      const taskExists = (await this.dependencies.tasks.list()).some((task) => task.id === taskId)
      Iif (!taskExists) {
        throw new Error(`Task not found: ${taskId}`)
      }
 
      await this.dependencies.timer.clearDeletedTaskBinding(taskId)
      await this.dependencies.sessions.deleteHistoricalFocusByTaskId(taskId)
      await this.dependencies.tasks.delete(taskId)
    })
  }
}