Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions apps/sim/tools/brex/create_spend_limit.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>): Record<string, unknown> {
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<typeof body>[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<string, unknown>): Record<string, unknown> {
return body.authorization_settings as Record<string, unknown>
}

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' })
})
})
12 changes: 10 additions & 2 deletions apps/sim/tools/brex/create_spend_limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) }
: {}),
},
Expand All @@ -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)
Expand Down
Loading