All files / src/application/baby update-baby.use-case.ts

100% Statements 6/6
91.66% Branches 22/24
100% Functions 2/2
100% Lines 6/6

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                                                26x 26x       16x             12x                                           16x   10x      
import { Baby, BabySexAtBirth } from '../../domain/baby/baby';
import { randomUUID } from 'node:crypto';
import { BabyMeasurementInput } from '../../domain/baby/baby-measurement';
import { BabyRepository } from './baby-repository';
import { BabyGuardianRepository } from './baby-guardian-repository';
import { ensureBabyAccess } from './ensure-baby-access';
 
export interface UpdateBabyInput {
  measurement?: BabyMeasurementInput;
  babyId: string;
  requestingUserId: 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;
}
 
export class UpdateBabyUseCase {
  constructor(
    private readonly babyRepository: BabyRepository,
    private readonly babyGuardianRepository: BabyGuardianRepository,
  ) {}
 
  async execute(input: UpdateBabyInput): Promise<Baby> {
    const existingBaby = await ensureBabyAccess(
      this.babyRepository,
      this.babyGuardianRepository,
      input.babyId,
      input.requestingUserId,
    );
 
    const updatedBaby = Baby.create({
      measurements: [...existingBaby.measurements, ...(input.measurement ? [{
        id: randomUUID(),
        measuredOn: new Date(`${input.measurement.measuredOn}T00:00:00.000Z`),
        weightGrams: input.measurement.weightGrams ?? null,
        heightMillimeters: input.measurement.heightMillimeters ?? null,
      }] : [])],
      id: existingBaby.id,
      userId: existingBaby.userId,
      name: input.name ?? existingBaby.name,
      birthDate: input.birthDate ?? existingBaby.birthDate,
      sexAtBirth: input.sexAtBirth !== undefined ? input.sexAtBirth : existingBaby.sexAtBirth,
      bloodType: input.bloodType !== undefined ? input.bloodType : existingBaby.bloodType,
      allergies: input.allergies ?? existingBaby.allergies,
      healthPlanName: input.healthPlanName !== undefined ? input.healthPlanName : existingBaby.healthPlanName,
      healthPlanNumber:
        input.healthPlanNumber !== undefined ? input.healthPlanNumber : existingBaby.healthPlanNumber,
      avatarUrl: input.avatarUrl !== undefined ? input.avatarUrl : existingBaby.avatarUrl,
      avatarColor: input.avatarColor !== undefined ? input.avatarColor : existingBaby.avatarColor,
      createdAt: existingBaby.createdAt,
    });
 
    await this.babyRepository.save(updatedBaby);
 
    return updatedBaby;
  }
}