import {z} from 'zod' export type TreatmentOrderKind = 'medication' | 'fluid' | 'diagnostic' | 'monitoring' | 'procedure' | 'nursing' | 'nutrition' | 'communication' | 'task'; export type TreatmentPriority = 'routine' | 'important' | 'critical'; export type TreatmentScheduleType = 'once' | 'interval' | 'times' | 'continuous' | 'prn'; export type TreatmentMedicationDetailsDto = {drug?: (string | null) | undefined, dose?: (number | unknown | null) | undefined, doseUnit?: (string | null) | undefined, calculatedDose?: (number | unknown | null) | undefined, calculatedDoseUnit?: (string | null) | undefined, concentration?: (number | unknown | null) | undefined, concentrationUnit?: (string | null) | undefined, calculatedVolumeMl?: (number | unknown | null) | undefined, route?: (string | null) | undefined, maxDose?: (number | unknown | null) | undefined}; export type TreatmentFluidDetailsDto = {fluid?: (string | null) | undefined, rateMlHr?: (number | unknown | null) | undefined, bolusMlKg?: (number | unknown | null) | undefined, calculatedBolusMl?: (number | unknown | null) | undefined, additives?: (Array | null) | undefined}; export type CreateTreatmentOrderRequest = {kind: TreatmentOrderKind, title: string, subtitle?: (string | null) | undefined, priority?: (TreatmentPriority | null) | undefined, scheduleType?: (TreatmentScheduleType | null) | undefined, scheduleLabel?: (string | null) | undefined, intervalMinutes?: (number | unknown | null) | undefined, startAt?: (string | null) | undefined, endAt?: (string | null) | undefined, times?: (Array | null) | undefined, maxOccurrences?: (number | unknown | null) | undefined, medication?: (TreatmentMedicationDetailsDto | null) | undefined, fluid?: (TreatmentFluidDetailsDto | null) | undefined, instructions?: (string | null) | undefined, resultUnit?: (string | null) | undefined, serviceCatalogItemId?: (string | null) | undefined, inventoryItemId?: (string | null) | undefined, billableItemId?: (string | null) | undefined}; export type CreateApiHospitalstaysIdOrdersArgs = {id: string, body: CreateTreatmentOrderRequest}; export type CreateApiHospitalstaysIdOrdersOptions = {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 treatmentOrderState = z.enum(["active", "held", "discontinued", "completed"]); export const treatmentPriority = z.enum(["routine", "important", "critical"]); export const treatmentScheduleType = z.enum(["once", "interval", "times", "continuous", "prn"]); export const treatmentMedicationDetailsDto = z.object({drug: z.string().nullable().optional(), dose: z.union([z.number(), z.string()]).nullable().optional(), doseUnit: z.string().nullable().optional(), calculatedDose: z.union([z.number(), z.string()]).nullable().optional(), calculatedDoseUnit: z.string().nullable().optional(), concentration: z.union([z.number(), z.string()]).nullable().optional(), concentrationUnit: z.string().nullable().optional(), calculatedVolumeMl: z.union([z.number(), z.string()]).nullable().optional(), route: z.string().nullable().optional(), maxDose: z.union([z.number(), z.string()]).nullable().optional()}); export const treatmentFluidDetailsDto = z.object({fluid: z.string().nullable().optional(), rateMlHr: z.union([z.number(), z.string()]).nullable().optional(), bolusMlKg: z.union([z.number(), z.string()]).nullable().optional(), calculatedBolusMl: z.union([z.number(), z.string()]).nullable().optional(), additives: z.array(z.string()).nullable().optional()}); export const treatmentOccurrenceStatus = z.enum(["pending", "completed", "skipped", "held"]); export const treatmentOccurrenceKind = z.enum(["clinical", "billing"]); export const treatmentOccurrenceDto = z.object({id: z.string().optional(), orderId: z.string().optional(), dueAt: z.string().optional(), status: treatmentOccurrenceStatus.optional(), kind: treatmentOccurrenceKind.optional(), completedAt: z.string().nullable().optional(), completedById: z.string().nullable().optional(), completedBy: z.string().nullable().optional(), actualValue: z.string().nullable().optional(), note: z.string().nullable().optional(), skippedReason: z.string().nullable().optional(), heldAt: z.string().nullable().optional(), occurrenceKey: z.string().optional(), estimateLineItemId: z.string().nullable().optional()}); export const treatmentOrderEventAction = z.enum(["created", "edited", "completed", "skipped", "held", "resumed", "discontinued", "delayed", "charge_captured", "charge_capture_failed"]); export const treatmentOrderEventDto = z.object({id: z.string().optional(), orderId: z.string().optional(), occurrenceId: z.string().nullable().optional(), action: treatmentOrderEventAction.optional(), at: z.string().optional(), actor: z.string().optional(), actorUserId: z.string().nullable().optional(), detail: z.string().nullable().optional()}); export const treatmentOrderDto = z.object({id: z.string().optional(), stayId: z.string().optional(), kind: treatmentOrderKind.optional(), title: z.string().optional(), subtitle: z.string().nullable().optional(), state: treatmentOrderState.optional(), priority: treatmentPriority.optional(), scheduleType: treatmentScheduleType.optional(), scheduleLabel: z.string().nullable().optional(), intervalMinutes: z.union([z.number().int(), z.string()]).nullable().optional(), startAt: z.string().nullable().optional(), endAt: z.string().nullable().optional(), times: z.array(z.string()).optional(), maxOccurrences: z.union([z.number().int(), z.string()]).nullable().optional(), medication: treatmentMedicationDetailsDto.nullable().optional(), fluid: treatmentFluidDetailsDto.nullable().optional(), instructions: z.string().nullable().optional(), resultUnit: z.string().nullable().optional(), serviceCatalogItemId: z.string().nullable().optional(), inventoryItemId: z.string().nullable().optional(), billableItemId: z.string().nullable().optional(), createdBy: z.string().optional(), createdById: z.string().nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), heldAt: z.string().nullable().optional(), discontinuedAt: z.string().nullable().optional(), completedAt: z.string().nullable().optional(), occurrences: z.array(treatmentOccurrenceDto).optional(), events: z.array(treatmentOrderEventDto).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 }; /** * Create a treatment order on the stay. * * POST /hospitalstays/{id}/orders */ export const createApiHospitalstaysIdOrders = async (args: CreateApiHospitalstaysIdOrdersArgs, options?: CreateApiHospitalstaysIdOrdersOptions) => { 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(/\/+$/, '')}/hospitalstays/${encodeURIComponent(String(args.id))}/orders${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(treatmentOrderDto, await res.json()) };