import {z} from 'zod' export type AggregateInvoicePatchRequest = {startTime?: string | undefined, endTime?: string | undefined, dueTime?: string | undefined, currency?: string | undefined, timezone?: string | undefined, addInvoices: Array, removeInvoices: Array}; export type PatchApiBillingTenantLocatorInvoicesLocatorAggregateArgs = {tenantLocator: string, locator: unknown, body: AggregateInvoicePatchRequest}; export type PatchApiBillingTenantLocatorInvoicesLocatorAggregateOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const invoiceItemResponse = z.object({locator: z.string(), chargeType: z.string(), chargeCategory: z.string(), amount: z.number(), remainingAmount: z.number().optional(), settlementTime: z.string().optional(), invoiceLocator: z.string(), installmentItemLocators: z.array(z.string()), timezone: z.string(), quoteLocator: z.string().optional(), policyLocator: z.string().optional(), elementStaticLocator: z.string().optional(), elementType: z.string().optional(), transactionLocators: z.array(z.string()), unsettledTime: z.string().optional()}); export const invoiceResponse = z.object({locator: z.string(), accountLocator: z.string(), invoiceState: z.enum(["building", "open", "settled", "discarded"]), invoiceItems: z.array(invoiceItemResponse).optional(), generatedTime: z.string(), dueTime: z.string(), currency: z.string(), startTime: z.string(), endTime: z.string(), unsettledTime: z.string().optional(), timezone: z.string(), invoiceNumber: z.string().optional(), autopayTime: z.string().optional(), settledTime: z.string().optional(), aggregateInvoiceLocator: z.string().optional(), invoiceType: z.enum(["normal", "aggregate"]), totalAmount: z.number().optional(), totalRemainingAmount: z.number().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 }; /** * PATCH /billing/{tenantLocator}/invoices/{locator}/aggregate */ export const patchApiBillingTenantLocatorInvoicesLocatorAggregate = async (args: PatchApiBillingTenantLocatorInvoicesLocatorAggregateArgs, options: PatchApiBillingTenantLocatorInvoicesLocatorAggregateOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/billing/${encodeURIComponent(String(args.tenantLocator))}/invoices/${encodeURIComponent(String(args.locator))}/aggregate${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(invoiceResponse, await res.json()) };