@tuanltntu/n8n-nodes-bitrix24
Version:
Comprehensive n8n community node for Bitrix24 API integration with CRM, Tasks, Chat, Telephony, and more
191 lines • 7.65 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventsResourceHandler = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const ResourceHandlerBase_1 = require("./ResourceHandlerBase");
/**
* Handles Events operations in Bitrix24
*/
class EventsResourceHandler extends ResourceHandlerBase_1.ResourceHandlerBase {
constructor(executeFunctions, returnData, options = {}) {
super(executeFunctions, returnData, options);
this.resourceEndpoints = {
handler: {
register: "event.bind",
unregister: "event.unbind",
list: "event.get",
},
event: {
send: "event.send",
types: "event.types",
},
offline: {
get: "event.offline.get",
process: "event.offline.process",
},
};
}
/**
* Process all items with events operations
*/
async process() {
for (let itemIndex = 0; itemIndex < this.items.length; itemIndex++) {
try {
const operation = this.getNodeParameter("operation", itemIndex);
switch (operation) {
// Event handler operations
case "registerHandler":
await this.handleRegisterHandler(itemIndex);
break;
case "unregisterHandler":
await this.handleUnregisterHandler(itemIndex);
break;
case "getHandlers":
await this.handleGetHandlers(itemIndex);
break;
// Event sending operations
case "sendEvent":
await this.handleSendEvent(itemIndex);
break;
case "getEventTypes":
await this.handleGetEventTypes(itemIndex);
break;
// Offline event operations
case "getOfflineEvents":
await this.handleGetOfflineEvents(itemIndex);
break;
case "processOfflineEvents":
await this.handleProcessOfflineEvents(itemIndex);
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `Operation ${operation} is not supported for resource events`);
}
}
catch (error) {
if (this.continueOnFail()) {
this.addErrorToReturnData(error, itemIndex);
}
else {
throw error;
}
}
}
return this.returnData;
}
/**
* Handle 'registerHandler' operation
*/
async handleRegisterHandler(itemIndex) {
const eventName = this.getNodeParameter("eventName", itemIndex);
const handlerUrl = this.getNodeParameter("handlerUrl", itemIndex);
const authType = this.getNodeParameter("authType", itemIndex);
const requestParams = {
event: eventName,
handler: handlerUrl,
};
// Add authentication details if needed
if (authType === "basic") {
const username = this.getNodeParameter("username", itemIndex);
const password = this.getNodeParameter("password", itemIndex);
requestParams.auth = {
type: "basic",
username,
password,
};
}
else if (authType === "bearer") {
const token = this.getNodeParameter("token", itemIndex);
requestParams.auth = {
type: "bearer",
token,
};
}
const endpoint = this.resourceEndpoints.handler.register;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'unregisterHandler' operation
*/
async handleUnregisterHandler(itemIndex) {
const eventName = this.getNodeParameter("eventName", itemIndex);
const handlerId = this.getNodeParameter("handlerId", itemIndex);
const requestParams = {
event: eventName,
handler_id: handlerId,
};
const endpoint = this.resourceEndpoints.handler.unregister;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'getHandlers' operation
*/
async handleGetHandlers(itemIndex) {
const filter = this.getNodeParameter("filter", itemIndex, {});
const requestParams = {};
if (Object.keys(filter).length) {
requestParams.filter = filter;
}
const endpoint = this.resourceEndpoints.handler.list;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'sendEvent' operation
*/
async handleSendEvent(itemIndex) {
const eventName = this.getNodeParameter("eventName", itemIndex);
const eventDataJson = this.getNodeParameter("eventData", itemIndex);
const eventData = this.parseJsonParameter(eventDataJson, "eventData", itemIndex);
const requestParams = {
event: eventName,
data: eventData,
};
const endpoint = this.resourceEndpoints.event.send;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'getEventTypes' operation
*/
async handleGetEventTypes(itemIndex) {
const moduleId = this.getNodeParameter("moduleId", itemIndex, "");
const requestParams = {};
if (moduleId) {
requestParams.module_id = moduleId;
}
const endpoint = this.resourceEndpoints.event.types;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'getOfflineEvents' operation
*/
async handleGetOfflineEvents(itemIndex) {
const filter = this.getNodeParameter("filter", itemIndex, {});
const requestParams = {};
if (Object.keys(filter).length) {
requestParams.filter = filter;
}
const endpoint = this.resourceEndpoints.offline.get;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'processOfflineEvents' operation
*/
async handleProcessOfflineEvents(itemIndex) {
const eventIds = this.getNodeParameter("eventIds", itemIndex);
// Convert comma-separated string to array
const ids = eventIds.split(",").map((id) => id.trim());
const requestParams = {
id: ids,
};
const endpoint = this.resourceEndpoints.offline.process;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
}
exports.EventsResourceHandler = EventsResourceHandler;
//# sourceMappingURL=EventsResourceHandler.js.map