import {z} from 'zod' export type BankTransaction = {id: number, is_deleted: boolean, deleted_at: string | null, account_name: string, entity_name: string | null, date_month: string | null, date_year: string | null, journal: number | null, journal_order: string | null, intercompany_journal: number | null, reconciliation_report: number, reconciliation_report_ending_date: string, last_modified_at: string, 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, customer: number, account: number}; export type UpdateApiCaApiTransactionIdArgs = {id: number, body: BankTransaction}; export type UpdateApiCaApiTransactionIdOptions = {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 }; /** * Update Bank Transaction * * PUT /ca/api/transaction/{id} */ export const updateApiCaApiTransactionId = async (args: UpdateApiCaApiTransactionIdArgs, options?: UpdateApiCaApiTransactionIdOptions) => { 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: "PUT", 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()) };