All files / src/application/vaccine get-baby-vaccine-schedule.use-case.ts

100% Statements 27/27
96.29% Branches 26/27
100% Functions 5/5
100% Lines 27/27

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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133                                                                                                                30x 30x 30x 30x       19x             15x         15x 15x 46x 46x     15x   19x 316x 316x 316x   316x                 316x   316x                                   19x   19x 316x 316x 316x     15x 111x 126x   15x      
import { BabyRepository } from '../baby/baby-repository';
import { BabyGuardianRepository } from '../baby/baby-guardian-repository';
import { ensureBabyAccess } from '../baby/ensure-baby-access';
import { BabyVaccineRecord, VaccineRecordStatus } from '../../domain/vaccine/baby-vaccine-record';
import { VaccineRecommendationKind } from '../../domain/vaccine/vaccine';
import { ACTIVE_VACCINE_CATALOG } from '../../shared/config/vaccine-catalog';
import { addMonthsClamped } from '../../shared/utils/date';
import { BabyVaccineRecordRepository } from './baby-vaccine-record-repository';
import { VaccineRepository } from './vaccine-repository';
 
export interface VaccineScheduleItem {
  vaccineId: string;
  name: string;
  description: string;
  guidance: string | null;
  doseNumber: number;
  recommendedAgeInMonths: number;
  recommendationKind: VaccineRecommendationKind;
  status: VaccineScheduleStatus;
  applicationDate: Date | null;
  notes: string | null;
  batchNumber: string | null;
  location: string | null;
  professional: string | null;
  photoUrl: string | null;
}
 
export type VaccineScheduleStatus = VaccineRecordStatus | 'GUIDANCE';
 
export interface AgeGroupSchedule {
  ageInMonths: number;
  items: VaccineScheduleItem[];
}
 
export interface VaccineSchedule {
  metadata: {
    version: string;
    sourceName: string;
    sourceOrganization: string;
    sourceUrl: string;
    sourceUpdatedAt: string;
    effectiveFrom: string;
    minimumAgeInMonths: number;
    maximumAgeInMonths: number;
  };
  groups: AgeGroupSchedule[];
}
 
export interface GetBabyVaccineScheduleInput {
  babyId: string;
  requestingUserId: string;
  referenceDate?: Date;
}
 
export class GetBabyVaccineScheduleUseCase {
  constructor(
    private readonly babyRepository: BabyRepository,
    private readonly babyGuardianRepository: BabyGuardianRepository,
    private readonly vaccineRepository: VaccineRepository,
    private readonly babyVaccineRecordRepository: BabyVaccineRecordRepository,
  ) {}
 
  async execute(input: GetBabyVaccineScheduleInput): Promise<VaccineSchedule> {
    const baby = await ensureBabyAccess(
      this.babyRepository,
      this.babyGuardianRepository,
      input.babyId,
      input.requestingUserId,
    );
 
    const [vaccines, appliedRecords] = await Promise.all([
      this.vaccineRepository.findAll(),
      this.babyVaccineRecordRepository.findAllByBabyId(input.babyId),
    ]);
 
    const appliedByVaccineId = new Map<string, BabyVaccineRecord>();
    for (const record of appliedRecords) {
      Eif (record.vaccineId) {
        appliedByVaccineId.set(record.vaccineId, record);
      }
    }
    const referenceDate = input.referenceDate ?? new Date();
 
    const items: VaccineScheduleItem[] = vaccines.map((vaccine) => {
      const dueDate = addMonthsClamped(baby.birthDate, vaccine.recommendedAgeInMonths);
      const appliedRecord = appliedByVaccineId.get(vaccine.id);
      const isCoveredByCurrentVersion = dueDate.getTime() >= vaccine.effectiveFrom.getTime();
      const derivedRecord: BabyVaccineRecord | null =
        !appliedRecord && vaccine.recommendationKind === 'ROUTINE' && isCoveredByCurrentVersion
          ? BabyVaccineRecord.derive({
              id: `${input.babyId}:${vaccine.id}`,
              babyId: input.babyId,
              vaccineId: vaccine.id,
              dueDate,
              referenceDate,
            })
          : null;
      const record = appliedRecord ?? derivedRecord;
 
      return {
        vaccineId: vaccine.id,
        name: vaccine.name,
        description: vaccine.description,
        guidance: vaccine.guidance,
        doseNumber: vaccine.doseNumber,
        recommendedAgeInMonths: vaccine.recommendedAgeInMonths,
        recommendationKind: vaccine.recommendationKind,
        status: record?.status ?? 'GUIDANCE',
        applicationDate: record?.applicationDate ?? null,
        notes: record?.notes ?? null,
        batchNumber: record?.batchNumber ?? null,
        location: record?.location ?? null,
        professional: record?.professional ?? null,
        photoUrl: record?.photoUrl ?? null,
      };
    });
 
    const groupedByAge = new Map<number, VaccineScheduleItem[]>();
 
    for (const item of items) {
      const group = groupedByAge.get(item.recommendedAgeInMonths) ?? [];
      group.push(item);
      groupedByAge.set(item.recommendedAgeInMonths, group);
    }
 
    const groups = [...groupedByAge.entries()]
      .sort(([a], [b]) => a - b)
      .map(([ageInMonths, groupItems]) => ({ ageInMonths, items: groupItems }));
 
    return { metadata: { ...ACTIVE_VACCINE_CATALOG }, groups };
  }
}