From 30aa015ecaf4c06e23b4cd4daaf9db0d19d66dcd Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sun, 15 Nov 2026 18:42:13 -0800 Subject: [PATCH 1/2] fix(brex): leave a blank spend-limit buffer and transaction limit unset Saved Create Spend Limit workflows can carry null or '' for the optional buffer and transaction limit. The buffer reached Number(), which turned the blank into limit_buffer_percentage: 0, and a null transaction limit sent { amount: null } although Brex's Money.amount is a required integer. Both fields are optional and nullable on CreateSpendLimitRequest, so omit them when blank. --- .../sim/tools/brex/create_spend_limit.test.ts | 57 +++++++++++++++++++ apps/sim/tools/brex/create_spend_limit.ts | 12 +++- 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 apps/sim/tools/brex/create_spend_limit.test.ts diff --git a/apps/sim/tools/brex/create_spend_limit.test.ts b/apps/sim/tools/brex/create_spend_limit.test.ts new file mode 100644 index 00000000000..e641628e8e4 --- /dev/null +++ b/apps/sim/tools/brex/create_spend_limit.test.ts @@ -0,0 +1,57 @@ +import { describe, expect, it } from 'vitest' +import { brexCreateSpendLimitTool } from '@/tools/brex/create_spend_limit' + +const REQUIRED = { + apiKey: 'token', + name: 'Travel', + periodRecurrenceType: 'PER_MONTH', + spendType: 'BUDGET_PROVISIONED_CARDS_ONLY', + expenseVisibility: 'SHARED', + authorizationVisibility: 'PUBLIC', + limitIncreaseSetting: 'DISABLED', + autoTransferCardsSetting: 'DISABLED', + autoCreateLimitCardsSetting: 'DISABLED', + expensePolicyId: 'pol_1', + baseLimitAmount: 50000, + authorizationType: 'HARD', + rolloverRefreshRate: 'OFF', +} + +/** Builds the request body from canvas params the way a saved workflow delivers them, blanks included. */ +function buildBody(overrides: Record): Record { + const body = brexCreateSpendLimitTool.request.body + if (!body) throw new Error('create_spend_limit has no request body builder') + return body({ ...REQUIRED, ...overrides } as Parameters[0]) +} + +function authorizationSettings(body: Record): Record { + return body.authorization_settings as Record +} + +describe('brexCreateSpendLimitTool request body', () => { + it.each([null, '', ' '])( + 'omits limit_buffer_percentage when the saved value is %j instead of sending 0', + (blank) => { + const body = buildBody({ limitBufferPercentage: blank }) + expect(authorizationSettings(body)).not.toHaveProperty('limit_buffer_percentage') + } + ) + + it('sends an explicit 0 buffer the user set', () => { + const body = buildBody({ limitBufferPercentage: 0 }) + expect(authorizationSettings(body).limit_buffer_percentage).toBe(0) + }) + + it.each([null, '', ' '])( + 'omits transaction_limit when the saved amount is %j instead of sending a null Money amount', + (blank) => { + const body = buildBody({ transactionLimitAmount: blank }) + expect(body).not.toHaveProperty('transaction_limit') + } + ) + + it('sends a set transaction limit in the spend limit currency', () => { + const body = buildBody({ transactionLimitAmount: 2500, currency: 'EUR' }) + expect(body.transaction_limit).toEqual({ amount: 2500, currency: 'EUR' }) + }) +}) diff --git a/apps/sim/tools/brex/create_spend_limit.ts b/apps/sim/tools/brex/create_spend_limit.ts index a76ce63af32..4ee0db0ed98 100644 --- a/apps/sim/tools/brex/create_spend_limit.ts +++ b/apps/sim/tools/brex/create_spend_limit.ts @@ -14,6 +14,14 @@ const DELIVERY = defineBrexKeyedSite( 'a second card program of the same size would be created, doubling the spend the company has authorized' ) +/** + * True when an optional canvas value was left blank. Saved workflows can carry `null` or `''` + * for a cleared field, which `Number()` would silently turn into `0`. + */ +function isUnset(value: unknown): value is undefined | null | '' { + return value === undefined || value === null || (typeof value === 'string' && !value.trim()) +} + /** Brex accepts `limit_buffer_percentage` only as a 0-100 integer; reject anything else before the request. */ function toLimitBufferPercentage(value: number): number { const percentage = Number(value) @@ -202,7 +210,7 @@ export const brexCreateSpendLimitTool: ToolConfig< }, authorization_type: params.authorizationType, rollover_refresh_rate: params.rolloverRefreshRate, - ...(params.limitBufferPercentage !== undefined + ...(!isUnset(params.limitBufferPercentage) ? { limit_buffer_percentage: toLimitBufferPercentage(params.limitBufferPercentage) } : {}), }, @@ -211,7 +219,7 @@ export const brexCreateSpendLimitTool: ToolConfig< if (params.parentBudgetId) body.parent_budget_id = params.parentBudgetId if (params.startDate) body.start_date = params.startDate if (params.endDate) body.end_date = params.endDate - if (params.transactionLimitAmount !== undefined) { + if (!isUnset(params.transactionLimitAmount)) { body.transaction_limit = { amount: params.transactionLimitAmount, currency } } const ownerUserIds = splitBrexIdList(params.ownerUserIds) From 2ec8f91adc76af964443c9dd5d3117c7cfde7c1c Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Sun, 15 Nov 2026 18:52:07 -0800 Subject: [PATCH 2/2] test(brex): narrow the spend-limit body builder result to a JSON object --- apps/sim/tools/brex/create_spend_limit.test.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/sim/tools/brex/create_spend_limit.test.ts b/apps/sim/tools/brex/create_spend_limit.test.ts index e641628e8e4..e5f9e026651 100644 --- a/apps/sim/tools/brex/create_spend_limit.test.ts +++ b/apps/sim/tools/brex/create_spend_limit.test.ts @@ -21,7 +21,11 @@ const REQUIRED = { function buildBody(overrides: Record): Record { const body = brexCreateSpendLimitTool.request.body if (!body) throw new Error('create_spend_limit has no request body builder') - return body({ ...REQUIRED, ...overrides } as Parameters[0]) + const result = body({ ...REQUIRED, ...overrides } as Parameters[0]) + if (typeof result !== 'object' || result instanceof FormData) { + throw new Error('create_spend_limit must build a JSON object body') + } + return result } function authorizationSettings(body: Record): Record {