All files / src/domain/baby baby.ts

100% Statements 21/21
100% Branches 24/24
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 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                                                                                                              498x 498x 498x 498x 498x 498x 498x 498x 498x 498x 498x 498x 498x       503x 502x   502x 1x     501x 3x     498x                                  
import { FutureBirthDateError } from './errors/future-birth-date.error';
import { InvalidBabyNameError } from './errors/invalid-baby-name.error';
import { BabyMeasurement, validateBabyMeasurement } from './baby-measurement';
 
/** Sexo ao nascer — variável clínica, não identidade de gênero. Opcional: `null` é "não informado". */
export type BabySexAtBirth = 'MALE' | 'FEMALE';
 
export interface BabyProps {
  measurements: BabyMeasurement[];
  id: string;
  userId: string;
  name: string;
  birthDate: Date;
  sexAtBirth: BabySexAtBirth | null;
  bloodType: string | null;
  allergies: string[];
  healthPlanName: string | null;
  healthPlanNumber: string | null;
  avatarUrl: string | null;
  avatarColor: string | null;
  createdAt: Date;
}
 
export interface CreateBabyProps {
  measurements?: BabyMeasurement[];
  id: string;
  userId: string;
  name: string;
  birthDate: Date;
  sexAtBirth?: BabySexAtBirth | null;
  bloodType?: string | null;
  allergies?: string[];
  healthPlanName?: string | null;
  healthPlanNumber?: string | null;
  avatarUrl?: string | null;
  avatarColor?: string | null;
  createdAt?: Date;
}
 
export class Baby {
  readonly measurements: BabyMeasurement[];
  readonly id: string;
  readonly userId: string;
  readonly name: string;
  readonly birthDate: Date;
  readonly sexAtBirth: BabySexAtBirth | null;
  readonly bloodType: string | null;
  readonly allergies: string[];
  readonly healthPlanName: string | null;
  readonly healthPlanNumber: string | null;
  readonly avatarUrl: string | null;
  readonly avatarColor: string | null;
  readonly createdAt: Date;
 
  private constructor(props: BabyProps) {
    this.measurements = props.measurements;
    this.id = props.id;
    this.userId = props.userId;
    this.name = props.name;
    this.birthDate = props.birthDate;
    this.sexAtBirth = props.sexAtBirth;
    this.bloodType = props.bloodType;
    this.allergies = props.allergies;
    this.healthPlanName = props.healthPlanName;
    this.healthPlanNumber = props.healthPlanNumber;
    this.avatarUrl = props.avatarUrl;
    this.avatarColor = props.avatarColor;
    this.createdAt = props.createdAt;
  }
 
  static create(props: CreateBabyProps): Baby {
    for (const measurement of props.measurements ?? []) validateBabyMeasurement(measurement, props.birthDate);
    const name = props.name.trim();
 
    if (name.length === 0) {
      throw new InvalidBabyNameError();
    }
 
    if (props.birthDate.getTime() > Date.now()) {
      throw new FutureBirthDateError();
    }
 
    return new Baby({
      measurements: props.measurements ?? [],
      id: props.id,
      userId: props.userId,
      name,
      birthDate: props.birthDate,
      sexAtBirth: props.sexAtBirth ?? null,
      bloodType: props.bloodType ?? null,
      allergies: props.allergies ?? [],
      healthPlanName: props.healthPlanName ?? null,
      healthPlanNumber: props.healthPlanNumber ?? null,
      avatarUrl: props.avatarUrl ?? null,
      avatarColor: props.avatarColor ?? null,
      createdAt: props.createdAt ?? new Date(),
    });
  }
}