@tuanltntu/n8n-nodes-bitrix24
Version:
Comprehensive n8n community node for Bitrix24 API integration with CRM, Tasks, Chat, Telephony, and more
378 lines • 15.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CalendarResourceHandler = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const ResourceHandlerBase_1 = require("./ResourceHandlerBase");
/**
* Class handling Calendar-related resources in Bitrix24
*/
class CalendarResourceHandler extends ResourceHandlerBase_1.ResourceHandlerBase {
/**
* Constructor for CalendarResourceHandler
*/
constructor(executeFunctions, returnData, options = {}) {
super(executeFunctions, returnData, options);
this.resourceEndpoints = {
// Regular calendar operations
getEvents: "calendar.event.get",
getEvent: "calendar.event.get",
addEvent: "calendar.event.add",
updateEvent: "calendar.event.update",
deleteEvent: "calendar.event.delete",
getMeetingStatus: "calendar.meeting.status.get",
setMeetingStatus: "calendar.meeting.status.set",
getAccessibility: "calendar.accessibility.get",
getSections: "calendar.section.get",
addSection: "calendar.section.add",
updateSection: "calendar.section.update",
deleteSection: "calendar.section.delete",
// Full event sync operation
syncEvents: "calendar.event.get.sync",
};
}
/**
* Processes Calendar operations
*/
async process() {
const items = this.executeFunctions.getInputData();
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
const operation = this.executeFunctions.getNodeParameter("operation", itemIndex);
const resourceEndpoint = this.resourceEndpoints[operation];
if (!resourceEndpoint) {
throw new Error(`The operation ${operation} is not supported for resource Calendar!`);
}
switch (operation) {
case "getEvents":
await this.getEvents(itemIndex);
break;
case "getEvent":
await this.getEvent(itemIndex);
break;
case "addEvent":
await this.addEvent(itemIndex);
break;
case "updateEvent":
await this.updateEvent(itemIndex);
break;
case "deleteEvent":
await this.deleteEvent(itemIndex);
break;
case "getMeetingStatus":
await this.getMeetingStatus(itemIndex);
break;
case "setMeetingStatus":
await this.setMeetingStatus(itemIndex);
break;
case "getAccessibility":
await this.getAccessibility(itemIndex);
break;
case "getSections":
await this.getSections(itemIndex);
break;
case "addSection":
await this.addSection(itemIndex);
break;
case "updateSection":
await this.updateSection(itemIndex);
break;
case "deleteSection":
await this.deleteSection(itemIndex);
break;
case "syncEvents":
await this.syncEvents(itemIndex);
break;
default:
throw new Error(`The operation "${operation}" is not supported for resource Calendar!`);
}
}
return this.returnData;
}
/**
* Get a list of calendar events
*/
async getEvents(itemIndex) {
const type = this.executeFunctions.getNodeParameter("type", itemIndex);
const ownerId = this.executeFunctions.getNodeParameter("ownerId", itemIndex);
const from = this.executeFunctions.getNodeParameter("from", itemIndex, "");
const to = this.executeFunctions.getNodeParameter("to", itemIndex, "");
const sectionId = this.executeFunctions.getNodeParameter("sectionId", itemIndex, "");
const params = {
type,
ownerId,
};
if (from)
params.from = from;
if (to)
params.to = to;
if (sectionId)
params.section = sectionId;
const returnAll = this.getNodeParameter("returnAll", itemIndex, false);
const maxPages = this.getNodeParameter("maxPages", itemIndex, 5);
if (returnAll) {
// Return All: Load all pages until no more data
await this.handleCalendarListWithPagination("calendar.event.get", params, itemIndex, 999);
}
else {
// Use limited pagination
await this.handleCalendarListWithPagination("calendar.event.get", params, itemIndex, maxPages);
}
return { result: [] }; // Return empty result since pagination method handles response
}
/**
* Handle pagination for Calendar list operations
*/
async handleCalendarListWithPagination(endpoint, baseParams, itemIndex, maxPages) {
let allResults = [];
let pageCount = 0;
let start = 0;
while (pageCount < maxPages) {
const params = { ...baseParams };
params.start = start;
params.limit = 50;
try {
const response = await this.makeApiCall(endpoint, params, {}, itemIndex);
if (response && response.result) {
// Extract data from response
let results;
if (Array.isArray(response.result)) {
results = response.result;
}
else {
results = [response.result];
}
allResults = allResults.concat(results);
pageCount++;
// Check if there's more data - if no "next" property, stop
if (!response.next) {
break;
}
start = response.next;
}
else {
break;
}
}
catch (error) {
console.error(`Calendar pagination error:`, error);
break;
}
}
// Return paginated data
this.addResponseToReturnData({ result: allResults }, itemIndex);
}
/**
* Get a specific calendar event by ID
*/
async getEvent(itemIndex) {
const type = this.executeFunctions.getNodeParameter("type", itemIndex);
const ownerId = this.executeFunctions.getNodeParameter("ownerId", itemIndex);
const eventId = this.executeFunctions.getNodeParameter("eventId", itemIndex);
const response = await this.makeApiCall("calendar.event.get", {
type,
ownerId,
id: eventId,
}, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
return response;
}
/**
* Add a new calendar event
*/
async addEvent(itemIndex) {
const type = this.executeFunctions.getNodeParameter("type", itemIndex);
const ownerId = this.executeFunctions.getNodeParameter("ownerId", itemIndex);
const eventDataStr = this.executeFunctions.getNodeParameter("eventData", itemIndex);
const sectionId = this.executeFunctions.getNodeParameter("sectionId", itemIndex, "");
if (!eventDataStr || eventDataStr.trim() === "") {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Event data must not be empty", { itemIndex });
}
const eventData = this.parseJsonParameter(eventDataStr, "Failed to parse event data JSON", itemIndex);
const params = {
type,
ownerId,
...eventData,
};
if (sectionId)
params.section = sectionId;
const response = await this.makeApiCall("calendar.event.add", params, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
return response;
}
/**
* Update an existing calendar event
*/
async updateEvent(itemIndex) {
const type = this.executeFunctions.getNodeParameter("type", itemIndex);
const ownerId = this.executeFunctions.getNodeParameter("ownerId", itemIndex);
const eventId = this.executeFunctions.getNodeParameter("eventId", itemIndex);
const eventDataStr = this.executeFunctions.getNodeParameter("eventData", itemIndex);
if (!eventDataStr || eventDataStr.trim() === "") {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Event data must not be empty", { itemIndex });
}
const eventData = this.parseJsonParameter(eventDataStr, "Failed to parse event data JSON", itemIndex);
const params = {
type,
ownerId,
id: eventId,
...eventData,
};
const response = await this.makeApiCall("calendar.event.update", params, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
return response;
}
/**
* Delete a calendar event
*/
async deleteEvent(itemIndex) {
const type = this.executeFunctions.getNodeParameter("type", itemIndex);
const ownerId = this.executeFunctions.getNodeParameter("ownerId", itemIndex);
const eventId = this.executeFunctions.getNodeParameter("eventId", itemIndex);
const response = await this.makeApiCall("calendar.event.delete", {
type,
ownerId,
id: eventId,
}, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
return response;
}
/**
* Get meeting status
*/
async getMeetingStatus(itemIndex) {
const eventId = this.executeFunctions.getNodeParameter("eventId", itemIndex);
const response = await this.makeApiCall("calendar.meeting.status.get", {
id: eventId,
}, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
return response;
}
/**
* Set meeting status
*/
async setMeetingStatus(itemIndex) {
const eventId = this.executeFunctions.getNodeParameter("eventId", itemIndex);
const status = this.executeFunctions.getNodeParameter("status", itemIndex);
const response = await this.makeApiCall("calendar.meeting.status.set", {
id: eventId,
status,
}, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
return response;
}
/**
* Get users accessibility (free/busy) info
*/
async getAccessibility(itemIndex) {
const userIds = this.executeFunctions.getNodeParameter("userIds", itemIndex);
const from = this.executeFunctions.getNodeParameter("from", itemIndex, "");
const to = this.executeFunctions.getNodeParameter("to", itemIndex, "");
const params = {
users: userIds.split(",").map((id) => id.trim()),
};
if (from)
params.from = from;
if (to)
params.to = to;
const response = await this.makeApiCall("calendar.accessibility.get", params, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
return response;
}
/**
* Get calendar sections
*/
async getSections(itemIndex) {
const type = this.executeFunctions.getNodeParameter("type", itemIndex);
const ownerId = this.executeFunctions.getNodeParameter("ownerId", itemIndex);
const response = await this.makeApiCall("calendar.section.get", {
type,
ownerId,
}, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
return response;
}
/**
* Add calendar section
*/
async addSection(itemIndex) {
const type = this.executeFunctions.getNodeParameter("type", itemIndex);
const ownerId = this.executeFunctions.getNodeParameter("ownerId", itemIndex);
const name = this.executeFunctions.getNodeParameter("name", itemIndex);
const sectionDataStr = this.executeFunctions.getNodeParameter("sectionData", itemIndex, "{}");
if (!sectionDataStr || sectionDataStr.trim() === "") {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Section data must not be empty", { itemIndex });
}
const sectionData = this.parseJsonParameter(sectionDataStr, "Failed to parse section data JSON", itemIndex);
const params = {
type,
ownerId,
...sectionData,
};
const response = await this.makeApiCall("calendar.section.add", params, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
return response;
}
/**
* Update calendar section
*/
async updateSection(itemIndex) {
const type = this.executeFunctions.getNodeParameter("type", itemIndex);
const ownerId = this.executeFunctions.getNodeParameter("ownerId", itemIndex);
const id = this.executeFunctions.getNodeParameter("id", itemIndex);
const sectionDataStr = this.executeFunctions.getNodeParameter("sectionData", itemIndex);
if (!sectionDataStr || sectionDataStr.trim() === "") {
throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), "Section data must not be empty", { itemIndex });
}
const sectionData = this.parseJsonParameter(sectionDataStr, "Failed to parse section data JSON", itemIndex);
const params = {
type,
ownerId,
id: id,
...sectionData,
};
const response = await this.makeApiCall("calendar.section.update", params, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
return response;
}
/**
* Delete calendar section
*/
async deleteSection(itemIndex) {
const type = this.executeFunctions.getNodeParameter("type", itemIndex);
const ownerId = this.executeFunctions.getNodeParameter("ownerId", itemIndex);
const sectionId = this.executeFunctions.getNodeParameter("sectionId", itemIndex);
const response = await this.makeApiCall("calendar.section.delete", {
type,
ownerId,
id: sectionId,
}, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
return response;
}
/**
* Sync calendar events (full event sync)
* This method implements the full event sync functionality
*/
async syncEvents(itemIndex) {
const type = this.executeFunctions.getNodeParameter("type", itemIndex);
const ownerId = this.executeFunctions.getNodeParameter("ownerId", itemIndex);
const from = this.executeFunctions.getNodeParameter("from", itemIndex, "");
const to = this.executeFunctions.getNodeParameter("to", itemIndex, "");
const syncToken = this.executeFunctions.getNodeParameter("syncToken", itemIndex, "");
const params = {
type,
ownerId,
};
if (from)
params.from = from;
if (to)
params.to = to;
if (syncToken)
params.syncToken = syncToken;
// Use the special sync endpoint for full event synchronization
const response = await this.makeApiCall("calendar.event.get.sync", params, {}, itemIndex);
this.addResponseToReturnData(response, itemIndex);
return response;
}
}
exports.CalendarResourceHandler = CalendarResourceHandler;
//# sourceMappingURL=CalendarResourceHandler.js.map