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

100% Statements 13/13
66.66% Branches 4/6
100% Functions 5/5
100% Lines 13/13

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                          6x 6x       40x 40x       2x 2x             2x 2x       1x     1x             1x 1x       2x    
import { httpClient } from '@/lib/http-client'
 
import {
  specialistFormSchema,
  specialistListSchema,
  specialistSchema,
  type Specialist,
  type SpecialistFormInput,
} from './specialists.schemas'
 
// Blank and whitespace-only both mean "not given"; the API takes `min(1)` on these — and a phone
// stored as a single space looks filled in the list and is useless when it is needed.
function optionalText(value: string | undefined): string | undefined {
  const trimmed = value?.trim()
  return trimmed ? trimmed : undefined
}
 
export async function fetchSpecialists(): Promise<Specialist[]> {
  const response = await httpClient.get<unknown>('/specialists')
  return specialistListSchema.parse(response)
}
 
export async function createSpecialist(input: SpecialistFormInput): Promise<Specialist> {
  const parsed = specialistFormSchema.parse(input)
  const body = {
    name: parsed.name.trim(),
    specialty: optionalText(parsed.specialty),
    phone: optionalText(parsed.phone),
    babyIds: parsed.babyIds,
    sharedWithUserIds: parsed.sharedWithUserIds,
  }
  const response = await httpClient.post<unknown>('/specialists', body)
  return specialistSchema.parse(response)
}
 
export async function updateSpecialist(specialistId: string, input: SpecialistFormInput): Promise<Specialist> {
  const parsed = specialistFormSchema.parse(input)
  // O formulário mostra o estado inteiro, então tudo o que ele manda é resposta explícita: uma
  // lista vazia significa "nenhuma criança", e não "não mexi".
  const body = {
    name: parsed.name.trim(),
    specialty: optionalText(parsed.specialty) ?? null,
    phone: optionalText(parsed.phone) ?? null,
    babyIds: parsed.babyIds,
    sharedWithUserIds: parsed.sharedWithUserIds,
  }
  const response = await httpClient.patch<unknown>(`/specialists/${specialistId}`, body)
  return specialistSchema.parse(response)
}
 
export async function deleteSpecialist(specialistId: string): Promise<void> {
  await httpClient.delete<void>(`/specialists/${specialistId}`)
}