import {z} from 'zod' export type DispenseDisposition = number; export type DispenseItemDto = {lotId?: (string | null) | undefined, containerId?: (string | null) | undefined, containerPins?: ({[key: string]: string} | null) | undefined, quantity?: (number | unknown) | undefined, patientId?: (string | null) | undefined, appointmentId?: (string | null) | undefined, witnessUserId?: (string | null) | undefined, notes?: (string | null) | undefined, locationId: string, dispenseType?: string | undefined, disposition?: (DispenseDisposition | null) | undefined, dosage?: (string | null) | undefined, frequency?: (string | null) | undefined, duration?: (string | null) | undefined, instructions?: (string | null) | undefined, refillsAllowed?: (number | unknown) | undefined, prescriptionItemId?: (string | null) | undefined, deferRxDetails?: boolean | undefined, invoiceId?: (string | null) | undefined, targetInvoiceLineItemId?: (string | null) | undefined, invoiceOverridePrice?: (number | unknown | null) | undefined, targetEstimateLineItemId?: (string | null) | undefined, markedForBilling?: boolean | undefined, skipInvoiceAttach?: boolean | undefined, overrideValidation?: boolean | undefined, overrideReason?: (string | null) | undefined, collectorName?: (string | null) | undefined, collectorIdChecked?: (boolean | null) | undefined}; export type CreateApiInventoryIdValidateDispenseArgs = {id: string, body: DispenseItemDto}; export type CreateApiInventoryIdValidateDispenseOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dispenseValidationIssueDto = z.object({code: z.string().optional(), message: z.string().optional(), canOverride: z.boolean().optional(), appliesAt: z.string().optional()}); export const dispenseValidationResultDto = z.object({isValid: z.boolean().optional(), issues: z.array(dispenseValidationIssueDto).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 }; /** * POST /Inventory/{id}/validate-dispense */ export const createApiInventoryIdValidateDispense = async (args: CreateApiInventoryIdValidateDispenseArgs, options?: CreateApiInventoryIdValidateDispenseOptions) => { 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(/\/+$/, '')}/Inventory/${encodeURIComponent(String(args.id))}/validate-dispense${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(dispenseValidationResultDto, await res.json()) };