@tuanltntu/n8n-nodes-bitrix24
Version:
Comprehensive n8n community node for Bitrix24 API integration with CRM, Tasks, Chat, Telephony, and more
322 lines • 13.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityResourceHandler = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const ResourceHandlerBase_1 = require("./ResourceHandlerBase");
/**
* Handles Entity operations in Bitrix24
*/
class EntityResourceHandler extends ResourceHandlerBase_1.ResourceHandlerBase {
constructor(executeFunctions, returnData, options = {}) {
super(executeFunctions, returnData, options);
this.resourceEndpoints = {
entity: {
get: "entity.get",
add: "entity.add",
update: "entity.update",
delete: "entity.delete",
rights: "entity.rights",
propertyAdd: "entity.item.property.add",
propertyUpdate: "entity.item.property.update",
propertyDelete: "entity.item.property.delete",
propertyGet: "entity.item.property.get",
addSection: "entity.section.add",
updateSection: "entity.section.update",
deleteSection: "entity.section.delete",
getSections: "entity.section.get",
addItem: "entity.item.add",
getItem: "entity.item.get",
updateItem: "entity.item.update",
deleteItem: "entity.item.delete",
},
};
}
/**
* Process Entity operations
*/
async process() {
for (let i = 0; i < this.items.length; i++) {
try {
const operation = this.getNodeParameter("operation", i);
switch (operation) {
case "get":
await this.handleGet(i);
break;
case "add":
await this.handleAdd(i);
break;
case "update":
await this.handleUpdate(i);
break;
case "delete":
await this.handleDelete(i);
break;
case "addSection":
await this.handleAddSection(i);
break;
case "updateSection":
await this.handleUpdateSection(i);
break;
case "deleteSection":
await this.handleDeleteSection(i);
break;
case "getSections":
await this.handleGetSections(i);
break;
case "addItem":
await this.handleAddItem(i);
break;
case "getItem":
await this.handleGetItem(i);
break;
case "updateItem":
await this.handleUpdateItem(i);
break;
case "deleteItem":
await this.handleDeleteItem(i);
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `The operation "${operation}" is not supported for Entity`, { itemIndex: i });
}
}
catch (error) {
if (this.executeFunctions.continueOnFail()) {
this.returnData.push({ json: { error: error.message } });
continue;
}
throw error;
}
}
return this.returnData;
}
/**
* Handle 'get' operation - Get entities (list all entities)
*/
async handleGet(itemIndex) {
// entity.get might not require parameters - it could return all entities
const requestParams = {};
const endpoint = this.resourceEndpoints.entity.get;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'add' operation - Add a new entity
*/
async handleAdd(itemIndex) {
const entityType = this.getNodeParameter("entityType", itemIndex);
// Get entity data
let entityData = {};
try {
const entityDataJson = this.getNodeParameter("entityData", itemIndex, "{}");
if (entityDataJson) {
entityData = this.parseJsonParameter(entityDataJson, "Entity data must be valid JSON", itemIndex);
}
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Entity data must be valid JSON", { itemIndex });
}
const requestParams = {
ENTITY_TYPE_ID: entityType,
FIELDS: entityData,
};
const endpoint = this.resourceEndpoints.entity.add;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'update' operation - Update an entity
*/
async handleUpdate(itemIndex) {
const entityType = this.getNodeParameter("entityType", itemIndex);
const entityId = this.getNodeParameter("entityId", itemIndex);
// Get entity data
let entityData = {};
try {
const entityDataJson = this.getNodeParameter("entityData", itemIndex, "{}");
if (entityDataJson) {
entityData = this.parseJsonParameter(entityDataJson, "Entity data must be valid JSON", itemIndex);
}
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Entity data must be valid JSON", { itemIndex });
}
const requestParams = {
ENTITY_TYPE_ID: entityType,
ID: entityId,
FIELDS: entityData,
};
const endpoint = this.resourceEndpoints.entity.update;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'delete' operation - Delete an entity
*/
async handleDelete(itemIndex) {
const entityType = this.getNodeParameter("entityType", itemIndex);
const entityId = this.getNodeParameter("entityId", itemIndex);
const requestParams = {
ENTITY_TYPE_ID: entityType,
ID: entityId,
};
const endpoint = this.resourceEndpoints.entity.delete;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'addSection' operation - Add an entity section
*/
async handleAddSection(itemIndex) {
const entityType = this.getNodeParameter("entityType", itemIndex);
// Get section data
let sectionData = {};
try {
const dataJson = this.getNodeParameter("entityData", itemIndex, "{}");
if (dataJson) {
sectionData = this.parseJsonParameter(dataJson, "Section data must be valid JSON", itemIndex);
}
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Section data must be valid JSON", { itemIndex });
}
const requestParams = {
ENTITY_TYPE_ID: entityType,
FIELDS: sectionData,
};
const endpoint = this.resourceEndpoints.entity.addSection;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'updateSection' operation - Update an entity section
*/
async handleUpdateSection(itemIndex) {
const entityType = this.getNodeParameter("entityType", itemIndex);
const sectionId = this.getNodeParameter("sectionId", itemIndex);
// Get section data
let sectionData = {};
try {
const dataJson = this.getNodeParameter("entityData", itemIndex, "{}");
if (dataJson) {
sectionData = this.parseJsonParameter(dataJson, "Section data must be valid JSON", itemIndex);
}
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Section data must be valid JSON", { itemIndex });
}
const requestParams = {
ENTITY_TYPE_ID: entityType,
ID: sectionId,
FIELDS: sectionData,
};
const endpoint = this.resourceEndpoints.entity.updateSection;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'deleteSection' operation - Delete an entity section
*/
async handleDeleteSection(itemIndex) {
const entityType = this.getNodeParameter("entityType", itemIndex);
const sectionId = this.getNodeParameter("sectionId", itemIndex);
const requestParams = {
ENTITY_TYPE_ID: entityType,
ID: sectionId,
};
const endpoint = this.resourceEndpoints.entity.deleteSection;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'getSections' operation - Get entity sections
*/
async handleGetSections(itemIndex) {
const entityType = this.getNodeParameter("entityType", itemIndex);
const requestParams = {
ENTITY_TYPE_ID: entityType,
};
const endpoint = this.resourceEndpoints.entity.getSections;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'addItem' operation - Add an entity item
*/
async handleAddItem(itemIndex) {
const entityType = this.getNodeParameter("entityType", itemIndex);
// Get item data
let itemData = {};
try {
const dataJson = this.getNodeParameter("itemData", itemIndex, "{}");
if (dataJson) {
itemData = this.parseJsonParameter(dataJson, "Item data must be valid JSON", itemIndex);
}
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Item data must be valid JSON", { itemIndex });
}
const requestParams = {
ENTITY_TYPE_ID: entityType,
FIELDS: itemData,
};
const endpoint = this.resourceEndpoints.entity.addItem;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'getItem' operation - Get an entity item
*/
async handleGetItem(itemIndex) {
const entityType = this.getNodeParameter("entityType", itemIndex);
const itemId = this.getNodeParameter("itemId", itemIndex);
const requestParams = {
ENTITY_TYPE_ID: entityType,
ID: itemId,
};
const endpoint = this.resourceEndpoints.entity.getItem;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'updateItem' operation - Update an entity item
*/
async handleUpdateItem(itemIndex) {
const entityType = this.getNodeParameter("entityType", itemIndex);
const itemId = this.getNodeParameter("itemId", itemIndex);
// Get item data
let itemData = {};
try {
const dataJson = this.getNodeParameter("itemData", itemIndex, "{}");
if (dataJson) {
itemData = this.parseJsonParameter(dataJson, "Item data must be valid JSON", itemIndex);
}
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Item data must be valid JSON", { itemIndex });
}
const requestParams = {
ENTITY_TYPE_ID: entityType,
ID: itemId,
FIELDS: itemData,
};
const endpoint = this.resourceEndpoints.entity.updateItem;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Handle 'deleteItem' operation - Delete an entity item
*/
async handleDeleteItem(itemIndex) {
const entityType = this.getNodeParameter("entityType", itemIndex);
const itemId = this.getNodeParameter("itemId", itemIndex);
const requestParams = {
ENTITY_TYPE_ID: entityType,
ID: itemId,
};
const endpoint = this.resourceEndpoints.entity.deleteItem;
const responseData = await this.makeApiCall(endpoint, requestParams, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
}
exports.EntityResourceHandler = EntityResourceHandler;
//# sourceMappingURL=EntityResourceHandler.js.map