UNPKG

@tuanltntu/n8n-nodes-bitrix24

Version:

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

231 lines 8.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BizprocResourceHandler = void 0; const n8n_workflow_1 = require("n8n-workflow"); const ResourceHandlerBase_1 = require("./ResourceHandlerBase"); /** * Handler for Bitrix24 Bizproc operations */ class BizprocResourceHandler extends ResourceHandlerBase_1.ResourceHandlerBase { constructor(executeFunctions, returnData, options = {}) { super(executeFunctions, returnData, options); this.resourceEndpoints = { startWorkflow: "bizproc.workflow.start", getTask: "bizproc.task.get", getTasks: "bizproc.task.list", completeTask: "bizproc.task.complete", getWorkflow: "bizproc.workflow.get", getWorkflows: "bizproc.workflow.instances", }; } /** * Process Bizproc operations */ async process() { for (let i = 0; i < this.items.length; i++) { try { const operation = this.getNodeParameter("operation", i); switch (operation) { case "startWorkflow": await this.handleStartWorkflow(i); break; case "getTask": await this.handleGetTask(i); break; case "getTasks": await this.handleGetTasks(i); break; case "completeTask": await this.handleCompleteTask(i); break; case "getWorkflow": await this.handleGetWorkflow(i); break; case "getWorkflows": await this.handleGetWorkflows(i); break; default: throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `Unsupported operation "${operation}" for Bizproc resource`, { itemIndex: i }); } } catch (error) { if (this.continueOnFail()) { this.addErrorToReturnData(error, i); } else { throw error; } } } return this.returnData; } /** * Handle start workflow operation */ async handleStartWorkflow(itemIndex) { const templateId = this.getNodeParameter("templateId", itemIndex); const documentId = this.getNodeParameter("documentId", itemIndex); const parameters = this.getNodeParameter("parameters", itemIndex, "{}"); const body = { TEMPLATE_ID: templateId, DOCUMENT_ID: documentId, }; if (parameters) { try { body.PARAMETERS = JSON.parse(parameters); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `Invalid JSON in parameters: ${error.message}`, { itemIndex }); } } const responseData = await this.makeApiCall(this.resourceEndpoints.startWorkflow, body, {}, itemIndex); this.addResponseToReturnData(responseData, itemIndex); } /** * Handle get task operation */ async handleGetTask(itemIndex) { const taskId = this.getNodeParameter("taskId", itemIndex); const body = { ID: taskId, }; const responseData = await this.makeApiCall(this.resourceEndpoints.getTask, body, {}, itemIndex); this.addResponseToReturnData(responseData, itemIndex); } /** * Handle get tasks operation */ async handleGetTasks(itemIndex) { const options = this.getNodeParameter("options", itemIndex, {}); const body = {}; if (options.filter) { try { body.filter = JSON.parse(options.filter); } catch (error) { // Ignore invalid JSON } } if (options.order) { try { body.order = JSON.parse(options.order); } catch (error) { // Ignore invalid JSON } } const returnAll = this.getNodeParameter("returnAll", itemIndex, false); const maxPages = this.getNodeParameter("maxPages", itemIndex, 5); if (returnAll) { // Return All: Load all pages until no more data await this.handleBizprocListWithPagination(this.resourceEndpoints.getTasks, body, itemIndex, 999); } else { // Use limited pagination await this.handleBizprocListWithPagination(this.resourceEndpoints.getTasks, body, itemIndex, maxPages); } } /** * Handle pagination for Bizproc list operations */ async handleBizprocListWithPagination(endpoint, baseParams, itemIndex, maxPages) { let allResults = []; let pageCount = 0; let start = 0; while (pageCount < maxPages) { const params = { ...baseParams }; params.start = start; params.limit = 50; try { const response = await this.makeApiCall(endpoint, params, {}, itemIndex); if (response && response.result) { // Extract data from response let results; if (Array.isArray(response.result)) { results = response.result; } else { results = [response.result]; } allResults = allResults.concat(results); pageCount++; // Check if there's more data - if no "next" property, stop if (!response.next) { break; } start = response.next; } else { break; } } catch (error) { console.error(`Bizproc pagination error:`, error); break; } } // Return paginated data this.addResponseToReturnData({ result: allResults }, itemIndex); } /** * Handle complete task operation */ async handleCompleteTask(itemIndex) { const taskId = this.getNodeParameter("taskId", itemIndex); const parameters = this.getNodeParameter("parameters", itemIndex, "{}"); const body = { TASK_ID: taskId, }; if (parameters) { try { body.PARAMETERS = JSON.parse(parameters); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `Invalid JSON in parameters: ${error.message}`, { itemIndex }); } } const responseData = await this.makeApiCall(this.resourceEndpoints.completeTask, body, {}, itemIndex); this.addResponseToReturnData(responseData, itemIndex); } /** * Handle get workflow operation */ async handleGetWorkflow(itemIndex) { const workflowId = this.getNodeParameter("workflowId", itemIndex); const body = { ID: workflowId, }; const responseData = await this.makeApiCall(this.resourceEndpoints.getWorkflow, body, {}, itemIndex); this.addResponseToReturnData(responseData, itemIndex); } /** * Handle get workflows operation */ async handleGetWorkflows(itemIndex) { const options = this.getNodeParameter("options", itemIndex, {}); const body = {}; if (options.filter) { try { body.filter = JSON.parse(options.filter); } catch (error) { // Ignore invalid JSON } } if (options.order) { try { body.order = JSON.parse(options.order); } catch (error) { // Ignore invalid JSON } } if (options.start) { body.start = options.start; } const responseData = await this.makeApiCall(this.resourceEndpoints.getWorkflows, body, {}, itemIndex); this.addResponseToReturnData(responseData, itemIndex); } } exports.BizprocResourceHandler = BizprocResourceHandler; //# sourceMappingURL=BizprocResourceHandler.js.map