UNPKG

@tuanltntu/n8n-nodes-bitrix24

Version:

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

213 lines 8.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TaskResourceHandler = void 0; const n8n_workflow_1 = require("n8n-workflow"); const ResourceHandlerBase_1 = require("./ResourceHandlerBase"); /** * Xử lý các tác vụ Task của Bitrix24 */ class TaskResourceHandler extends ResourceHandlerBase_1.ResourceHandlerBase { constructor(executeFunctions, returnData, options = {}) { super(executeFunctions, returnData, options); this.resourceEndpoints = { create: "tasks.task.add", update: "tasks.task.update", delete: "tasks.task.delete", get: "tasks.task.get", getAll: "tasks.task.list", addComment: "tasks.task.commentitem.add", }; } /** * Xử lý tác vụ Task */ async process() { for (let i = 0; i < this.items.length; i++) { try { const operation = this.getNodeParameter("operation", i); switch (operation) { case "create": await this.handleCreate(i); break; case "update": await this.handleUpdate(i); break; case "delete": await this.handleDelete(i); break; case "get": await this.handleGet(i); break; case "getAll": await this.handleGetAll(i); break; case "addComment": await this.handleAddComment(i); break; default: throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `Không hỗ trợ tác vụ "${operation}" cho Task`, { itemIndex: i }); } } catch (error) { if (this.executeFunctions.continueOnFail()) { this.returnData.push({ json: { error: error.message } }); continue; } throw error; } } return this.returnData; } /** * Lấy endpoint dựa trên resource và action */ getEndpoint(action) { return this.resourceEndpoints[action]; } /** * 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ý 'create' */ async handleCreate(itemIndex) { const fields = this.getNodeParameter("fields", itemIndex, {}); const options = this.getNodeParameter("options", itemIndex, {}); const requestParams = { fields }; this.processCustomParameters(options, requestParams, itemIndex); const endpoint = this.getEndpoint("create"); const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex); this.addResponseToReturnData(responseData, itemIndex); } /** * Xử lý 'update' */ async handleUpdate(itemIndex) { const taskId = this.getNodeParameter("taskId", itemIndex); const fieldsValues = this.getNodeParameter("fields", itemIndex); const options = this.getNodeParameter("options", itemIndex, {}); const requestParams = { taskId, fields: fieldsValues, }; this.processCustomParameters(options, requestParams, itemIndex); const endpoint = this.getEndpoint("update"); const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex); this.addResponseToReturnData(responseData, itemIndex); } /** * Xử lý 'delete' */ async handleDelete(itemIndex) { const taskId = this.getNodeParameter("taskId", itemIndex); const options = this.getNodeParameter("options", itemIndex, {}); const requestParams = { taskId }; this.processCustomParameters(options, requestParams, itemIndex); const endpoint = this.getEndpoint("delete"); const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex); this.addResponseToReturnData(responseData, itemIndex); } /** * Xử lý 'get' */ async handleGet(itemIndex) { const taskId = this.getNodeParameter("taskId", itemIndex); const options = this.getNodeParameter("options", itemIndex, {}); const queryParams = { id: taskId }; if (options.customParameters) { try { const customParams = this.parseJsonParameter(options.customParameters, 'JSON không hợp lệ trong trường "Custom Parameters"', itemIndex); Object.assign(queryParams, customParams); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), 'JSON không hợp lệ trong trường "Custom Parameters"', { itemIndex }); } } const endpoint = this.getEndpoint("get"); const responseData = await this.makeApiCall(endpoint, {}, queryParams, itemIndex); this.addResponseToReturnData(responseData, itemIndex); } /** * Xử lý 'getAll' */ async handleGetAll(itemIndex) { const options = this.getNodeParameter("options", itemIndex, {}); const qs = {}; // Xử lý filter if (options.filter) { try { qs.filter = typeof options.filter === "string" ? this.parseJsonParameter(options.filter, "Filter phải là JSON hợp lệ", itemIndex) : options.filter; } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Filter phải là JSON hợp lệ", { itemIndex }); } } // Xử lý order if (options.order) { try { qs.order = typeof options.order === "string" ? this.parseJsonParameter(options.order, "Order phải là JSON hợp lệ", itemIndex) : options.order; } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Order phải là JSON hợp lệ", { itemIndex }); } } // Xử lý select if (options.select && Array.isArray(options.select) && options.select.length > 0) { qs.select = options.select; } // Xử lý custom parameters if (options.customParameters) { try { const customParams = this.parseJsonParameter(options.customParameters, 'JSON không hợp lệ trong trường "Custom Parameters"', itemIndex); Object.assign(qs, customParams); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), 'JSON không hợp lệ trong trường "Custom Parameters"', { itemIndex }); } } const returnAll = this.getNodeParameter("returnAll", itemIndex, false); const endpoint = this.getEndpoint("getAll"); const responseData = await this.makeApiCall(endpoint, {}, qs, itemIndex, returnAll); this.addResponseToReturnData(responseData, itemIndex); } /** * Xử lý 'addComment' */ async handleAddComment(itemIndex) { const taskId = this.getNodeParameter("taskId", itemIndex); const comment = this.getNodeParameter("comment", itemIndex); const requestParams = { taskId: taskId, fields: { AUTHOR_ID: 0, POST_MESSAGE: comment, }, }; const endpoint = this.getEndpoint("addComment"); const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex); this.addResponseToReturnData(responseData, itemIndex); } } exports.TaskResourceHandler = TaskResourceHandler; //# sourceMappingURL=TaskResourceHandler.js.map