UNPKG

@tuanltntu/n8n-nodes-bitrix24

Version:

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

210 lines 9.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileResourceHandler = void 0; const n8n_workflow_1 = require("n8n-workflow"); const ResourceHandlerBase_1 = require("./ResourceHandlerBase"); const GenericFunctions_1 = require("../GenericFunctions"); /** * Xử lý các tác vụ File của Bitrix24 */ class FileResourceHandler extends ResourceHandlerBase_1.ResourceHandlerBase { constructor(executeFunctions, returnData, options = {}) { super(executeFunctions, returnData, options); this.resourceEndpoints = { upload: "disk.storage.uploadfile", uploadToEntity: { contact: "crm.contact.update.file", company: "crm.company.update.file", deal: "crm.deal.update.file", lead: "crm.lead.update.file", task: "tasks.task.update.file", chat: "im.disk.file.upload", }, get: "disk.file.get", list: "disk.file.list", delete: "disk.file.delete", }; } /** * Xử lý tác vụ File */ async process() { for (let i = 0; i < this.items.length; i++) { try { const operation = this.getNodeParameter("operation", i); switch (operation) { case "upload": await this.handleUpload(i); break; case "get": await this.handleGet(i); break; case "getAll": await this.handleGetAll(i); break; case "delete": await this.handleDelete(i); break; default: throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `Không hỗ trợ tác vụ "${operation}" cho File`, { itemIndex: i }); } } catch (error) { if (this.executeFunctions.continueOnFail()) { this.returnData.push({ json: { error: error.message } }); continue; } throw error; } } return this.returnData; } /** * Xử lý tham số tùy chỉnh */ processCustomParameters(options, params, itemIndex) { if (!options.customParameters) return; try { const customParams = typeof options.customParameters === "string" ? this.parseJsonParameter(options.customParameters, "Custom parameters phải là JSON hợp lệ", itemIndex) : options.customParameters; Object.assign(params, customParams); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Custom parameters phải là JSON hợp lệ", { itemIndex }); } } /** * Xử lý 'upload' */ async handleUpload(itemIndex) { const binaryPropertyName = this.getNodeParameter("binaryPropertyName", itemIndex); const entityType = this.getNodeParameter("entityType", itemIndex); // Kiểm tra tồn tại dữ liệu nhị phân (0, GenericFunctions_1.validateBinaryDataExists)(this.executeFunctions.helpers, this.items[itemIndex], binaryPropertyName); // Lấy nội dung file const binaryData = this.items[itemIndex].binary; const binaryContent = await this.executeFunctions.helpers.getBinaryDataBuffer(itemIndex, binaryPropertyName); // Xác định endpoint dựa trên loại entity let endpoint = ""; let formData = {}; // Lấy options nếu có const options = this.getNodeParameter("options", itemIndex, {}); if (entityType === "disk") { // Upload lên disk storage const folderId = this.getNodeParameter("folderId", itemIndex); endpoint = this.resourceEndpoints.upload; formData = { id: folderId, }; } else { // Upload cho một entity cụ thể const entityId = this.getNodeParameter("entityId", itemIndex); if (!this.resourceEndpoints.uploadToEntity[entityType]) { throw new Error(`Không hỗ trợ loại entity: ${entityType}`); } endpoint = this.resourceEndpoints.uploadToEntity[entityType]; formData = { ID: entityId, ELEMENT_ID: entityId, }; } // Thiết lập form data cho upload file formData.NAME = binaryData[binaryPropertyName].fileName || "file"; formData.CONTENT = binaryContent; // Thêm custom parameters nếu có this.processCustomParameters(options, formData, itemIndex); // Gọi API const responseData = await this.makeApiCall(endpoint, {}, formData, itemIndex); this.addResponseToReturnData(responseData, itemIndex); } /** * Xử lý 'get' */ async handleGet(itemIndex) { var _a; const fileId = this.getNodeParameter("fileId", itemIndex); const download = this.getNodeParameter("download", itemIndex, false); const options = this.getNodeParameter("options", itemIndex, {}); const queryParams = { id: fileId }; this.processCustomParameters(options, queryParams, itemIndex); const responseData = await this.makeApiCall(this.resourceEndpoints.get, {}, queryParams, itemIndex); // Nếu có yêu cầu download và có URL download trong phản hồi if (download && ((_a = responseData === null || responseData === void 0 ? void 0 : responseData.result) === null || _a === void 0 ? void 0 : _a.DOWNLOAD_URL)) { const binaryPropertyName = this.getNodeParameter("binaryPropertyName", itemIndex); const downloadUrl = responseData.result.DOWNLOAD_URL; const fileName = responseData.result.NAME; // Tải file const fileData = await GenericFunctions_1.bitrix24DownloadFile.call(this.executeFunctions, downloadUrl); const binaryData = await this.executeFunctions.helpers.prepareBinaryData(fileData, fileName); // Tạo bản sao của item với dữ liệu nhị phân const newItem = { json: responseData.result, binary: { [binaryPropertyName]: binaryData, }, }; this.addResponseToReturnData([newItem], itemIndex); } else { // Chỉ trả về thông tin file mà không download this.addResponseToReturnData(this.executeFunctions.helpers.returnJsonArray(responseData.result), itemIndex); } } /** * Xử lý 'getAll' */ async handleGetAll(itemIndex) { const returnAll = this.getNodeParameter("returnAll", itemIndex); const options = this.getNodeParameter("options", itemIndex, {}); // Xây dựng tham số lọc const queryParams = {}; if (options.entityType && options.entityId) { const entityId = options.entityId; switch (options.entityType) { case "contact": queryParams.filter = { CONTACT_ID: entityId }; break; case "company": queryParams.filter = { COMPANY_ID: entityId }; break; case "deal": queryParams.filter = { DEAL_ID: entityId }; break; case "lead": queryParams.filter = { LEAD_ID: entityId }; break; case "task": queryParams.filter = { TASK_ID: entityId }; break; case "disk": queryParams.filter = { FOLDER_ID: entityId }; break; } } if (options.order) { queryParams.order = { DATE_CREATE: options.order }; } // Thêm custom parameters nếu có this.processCustomParameters(options, queryParams, itemIndex); const responseData = await this.makeApiCall(this.resourceEndpoints.list, {}, queryParams, itemIndex, returnAll); this.addResponseToReturnData(responseData, itemIndex); } /** * Xử lý 'delete' */ async handleDelete(itemIndex) { const fileId = this.getNodeParameter("fileId", itemIndex); const options = this.getNodeParameter("options", itemIndex, {}); const queryParams = { id: fileId }; this.processCustomParameters(options, queryParams, itemIndex); const responseData = await this.makeApiCall(this.resourceEndpoints.delete, {}, queryParams, itemIndex); this.addResponseToReturnData(this.executeFunctions.helpers.returnJsonArray({ success: responseData.result, }), itemIndex); } } exports.FileResourceHandler = FileResourceHandler; //# sourceMappingURL=FileResourceHandler.js.map