UNPKG

n8n-nodes-smartsuite

Version:
77 lines 3.67 kB
"use strict"; // src/nodes/SmartSuite/actions/record/searchRecord.operation.ts Object.defineProperty(exports, "__esModule", { value: true }); exports.execute = execute; const n8n_workflow_1 = require("n8n-workflow"); const utils_1 = require("../../helpers/utils"); const smartSuiteApi_1 = require("../../transport/smartSuiteApi"); const validation_1 = require("../../helpers/validation"); async function execute() { // Read parameters const returnAll = this.getNodeParameter('returnAll', 0); const limit = this.getNodeParameter('limit', 0, 50); const searchOp = this.getNodeParameter('searchOperator', 0).toLowerCase(); const hydrated = this.getNodeParameter('hydrated', 0); const tableId = await validation_1.getTableId.call(this, 0); // Build filters const uiFilters = this.getNodeParameter('filters.filter', 0, []); const apiFilters = uiFilters.map(f => ({ field: (0, utils_1.asIdString)(f.field), comparison: f.condition, value: f.value, })); let recordsArray = []; try { // First page const body = { hydrated, offset: 0, limit }; if (apiFilters.length) { body.filter = { operator: searchOp, fields: apiFilters }; } (0, utils_1.debugLog)('[Record] apiRequest →', { method: 'POST', endpoint: `/tables/${tableId}/records/list/`, body }, 3); const resp = (await smartSuiteApi_1.apiRequest.call(this, 'POST', `/tables/${tableId}/records/list/`, body)); (0, utils_1.debugLog)('[Record] apiResponse ←', resp, 3); // Normalize recordsArray = Array.isArray(resp) ? resp : resp.data ?? resp.items ?? resp.results ?? []; // If Return All is on, page through if (returnAll) { let offset = recordsArray.length; while (recordsArray.length % limit === 0) { const nextBody = { hydrated, offset, limit, filter: body.filter }; (0, utils_1.debugLog)('[Record] apiRequest (next page) →', { nextBody }, 3); const nextResp = (await smartSuiteApi_1.apiRequest.call(this, 'POST', `/tables/${tableId}/records/list/`, nextBody)); (0, utils_1.debugLog)('[Record] apiResponse (next page) ←', nextResp, 3); const pageItems = Array.isArray(nextResp) ? nextResp : nextResp.data ?? nextResp.items ?? nextResp.results ?? []; if (!pageItems.length) { break; } recordsArray.push(...pageItems); offset += pageItems.length; } } } catch (error) { (0, utils_1.debugLog)('[Record] caught error', error, 3); if (error.statusCode === 400 && error.message?.toLowerCase().includes('not allowed comparison')) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid comparison for that field type.', { description: '[See valid operators](https://developers.smartsuite.com/docs/solution-data/records/sort-filter#operators-by-field-type)', }); } throw new n8n_workflow_1.NodeOperationError(this.getNode(), error.message); } // Enforce client-side limit if (!returnAll && recordsArray.length > limit) { recordsArray = recordsArray.slice(0, limit); } // If no records found if (recordsArray.length === 0) { return this.helpers.returnJsonArray([{ search: 'No records match' }]); } // Emit records return this.helpers.returnJsonArray(recordsArray); } //# sourceMappingURL=searchRecord.operation.js.map