UNPKG

@tuanltntu/n8n-nodes-bitrix24

Version:

Comprehensive n8n community node for Bitrix24 API integration with CRM, Tasks, Chat, Telephony, and more

442 lines 20 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EntityResourceHandler = void 0; const n8n_workflow_1 = require("n8n-workflow"); const ResourceHandlerBase_1 = require("./ResourceHandlerBase"); const GenericFunctions_1 = require("../GenericFunctions"); /** * Handles Entity operations in Bitrix24 * Based on: https://apidocs.bitrix24.com/api-reference/entity/index.html */ class EntityResourceHandler extends ResourceHandlerBase_1.ResourceHandlerBase { constructor(executeFunctions, returnData, options = {}) { super(executeFunctions, returnData, options); this.resourceEndpoints = { // Entity operations get: "entity.get", add: "entity.add", update: "entity.update", delete: "entity.delete", getRights: "entity.rights", // Entity section operations getSections: "entity.section.get", addSection: "entity.section.add", updateSection: "entity.section.update", deleteSection: "entity.section.delete", // Entity item operations getItem: "entity.item.get", addItem: "entity.item.add", updateItem: "entity.item.update", deleteItem: "entity.item.delete", // Entity item property operations getItemProperties: "entity.item.property.get", addItemProperty: "entity.item.property.add", updateItemProperty: "entity.item.property.update", deleteItemProperty: "entity.item.property.delete", }; } /** * Process Entity operations */ async process() { for (let i = 0; i < this.items.length; i++) { try { const operation = this.getNodeParameter("operation", i); if (!this.resourceEndpoints[operation]) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `The operation "${operation}" is not supported for Entity resource`, { itemIndex: i }); } const endpoint = this.resourceEndpoints[operation]; let responseData; switch (operation) { case "get": responseData = await this.handleGet(endpoint, i); break; case "add": responseData = await this.handleAdd(endpoint, i); break; case "update": responseData = await this.handleUpdate(endpoint, i); break; case "delete": responseData = await this.handleDelete(endpoint, i); break; case "getRights": responseData = await this.handleGetRights(endpoint, i); break; case "getSections": responseData = await this.handleGetSections(endpoint, i); break; case "addSection": responseData = await this.handleAddSection(endpoint, i); break; case "updateSection": responseData = await this.handleUpdateSection(endpoint, i); break; case "deleteSection": responseData = await this.handleDeleteSection(endpoint, i); break; case "getItem": responseData = await this.handleGetItem(endpoint, i); break; case "addItem": responseData = await this.handleAddItem(endpoint, i); break; case "updateItem": responseData = await this.handleUpdateItem(endpoint, i); break; case "deleteItem": responseData = await this.handleDeleteItem(endpoint, i); break; case "getItemProperties": responseData = await this.handleGetItemProperties(endpoint, i); break; case "addItemProperty": responseData = await this.handleAddItemProperty(endpoint, i); break; case "updateItemProperty": responseData = await this.handleUpdateItemProperty(endpoint, i); break; case "deleteItemProperty": responseData = await this.handleDeleteItemProperty(endpoint, i); break; default: throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `The operation "${operation}" is not supported`, { itemIndex: i }); } this.addResponseToReturnData(responseData, i); } catch (error) { if (this.continueOnFail()) { this.addErrorToReturnData(error, i); } else { throw error; } } } return this.returnData; } /** * Handle get entity operation */ async handleGet(endpoint, itemIndex) { const entityId = this.getNodeParameter("entityId", itemIndex); return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, { id: entityId }); } /** * Handle add entity operation */ async handleAdd(endpoint, itemIndex) { const entityData = { name: this.getNodeParameter("entityName", itemIndex), code: this.getNodeParameter("entityCode", itemIndex), }; // Add optional fields if (this.getNodeParameter("entityActive", itemIndex) !== undefined) { entityData.active = this.getNodeParameter("entityActive", itemIndex); } if (this.getNodeParameter("entitySort", itemIndex)) { entityData.sort = this.getNodeParameter("entitySort", itemIndex); } if (this.getNodeParameter("entityDescription", itemIndex)) { entityData.description = this.getNodeParameter("entityDescription", itemIndex); } return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, entityData); } /** * Handle update entity operation */ async handleUpdate(endpoint, itemIndex) { const entityId = this.getNodeParameter("entityId", itemIndex); const entityData = { id: entityId, }; // Add fields to update if (this.getNodeParameter("entityName", itemIndex)) { entityData.name = this.getNodeParameter("entityName", itemIndex); } if (this.getNodeParameter("entityCode", itemIndex)) { entityData.code = this.getNodeParameter("entityCode", itemIndex); } if (this.getNodeParameter("entityActive", itemIndex) !== undefined) { entityData.active = this.getNodeParameter("entityActive", itemIndex); } if (this.getNodeParameter("entitySort", itemIndex)) { entityData.sort = this.getNodeParameter("entitySort", itemIndex); } if (this.getNodeParameter("entityDescription", itemIndex)) { entityData.description = this.getNodeParameter("entityDescription", itemIndex); } return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, entityData); } /** * Handle delete entity operation */ async handleDelete(endpoint, itemIndex) { const entityId = this.getNodeParameter("entityId", itemIndex); return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, { id: entityId }); } /** * Handle get entity item operation */ async handleGetItem(endpoint, itemIndex) { const entityId = this.getNodeParameter("entityId", itemIndex); const itemId = this.getNodeParameter("itemId", itemIndex); return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, { entityId: entityId, id: itemId, }); } /** * Handle add entity item operation */ async handleAddItem(endpoint, itemIndex) { const entityId = this.getNodeParameter("entityId", itemIndex); const itemData = { entityId: entityId, name: this.getNodeParameter("itemName", itemIndex), }; // Add optional fields if (this.getNodeParameter("itemActive", itemIndex) !== undefined) { itemData.active = this.getNodeParameter("itemActive", itemIndex); } if (this.getNodeParameter("itemSort", itemIndex)) { itemData.sort = this.getNodeParameter("itemSort", itemIndex); } if (this.getNodeParameter("itemDescription", itemIndex)) { itemData.description = this.getNodeParameter("itemDescription", itemIndex); } // Add properties const properties = this.getNodeParameter("itemProperties", itemIndex, {}); if (Object.keys(properties).length > 0) { Object.assign(itemData, properties); } return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, itemData); } /** * Handle update entity item operation */ async handleUpdateItem(endpoint, itemIndex) { const entityId = this.getNodeParameter("entityId", itemIndex); const itemId = this.getNodeParameter("itemId", itemIndex); const itemData = { entityId: entityId, id: itemId, }; // Add fields to update if (this.getNodeParameter("itemName", itemIndex)) { itemData.name = this.getNodeParameter("itemName", itemIndex); } if (this.getNodeParameter("itemActive", itemIndex) !== undefined) { itemData.active = this.getNodeParameter("itemActive", itemIndex); } if (this.getNodeParameter("itemSort", itemIndex)) { itemData.sort = this.getNodeParameter("itemSort", itemIndex); } if (this.getNodeParameter("itemDescription", itemIndex)) { itemData.description = this.getNodeParameter("itemDescription", itemIndex); } // Add properties const properties = this.getNodeParameter("itemProperties", itemIndex, {}); if (Object.keys(properties).length > 0) { Object.assign(itemData, properties); } return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, itemData); } /** * Handle delete entity item operation */ async handleDeleteItem(endpoint, itemIndex) { const entityId = this.getNodeParameter("entityId", itemIndex); const itemId = this.getNodeParameter("itemId", itemIndex); return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, { entityId: entityId, id: itemId, }); } /** * Get all items with pagination */ async getAllItems(endpoint, qs, maxPages) { const allItems = []; let currentPage = 1; let hasMore = true; while (hasMore && currentPage <= maxPages) { const response = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, {}, { ...qs, start: (currentPage - 1) * 50 }); if (response.result && Array.isArray(response.result)) { allItems.push(...response.result); } // Check if there are more pages hasMore = response.next !== undefined; currentPage++; } return { result: allItems, total: allItems.length, }; } /** * Handle get entity rights operation */ async handleGetRights(endpoint, itemIndex) { const entityId = this.getNodeParameter("entityId", itemIndex); return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, { entityId: entityId }); } /** * Handle get entity sections operation */ async handleGetSections(endpoint, itemIndex) { const entityId = this.getNodeParameter("entityId", itemIndex); return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, { entityId: entityId }); } /** * Handle add entity section operation */ async handleAddSection(endpoint, itemIndex) { const entityId = this.getNodeParameter("entityId", itemIndex); const sectionData = { entityId: entityId, name: this.getNodeParameter("sectionName", itemIndex), code: this.getNodeParameter("sectionCode", itemIndex), }; // Add optional fields if (this.getNodeParameter("sectionActive", itemIndex) !== undefined) { sectionData.active = this.getNodeParameter("sectionActive", itemIndex); } if (this.getNodeParameter("sectionSort", itemIndex)) { sectionData.sort = this.getNodeParameter("sectionSort", itemIndex); } if (this.getNodeParameter("sectionDescription", itemIndex)) { sectionData.description = this.getNodeParameter("sectionDescription", itemIndex); } return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, sectionData); } /** * Handle update entity section operation */ async handleUpdateSection(endpoint, itemIndex) { const entityId = this.getNodeParameter("entityId", itemIndex); const sectionId = this.getNodeParameter("sectionId", itemIndex); const sectionData = { entityId: entityId, id: sectionId, }; // Add fields to update if (this.getNodeParameter("sectionName", itemIndex)) { sectionData.name = this.getNodeParameter("sectionName", itemIndex); } if (this.getNodeParameter("sectionCode", itemIndex)) { sectionData.code = this.getNodeParameter("sectionCode", itemIndex); } if (this.getNodeParameter("sectionActive", itemIndex) !== undefined) { sectionData.active = this.getNodeParameter("sectionActive", itemIndex); } if (this.getNodeParameter("sectionSort", itemIndex)) { sectionData.sort = this.getNodeParameter("sectionSort", itemIndex); } if (this.getNodeParameter("sectionDescription", itemIndex)) { sectionData.description = this.getNodeParameter("sectionDescription", itemIndex); } return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, sectionData); } /** * Handle delete entity section operation */ async handleDeleteSection(endpoint, itemIndex) { const entityId = this.getNodeParameter("entityId", itemIndex); const sectionId = this.getNodeParameter("sectionId", itemIndex); return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, { entityId: entityId, id: sectionId, }); } /** * Handle get entity item properties operation */ async handleGetItemProperties(endpoint, itemIndex) { const entityId = this.getNodeParameter("entityId", itemIndex); return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, { entityId: entityId }); } /** * Handle add entity item property operation */ async handleAddItemProperty(endpoint, itemIndex) { const entityId = this.getNodeParameter("entityId", itemIndex); const propertyData = { entityId: entityId, name: this.getNodeParameter("propertyName", itemIndex), code: this.getNodeParameter("propertyCode", itemIndex), type: this.getNodeParameter("propertyType", itemIndex), }; // Add optional fields if (this.getNodeParameter("propertyRequired", itemIndex) !== undefined) { propertyData.required = this.getNodeParameter("propertyRequired", itemIndex); } if (this.getNodeParameter("propertySort", itemIndex)) { propertyData.sort = this.getNodeParameter("propertySort", itemIndex); } if (this.getNodeParameter("propertyMultiple", itemIndex) !== undefined) { propertyData.multiple = this.getNodeParameter("propertyMultiple", itemIndex); } if (this.getNodeParameter("propertyActive", itemIndex) !== undefined) { propertyData.active = this.getNodeParameter("propertyActive", itemIndex); } // Add property values for list type const propertyValues = this.getNodeParameter("propertyValues", itemIndex, {}); if (Object.keys(propertyValues).length > 0) { Object.assign(propertyData, propertyValues); } return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, propertyData); } /** * Handle update entity item property operation */ async handleUpdateItemProperty(endpoint, itemIndex) { const entityId = this.getNodeParameter("entityId", itemIndex); const propertyId = this.getNodeParameter("propertyId", itemIndex); const propertyData = { entityId: entityId, id: propertyId, }; // Add fields to update if (this.getNodeParameter("propertyName", itemIndex)) { propertyData.name = this.getNodeParameter("propertyName", itemIndex); } if (this.getNodeParameter("propertyCode", itemIndex)) { propertyData.code = this.getNodeParameter("propertyCode", itemIndex); } if (this.getNodeParameter("propertyType", itemIndex)) { propertyData.type = this.getNodeParameter("propertyType", itemIndex); } if (this.getNodeParameter("propertyRequired", itemIndex) !== undefined) { propertyData.required = this.getNodeParameter("propertyRequired", itemIndex); } if (this.getNodeParameter("propertySort", itemIndex)) { propertyData.sort = this.getNodeParameter("propertySort", itemIndex); } if (this.getNodeParameter("propertyMultiple", itemIndex) !== undefined) { propertyData.multiple = this.getNodeParameter("propertyMultiple", itemIndex); } if (this.getNodeParameter("propertyActive", itemIndex) !== undefined) { propertyData.active = this.getNodeParameter("propertyActive", itemIndex); } // Add property values for list type const propertyValues = this.getNodeParameter("propertyValues", itemIndex, {}); if (Object.keys(propertyValues).length > 0) { Object.assign(propertyData, propertyValues); } return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, propertyData); } /** * Handle delete entity item property operation */ async handleDeleteItemProperty(endpoint, itemIndex) { const entityId = this.getNodeParameter("entityId", itemIndex); const propertyId = this.getNodeParameter("propertyId", itemIndex); return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, { entityId: entityId, id: propertyId, }); } } exports.EntityResourceHandler = EntityResourceHandler; //# sourceMappingURL=EntityResourceHandler.js.map