import {z} from 'zod' export type LineItemNoteAttachmentItemType = number; export type LineItemNoteAttachmentDto = {itemType?: LineItemNoteAttachmentItemType | undefined, itemId?: string | undefined}; export type LineItemNoteTemplateScope = number; export type SaveLineItemNoteTemplateRequest = {text?: string | undefined, title?: (string | null) | undefined, appliesToAnyItem?: boolean | undefined, attachments?: Array | undefined, scope?: LineItemNoteTemplateScope | undefined}; export type CreateApiLineItemNotesArgs = {body: SaveLineItemNoteTemplateRequest}; export type CreateApiLineItemNotesOptions = {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 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 }; /** * Create a template. Duplicate rule: same trimmed case-folded text + same scope (and * same owner, for Personal) UNIONS the incoming attachments into the existing row and * returns it with 200 instead of minting a second one — so "Save note" of the same * wording on a second med naturally accretes attachments. Any-item absorbs: if either * side applies to any item, the merged result does. * * POST /line-item-notes */ export const createApiLineItemNotes = async (args: CreateApiLineItemNotesArgs, options?: CreateApiLineItemNotesOptions) => { 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(/\/+$/, '')}/line-item-notes${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(lineItemNoteTemplateDto, await res.json()) };