All files / src/application/milestone delete-milestone.use-case.ts

100% Statements 8/8
100% Branches 4/4
100% Functions 2/2
100% Lines 8/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 28 29 30 31 32                            26x 26x 26x       7x   5x   5x 3x     2x      
import { BabyRepository } from '../baby/baby-repository';
import { BabyGuardianRepository } from '../baby/baby-guardian-repository';
import { ensureBabyAccess } from '../baby/ensure-baby-access';
import { MilestoneRepository } from './milestone-repository';
import { MilestoneNotFoundError } from './errors/milestone-not-found.error';
 
export interface DeleteMilestoneInput {
  babyId: string;
  milestoneId: string;
  requestingUserId: string;
}
 
export class DeleteMilestoneUseCase {
  constructor(
    private readonly babyRepository: BabyRepository,
    private readonly babyGuardianRepository: BabyGuardianRepository,
    private readonly milestoneRepository: MilestoneRepository,
  ) {}
 
  async execute(input: DeleteMilestoneInput): Promise<void> {
    await ensureBabyAccess(this.babyRepository, this.babyGuardianRepository, input.babyId, input.requestingUserId);
 
    const existingMilestone = await this.milestoneRepository.findById(input.milestoneId);
 
    if (!existingMilestone || existingMilestone.babyId !== input.babyId) {
      throw new MilestoneNotFoundError();
    }
 
    await this.milestoneRepository.delete(input.milestoneId);
  }
}