@tuanltntu/n8n-nodes-bitrix24
Version:
Comprehensive n8n community node for Bitrix24 API integration with CRM, Tasks, Chat, Telephony, and more
177 lines • 7.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserResourceHandler = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const ResourceHandlerBase_1 = require("./ResourceHandlerBase");
/**
* Xử lý các tác vụ User của Bitrix24
*/
class UserResourceHandler extends ResourceHandlerBase_1.ResourceHandlerBase {
constructor(executeFunctions, returnData, options = {}) {
super(executeFunctions, returnData, options);
this.resourceEndpoints = {
get: "user.get",
getAll: "user.get",
getCurrent: "user.current",
};
}
/**
* Xử lý tác vụ User
*/
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 "getAll":
await this.handleGetAll(i);
break;
case "getCurrent":
await this.handleGetCurrent(i);
break;
default:
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `Không hỗ trợ tác vụ "${operation}" cho User`, { itemIndex: i });
}
}
catch (error) {
if (this.executeFunctions.continueOnFail()) {
this.returnData.push({ json: { error: error.message } });
continue;
}
throw error;
}
}
return this.returnData;
}
/**
* Xử lý tham số tùy chỉnh
*/
processCustomParameters(options, params, itemIndex) {
if (!options.customParameters)
return;
try {
const customParams = typeof options.customParameters === "string"
? this.parseJsonParameter(options.customParameters, "Custom parameters phải là JSON hợp lệ", itemIndex)
: options.customParameters;
Object.assign(params, customParams);
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Custom parameters phải là JSON hợp lệ", { itemIndex });
}
}
/**
* Xử lý 'get'
*/
async handleGet(itemIndex) {
const userId = this.getNodeParameter("userId", itemIndex);
const options = this.getNodeParameter("options", itemIndex, {});
const queryParams = { ID: userId };
if (options.customParameters) {
try {
const customParams = this.parseJsonParameter(options.customParameters, 'JSON không hợp lệ trong trường "Custom Parameters"', itemIndex);
Object.assign(queryParams, customParams);
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), 'JSON không hợp lệ trong trường "Custom Parameters"', { itemIndex });
}
}
const responseData = await this.makeApiCall(this.resourceEndpoints.get, {}, queryParams, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
/**
* Xử lý 'getAll' với pagination
*/
async handleGetAll(itemIndex) {
const returnAll = this.getNodeParameter("returnAll", itemIndex, false);
const maxPages = this.getNodeParameter("maxPages", itemIndex, 5);
const options = this.getNodeParameter("options", itemIndex, {});
const baseQs = {};
if (options.filter) {
baseQs.FILTER = options.filter;
}
if (options.order) {
baseQs.ORDER = options.order;
}
if (options.adminMode) {
baseQs.ADMIN_MODE = options.adminMode ? "Y" : "N";
}
// Xử lý custom parameters
if (options.customParameters) {
try {
const customParams = this.parseJsonParameter(options.customParameters, 'JSON không hợp lệ trong trường "Custom Parameters"', itemIndex);
Object.assign(baseQs, customParams);
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), 'JSON không hợp lệ trong trường "Custom Parameters"', { itemIndex });
}
}
if (returnAll) {
// Legacy returnAll behavior - single API call
baseQs.start = -1;
const responseData = await this.makeApiCall(this.resourceEndpoints.get, {}, baseQs, itemIndex, returnAll);
this.addResponseToReturnData(responseData, itemIndex);
}
else {
// New pagination logic
await this.handleGetAllWithPagination(baseQs, itemIndex, maxPages);
}
}
/**
* Handle getAll operation with pagination
*/
async handleGetAllWithPagination(baseQs, itemIndex, maxPages) {
let allResults = [];
let start = 0;
const limit = 50;
let hasMore = true;
let pageCount = 0;
while (hasMore && pageCount < maxPages) {
const qs = { ...baseQs, start, limit };
const responseData = await this.makeApiCall(this.resourceEndpoints.get, {}, qs, itemIndex);
// Extract results from response
let results = [];
if (responseData.result) {
results = Array.isArray(responseData.result)
? responseData.result
: Object.values(responseData.result);
}
allResults = allResults.concat(results);
pageCount++;
// Check if we should continue based on response.next
hasMore = !!responseData.next;
// Safety break
if (pageCount >= 20) {
console.log("Safety break: Too many pages, limiting to 20 pages");
hasMore = false;
}
start += limit;
}
// Create response with all results
const finalResponse = {
result: allResults,
total: allResults.length,
pages_loaded: pageCount,
time: {
start: Date.now(),
finish: Date.now(),
duration: 0,
processing: 0,
date_start: new Date().toISOString(),
date_finish: new Date().toISOString(),
},
};
this.addResponseToReturnData(finalResponse, itemIndex);
}
/**
* Xử lý 'getCurrent'
*/
async handleGetCurrent(itemIndex) {
const responseData = await this.makeApiCall(this.resourceEndpoints.getCurrent, {}, {}, itemIndex);
this.addResponseToReturnData(responseData, itemIndex);
}
}
exports.UserResourceHandler = UserResourceHandler;
//# sourceMappingURL=UserResourceHandler.js.map