All files / src/features/appointments/api appointments.api.ts

100% Statements 12/12
88.88% Branches 16/18
100% Functions 5/5
100% Lines 12/12

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                            31x 29x       13x 13x                   4x 4x                                 4x 3x               5x 5x 5x       2x    
import { httpClient } from '@/lib/http-client'
import { centimetersInputToMillimeters, kilogramsInputToGrams } from '@/shared/utils/measurements'
 
import {
  appointmentListSchema,
  appointmentSchema,
  medicalSpecialtyListSchema,
  updateAppointmentSchema,
  type Appointment,
  type AppointmentFormInput,
  type UpdateAppointmentInput,
} from './appointments.schemas'
 
export async function fetchAppointments(babyId: string): Promise<Appointment[]> {
  const response = await httpClient.get<unknown>(`/babies/${babyId}/appointments`)
  return appointmentListSchema.parse(response)
}
 
export async function fetchMedicalSpecialties(): Promise<string[]> {
  const response = await httpClient.get<unknown>('/specialties')
  return medicalSpecialtyListSchema.parse(response)
}
 
export async function createAppointment(
  babyId: string,
  input: AppointmentFormInput,
  // Passed in rather than read from `input` because the specialist may have been created moments
  // earlier, in the same submit, and its id does not exist until then.
  specialistId?: string,
): Promise<Appointment> {
  const scheduledAt = new Date(`${input.date}T${input.time}`).toISOString()
  const body = {
    scheduledAt,
    doctorName: input.doctorName,
    specialty: input.specialty || undefined,
    location: input.location || undefined,
    reason: input.reason || undefined,
    // Only sent when recording the past. SCHEDULED is the API's default and the
    // contract is additive, so omitting it keeps the request byte-identical to
    // what this app sent before COMPLETED existed.
    status: input.status === 'COMPLETED' ? 'COMPLETED' : undefined,
    specialistId: specialistId ?? input.specialistId,
    // Same reason, and one the API enforces: a measurement belongs to a visit that happened, so a
    // booking never carries one. Sending it would be a 400 the person could do nothing about.
    weightGrams: input.status === 'COMPLETED' ? (kilogramsInputToGrams(input.weightKg) ?? undefined) : undefined,
    heightMillimeters:
      input.status === 'COMPLETED' ? (centimetersInputToMillimeters(input.heightCm) ?? undefined) : undefined,
  }
  const response = await httpClient.post<unknown>(`/babies/${babyId}/appointments`, body)
  return appointmentSchema.parse(response)
}
 
export async function updateAppointment(
  babyId: string,
  appointmentId: string,
  input: UpdateAppointmentInput,
): Promise<Appointment> {
  const body = updateAppointmentSchema.parse(input)
  const response = await httpClient.patch<unknown>(`/babies/${babyId}/appointments/${appointmentId}`, body)
  return appointmentSchema.parse(response)
}
 
export async function deleteAppointment(babyId: string, appointmentId: string): Promise<void> {
  await httpClient.delete<void>(`/babies/${babyId}/appointments/${appointmentId}`)
}