import {z} from 'zod' export type UpdateAccountingRecordsBody = {accounting_fields: Array<{bool_value?: (boolean | null) | undefined, date_value?: (string | null) | undefined, enum_value?: (string | null) | undefined, field_id: string, is_deleted?: (boolean | null) | undefined, number_value?: (number | null) | undefined, string_value?: (string | null) | undefined, value_id?: (string | null) | undefined}>, accounting_record_ids: Array}; export type CreateApiV3AccountingRecordsArgs = {body: UpdateAccountingRecordsBody}; export type CreateApiV3AccountingRecordsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const updateAccountingRecordsResponseOutput = z.object({results: z.array(z.union([z.object({accounting_record: z.object({amount: z.object({amount: z.number(), currency: z.string()}), billing_amount: z.object({amount: z.number(), currency: z.string()}).nullable().optional(), due_at: z.string().nullable().optional(), erp_posting_date: z.string(), id: z.string(), invoice_number: z.string().nullable().optional(), line_items: z.array(z.object({accounting_field_values: z.array(z.object({brex_field_id: z.string(), brex_field_value_id: z.string().nullable(), field_name: z.string().nullable(), field_value_name: z.string().nullable(), remote_field_id: z.string().nullable(), remote_field_value_id: z.string().nullable(), set_by: z.enum(["MANUAL", "AUTOMATED", "AGENT", "OCR", "null"]).nullable().optional(), set_by_details: z.object({mapping_id: z.string().nullable().optional(), mapping_rule_id: z.string().nullable().optional(), rule_id: z.string().nullable().optional(), type: z.enum(["CUSTOM_RULE", "MAPPING_RULE", "INTEGRATION_SETTINGS"])}).nullable().optional(), type: z.enum(["IDENTIFIER", "STRING", "BOOL", "DATE", "null"]).nullable().optional(), value: z.union([z.string().nullable(), z.boolean().nullable()]).optional()})), amortization: z.object({amortization_end_date: z.string().nullable().optional(), amortization_start_date: z.string().nullable().optional(), amortization_template: z.object({display_name: z.string(), id: z.string(), remote_id: z.string().nullable().optional()}).nullable().optional()}).nullable().optional(), amount: z.object({amount: z.number(), currency: z.string()}), billing_amount: z.object({amount: z.number(), currency: z.string()}).nullable().optional(), id: z.string(), memo: z.string().nullable().optional(), original_amount: z.object({amount: z.number(), currency: z.string()}).nullable().optional(), type: z.enum(["CREDIT", "DEBIT"])})), memo: z.string().nullable().optional(), original_amount: z.object({amount: z.number(), currency: z.string()}).nullable().optional(), posted_at: z.string(), receipts: z.array(z.object({download_uris: z.array(z.string()).optional(), id: z.string()})).nullable().optional(), review_status: z.enum(["PREPARE", "REVIEW", "EXPORTED", "READY_FOR_EXPORT"]), source_id: z.string().optional(), source_type: z.enum(["CARD", "REIMBURSEMENT", "BILL"]), status: z.enum(["APPROVED", "CANCELED", "DRAFT", "OUT_OF_POLICY", "SETTLED", "SPLIT", "SUBMITTED", "VOID", "null"]).nullable().optional(), type: z.enum(["CARD_EXPENSE_POST", "CARD_EXPENSE_REPAYMENT", "REIMBURSEMENT_EXPENSE_REPAYMENT", "REIMBURSEMENT_EXPENSE_APPROVAL", "REIMBURSEMENT_EXPENSE_MONEY_MOVEMENT_PAYOUT", "REIMBURSEMENT_EXPENSE_MONEY_MOVEMENT_PAYIN", "REIMBURSEMENT_EXPENSE_POST", "CARD_PAYMENT", "REWARD_REDEMPTION", "REIMBURSEMENT_EXPENSE_MONEY_MOVEMENT_REFUND", "CARD_EXPENSE_REFUND", "CARD_PAYMENT_REFUND", "REIMBURSEMENT_EXPENSE_MONEY_MOVEMENT_PAYOUT_REVERSAL", "INVOICE", "INVOICE_PAYMENT", "BILL_EXPENSE", "BILL_EXPENSE_PAYMENT", "BILL_EXPENSE_CARD_PAYMENT", "BILL_MONEY_MOVEMENT_PAYIN"]), updated_at: z.string(), user: z.object({account_id: z.string().nullable().optional(), brex_assistant: z.object({disabled_reason: z.enum(["ACCOUNT_DISABLED", "INHERITED_FROM_ACCOUNT_DEFAULT", "USER_DISABLED", "null"]).nullable(), experience: z.enum(["IAF", "LEGACY", "null"]).nullable(), is_enabled: z.boolean().nullable()}).nullable().optional(), department_id: z.string().nullable().optional(), department_name: z.string().nullable().optional(), email: z.string().nullable().optional(), first_name: z.string(), id: z.string(), last_name: z.string(), location_id: z.string().nullable().optional(), location_name: z.string().nullable().optional(), manager_first_name: z.string().nullable().optional(), manager_id: z.string().nullable().optional(), manager_last_name: z.string().nullable().optional(), manager_title_id: z.string().nullable().optional(), manager_title_name: z.string().nullable().optional(), metadata: z.record(z.string(), z.string()).nullable().optional(), notification_preferences: z.object({card_transactions_completed: z.object({brex_assistant: z.boolean()}).nullable(), preferred_channels: z.array(z.object({channel: z.enum(["SLACK", "SMS", "WHATSAPP", "null"]).nullable(), reply_mode: z.enum(["ONE_WAY", "TWO_WAY", "null"]).nullable()})).nullable(), text: z.enum(["OFF", "SMS", "WHATSAPP", "null"]).nullable()}).nullable().optional(), phone_number: z.string().nullable().optional(), role: z.enum(["ACCOUNT_ADMIN", "AP_CLERK", "BOOKKEEPER", "CARD_ADMIN", "EMPLOYEE", "EMPLOYEE_WITHOUT_CORP_CARDS", "EXPENSE_ADMIN", "SUPER_ADMIN", "USER_MANAGEMENT_ADMIN", "null"]).nullable().optional(), status: z.enum(["ACTIVE", "ARCHIVED", "DELETED", "DISABLED", "INACTIVE", "INVITED", "PENDING_ACTIVATION"]), title_id: z.string().nullable().optional(), title_name: z.string().nullable().optional()}).nullable().optional(), vendor: z.object({contact_name: z.string().nullable().optional(), email: z.string().nullable().optional(), id: z.string(), name: z.string(), phone: z.string().nullable().optional(), website: z.string().nullable().optional()}).nullable().optional(), version: z.number().nullable().optional()}), accounting_record_id: z.string(), status: z.literal("updated")}), z.object({accounting_record_id: z.string(), error: z.object({code: z.enum(["NOT_FOUND", "UNSUPPORTED_SOURCE_TYPE", "ITEMIZED_EXPENSE_NOT_SUPPORTED", "RECORD_NOT_EDITABLE", "FIELD_NOT_EDITABLE", "INVALID_FIELD_VALUE", "PERMISSION_DENIED", "UPDATE_FAILED", "UPDATE_OUTCOME_UNKNOWN"]), message: z.string()}), status: z.literal("failed")})]))}); 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 accounting records * * POST /v3/accounting/records * * Also accepts auth: oauth2 */ export const createApiV3AccountingRecords = async (args: CreateApiV3AccountingRecordsArgs, options?: CreateApiV3AccountingRecordsOptions) => { const {baseUrl = "https://api.brex.com", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/accounting/records${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(updateAccountingRecordsResponseOutput, await res.json()) };