import {z} from 'zod' export type PatchedBankTransaction = {id?: number | undefined, is_deleted?: boolean | undefined, deleted_at?: (string | null) | undefined, account_name?: string | undefined, entity_name?: (string | null) | undefined, date_month?: (string | null) | undefined, date_year?: (string | null) | undefined, journal?: (number | null) | undefined, journal_order?: (string | null) | undefined, intercompany_journal?: (number | null) | undefined, reconciliation_report?: number | undefined, reconciliation_report_ending_date?: string | undefined, last_modified_at?: string | undefined, transaction_id?: string | undefined, external_transaction_id?: (string | null) | undefined, currency?: string | undefined, amount?: (number | null) | undefined, amount_native?: (number | null) | undefined, posted_at?: (string | null) | undefined, status?: (string | null) | undefined, note?: (string | null) | undefined, bank_description?: (string | null) | undefined, external_memo?: (string | null) | undefined, merchant_id?: (string | null) | undefined, merchant_name?: (string | null) | undefined, merchant_nickname?: (string | null) | undefined, kind?: (string | null) | undefined, excluded?: boolean | undefined, assigned?: boolean | undefined, metadata: unknown, created_at?: string | undefined, customer?: number | undefined, account?: number | undefined}; export type PatchApiCaApiTransactionIdArgs = {id: number, body: PatchedBankTransaction}; export type PatchApiCaApiTransactionIdOptions = {baseUrl?: string | undefined, knoxApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const bankTransaction = z.object({id: z.number().int(), is_deleted: z.boolean(), deleted_at: z.string().nullable(), account_name: z.string(), entity_name: z.string().nullable(), date_month: z.string().nullable(), date_year: z.string().nullable(), journal: z.number().int().nullable(), journal_order: z.string().nullable(), intercompany_journal: z.number().int().nullable(), reconciliation_report: z.number().int(), reconciliation_report_ending_date: z.string(), last_modified_at: z.string(), transaction_id: z.string().optional(), external_transaction_id: z.string().max(200).nullable().optional(), currency: z.string().max(3).optional(), amount: z.number().gt(-1000000000000000000).lt(1000000000000000000).nullable().optional(), amount_native: z.number().gt(-1000000000000000000).lt(1000000000000000000).nullable().optional(), posted_at: z.string().nullable().optional(), status: z.string().max(250).nullable().optional(), note: z.string().max(1024).nullable().optional(), bank_description: z.string().nullable().optional(), external_memo: z.string().nullable().optional(), merchant_id: z.string().nullable().optional(), merchant_name: z.string().nullable().optional(), merchant_nickname: z.string().nullable().optional(), kind: z.string().nullable().optional(), excluded: z.boolean().optional(), assigned: z.boolean().optional(), metadata: z.unknown(), created_at: z.string(), customer: z.number().int(), account: z.number().int()}); 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 }; /** * Partial Update Bank Transaction * * PATCH /ca/api/transaction/{id} */ export const patchApiCaApiTransactionId = async (args: PatchApiCaApiTransactionIdArgs, options?: PatchApiCaApiTransactionIdOptions) => { 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(/\/+$/, '')}/ca/api/transaction/${encodeURIComponent(String(args.id))}${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(bankTransaction, await res.json()) };