All files / src/domain/vaccine vaccine.ts

100% Statements 20/20
100% Branches 6/6
100% Functions 2/2
100% Lines 20/20

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                                                                        1186x 1186x 1186x 1186x 1186x 1186x 1186x 1186x 1186x 1186x 1186x 1186x       1189x   1189x 1x     1188x 1x     1187x 1x     1186x      
import { InvalidDoseNumberError } from './errors/invalid-dose-number.error';
import { InvalidRecommendedAgeError } from './errors/invalid-recommended-age.error';
import { InvalidVaccineNameError } from './errors/invalid-vaccine-name.error';
 
export interface VaccineProps {
  id: string;
  code: string;
  name: string;
  description: string;
  guidance: string | null;
  recommendedAgeInMonths: number;
  doseNumber: number;
  recommendationKind: VaccineRecommendationKind;
  scheduleVersion: string;
  effectiveFrom: Date;
  effectiveTo: Date | null;
  isActive: boolean;
}
 
export type VaccineRecommendationKind = 'ROUTINE' | 'CONDITIONAL' | 'RECURRING';
 
export class Vaccine {
  readonly id: string;
  readonly code: string;
  readonly name: string;
  readonly description: string;
  readonly guidance: string | null;
  readonly recommendedAgeInMonths: number;
  readonly doseNumber: number;
  readonly recommendationKind: VaccineRecommendationKind;
  readonly scheduleVersion: string;
  readonly effectiveFrom: Date;
  readonly effectiveTo: Date | null;
  readonly isActive: boolean;
 
  private constructor(props: VaccineProps) {
    this.id = props.id;
    this.code = props.code;
    this.name = props.name;
    this.description = props.description;
    this.guidance = props.guidance;
    this.recommendedAgeInMonths = props.recommendedAgeInMonths;
    this.doseNumber = props.doseNumber;
    this.recommendationKind = props.recommendationKind;
    this.scheduleVersion = props.scheduleVersion;
    this.effectiveFrom = props.effectiveFrom;
    this.effectiveTo = props.effectiveTo;
    this.isActive = props.isActive;
  }
 
  static create(props: VaccineProps): Vaccine {
    const name = props.name.trim();
 
    if (name.length === 0) {
      throw new InvalidVaccineNameError();
    }
 
    if (props.recommendedAgeInMonths < 0) {
      throw new InvalidRecommendedAgeError();
    }
 
    if (props.doseNumber < 1) {
      throw new InvalidDoseNumberError();
    }
 
    return new Vaccine({ ...props, name });
  }
}