import {z} from 'zod' export type SaveCustomExamRequest = {customExamTemplateId?: (string | null) | undefined, customExamTemplateVersion?: (number | unknown | null) | undefined, customExamResponseJson?: (string | null) | undefined}; export type CreateApiRecordingsIdExamSaveArgs = {id: string, body: SaveCustomExamRequest}; export type CreateApiRecordingsIdExamSaveOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const examEmissionFieldResult = z.object({fieldId: z.string(), status: z.string(), message: z.string().nullable().optional()}); export const saveCustomExamResponse = z.object({fieldResults: z.array(examEmissionFieldResult).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 }; /** * Explicit "Save exam" for the custom exam step (PAW-740): persists the narrative * responses like the autosave PATCH, then runs the binding interpreter so bound fields * emit real vitals and physical-exam findings. This is the single semantic commit point — * the debounced autosave keeps persisting narrative JSON but never interprets. * Binding failures never block the save; they come back as per-field results. * * POST /Recordings/{id}/exam/save */ export const createApiRecordingsIdExamSave = async (args: CreateApiRecordingsIdExamSaveArgs, options?: CreateApiRecordingsIdExamSaveOptions) => { const {baseUrl = "https://api.clinicos.vet/backend/business", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/Recordings/${encodeURIComponent(String(args.id))}/exam/save${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(saveCustomExamResponse, await res.json()) };