@tuanltntu/n8n-nodes-bitrix24
Version:
Comprehensive n8n community node for Bitrix24 API integration with CRM, Tasks, Chat, Telephony, and more
184 lines • 6.76 kB
JavaScript
"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
}
}
if (options.start) {
body.start = options.start;
}
const responseData = await this.makeApiCall(this.resourceEndpoints.getTasks, body, {}, itemIndex);
this.addResponseToReturnData(responseData, 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