n8n-nodes-inflow-crm
Version:
Official Inflow CRM integration for n8n. Create, update, search records and handle webhooks with dynamic field support.
130 lines (129 loc) • 5.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeGetByIdsAction = void 0;
const api_1 = require("../../lib/api");
const endpoints_1 = require("../../lib/endpoints");
const logger_1 = require("../../lib/logger");
/**
* Parses record IDs from comma-separated string or array format
*/
function parseRecordIds(recordIds) {
if (Array.isArray(recordIds)) {
return recordIds.filter(id => id && typeof id === 'string' && id.trim().length > 0);
}
if (typeof recordIds === 'string') {
return recordIds
.split(',')
.map(id => id.trim())
.filter(id => id.length > 0);
}
return [];
}
async function executeGetByIdsAction(itemIndex, moduleName, recordIds, options = {}) {
// Context and parameter validation
if (!this || typeof this.getNodeParameter !== 'function') {
throw new Error('Context error: IExecuteFunctions context is required');
}
if (!moduleName || typeof moduleName !== 'string') {
throw new Error('module name is required');
}
if (!recordIds) {
throw new Error('recordIds parameter is required');
}
logger_1.actionsLogger.info(`Getting records by IDs in module: ${moduleName}`);
logger_1.actionsLogger.info('Record IDs input:', recordIds);
logger_1.actionsLogger.info('Options:', JSON.stringify(options, null, 2));
try {
// Parse and validate IDs first
const parsedIds = parseRecordIds(recordIds);
// Check if user provided a string with spaces but no commas (common mistake)
// Only check this after parsing to avoid false positives with whitespace-only strings
if (typeof recordIds === 'string' && parsedIds.length === 0 &&
recordIds.trim().length > 0 && recordIds.includes(' ') && !recordIds.includes(',')) {
throw new Error('Multiple IDs must be separated by commas. Example: "id1, id2, id3" or "id1,id2,id3"');
}
if (parsedIds.length === 0) {
logger_1.actionsLogger.warn('No valid record IDs provided');
return [{
json: {
success: true,
count: 0,
data: [],
meta: { page: 1, limit: 50, total: 0 }
},
pairedItem: { item: itemIndex }
}];
}
logger_1.actionsLogger.info('Parsed IDs:', parsedIds);
// Validate pagination parameters
const limit = options.limit ?? 50;
if (limit > 200) {
throw new Error('The maximum allowed value for "limit" is 200. Please set a value between 1 and 200.');
}
if (limit < 1) {
throw new Error('The minimum allowed value for "limit" is 1. Please set a value between 1 and 200.');
}
const page = options.page ?? 1;
if (page < 1) {
throw new Error('Page number must be 1 or greater.');
}
// Build request body
const requestBody = {
module: moduleName,
ids: parsedIds,
page,
limit,
};
// Add sorting if specified
if (options.sortField && options.sortField.trim()) {
requestBody.sort = {
[options.sortField.trim()]: options.sortDirection === 'desc' ? -1 : 1,
};
}
logger_1.actionsLogger.info('Request body:', JSON.stringify(requestBody, null, 2));
let response;
try {
response = await (0, api_1.inflowApiRequest)(this, 'POST', endpoints_1.Endpoints.GET_BY_IDS, requestBody);
}
catch (apiError) {
logger_1.actionsLogger.error('API request failed with error:', apiError);
if (apiError?.error && apiError.error.code) {
throw new Error(`[${apiError.error.code}] ${apiError.error.message}`);
}
throw apiError;
}
logger_1.actionsLogger.info('API Response:', JSON.stringify(response, null, 2));
// Expect { data: [...], meta: { page, limit, total } }
if (response && typeof response === 'object' && 'data' in response) {
const { data, meta } = response;
if (!Array.isArray(data)) {
logger_1.actionsLogger.warn('GetByIds returned non-array data for', moduleName);
return [{
json: {
success: true,
count: 0,
data: [],
meta: meta || { page, limit, total: 0 }
},
pairedItem: { item: itemIndex }
}];
}
// Return records with metadata
// Note: API handles mixed valid/invalid IDs by returning only existing records
return data.map(record => ({
json: { ...record, meta },
pairedItem: { item: itemIndex }
}));
}
logger_1.actionsLogger.error('Unexpected API response format:', response);
throw new Error('API returned unexpected response format');
}
catch (error) {
logger_1.actionsLogger.error('Error getting records by IDs:', error);
if (error?.error && error.error.code) {
throw new Error(`[${error.error.code}] ${error.error.message}`);
}
throw new Error(`Failed to get records by IDs in "${moduleName}": ${error.message || 'unknown error'}`);
}
}
exports.executeGetByIdsAction = executeGetByIdsAction;