@tuanltntu/n8n-nodes-bitrix24
Version:
Comprehensive n8n community node for Bitrix24 API integration with CRM, Tasks, Chat, Telephony, and more
320 lines • 13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotifyResourceHandler = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const ResourceHandlerBase_1 = require("./ResourceHandlerBase");
class NotifyResourceHandler extends ResourceHandlerBase_1.ResourceHandlerBase {
constructor(executeFunctions, returnData, options = {}) {
super(executeFunctions, returnData, options);
this.resourceEndpoints = {
sendSystemNotification: "im.notify.system.add",
sendPersonalNotification: "im.notify.personal.add",
sendPublicNotification: "im.notify.public.add",
deleteNotification: "im.notify.delete",
markAsRead: "im.notify.read",
markAsUnread: "im.notify.unread",
getNotifications: "im.notify.history.get",
getSchema: "im.notify.schema.get",
confirmNotification: "im.notify.confirm",
answerNotification: "im.notify.answer",
};
}
async process() {
for (let itemIndex = 0; itemIndex < this.items.length; itemIndex++) {
try {
const operation = this.getNodeParameter("operation", itemIndex);
switch (operation) {
case "sendSystemNotification":
await this.handleSendSystemNotification(itemIndex);
break;
case "sendPersonalNotification":
await this.handleSendPersonalNotification(itemIndex);
break;
case "sendPublicNotification":
await this.handleSendPublicNotification(itemIndex);
break;
case "deleteNotification":
await this.handleDeleteNotification(itemIndex);
break;
case "markAsRead":
await this.handleMarkAsRead(itemIndex);
break;
case "markAsUnread":
await this.handleMarkAsUnread(itemIndex);
break;
case "getNotifications":
await this.handleGetNotifications(itemIndex);
break;
case "getSchema":
await this.handleGetSchema(itemIndex);
break;
case "confirmNotification":
await this.handleConfirmNotification(itemIndex);
break;
case "answerNotification":
await this.handleAnswerNotification(itemIndex);
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `Unsupported operation "${operation}" for Notify resource`, { itemIndex });
}
}
catch (error) {
if (this.continueOnFail()) {
this.addErrorToReturnData(error, itemIndex);
}
else {
throw error;
}
}
}
return this.returnData;
}
/**
* Handle send system notification operation
*/
async handleSendSystemNotification(itemIndex) {
const userId = this.getNodeParameter("userId", itemIndex);
const message = this.getNodeParameter("message", itemIndex);
const tag = this.getNodeParameter("tag", itemIndex, "");
const subTag = this.getNodeParameter("subTag", itemIndex, "");
const notifyType = this.getNodeParameter("notifyType", itemIndex, "simple");
const options = this.getNodeParameter("options", itemIndex, {});
// Validate required parameters
if (!userId || userId <= 0) {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "User ID is required and must be greater than 0", { itemIndex });
}
if (!message || message.trim() === "") {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Message is required and cannot be empty", { itemIndex });
}
const body = {
USER_ID: userId,
MESSAGE: message,
};
if (tag) {
body.TAG = tag;
}
if (subTag) {
body.SUB_TAG = subTag;
}
if (notifyType && notifyType !== "simple") {
body.NOTIFY_TYPE = notifyType;
}
// Add optional parameters
if (options.attachments) {
try {
body.ATTACH = JSON.parse(options.attachments);
}
catch (error) {
// Ignore invalid JSON
}
}
if (options.buttons && notifyType === "buttons") {
try {
body.BUTTONS = JSON.parse(options.buttons);
}
catch (error) {
// Ignore invalid JSON
}
}
if (options.confirmText && notifyType === "confirm") {
body.CONFIRM_TEXT = options.confirmText;
}
if (options.declineText && notifyType === "confirm") {
body.DECLINE_TEXT = options.declineText;
}
if (options.urlPreview !== undefined) {
body.URL_PREVIEW = options.urlPreview ? "Y" : "N";
}
if (options.sound !== undefined) {
body.SOUND = options.sound ? "Y" : "N";
}
if (options.push !== undefined) {
body.PUSH = options.push ? "Y" : "N";
}
if (options.email !== undefined) {
body.EMAIL = options.email ? "Y" : "N";
}
const responseData = await this.makeApiCall(this.resourceEndpoints.sendSystemNotification, body, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle send personal notification operation
*/
async handleSendPersonalNotification(itemIndex) {
const userId = this.getNodeParameter("userId", itemIndex);
const message = this.getNodeParameter("message", itemIndex);
const tag = this.getNodeParameter("tag", itemIndex, "");
const subTag = this.getNodeParameter("subTag", itemIndex, "");
const options = this.getNodeParameter("options", itemIndex, {});
// Validate required parameters
if (!userId || userId <= 0) {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "User ID is required and must be greater than 0", { itemIndex });
}
if (!message || message.trim() === "") {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Message is required and cannot be empty", { itemIndex });
}
const body = {
USER_ID: userId,
MESSAGE: message,
};
if (tag) {
body.TAG = tag;
}
if (subTag) {
body.SUB_TAG = subTag;
}
// Add optional parameters
if (options.attachments) {
try {
body.ATTACH = JSON.parse(options.attachments);
}
catch (error) {
// Ignore invalid JSON
}
}
if (options.urlPreview !== undefined) {
body.URL_PREVIEW = options.urlPreview ? "Y" : "N";
}
const responseData = await this.makeApiCall(this.resourceEndpoints.sendPersonalNotification, body, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle send public notification operation
*/
async handleSendPublicNotification(itemIndex) {
const chatId = this.getNodeParameter("chatId", itemIndex);
const message = this.getNodeParameter("message", itemIndex);
const tag = this.getNodeParameter("tag", itemIndex, "");
const subTag = this.getNodeParameter("subTag", itemIndex, "");
const options = this.getNodeParameter("options", itemIndex, {});
// Validate required parameters
if (!chatId || chatId <= 0) {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Chat ID is required and must be greater than 0", { itemIndex });
}
if (!message || message.trim() === "") {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Message is required and cannot be empty", { itemIndex });
}
const body = {
CHAT_ID: chatId,
MESSAGE: message,
};
if (tag) {
body.TAG = tag;
}
if (subTag) {
body.SUB_TAG = subTag;
}
// Add optional parameters
if (options.attachments) {
try {
body.ATTACH = JSON.parse(options.attachments);
}
catch (error) {
// Ignore invalid JSON
}
}
if (options.urlPreview !== undefined) {
body.URL_PREVIEW = options.urlPreview ? "Y" : "N";
}
const responseData = await this.makeApiCall(this.resourceEndpoints.sendPublicNotification, body, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle delete notification operation
*/
async handleDeleteNotification(itemIndex) {
const notificationId = this.getNodeParameter("notificationId", itemIndex);
const body = {
ID: notificationId,
};
const responseData = await this.makeApiCall(this.resourceEndpoints.deleteNotification, body, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle mark as read operation
*/
async handleMarkAsRead(itemIndex) {
const notificationId = this.getNodeParameter("notificationId", itemIndex);
const body = {
ID: notificationId,
};
const responseData = await this.makeApiCall(this.resourceEndpoints.markAsRead, body, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle mark as unread operation
*/
async handleMarkAsUnread(itemIndex) {
const notificationId = this.getNodeParameter("notificationId", itemIndex);
const body = {
ID: notificationId,
};
const responseData = await this.makeApiCall(this.resourceEndpoints.markAsUnread, body, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle get notifications operation
*/
async handleGetNotifications(itemIndex) {
const options = this.getNodeParameter("options", itemIndex, {});
const body = {};
if (options.limit) {
body.LIMIT = options.limit;
}
if (options.offset) {
body.OFFSET = options.offset;
}
if (options.filter) {
try {
const filter = JSON.parse(options.filter);
Object.assign(body, filter);
}
catch (error) {
// Ignore invalid JSON
}
}
if (options.order) {
try {
body.ORDER = JSON.parse(options.order);
}
catch (error) {
// Ignore invalid JSON
}
}
const responseData = await this.makeApiCall(this.resourceEndpoints.getNotifications, body, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle get schema operation
*/
async handleGetSchema(itemIndex) {
const responseData = await this.makeApiCall(this.resourceEndpoints.getSchema, {}, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle confirm notification operation
*/
async handleConfirmNotification(itemIndex) {
const notificationId = this.getNodeParameter("notificationId", itemIndex);
const body = {
ID: notificationId,
};
const responseData = await this.makeApiCall(this.resourceEndpoints.confirmNotification, body, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle answer notification operation
*/
async handleAnswerNotification(itemIndex) {
const notificationId = this.getNodeParameter("notificationId", itemIndex);
const message = this.getNodeParameter("message", itemIndex);
const body = {
ID: notificationId,
ANSWER_TEXT: message,
};
const responseData = await this.makeApiCall(this.resourceEndpoints.answerNotification, body, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
}
exports.NotifyResourceHandler = NotifyResourceHandler;
//# sourceMappingURL=NotifyResourceHandler.js.map