import {z} from 'zod' export type GetApiEstimatesIdItemsLineIdAvailabilityArgs = {id: string, lineId: string}; export type GetApiEstimatesIdItemsLineIdAvailabilityOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const billableItemComponentAvailabilityDto = z.object({componentId: z.string().optional(), inventoryItemId: z.string().optional(), itemName: z.string().optional(), trackLots: z.boolean().optional(), required: z.union([z.number(), z.string()]).optional(), available: z.union([z.number(), z.string()]).optional(), isAvailable: z.boolean().optional(), shortBy: z.union([z.number(), z.string()]).optional()}); export const billableItemAvailabilityDto = z.object({billableItemId: z.string().optional(), quantity: z.union([z.number(), z.string()]).optional(), isAvailable: z.boolean().optional(), components: z.array(billableItemComponentAvailabilityDto).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 }; /** * Stock-only availability pre-check for a charge line on the estimate (design * §5 / §9.1): are the BillableItem's components on hand at the line's quantity? * Read-only — no validate-pass, no stock movement. Mirrors the invoice-side * check off the same `CheckAvailabilityAsync`, so the estimate UI can flag * a short charge before the user opens the perform modal. * * GET /Estimates/{id}/items/{lineId}/availability */ export const getApiEstimatesIdItemsLineIdAvailability = async (args: GetApiEstimatesIdItemsLineIdAvailabilityArgs, options?: GetApiEstimatesIdItemsLineIdAvailabilityOptions) => { 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(/\/+$/, '')}/Estimates/${encodeURIComponent(String(args.id))}/items/${encodeURIComponent(String(args.lineId))}/availability${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(billableItemAvailabilityDto, await res.json()) };