import {z} from 'zod' export type GetApiEstimatesLinkableArgs = {patientId?: string | undefined}; export type GetApiEstimatesLinkableOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const estimateSummaryDto = z.object({id: z.string().optional(), estimateNumber: z.string().optional(), clientName: z.string().nullable().optional(), patientId: z.string().nullable().optional(), patientName: z.string().nullable().optional(), status: z.string().optional(), selectedTier: z.string().nullable().optional(), itemCount: z.union([z.number().int(), z.string()]).optional(), total: z.union([z.number(), z.string()]).optional(), appointmentId: z.string().nullable().optional(), invoiceId: z.string().nullable().optional(), invoiceStatus: z.string().nullable().optional(), invoiceNumber: z.string().nullable().optional(), invoiceItemCount: z.union([z.number().int(), z.string()]).nullable().optional(), invoiceTotal: z.union([z.number(), z.string()]).nullable().optional(), mergedIntoInvoiceId: z.string().nullable().optional(), mergedIntoInvoiceNumber: z.string().nullable().optional(), mergedIntoInvoiceStatus: z.string().nullable().optional(), createdAt: z.string().optional()}); export const estimateListResponseDto = z.object({items: z.array(estimateSummaryDto).optional(), total: z.union([z.number().int(), 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 }; /** * List a patient's open, standalone estimates that can be linked to an appointment — i.e. * pre-existing quotes (created from the client/patient record) that are not yet converted * (`InvoiceId == null`), not declined, and not already tied to an appointment * (`AppointmentId == null`). Drives the "Link existing estimate" picker on the * Services tab. * * GET /Estimates/linkable */ export const getApiEstimatesLinkable = async (args: GetApiEstimatesLinkableArgs, options?: GetApiEstimatesLinkableOptions) => { 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 (args.patientId != null) query.append("patientId", String(args.patientId)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/Estimates/linkable${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(estimateListResponseDto, await res.json()) };