import {z} from 'zod' export type LineItemNoteTemplateScope = number; export type GetApiLineItemNotesArgs = {serviceCatalogItemId?: string | undefined, inventoryItemId?: string | undefined, retailItemId?: string | undefined, billableItemId?: string | undefined, scope?: LineItemNoteTemplateScope | undefined, q?: string | undefined}; export type GetApiLineItemNotesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const lineItemNoteAttachmentItemType = z.number().int(); export const lineItemNoteAttachmentDto = z.object({itemType: lineItemNoteAttachmentItemType.optional(), itemId: z.string().optional()}); export const lineItemNoteTemplateScope = z.number().int(); export const lineItemNoteTemplateDto = z.object({id: z.string().optional(), text: z.string().optional(), title: z.string().nullable().optional(), appliesToAnyItem: z.boolean().optional(), attachments: z.array(lineItemNoteAttachmentDto).optional(), scope: lineItemNoteTemplateScope.optional(), createdByUserId: z.string().nullable().optional(), usageCount: z.union([z.number().int(), z.string()]).optional(), lastUsedAt: z.string().nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const getApiLineItemNotesResponse = z.array(lineItemNoteTemplateDto); 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 templates visible to the caller (all Clinic + their own Personal), most-used * first. Item-id filters are literal attachment filters; the suggestions endpoint on * EstimatesController is what folds in item-agnostic templates for a given line. * * GET /line-item-notes */ export const getApiLineItemNotes = async (args: GetApiLineItemNotesArgs, options?: GetApiLineItemNotesOptions) => { 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.serviceCatalogItemId != null) query.append("serviceCatalogItemId", String(args.serviceCatalogItemId)) if (args.inventoryItemId != null) query.append("inventoryItemId", String(args.inventoryItemId)) if (args.retailItemId != null) query.append("retailItemId", String(args.retailItemId)) if (args.billableItemId != null) query.append("billableItemId", String(args.billableItemId)) if (args.scope != null) query.append("scope", String(args.scope)) if (args.q != null) query.append("q", String(args.q)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/line-item-notes${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(getApiLineItemNotesResponse, await res.json()) };