All files / src/domain/milestone milestone.ts

100% Statements 19/19
100% Branches 13/13
100% Functions 4/4
100% Lines 19/19

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                                                                                    56x 56x 56x 56x 56x 56x 56x 56x         41x   41x 4x     37x 3x         41x   41x 1x     40x   40x                         22x      
import { startOfUtcDay } from '../../shared/utils/date';
import { FutureMilestoneDateError } from './errors/future-milestone-date.error';
import { InvalidMilestoneTitleError } from './errors/invalid-milestone-title.error';
import { MilestoneBeforeBirthError } from './errors/milestone-before-birth.error';
 
export type MilestoneCategory = 'MOTOR' | 'LANGUAGE' | 'SOCIAL' | 'COGNITIVE' | 'OTHER';
 
export interface MilestoneProps {
  id: string;
  babyId: string;
  title: string;
  description: string | null;
  achievedAt: Date;
  category: MilestoneCategory;
  photoUrl: string | null;
  createdAt: Date;
}
 
export interface RecordMilestoneProps {
  id: string;
  babyId: string;
  title: string;
  achievedAt: Date;
  category: MilestoneCategory;
  babyBirthDate: Date;
  description?: string | null;
  photoUrl?: string | null;
  createdAt?: Date;
  referenceDate?: Date;
}
 
export class Milestone {
  readonly id: string;
  readonly babyId: string;
  readonly title: string;
  readonly description: string | null;
  readonly achievedAt: Date;
  readonly category: MilestoneCategory;
  readonly photoUrl: string | null;
  readonly createdAt: Date;
 
  private constructor(props: MilestoneProps) {
    this.id = props.id;
    this.babyId = props.babyId;
    this.title = props.title;
    this.description = props.description;
    this.achievedAt = props.achievedAt;
    this.category = props.category;
    this.photoUrl = props.photoUrl;
    this.createdAt = props.createdAt;
  }
 
  // Only enforced here (creation/explicit date change), never on plain reconstruction.
  static assertAchievedAtIsValid(achievedAt: Date, babyBirthDate: Date, referenceDate: Date = new Date()): void {
    const day = startOfUtcDay(achievedAt).getTime();
 
    if (day > startOfUtcDay(referenceDate).getTime()) {
      throw new FutureMilestoneDateError();
    }
 
    if (day < startOfUtcDay(babyBirthDate).getTime()) {
      throw new MilestoneBeforeBirthError();
    }
  }
 
  static record(props: RecordMilestoneProps): Milestone {
    const title = props.title.trim();
 
    if (title.length === 0) {
      throw new InvalidMilestoneTitleError();
    }
 
    Milestone.assertAchievedAtIsValid(props.achievedAt, props.babyBirthDate, props.referenceDate);
 
    return new Milestone({
      id: props.id,
      babyId: props.babyId,
      title,
      description: props.description ?? null,
      achievedAt: props.achievedAt,
      category: props.category,
      photoUrl: props.photoUrl ?? null,
      createdAt: props.createdAt ?? new Date(),
    });
  }
 
  static restore(props: MilestoneProps): Milestone {
    return new Milestone(props);
  }
}