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

100% Statements 15/15
81.25% Branches 13/16
100% Functions 6/6
100% Lines 15/15

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                        15x 15x       27x 26x       2x 2x                   2x 1x               1x       1x                   1x 1x                   2x 1x       2x    
import { httpClient } from '@/lib/http-client'
 
import {
  medicationFormSchema,
  medicationListSchema,
  medicationSchema,
  type Medication,
  type MedicationFormInput,
} from './medications.schemas'
 
// Blank and whitespace-only both mean "not given"; the API takes `min(1)` on these.
function optionalText(value: string | undefined): string | undefined {
  const trimmed = value?.trim()
  return trimmed ? trimmed : undefined
}
 
export async function fetchMedications(babyId: string): Promise<Medication[]> {
  const response = await httpClient.get<unknown>(`/babies/${babyId}/medications`)
  return medicationListSchema.parse(response)
}
 
export async function createMedication(babyId: string, input: MedicationFormInput): Promise<Medication> {
  const parsed = medicationFormSchema.parse(input)
  const body = {
    name: parsed.name.trim(),
    startedOn: parsed.startedOn,
    dosage: optionalText(parsed.dosage),
    frequency: optionalText(parsed.frequency),
    reason: optionalText(parsed.reason),
    prescriberName: optionalText(parsed.prescriberName),
    endedOn: parsed.endedOn ? parsed.endedOn : undefined,
    notes: optionalText(parsed.notes),
  }
  const response = await httpClient.post<unknown>(`/babies/${babyId}/medications`, body)
  return medicationSchema.parse(response)
}
 
export async function updateMedication(
  babyId: string,
  medicationId: string,
  input: MedicationFormInput,
): Promise<Medication> {
  const parsed = medicationFormSchema.parse(input)
  // The edit form is pre-filled, so every field on screen is an explicit answer: an emptied one is
  // `null` (clear it), never an omission. Clearing `endedOn` reopens the course, which is the
  // correct reading of somebody deleting the end date they had typed.
  const body = {
    name: parsed.name.trim(),
    startedOn: parsed.startedOn,
    dosage: optionalText(parsed.dosage) ?? null,
    frequency: optionalText(parsed.frequency) ?? null,
    reason: optionalText(parsed.reason) ?? null,
    prescriberName: optionalText(parsed.prescriberName) ?? null,
    endedOn: parsed.endedOn ? parsed.endedOn : null,
    notes: optionalText(parsed.notes) ?? null,
  }
  const response = await httpClient.patch<unknown>(`/babies/${babyId}/medications/${medicationId}`, body)
  return medicationSchema.parse(response)
}
 
/**
 * Ending a course, which is the one edit this feature will get most often.
 *
 * A dedicated call rather than the full form: it sends `endedOn` alone, so nothing else about the
 * record can be touched by the act of closing it.
 */
export async function endMedication(babyId: string, medicationId: string, endedOn: string): Promise<Medication> {
  const response = await httpClient.patch<unknown>(`/babies/${babyId}/medications/${medicationId}`, { endedOn })
  return medicationSchema.parse(response)
}
 
export async function deleteMedication(babyId: string, medicationId: string): Promise<void> {
  await httpClient.delete<void>(`/babies/${babyId}/medications/${medicationId}`)
}