import {z} from 'zod' export type GetApiHospitalstaysIdChangesArgs = {id: string}; export type GetApiHospitalstaysIdChangesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const treatmentOrderKind = z.enum(["medication", "fluid", "diagnostic", "monitoring", "procedure", "nursing", "nutrition", "communication", "task"]); export const treatmentOrderEventAction = z.enum(["created", "edited", "completed", "skipped", "held", "resumed", "discontinued", "delayed", "charge_captured", "charge_capture_failed"]); export const hospitalStayChangeDto = z.object({id: z.string().optional(), orderId: z.string().optional(), orderTitle: z.string().optional(), orderKind: treatmentOrderKind.optional(), occurrenceId: z.string().nullable().optional(), action: treatmentOrderEventAction.optional(), at: z.string().optional(), actor: z.string().optional(), actorUserId: z.string().nullable().optional(), actorName: z.string().nullable().optional(), detail: z.string().nullable().optional()}); export const hospitalStayChangesDto = z.object({stayId: z.string().optional(), lastSeenAt: z.string().nullable().optional(), count: z.union([z.number().int(), z.string()]).optional(), truncated: z.boolean().optional(), changes: z.array(hospitalStayChangeDto).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 }; /** * What changed on this sheet since the caller last looked: the order audit events * after their marker, newest first, as terse facts. Empty with a null * `lastSeenAt` when they have never marked the stay. * * GET /hospitalstays/{id}/changes */ export const getApiHospitalstaysIdChanges = async (args: GetApiHospitalstaysIdChangesArgs, options?: GetApiHospitalstaysIdChangesOptions) => { 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(/\/+$/, '')}/hospitalstays/${encodeURIComponent(String(args.id))}/changes${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(hospitalStayChangesDto, await res.json()) };