import {z} from 'zod' export type InvoiceLayoutDto = {showPatientPhoto?: boolean | undefined, showPaymentDetails?: boolean | undefined, showNotes?: boolean | undefined, showProviders?: boolean | undefined, footerText?: (string | null) | undefined, defaultFooterText?: (string | null) | undefined}; export type EstimateLayoutDto = {showPatientPhoto?: boolean | undefined, footerText?: (string | null) | undefined}; export type LetterheadLayoutDto = {showLogo?: boolean | undefined, logoId?: (string | null) | undefined, footerText?: (string | null) | undefined}; export type DocumentLayoutSettingsDto = {invoice?: InvoiceLayoutDto | undefined, estimate?: EstimateLayoutDto | undefined, letterhead?: (LetterheadLayoutDto | null) | undefined}; export type UpdateApiClinicConfigDocumentLayoutArgs = {body: DocumentLayoutSettingsDto}; export type UpdateApiClinicConfigDocumentLayoutOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const invoiceLayoutDto = z.object({showPatientPhoto: z.boolean().optional(), showPaymentDetails: z.boolean().optional(), showNotes: z.boolean().optional(), showProviders: z.boolean().optional(), footerText: z.string().nullable().optional(), defaultFooterText: z.string().nullable().optional()}); export const estimateLayoutDto = z.object({showPatientPhoto: z.boolean().optional(), footerText: z.string().nullable().optional()}); export const letterheadLayoutDto = z.object({showLogo: z.boolean().optional(), logoId: z.string().nullable().optional(), footerText: z.string().nullable().optional()}); export const documentLayoutSettingsDto = z.object({invoice: invoiceLayoutDto.optional(), estimate: estimateLayoutDto.optional(), letterhead: letterheadLayoutDto.nullable().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 }; /** * PUT /clinic-config/document-layout — replaces the PDF layout (ITE-1334). Every field is * written, so a later default change never silently flips a clinic's saved choice. * * PUT /clinic-config/document-layout */ export const updateApiClinicConfigDocumentLayout = async (args: UpdateApiClinicConfigDocumentLayoutArgs, options?: UpdateApiClinicConfigDocumentLayoutOptions) => { 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(/\/+$/, '')}/clinic-config/document-layout${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(documentLayoutSettingsDto, await res.json()) };