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..e5f9e026651 --- /dev/null +++ b/apps/sim/tools/brex/create_spend_limit.test.ts @@ -0,0 +1,61 @@ +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') + 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 { + 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)