All files / src/application/medication create-medication.use-case.ts

100% Statements 7/7
100% Branches 0/0
100% Functions 2/2
100% Lines 7/7

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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                                            22x 22x 22x       9x   8x                         8x   7x      
import { randomUUID } from 'node:crypto';
import { BabyRepository } from '../baby/baby-repository';
import { BabyGuardianRepository } from '../baby/baby-guardian-repository';
import { ensureBabyAccess } from '../baby/ensure-baby-access';
import { Medication } from '../../domain/medication/medication';
import { MedicationRepository } from './medication-repository';
 
export interface CreateMedicationInput {
  babyId: string;
  requestingUserId: string;
  name: string;
  startedOn: Date;
  dosage?: string | null;
  frequency?: string | null;
  reason?: string | null;
  prescriberName?: string | null;
  endedOn?: Date | null;
  notes?: string | null;
}
 
export class CreateMedicationUseCase {
  constructor(
    private readonly babyRepository: BabyRepository,
    private readonly babyGuardianRepository: BabyGuardianRepository,
    private readonly medicationRepository: MedicationRepository,
  ) {}
 
  async execute(input: CreateMedicationInput): Promise<Medication> {
    await ensureBabyAccess(this.babyRepository, this.babyGuardianRepository, input.babyId, input.requestingUserId);
 
    const medication = Medication.record({
      id: randomUUID(),
      babyId: input.babyId,
      name: input.name,
      startedOn: input.startedOn,
      dosage: input.dosage,
      frequency: input.frequency,
      reason: input.reason,
      prescriberName: input.prescriberName,
      endedOn: input.endedOn,
      notes: input.notes,
    });
 
    await this.medicationRepository.save(medication);
 
    return medication;
  }
}