UNPKG

@tuanltntu/n8n-nodes-bitrix24

Version:

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

765 lines 32.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ListsResourceHandler = void 0; const n8n_workflow_1 = require("n8n-workflow"); const ResourceHandlerBase_1 = require("./ResourceHandlerBase"); const GenericFunctions_1 = require("../GenericFunctions"); /** * Handle Bitrix24 Lists operations */ class ListsResourceHandler extends ResourceHandlerBase_1.ResourceHandlerBase { constructor(executeFunctions, returnData, options = {}) { super(executeFunctions, returnData, options); this.resourceEndpoints = { add: "lists.add", delete: "lists.delete", get: "lists.get", update: "lists.update", getIblockTypeId: "lists.get.iblock.type.id", addField: "lists.field.add", deleteField: "lists.field.delete", getFields: "lists.field.get", getFieldTypes: "lists.field.type.get", updateField: "lists.field.update", getElementFields: "lists.element.field.get", getElements: "lists.element.get", getElement: "lists.element.get", addElement: "lists.element.add", updateElement: "lists.element.update", deleteElement: "lists.element.delete", getElementFileUrl: "lists.element.get.file.url", getSectionElement: "lists.section.element.get", getElementFile: "lists.element.file.get", getSections: "lists.section.get", addSection: "lists.section.add", getSection: "lists.section.get", updateSection: "lists.section.update", deleteSection: "lists.section.delete", }; } /** * Process Lists operations */ async process() { for (let i = 0; i < this.items.length; i++) { try { const operation = this.getNodeParameter("operation", i); switch (operation) { case "add": await this.handleAdd(i); break; case "delete": await this.handleDelete(i); break; case "get": await this.handleGet(i); break; case "update": await this.handleUpdate(i); break; case "getIblockTypeId": await this.handleGetIblockTypeId(i); break; case "addField": await this.handleAddField(i); break; case "deleteField": await this.handleDeleteField(i); break; case "getFields": await this.handleGetFields(i); break; case "getFieldTypes": await this.handleGetFieldTypes(i); break; case "updateField": await this.handleUpdateField(i); break; case "getElementFields": await this.handleGetElementFields(i); break; case "getElements": await this.handleGetElements(i); break; case "getElement": await this.handleGetElement(i); break; case "addElement": await this.handleAddElement(i); break; case "updateElement": await this.handleUpdateElement(i); break; case "deleteElement": await this.handleDeleteElement(i); break; case "getElementFileUrl": await this.handleGetElementFileUrl(i); break; case "getSectionElement": await this.handleGetSectionElement(i); break; case "getElementFile": await this.handleGetElementFile(i); break; case "getSections": await this.handleGetSections(i); break; case "addSection": await this.handleAddSection(i); break; case "getSection": await this.handleGetSection(i); break; case "updateSection": await this.handleUpdateSection(i); break; case "deleteSection": await this.handleDeleteSection(i); break; default: throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `Unsupported operation "${operation}" for Lists resource`, { itemIndex: i }); } } catch (error) { if (this.executeFunctions.continueOnFail()) { this.returnData.push({ json: { error: error.message } }); continue; } throw error; } } return this.returnData; } /** * Get endpoint for the specified operation */ getEndpoint(operation) { const endpoint = this.resourceEndpoints[operation]; if (!endpoint) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `Unsupported operation "${operation}" for Lists resource`); } return endpoint; } /** * Handle adding a new list */ async handleAdd(itemIndex) { // Lấy các tham số bắt buộc const iblockTypeId = this.getNodeParameter("IBLOCK_TYPE_ID", itemIndex); const iblockCode = this.getNodeParameter("IBLOCK_CODE", itemIndex); // Lấy tham số SOCNET_GROUP_ID (nếu có) const socnetGroupId = this.getNodeParameter("SOCNET_GROUP_ID", itemIndex, ""); // Xây dựng đối tượng FIELDS từ fixedCollection const fieldsUi = this.getNodeParameter("fieldsUi", itemIndex, { fieldsValues: [ { fieldName: "NAME", fieldValue: "New List", }, ], }); const fieldsValues = fieldsUi.fieldsValues || []; const fields = {}; // Chuyển đổi từ mảng sang object key-value for (const field of fieldsValues) { const fieldName = field.fieldName; const fieldValue = field.fieldValue; if (fieldName && fieldValue !== undefined) { // Chuyển đổi BIZPROC từ chuỗi sang Y/N if (fieldName === "BIZPROC") { fields[fieldName] = fieldValue === "true" || fieldValue === "Y" || fieldValue === "y" || fieldValue === "yes" ? "Y" : "N"; } else if (fieldName === "SORT") { // Chuyển đổi sang số nếu là SORT fields[fieldName] = parseInt(fieldValue, 10) || 500; } else { fields[fieldName] = fieldValue; } } } // Đảm bảo trường NAME luôn có giá trị if (!fields.NAME || fields.NAME === "") { fields.NAME = "New List"; } // Xây dựng đối tượng MESSAGES từ form const messagesUi = this.getNodeParameter("messagesUi", itemIndex, {}); // Xây dựng đối tượng RIGHTS từ form const rightsUi = this.getNodeParameter("rightsUi", itemIndex, { rightsValues: [], }); const rightsValues = rightsUi.rightsValues || []; const rights = {}; for (const right of rightsValues) { const code = right.code; const permission = right.permission; if (code && permission) { rights[code] = permission; } } // Xây dựng body request const body = { IBLOCK_TYPE_ID: iblockTypeId, IBLOCK_CODE: iblockCode, FIELDS: fields, }; // Thêm SOCNET_GROUP_ID nếu có if (socnetGroupId) { body.SOCNET_GROUP_ID = socnetGroupId; } // Thêm MESSAGES nếu có if (Object.keys(messagesUi).length > 0) { body.MESSAGES = messagesUi; } // Thêm RIGHTS nếu có if (Object.keys(rights).length > 0) { body.RIGHTS = rights; } const endpoint = this.getEndpoint("add"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, body, {}, // empty query params itemIndex); this.returnData.push({ json: responseData }); } /** * Handle deleting a list */ async handleDelete(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); const params = { IBLOCK_ID: listId, }; const endpoint = this.getEndpoint("delete"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } /** * Handle getting lists */ async handleGet(itemIndex) { // Tạo body parameter chứ không phải query parameter const body = {}; // Lấy các tham số required trực tiếp từ form const iblockTypeId = this.getNodeParameter("IBLOCK_TYPE_ID", itemIndex); body.IBLOCK_TYPE_ID = iblockTypeId; // Lấy tham số filter try { const filterParam = this.getNodeParameter("FILTER", itemIndex, {}); body.FILTER = typeof filterParam === "string" ? JSON.parse(filterParam) : filterParam; } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Filter must be a valid JSON", { itemIndex }); } // Lấy các tham số phụ const socnetGroupId = this.getNodeParameter("SOCNET_GROUP_ID", itemIndex, ""); if (socnetGroupId) { body.SOCNET_GROUP_ID = socnetGroupId; } const checkPermissions = this.getNodeParameter("CHECK_PERMISSIONS", itemIndex, true); body.CHECK_PERMISSIONS = checkPermissions ? "Y" : "N"; const endpoint = this.getEndpoint("get"); // Dùng makeStandardBitrix24Call thay vì bitrix24ApiRequestAllItems vì không cần phân trang const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, body, {}, // empty query params itemIndex); this.returnData.push({ json: responseData }); } /** * Handle updating an existing list */ async handleUpdate(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); let fields = this.getNodeParameter("fields", itemIndex); if (typeof fields === "string") { try { fields = JSON.parse(fields); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Fields must be a valid JSON", { itemIndex }); } } const params = { IBLOCK_ID: listId, FIELDS: fields, }; const endpoint = this.getEndpoint("update"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } /** * Handle getting IBLOCK type ID */ async handleGetIblockTypeId(itemIndex) { const body = {}; try { const filterParam = this.getNodeParameter("filter", itemIndex, {}); if (filterParam) { body.FILTER = typeof filterParam === "string" ? JSON.parse(filterParam) : filterParam; } } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Filter must be a valid JSON", { itemIndex }); } const endpoint = this.getEndpoint("getIblockTypeId"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, body, {}, // empty query params itemIndex); this.returnData.push({ json: responseData }); } /** * Handle adding a field to a list */ async handleAddField(itemIndex) { // Lấy các tham số cơ bản const listId = this.getNodeParameter("listId", itemIndex); const iblockTypeId = this.getNodeParameter("IBLOCK_TYPE_ID", itemIndex); const fieldType = this.getNodeParameter("FIELD_TYPE", itemIndex); const name = this.getNodeParameter("NAME", itemIndex); const fieldId = this.getNodeParameter("FIELD_ID", itemIndex); // Xây dựng đối tượng FIELD_DATA const fieldData = { FIELD_NAME: name, FIELD_TYPE: fieldType, FIELD_ID: fieldId, }; // Lấy các tham số bổ sung từ fixedCollection const fieldParametersUi = this.getNodeParameter("fieldParametersUi", itemIndex, { parameters: [] }); const parameters = fieldParametersUi.parameters || []; // Xử lý từng tham số for (const param of parameters) { const paramName = param.name; let value = param.value; if (paramName && value !== undefined) { if (paramName === "custom" && param.customName) { // Xử lý tham số tùy chỉnh fieldData[param.customName] = value; } else if (paramName === "IS_REQUIRED" || paramName === "MULTIPLE") { // Chuyển đổi sang Y/N cho các tham số boolean fieldData[paramName] = value.toString().toLowerCase() === "true" || value === "Y" || value === "y" ? "Y" : "N"; } else if (paramName === "VALUES" || paramName === "SETTINGS") { // Xử lý các tham số JSON try { // Nếu là chuỗi, thử parse thành JSON if (typeof value === "string") { value = JSON.parse(value); } // Đã là đối tượng JSON, gán trực tiếp fieldData[paramName] = value; } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `${paramName} must be a valid JSON: ${error.message}`, { itemIndex }); } } else { // Các tham số khác fieldData[paramName] = value; } } } // Xây dựng request chính xác theo API docs Bitrix24 const params = { IBLOCK_ID: listId, IBLOCK_TYPE_ID: iblockTypeId, FIELD_ID: fieldId, FIELD_NAME: name, SORT: fieldData.SORT || "500", MULTIPLE: fieldData.MULTIPLE || "N", IS_REQUIRED: fieldData.IS_REQUIRED || "N", USER_TYPE_SETTINGS: fieldData.SETTINGS || {}, SETTINGS: fieldData.SETTINGS || {}, }; // Đưa tất cả tham số bắt buộc lên cấp cao nhất if (fieldType) { params.TYPE = fieldType; } if (fieldData.VALUES) { params.VALUES = fieldData.VALUES; } // Log request để debug chi tiết console.log("Field Add request params:", JSON.stringify(params, null, 2)); const endpoint = this.getEndpoint("addField"); try { const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); // Nếu thành công console.log("Field Add response data:", JSON.stringify(responseData, null, 2)); this.returnData.push({ json: responseData }); } catch (error) { // Log lỗi cụ thể console.error("Field Add error:", error); throw error; } } /** * Handle deleting a field from a list */ async handleDeleteField(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); const fieldId = this.getNodeParameter("fieldId", itemIndex); const params = { IBLOCK_ID: listId, FIELD_ID: fieldId, }; const endpoint = this.getEndpoint("deleteField"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } /** * Handle getting field types for a list */ async handleGetFieldTypes(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); const params = { IBLOCK_ID: listId, }; const endpoint = this.getEndpoint("getFieldTypes"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } /** * Handle updating a field in a list */ async handleUpdateField(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); const fieldId = this.getNodeParameter("fieldId", itemIndex); let fieldData = this.getNodeParameter("fieldData", itemIndex); if (typeof fieldData === "string") { try { fieldData = JSON.parse(fieldData); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Field data must be a valid JSON", { itemIndex }); } } const params = { IBLOCK_ID: listId, FIELD_ID: fieldId, FIELD_DATA: fieldData, }; const endpoint = this.getEndpoint("updateField"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } /** * Handle getting fields for a list */ async handleGetFields(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); const params = { IBLOCK_ID: listId, }; const endpoint = this.getEndpoint("getFields"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } /** * Handle getting element fields for a list */ async handleGetElementFields(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); const params = { IBLOCK_ID: listId, }; const endpoint = this.getEndpoint("getElementFields"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } /** * Handle getting all elements from a list */ async handleGetElements(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); const body = { IBLOCK_ID: listId, }; // Xử lý filter (required param) try { const filterParam = this.getNodeParameter("filter", itemIndex); body.FILTER = typeof filterParam === "string" ? JSON.parse(filterParam) : filterParam; } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Filter must be a valid JSON", { itemIndex }); } // Xử lý các tham số phụ try { const orderParam = this.getNodeParameter("order", itemIndex, {}); if (orderParam) { body.ORDER = typeof orderParam === "string" ? JSON.parse(orderParam) : orderParam; } } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Order must be a valid JSON", { itemIndex }); } const select = this.getNodeParameter("select", itemIndex, ""); if (select) { body.SELECT = select; } const endpoint = this.getEndpoint("getElements"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, body, {}, // empty query params itemIndex); this.returnData.push({ json: responseData }); } /** * Handle getting a single element by ID */ async handleGetElement(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); const elementId = this.getNodeParameter("elementId", itemIndex); const params = { IBLOCK_ID: listId, ELEMENT_ID: elementId, }; const endpoint = this.getEndpoint("getElement"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } /** * Handle adding a new element to a list */ async handleAddElement(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); let elementFields = this.getNodeParameter("elementFields", itemIndex); if (typeof elementFields === "string") { try { elementFields = JSON.parse(elementFields); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Element fields must be a valid JSON", { itemIndex }); } } const params = { IBLOCK_ID: listId, ELEMENT_CODE: elementFields.CODE || "", FIELDS: elementFields, }; const endpoint = this.getEndpoint("addElement"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } /** * Handle updating an element in a list */ async handleUpdateElement(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); const elementId = this.getNodeParameter("elementId", itemIndex); let elementFields = this.getNodeParameter("elementFields", itemIndex); if (typeof elementFields === "string") { try { elementFields = JSON.parse(elementFields); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Element fields must be a valid JSON", { itemIndex }); } } const params = { IBLOCK_ID: listId, ELEMENT_ID: elementId, FIELDS: elementFields, }; const endpoint = this.getEndpoint("updateElement"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } /** * Handle deleting an element from a list */ async handleDeleteElement(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); const elementId = this.getNodeParameter("elementId", itemIndex); const params = { IBLOCK_ID: listId, ELEMENT_ID: elementId, }; const endpoint = this.getEndpoint("deleteElement"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } /** * Handle getting element file URL */ async handleGetElementFileUrl(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); const elementId = this.getNodeParameter("elementId", itemIndex); const fieldId = this.getNodeParameter("fieldId", itemIndex); const fileId = this.getNodeParameter("fileId", itemIndex); const params = { IBLOCK_ID: listId, ELEMENT_ID: elementId, FIELD_ID: fieldId, FILE_ID: fileId, }; const endpoint = this.getEndpoint("getElementFileUrl"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } /** * Handle getting section element */ async handleGetSectionElement(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); const sectionId = this.getNodeParameter("sectionId", itemIndex); const params = { IBLOCK_ID: listId, SECTION_ID: sectionId, }; const endpoint = this.getEndpoint("getSectionElement"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } /** * Handle getting element file */ async handleGetElementFile(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); const elementId = this.getNodeParameter("elementId", itemIndex); const fieldId = this.getNodeParameter("fieldId", itemIndex); const params = { IBLOCK_ID: listId, ELEMENT_ID: elementId, FIELD_ID: fieldId, }; const endpoint = this.getEndpoint("getElementFile"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } /** * Handle getting sections */ async handleGetSections(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); const body = { IBLOCK_ID: listId, }; // Xử lý filter (required param) try { const filterParam = this.getNodeParameter("filter", itemIndex); body.FILTER = typeof filterParam === "string" ? JSON.parse(filterParam) : filterParam; } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Filter must be a valid JSON", { itemIndex }); } // Xử lý các tham số phụ try { const orderParam = this.getNodeParameter("order", itemIndex, {}); if (orderParam) { body.ORDER = typeof orderParam === "string" ? JSON.parse(orderParam) : orderParam; } } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Order must be a valid JSON", { itemIndex }); } const select = this.getNodeParameter("select", itemIndex, ""); if (select) { body.SELECT = select; } const endpoint = this.getEndpoint("getSections"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, body, {}, // empty query params itemIndex); this.returnData.push({ json: responseData }); } /** * Handle adding a new section */ async handleAddSection(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); let sectionFields = this.getNodeParameter("sectionFields", itemIndex); if (typeof sectionFields === "string") { try { sectionFields = JSON.parse(sectionFields); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Section fields must be a valid JSON", { itemIndex }); } } const params = { IBLOCK_ID: listId, FIELDS: sectionFields, }; const endpoint = this.getEndpoint("addSection"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } /** * Handle getting a single section by ID */ async handleGetSection(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); const sectionId = this.getNodeParameter("sectionId", itemIndex); const params = { IBLOCK_ID: listId, SECTION_ID: sectionId, }; const endpoint = this.getEndpoint("getSection"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } /** * Handle updating a section */ async handleUpdateSection(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); const sectionId = this.getNodeParameter("sectionId", itemIndex); let sectionFields = this.getNodeParameter("sectionFields", itemIndex); if (typeof sectionFields === "string") { try { sectionFields = JSON.parse(sectionFields); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Section fields must be a valid JSON", { itemIndex }); } } const params = { IBLOCK_ID: listId, SECTION_ID: sectionId, FIELDS: sectionFields, }; const endpoint = this.getEndpoint("updateSection"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } /** * Handle deleting a section */ async handleDeleteSection(itemIndex) { const listId = this.getNodeParameter("listId", itemIndex); const sectionId = this.getNodeParameter("sectionId", itemIndex); const params = { IBLOCK_ID: listId, SECTION_ID: sectionId, }; const endpoint = this.getEndpoint("deleteSection"); const responseData = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, params, itemIndex); this.returnData.push({ json: responseData }); } } exports.ListsResourceHandler = ListsResourceHandler; //# sourceMappingURL=ListsResourceHandler_backup.js.map