import {z} from 'zod' export type TransactionPaymentInput = {transaction_id: number, amount: number, posted_at: string, withholding_amount?: (number | null) | undefined}; export type CreditMemoPaymentInput = {credit_memo_id: number, amount: number, posted_at: string}; export type EmptyTransactionInput = {amount: number, payment_date: string, withholding_amount?: (number | null) | undefined}; export type CalculatePaymentInput = {transactions?: Array | undefined, credit_memos?: Array | undefined, empty_transactions?: Array | undefined}; export type CreateApiCoaApiV1InvoiceInvoiceIdCalculatePaymentArgs = {invoice_id: number, body: CalculatePaymentInput}; export type CreateApiCoaApiV1InvoiceInvoiceIdCalculatePaymentOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const calculatePaymentOutput = z.object({total_due: z.number().gt(-1000000000000000000).lt(1000000000000000000), applied_payments: z.number().gt(-1000000000000000000).lt(1000000000000000000), applied_credits: z.number().gt(-1000000000000000000).lt(1000000000000000000), applied_withholding: z.number().gt(-1000000000000000000).lt(1000000000000000000), term_discounts: z.number().gt(-1000000000000000000).lt(1000000000000000000), remaining_due: z.number().gt(-1000000000000000000).lt(1000000000000000000)}); 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 }; /** * Calculate Invoice Payment * * POST /coa/api/v1/invoice/{invoice_id}/calculate-payment */ export const createApiCoaApiV1InvoiceInvoiceIdCalculatePayment = async (args: CreateApiCoaApiV1InvoiceInvoiceIdCalculatePaymentArgs, options?: CreateApiCoaApiV1InvoiceInvoiceIdCalculatePaymentOptions) => { const {baseUrl = "https://api.meetcampfire.com", fetch: fetchFn = fetch, knoxApiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (knoxApiToken !== undefined) headers.set("Authorization", knoxApiToken) const url = `${baseUrl.replace(/\/+$/, '')}/coa/api/v1/invoice/${encodeURIComponent(String(args.invoice_id))}/calculate-payment${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(calculatePaymentOutput, await res.json()) };