UNPKG

n8n-nodes-bitrix

Version:
81 lines 3.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getEntityFields = getEntityFields; exports.getEntityTypes = getEntityTypes; const n8n_workflow_1 = require("n8n-workflow"); const GenericFunctions_1 = require("../GenericFunctions"); async function getEntityFields() { var _a; const resource = this.getNodeParameter('resource', 0); const endpoint = `${resource}.fields`; let body = {}; try { if (resource === 'crm.item') { const entityTypeId = this.getNodeParameter('entityTypeId', 0); if (!entityTypeId) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Entity Type ID is required for crm.item', { description: 'Please select entity type first' }); } body.entityTypeId = entityTypeId; body.useOriginalUfNames = this.getNodeParameter('useOriginalUfNames', false) ? 'Y' : 'N'; } const responseData = await GenericFunctions_1.bitrixApiRequest.call(this, 'POST', endpoint, body); if (!(responseData === null || responseData === void 0 ? void 0 : responseData.result) || typeof responseData.result !== 'object') { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No valid fields data returned'); } const fieldsData = resource === 'crm.item' ? ((_a = responseData.result) === null || _a === void 0 ? void 0 : _a.fields) || {} : responseData.result; if (!Object.keys(fieldsData).length) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No fields available for selected entity'); } return Object.entries(fieldsData).map(([fieldName, field]) => { const fieldType = field.type || 'string'; const displayName = (field === null || field === void 0 ? void 0 : field.title) && field.title !== fieldName ? `${field.title} (${fieldName})` : fieldName; const descriptionParts = [ `Type: ${fieldType}`, ...(field.isRequired ? ['Required'] : []), ...(field.isReadOnly ? ['Read-only'] : []), ...(field.isDynamic ? ['Dynamic'] : []), ...(field.isMultiple ? ['Multiple'] : []) ]; return { name: displayName, value: fieldName, description: descriptionParts.join(' | '), action: field.type }; }); } catch (error) { throw new Error(`Bitrix24 Error: ${error.message}`); } } ; async function getEntityTypes() { return [ { name: 'Lead', value: '1' }, { name: 'Deal', value: '2' }, { name: 'Contact', value: '3' }, { name: 'Company', value: '4' }, { name: 'Invoice', value: '31' }, { name: 'Quote', value: '32' }, ...(await getCustomEntityTypes.call(this)) ]; } ; async function getCustomEntityTypes() { try { const types = await GenericFunctions_1.bitrixApiRequest.call(this, 'GET', 'crm.type.list'); return types.result.map((type) => ({ name: type.NAME, value: type.ENTITY_TYPE_ID })); } catch (error) { return []; } } ; //# sourceMappingURL=index.js.map