UNPKG

n8n-nodes-inflow-crm

Version:

Official Inflow CRM integration for n8n. Create, update, search records and handle webhooks with dynamic field support.

80 lines (79 loc) 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.filterFieldsByApiFieldName = exports.getValidApiFieldNames = exports.loadUserOptions = void 0; const api_1 = require("../lib/api"); const logger_1 = require("../lib/logger"); // Loads user options for resource mapper using the new /api/getUserList endpoint const endpoints_1 = require("../lib/endpoints"); async function loadUserOptions() { try { logger_1.apiLogger.info('Loading users from /api/getUserList endpoint'); const response = await (0, api_1.inflowApiRequest)(this, 'GET', endpoints_1.Endpoints.GET_USER_LIST); logger_1.apiLogger.info('API Response:', JSON.stringify(response, null, 2)); let users; if (response && typeof response === 'object' && 'data' in response) { users = response.data; } else if (Array.isArray(response)) { users = response; } else { logger_1.apiLogger.warn('Unexpected response format from getUserList:', response); return []; } if (!users || !Array.isArray(users)) { logger_1.apiLogger.warn('No valid users array found'); return []; } const userOptions = users.map((user) => { const systemLabel = user.systemLabel || 'Unknown User'; const email = user.email || 'no-email'; const label = `${systemLabel} (${email})`; return { name: label, value: user.id, }; }); logger_1.apiLogger.info(`Successfully loaded ${userOptions.length} user options`); return userOptions; } catch (error) { logger_1.apiLogger.error('Failed to load user options from getUserList:', error); return []; } } exports.loadUserOptions = loadUserOptions; /** * Fetches valid API field names for a module from /api/getModuleFields. * @param moduleName The module to fetch fields for. * @param context The n8n context (IExecuteFunctions or ILoadOptionsFunctions). * @returns Array of valid apiFieldName strings. */ async function getValidApiFieldNames(moduleName, context) { const logger = context?.logger || console; try { const response = await (0, api_1.inflowApiRequest)(context, 'POST', endpoints_1.Endpoints.GET_MODULE_FIELDS, { module: moduleName }); if (response && Array.isArray(response.data)) { return response.data.map((f) => f.apiFieldName).filter((name) => typeof name === 'string' && !!name); } logger.warn('Unexpected response from getModuleFields:', response); return []; } catch (error) { logger.error('Failed to fetch valid API field names:', error); return []; } } exports.getValidApiFieldNames = getValidApiFieldNames; /** * Filters an object to only include keys present in validFieldNames. * @param fields The input fields object. * @param validFieldNames Array of allowed field names. * @returns Filtered fields object. */ function filterFieldsByApiFieldName(fields, validFieldNames) { if (!fields || typeof fields !== 'object') return {}; return Object.fromEntries(Object.entries(fields).filter(([key]) => validFieldNames.includes(key))); } exports.filterFieldsByApiFieldName = filterFieldsByApiFieldName;