Skip to content

Commit 59179ba

Browse files
committed
fix(square): single-location invoice search and guard numeric coercion
- SearchInvoices: Square's invoice filter accepts only one location, so take a single locationId (string) instead of an array and wrap it as query.filter.location_ids: [locationId] - Block: fail locally with a clear "<field> must be a valid number" error when amount/limit/version/orderVersion are non-numeric instead of forwarding NaN
1 parent 4c7ea07 commit 59179ba

4 files changed

Lines changed: 37 additions & 15 deletions

File tree

apps/docs/content/docs/en/integrations/square.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ Search invoices across one or more locations
11291129
| Parameter | Type | Required | Description |
11301130
| --------- | ---- | -------- | ----------- |
11311131
| `apiKey` | string | Yes | Square access token \(personal access token\) |
1132-
| `locationIds` | array | Yes | Array of location IDs to search within |
1132+
| `locationId` | string | Yes | ID of the location to search within \(Square allows one location per search\) |
11331133
| `limit` | number | No | Maximum number of results to return per page |
11341134
| `cursor` | string | No | Pagination cursor from a previous response |
11351135

apps/sim/blocks/blocks/square.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,9 @@ export const SquareBlock: BlockConfig<SquareResponse> = {
381381
placeholder: '["L123", "L456"]',
382382
condition: {
383383
field: 'operation',
384-
value: ['search_orders', 'search_invoices', 'batch_retrieve_inventory_counts'],
384+
value: ['search_orders', 'batch_retrieve_inventory_counts'],
385385
},
386-
required: { field: 'operation', value: ['search_orders', 'search_invoices'] },
386+
required: { field: 'operation', value: 'search_orders' },
387387
},
388388

389389
// Invoices
@@ -563,9 +563,16 @@ export const SquareBlock: BlockConfig<SquareResponse> = {
563563
placeholder: 'Square location ID',
564564
condition: {
565565
field: 'operation',
566-
value: ['create_payment', 'list_payments', 'list_invoices', 'list_refunds', 'get_location'],
566+
value: [
567+
'create_payment',
568+
'list_payments',
569+
'list_invoices',
570+
'search_invoices',
571+
'list_refunds',
572+
'get_location',
573+
],
567574
},
568-
required: { field: 'operation', value: ['list_invoices', 'get_location'] },
575+
required: { field: 'operation', value: ['list_invoices', 'search_invoices', 'get_location'] },
569576
},
570577
{
571578
id: 'limit',
@@ -725,6 +732,22 @@ export const SquareBlock: BlockConfig<SquareResponse> = {
725732
const parsedCatalogObjectIds = parseJsonField(catalogObjectIds, 'catalogObjectIds')
726733
const parsedStates = parseJsonField(states, 'states')
727734

735+
// Coerce a numeric input, failing locally with a clear message rather than
736+
// forwarding NaN to Square when the value is non-numeric.
737+
const coerceNumber = (value: unknown, field: string): number | undefined => {
738+
if (value === undefined || value === null || value === '') return undefined
739+
const num = Number(value)
740+
if (!Number.isFinite(num)) {
741+
throw new Error(`"${field}" must be a valid number`)
742+
}
743+
return num
744+
}
745+
746+
const coercedAmount = coerceNumber(amount, 'amount')
747+
const coercedLimit = coerceNumber(limit, 'limit')
748+
const coercedVersion = coerceNumber(version, 'version')
749+
const coercedOrderVersion = coerceNumber(orderVersion, 'orderVersion')
750+
728751
return {
729752
...rest,
730753
...(normalizedFile && { file: normalizedFile }),
@@ -738,11 +761,10 @@ export const SquareBlock: BlockConfig<SquareResponse> = {
738761
...(parsedPaymentIds !== undefined && { paymentIds: parsedPaymentIds }),
739762
...(parsedCatalogObjectIds !== undefined && { catalogObjectIds: parsedCatalogObjectIds }),
740763
...(parsedStates !== undefined && { states: parsedStates }),
741-
...(amount !== undefined && amount !== '' && { amount: Number(amount) }),
742-
...(limit !== undefined && limit !== '' && { limit: Number(limit) }),
743-
...(version !== undefined && version !== '' && { version: Number(version) }),
744-
...(orderVersion !== undefined &&
745-
orderVersion !== '' && { orderVersion: Number(orderVersion) }),
764+
...(coercedAmount !== undefined && { amount: coercedAmount }),
765+
...(coercedLimit !== undefined && { limit: coercedLimit }),
766+
...(coercedVersion !== undefined && { version: coercedVersion }),
767+
...(coercedOrderVersion !== undefined && { orderVersion: coercedOrderVersion }),
746768
...(autocomplete !== undefined && { autocomplete: autocomplete === 'true' }),
747769
...(includeRelatedObjects !== undefined && {
748770
includeRelatedObjects: includeRelatedObjects === 'true',

apps/sim/tools/square/search_invoices.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ export const squareSearchInvoicesTool: ToolConfig<SearchInvoicesParams, InvoiceL
2222
visibility: 'user-only',
2323
description: 'Square access token (personal access token)',
2424
},
25-
locationIds: {
26-
type: 'array',
25+
locationId: {
26+
type: 'string',
2727
required: true,
2828
visibility: 'user-or-llm',
29-
description: 'Array of location IDs to search within',
29+
description: 'ID of the location to search within (Square allows one location per search)',
3030
},
3131
limit: {
3232
type: 'number',
@@ -49,7 +49,7 @@ export const squareSearchInvoicesTool: ToolConfig<SearchInvoicesParams, InvoiceL
4949
body: (params) => {
5050
const body: Record<string, unknown> = {
5151
query: {
52-
filter: { location_ids: params.locationIds },
52+
filter: { location_ids: [params.locationId] },
5353
},
5454
}
5555
if (params.limit !== undefined) body.limit = params.limit

apps/sim/tools/square/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ export interface PayOrderParams {
892892

893893
export interface SearchInvoicesParams {
894894
apiKey: string
895-
locationIds: string[]
895+
locationId: string
896896
limit?: number
897897
cursor?: string
898898
}

0 commit comments

Comments
 (0)