import {z} from 'zod' export type MarkPaidTransaction = {transaction_match_id?: (number | null) | undefined, transaction_id?: (number | null) | undefined, transaction_ids?: (Array | null) | undefined, account_id?: (number | null) | undefined, amount: number, amount_payable?: (number | null) | undefined, credit_account_id?: (number | null) | undefined, writeoff_account_id?: (number | null) | undefined, withholding_amount?: (number | null) | undefined, withholding_tax_rate_id?: (number | null) | undefined}; export type MarkPaidCreditMemo = {credit_memo_id: number, amount: number, posted_at: string}; export type MarkPaidDebitMemo = {debit_memo_id: number, amount: number, posted_at: string}; export type MarkPaidEmptyTransaction = {account_id: number, account_name: string, amount: number, payment_date: string, department_id?: (number | null) | undefined, tag_ids?: Array | undefined, memo?: (string | null) | undefined, external_id?: (string | null) | undefined, currency?: (string | null) | undefined, amount_in_invoice_currency?: (number | null) | undefined, amount_in_bill_currency?: (number | null) | undefined, withholding_amount?: (number | null) | undefined, withholding_tax_rate_id?: (number | null) | undefined}; export type MarkPaid = {transaction_match_id?: (number | null) | undefined, transactions?: (Array | null) | undefined, credit_memos?: (Array | null) | undefined, debit_memos?: (Array | null) | undefined, empty_transactions?: (Array | null) | undefined}; export type CreateApiCoaApiV1InvoiceInvoiceIdPayArgs = {invoice_id: number, body: MarkPaid}; export type CreateApiCoaApiV1InvoiceInvoiceIdPayOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const markInvoicePaidAutoCreatedCreditMemo = z.object({id: z.number().int(), credit_memo_number: z.string()}); export const markInvoicePaidResponse = z.object({journal_entry_ids: z.array(z.number().int()), payment_ids: z.array(z.number().int()), auto_created_credit_memos: z.array(markInvoicePaidAutoCreatedCreditMemo).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 }; /** * Mark Invoice as Paid * * POST /coa/api/v1/invoice/{invoice_id}/pay/ */ export const createApiCoaApiV1InvoiceInvoiceIdPay = async (args: CreateApiCoaApiV1InvoiceInvoiceIdPayArgs, options?: CreateApiCoaApiV1InvoiceInvoiceIdPayOptions) => { 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))}/pay/${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(markInvoicePaidResponse, await res.json()) };