All files / src/application/appointment create-appointment.use-case.ts

100% Statements 8/8
100% Branches 2/2
100% Functions 2/2
100% Lines 8/8

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                                                        25x 25x 25x       29x   26x   29x                           29x   22x      
import { randomUUID } from 'node:crypto';
import { BabyRepository } from '../baby/baby-repository';
import { BabyGuardianRepository } from '../baby/baby-guardian-repository';
import { ensureBabyAccess } from '../baby/ensure-baby-access';
import { Appointment } from '../../domain/appointment/appointment';
import { AppointmentRepository } from './appointment-repository';
 
export interface CreateAppointmentInput {
  babyId: string;
  requestingUserId: string;
  scheduledAt: Date;
  doctorName: string;
  specialty?: string | null;
  location?: string | null;
  reason?: string | null;
  specialistId?: string | null;
  weightGrams?: number | null;
  heightMillimeters?: number | null;
  referenceDate?: Date;
  /**
   * COMPLETED enters a consultation that already happened, and requires a date in the past.
   * SCHEDULED (the default) is the original behaviour and requires one in the future.
   */
  status?: 'SCHEDULED' | 'COMPLETED';
}
 
export class CreateAppointmentUseCase {
  constructor(
    private readonly babyRepository: BabyRepository,
    private readonly babyGuardianRepository: BabyGuardianRepository,
    private readonly appointmentRepository: AppointmentRepository,
  ) {}
 
  async execute(input: CreateAppointmentInput): Promise<Appointment> {
    await ensureBabyAccess(this.babyRepository, this.babyGuardianRepository, input.babyId, input.requestingUserId);
 
    const create = input.status === 'COMPLETED' ? Appointment.record : Appointment.schedule;
 
    const appointment = create({
      id: randomUUID(),
      babyId: input.babyId,
      scheduledAt: input.scheduledAt,
      doctorName: input.doctorName,
      specialty: input.specialty,
      location: input.location,
      reason: input.reason,
      specialistId: input.specialistId,
      weightGrams: input.weightGrams,
      heightMillimeters: input.heightMillimeters,
      referenceDate: input.referenceDate,
    });
 
    await this.appointmentRepository.save(appointment);
 
    return appointment;
  }
}