import {z} from 'zod' export type PreviewLineItemDto = {localId?: string | undefined, unitPrice?: (number | unknown) | undefined, quantity?: (number | unknown) | undefined, discount?: (number | unknown) | undefined, dispenseFee?: (number | unknown) | undefined, groupCodeId?: (string | null) | undefined, serviceCatalogItemId?: (string | null) | undefined, retailItemId?: (string | null) | undefined, inventoryItemId?: (string | null) | undefined}; export type PreviewPriceRulesRequestDto = {items?: Array | undefined, clientId?: (string | null) | undefined, patientId?: (string | null) | undefined}; export type CreateApiInvoicesPreviewRulesArgs = {body: PreviewPriceRulesRequestDto}; export type CreateApiInvoicesPreviewRulesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const lineRuleDiscountDto = z.object({localId: z.string().optional(), amount: z.union([z.number(), z.string()]).optional()}); export const previewPriceRulesResponseDto = z.object({lineRuleDiscounts: z.array(lineRuleDiscountDto).optional(), ruleOrderDiscountAmount: z.union([z.number(), z.string()]).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 }; /** * Runs the price rule engine over a proposed set of line items without * persisting. Used by the invoice grid to show rule discounts (BOGO, * spend thresholds, etc.) live as the user edits, while keeping the * server as the single source of truth for the math — no client-side * evaluator to drift. * * POST /Invoices/preview-rules */ export const createApiInvoicesPreviewRules = async (args: CreateApiInvoicesPreviewRulesArgs, options?: CreateApiInvoicesPreviewRulesOptions) => { 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(/\/+$/, '')}/Invoices/preview-rules${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(previewPriceRulesResponseDto, await res.json()) };