import {z} from 'zod' export type GetApiSurgicalRecordsIdPhasesArgs = {id: string}; export type GetApiSurgicalRecordsIdPhasesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const surgicalPhaseBoundaryDto = z.object({at: z.string().optional(), source: z.string().optional()}); export const surgeryPhase = z.number().int(); export const surgicalEventPhaseDto = z.object({eventId: z.string().optional(), phase: surgeryPhase.optional()}); export const surgicalGridRowPhaseDto = z.object({rowId: z.string().nullable().optional(), at: z.string().nullable().optional(), phase: surgeryPhase.optional()}); export const surgicalPhasesDto = z.object({induction: surgicalPhaseBoundaryDto.nullable().optional(), procedureStart: surgicalPhaseBoundaryDto.nullable().optional(), postOpStart: surgicalPhaseBoundaryDto.nullable().optional(), dischargeStart: surgicalPhaseBoundaryDto.nullable().optional(), events: z.array(surgicalEventPhaseDto).optional(), gridRows: z.array(surgicalGridRowPhaseDto).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * The record's surgical phases, resolved on read by SurgeryPhaseResolver * (ITE-1330 PR3, flag `SurgicalPhaseNotes`): the boundaries with the console signal each * came from, plus the phase of every timeline event and anesthesia-grid row. Nothing is * stored — phases follow corrections immediately and a finalized record is never written. * 404 with the flag off. * * GET /SurgicalRecords/{id}/phases */ export const getApiSurgicalRecordsIdPhases = async (args: GetApiSurgicalRecordsIdPhasesArgs, options?: GetApiSurgicalRecordsIdPhasesOptions) => { const {baseUrl = "https://api.clinicos.vet/backend/business", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/SurgicalRecords/${encodeURIComponent(String(args.id))}/phases${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(surgicalPhasesDto, await res.json()) };