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

100% Statements 10/10
80% Branches 16/20
100% Functions 2/2
100% Lines 10/10

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 50 51 52 53 54 55 56 57 58 59 60 61 62                                              22x 22x 22x       4x   4x       4x 1x     3x                                 4x   2x      
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';
import { MedicationNotFoundError } from './errors/medication-not-found.error';
 
export interface UpdateMedicationInput {
  babyId: string;
  medicationId: 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 UpdateMedicationUseCase {
  constructor(
    private readonly babyRepository: BabyRepository,
    private readonly babyGuardianRepository: BabyGuardianRepository,
    private readonly medicationRepository: MedicationRepository,
  ) {}
 
  async execute(input: UpdateMedicationInput): Promise<Medication> {
    await ensureBabyAccess(this.babyRepository, this.babyGuardianRepository, input.babyId, input.requestingUserId);
 
    const existing = await this.medicationRepository.findById(input.medicationId);
 
    // Not redundant with the access check above: knowing a medication id must not be enough to
    // reach that row through another baby the caller does happen to have access to.
    if (!existing || existing.babyId !== input.babyId) {
      throw new MedicationNotFoundError();
    }
 
    const updated = Medication.record({
      id: existing.id,
      babyId: existing.babyId,
      name: input.name ?? existing.name,
      startedOn: input.startedOn ?? existing.startedOn,
      dosage: input.dosage !== undefined ? input.dosage : existing.dosage,
      frequency: input.frequency !== undefined ? input.frequency : existing.frequency,
      reason: input.reason !== undefined ? input.reason : existing.reason,
      prescriberName: input.prescriberName !== undefined ? input.prescriberName : existing.prescriberName,
      // Ending a course is the most common edit this route will ever get, and it is a `PATCH` with
      // `endedOn` set. `null` (still going) and absent (leave it alone) stay distinct so that
      // correcting the dose does not silently reopen a course that had ended.
      endedOn: input.endedOn !== undefined ? input.endedOn : existing.endedOn,
      notes: input.notes !== undefined ? input.notes : existing.notes,
      createdAt: existing.createdAt,
    });
 
    await this.medicationRepository.save(updated);
 
    return updated;
  }
}