All files / src/domain/vaccine baby-vaccine-record.ts

100% Statements 22/22
96.42% Branches 27/28
100% Functions 5/5
100% Lines 22/22

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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175                                                                                                                                                                                365x 365x 365x 365x 365x 365x 365x 365x 365x 365x 365x 365x 365x         283x   283x   283x                                   13x                                       17x   17x 1x     16x                                   53x      
import { startOfUtcDay } from '../../shared/utils/date';
import { InvalidCustomVaccineNameError } from './errors/invalid-custom-vaccine-name.error';
 
export type VaccineRecordStatus = 'PENDING' | 'APPLIED' | 'DELAYED';
export type VaccineRecordSource = 'CATALOG' | 'CAMPAIGN' | 'CUSTOM';
 
export interface BabyVaccineRecordProps {
  id: string;
  babyId: string;
  vaccineId: string | null;
  source: VaccineRecordSource;
  customName: string | null;
  customDose: string | null;
  status: VaccineRecordStatus;
  applicationDate: Date | null;
  notes: string | null;
  batchNumber: string | null;
  location: string | null;
  professional: string | null;
  photoUrl: string | null;
}
 
export interface DeriveBabyVaccineRecordProps {
  id: string;
  babyId: string;
  vaccineId: string;
  dueDate: Date;
  referenceDate?: Date;
}
 
export interface RestoreBabyVaccineRecordProps {
  id: string;
  babyId: string;
  vaccineId: string | null;
  source: VaccineRecordSource;
  customName: string | null;
  customDose: string | null;
  status: VaccineRecordStatus;
  applicationDate: Date | null;
  notes: string | null;
  batchNumber: string | null;
  location: string | null;
  professional: string | null;
  photoUrl: string | null;
}
 
export interface MarkAppliedProps {
  id: string;
  babyId: string;
  vaccineId: string;
  applicationDate: Date;
  notes?: string | null;
  batchNumber?: string | null;
  location?: string | null;
  professional?: string | null;
  photoUrl?: string | null;
}
 
export interface RegisterAdhocProps {
  id: string;
  babyId: string;
  source: Exclude<VaccineRecordSource, 'CATALOG'>;
  customName: string;
  customDose?: string | null;
  applicationDate: Date;
  notes?: string | null;
  batchNumber?: string | null;
  location?: string | null;
  professional?: string | null;
  photoUrl?: string | null;
}
 
export class BabyVaccineRecord {
  readonly id: string;
  readonly babyId: string;
  readonly vaccineId: string | null;
  readonly source: VaccineRecordSource;
  readonly customName: string | null;
  readonly customDose: string | null;
  readonly status: VaccineRecordStatus;
  readonly applicationDate: Date | null;
  readonly notes: string | null;
  readonly batchNumber: string | null;
  readonly location: string | null;
  readonly professional: string | null;
  readonly photoUrl: string | null;
 
  private constructor(props: BabyVaccineRecordProps) {
    this.id = props.id;
    this.babyId = props.babyId;
    this.vaccineId = props.vaccineId;
    this.source = props.source;
    this.customName = props.customName;
    this.customDose = props.customDose;
    this.status = props.status;
    this.applicationDate = props.applicationDate;
    this.notes = props.notes;
    this.batchNumber = props.batchNumber;
    this.location = props.location;
    this.professional = props.professional;
    this.photoUrl = props.photoUrl;
  }
 
  // Compares calendar days, not timestamps, so a vaccine due today is PENDING, not DELAYED.
  static derive(props: DeriveBabyVaccineRecordProps): BabyVaccineRecord {
    const referenceDate = props.referenceDate ?? new Date();
    const status: VaccineRecordStatus =
      startOfUtcDay(props.dueDate).getTime() < startOfUtcDay(referenceDate).getTime() ? 'DELAYED' : 'PENDING';
 
    return new BabyVaccineRecord({
      id: props.id,
      babyId: props.babyId,
      vaccineId: props.vaccineId,
      source: 'CATALOG',
      customName: null,
      customDose: null,
      status,
      applicationDate: null,
      notes: null,
      batchNumber: null,
      location: null,
      professional: null,
      photoUrl: null,
    });
  }
 
  static markApplied(props: MarkAppliedProps): BabyVaccineRecord {
    return new BabyVaccineRecord({
      id: props.id,
      babyId: props.babyId,
      vaccineId: props.vaccineId,
      source: 'CATALOG',
      customName: null,
      customDose: null,
      status: 'APPLIED',
      applicationDate: props.applicationDate,
      notes: props.notes ?? null,
      batchNumber: props.batchNumber ?? null,
      location: props.location ?? null,
      professional: props.professional ?? null,
      photoUrl: props.photoUrl ?? null,
    });
  }
 
  // A campaign (e.g. seasonal flu drive) or fully custom vaccine the parent logs by hand — always
  // already applied, and deliberately not tied to a catalog Vaccine row (vaccineId stays null).
  static registerAdhoc(props: RegisterAdhocProps): BabyVaccineRecord {
    const customName = props.customName.trim();
 
    if (customName.length === 0) {
      throw new InvalidCustomVaccineNameError();
    }
 
    return new BabyVaccineRecord({
      id: props.id,
      babyId: props.babyId,
      vaccineId: null,
      source: props.source,
      customName,
      customDose: props.customDose ?? null,
      status: 'APPLIED',
      applicationDate: props.applicationDate,
      notes: props.notes ?? null,
      batchNumber: props.batchNumber ?? null,
      location: props.location ?? null,
      professional: props.professional ?? null,
      photoUrl: props.photoUrl ?? null,
    });
  }
 
  static restore(props: RestoreBabyVaccineRecordProps): BabyVaccineRecord {
    return new BabyVaccineRecord(props);
  }
}