All files / src/application/medication delete-medication.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                            22x 22x 22x       2x   2x   2x 1x     1x      
import { BabyRepository } from '../baby/baby-repository';
import { BabyGuardianRepository } from '../baby/baby-guardian-repository';
import { ensureBabyAccess } from '../baby/ensure-baby-access';
import { MedicationRepository } from './medication-repository';
import { MedicationNotFoundError } from './errors/medication-not-found.error';
 
export interface DeleteMedicationInput {
  babyId: string;
  medicationId: string;
  requestingUserId: string;
}
 
export class DeleteMedicationUseCase {
  constructor(
    private readonly babyRepository: BabyRepository,
    private readonly babyGuardianRepository: BabyGuardianRepository,
    private readonly medicationRepository: MedicationRepository,
  ) {}
 
  async execute(input: DeleteMedicationInput): Promise<void> {
    await ensureBabyAccess(this.babyRepository, this.babyGuardianRepository, input.babyId, input.requestingUserId);
 
    const existing = await this.medicationRepository.findById(input.medicationId);
 
    if (!existing || existing.babyId !== input.babyId) {
      throw new MedicationNotFoundError();
    }
 
    await this.medicationRepository.delete(input.medicationId);
  }
}