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

100% Statements 12/12
100% Branches 2/2
100% Functions 6/6
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                                  31x 30x             11x               7x 7x 5x       19x 18x             4x 4x 3x       2x    
import { httpClient } from '@/lib/http-client'
 
import {
  adhocVaccineListSchema,
  adhocVaccineRecordSchema,
  applyVaccineSchema,
  createAdhocVaccineSchema,
  vaccineCalendarSchema,
  vaccineItemSchema,
  type AdhocVaccineRecord,
  type ApplyVaccineInput,
  type CreateAdhocVaccineInput,
  type VaccineCalendar,
  type VaccineItem,
} from './vaccines.schemas'
 
export async function fetchVaccineCalendar(babyId: string): Promise<VaccineCalendar> {
  const response = await httpClient.get<unknown>(`/babies/${babyId}/vaccines`)
  return vaccineCalendarSchema.parse(response)
}
 
// The backend's photoUrl is a strict URL — a native <input> always yields ''
// (never undefined) when left blank, so that gets stripped before sending
// rather than loosening the API's validation (same convention as milestones).
function withoutEmptyPhotoUrl<T extends { photoUrl?: string }>(input: T): T {
  return { ...input, photoUrl: input.photoUrl || undefined }
}
 
export async function applyVaccine(
  babyId: string,
  vaccineId: string,
  input: ApplyVaccineInput,
): Promise<VaccineItem> {
  const body = applyVaccineSchema.parse(withoutEmptyPhotoUrl(input))
  const response = await httpClient.patch<unknown>(`/babies/${babyId}/vaccines/${vaccineId}/apply`, body)
  return vaccineItemSchema.parse(response)
}
 
export async function fetchAdhocVaccines(babyId: string): Promise<AdhocVaccineRecord[]> {
  const response = await httpClient.get<unknown>(`/babies/${babyId}/vaccines/adhoc`)
  return adhocVaccineListSchema.parse(response)
}
 
export async function registerAdhocVaccine(
  babyId: string,
  input: CreateAdhocVaccineInput,
): Promise<AdhocVaccineRecord> {
  const body = createAdhocVaccineSchema.parse(withoutEmptyPhotoUrl(input))
  const response = await httpClient.post<unknown>(`/babies/${babyId}/vaccines/adhoc`, body)
  return adhocVaccineRecordSchema.parse(response)
}
 
export async function deleteAdhocVaccine(babyId: string, recordId: string): Promise<void> {
  await httpClient.delete<void>(`/babies/${babyId}/vaccines/adhoc/${recordId}`)
}