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 CreateApiInventoryIdDispenseArgs = {id: string, body: DispenseItemDto}; export type CreateApiInventoryIdDispenseOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dispensedLotDto = z.object({transactionId: z.string().optional(), lotId: z.string().optional(), lotNumber: z.string().nullable().optional(), containerId: z.string().nullable().optional(), quantity: z.union([z.number(), z.string()]).optional(), newLotBalance: z.union([z.number(), z.string()]).optional(), deaEntryId: z.string().nullable().optional()}); export const dispenseResultDto = z.object({transactionId: z.string().optional(), invoiceLineItemId: z.string().nullable().optional(), invoiceId: z.string().nullable().optional(), estimateLineItemId: z.string().nullable().optional(), estimateId: z.string().nullable().optional(), deaEntryId: z.string().nullable().optional(), quantityDispensed: z.union([z.number(), z.string()]).optional(), lotId: z.string().optional(), containerId: z.string().nullable().optional(), newLotBalance: z.union([z.number(), z.string()]).optional(), dispenseType: z.string().optional(), prescriptionId: z.string().nullable().optional(), prescriptionItemId: z.string().nullable().optional(), lots: z.array(dispensedLotDto).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}/dispense */ export const createApiInventoryIdDispense = async (args: CreateApiInventoryIdDispenseArgs, options?: CreateApiInventoryIdDispenseOptions) => { 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))}/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(dispenseResultDto, await res.json()) };