import {z} from 'zod' export type GetApiBillingTenantLocatorPaymentsLocatorPreviewValidateArgs = {tenantLocator: string, locator: unknown}; export type GetApiBillingTenantLocatorPaymentsLocatorPreviewValidateOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const validationItemResponse = z.object({elementType: z.string(), locator: z.string(), errors: z.array(z.string())}); export const validationResult = z.object({validationItems: z.array(validationItemResponse).optional(), success: z.boolean()}); export const creditItem = z.object({containerLocator: z.string(), containerType: z.enum(["invoice", "account", "subpayment", "invoiceItem"]), amount: z.number().optional()}); export const creditWithBalance = z.object({type: z.enum(["accountCreditBalance", "invoiceCreditBalance", "cash", "creditCash", "charge", "credit", "installmentItem", "invoiceItem", "account", "policy", "accountExpenseBalance"]), locator: z.string(), amount: z.number()}); export const shortfallCreditPreviewResponse = z.object({locator: z.string(), creditType: z.enum(["creditDistribution", "disbursement", "payment", "subpayment", "shortfallWriteOff", "writeOff"]), shortfallCreditState: z.enum(["draft", "distributed", "reversed"]), currency: z.string(), amount: z.number(), accountLocator: z.string(), targets: z.array(creditItem)}); export const invoicePaymentPreview = z.object({locator: z.string(), accountLocator: z.string(), invoiceState: z.enum(["building", "open", "settled", "discarded"]), amount: z.number().optional(), remainingAmount: z.number().optional()}); export const listPageResponseInvoicePaymentPreview = z.object({listCompleted: z.boolean(), items: z.array(invoicePaymentPreview)}); export const previewPaymentResponse: z.ZodType = z.object({locator: z.string(), paymentState: z.enum(["draft", "validated", "requested", "executing", "posting", "posted", "failed", "cancelled", "reversing", "reversed", "discarded"]), type: z.string(), currency: z.string(), amount: z.number(), data: z.record(z.string(), z.object({})), createdAt: z.string(), createdBy: z.string(), accountLocator: z.string().optional(), remainingAmount: z.number().optional(), externalCashTransactionLocator: z.string().optional(), validationResult: validationResult.optional(), postedAt: z.string().optional(), paymentMode: z.enum(["normal", "aggregate"]).optional(), aggregatePaymentLocator: z.string().optional(), targets: z.array(creditItem), credits: z.array(creditWithBalance), shortfallCredits: z.array(shortfallCreditPreviewResponse), subpayments: z.array(z.lazy(() => previewPaymentResponse)).optional(), invoices: listPageResponseInvoicePaymentPreview.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 }; /** * GET /billing/{tenantLocator}/payments/{locator}/previewValidate */ export const getApiBillingTenantLocatorPaymentsLocatorPreviewValidate = async (args: GetApiBillingTenantLocatorPaymentsLocatorPreviewValidateArgs, options: GetApiBillingTenantLocatorPaymentsLocatorPreviewValidateOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/billing/${encodeURIComponent(String(args.tenantLocator))}/payments/${encodeURIComponent(String(args.locator))}/previewValidate${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(previewPaymentResponse, await res.json()) };