UNPKG

@tuanltntu/n8n-nodes-bitrix24

Version:

Comprehensive n8n community node for Bitrix24 API integration with CRM, Tasks, Chat, Telephony, and more

419 lines 19.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProductResourceHandler = void 0; const n8n_workflow_1 = require("n8n-workflow"); const ResourceHandlerBase_1 = require("./ResourceHandlerBase"); const GenericFunctions_1 = require("../GenericFunctions"); /** * Handle Bitrix24 Catalog Product operations * Based on: https://apidocs.bitrix24.com/api-reference/catalog/product/index.html */ class ProductResourceHandler extends ResourceHandlerBase_1.ResourceHandlerBase { constructor(executeFunctions, returnData, options = {}) { super(executeFunctions, returnData, options); this.resourceEndpoints = { // Product operations add: "catalog.product.add", update: "catalog.product.update", delete: "catalog.product.delete", get: "catalog.product.get", getAll: "catalog.product.list", getFields: "catalog.product.getFields", // Product property operations getProperty: "catalog.product.property.get", getProperties: "catalog.product.property.list", addProperty: "catalog.product.property.add", updateProperty: "catalog.product.property.update", deleteProperty: "catalog.product.property.delete", }; } /** * Process product operations */ async process() { for (let i = 0; i < this.items.length; i++) { try { const operation = this.getNodeParameter("operation", i); if (!this.resourceEndpoints[operation]) { throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `The operation "${operation}" is not supported for Product resource`, { itemIndex: i }); } const endpoint = this.resourceEndpoints[operation]; let responseData; switch (operation) { case "add": responseData = await this.handleAdd(endpoint, i); break; case "update": responseData = await this.handleUpdate(endpoint, i); break; case "delete": responseData = await this.handleDelete(endpoint, i); break; case "get": responseData = await this.handleGet(endpoint, i); break; case "getAll": responseData = await this.handleGetAll(endpoint, i); break; case "getFields": responseData = await this.handleGetFields(endpoint, i); break; case "getProperty": responseData = await this.handleGetProperty(endpoint, i); break; case "getProperties": responseData = await this.handleGetProperties(endpoint, i); break; case "addProperty": responseData = await this.handleAddProperty(endpoint, i); break; case "updateProperty": responseData = await this.handleUpdateProperty(endpoint, i); break; case "deleteProperty": responseData = await this.handleDeleteProperty(endpoint, i); break; default: throw new n8n_workflow_1.NodeOperationError(this.executeFunctions.getNode(), `The operation "${operation}" is not supported`, { itemIndex: i }); } this.addResponseToReturnData(responseData, i); } catch (error) { if (this.continueOnFail()) { this.addErrorToReturnData(error, i); } else { throw error; } } } return this.returnData; } /** * Handle add product operation */ async handleAdd(endpoint, itemIndex) { const fields = this.getNodeParameter("fields", itemIndex, {}); const productData = { name: this.getNodeParameter("name", itemIndex), price: this.getNodeParameter("price", itemIndex), currency: this.getNodeParameter("currency", itemIndex), catalogId: this.getNodeParameter("catalogId", itemIndex), }; // Add optional fields if (this.getNodeParameter("sectionId", itemIndex)) { productData.sectionId = this.getNodeParameter("sectionId", itemIndex); } if (this.getNodeParameter("active", itemIndex) !== undefined) { productData.active = this.getNodeParameter("active", itemIndex); } if (this.getNodeParameter("sort", itemIndex)) { productData.sort = this.getNodeParameter("sort", itemIndex); } if (this.getNodeParameter("description", itemIndex)) { productData.description = this.getNodeParameter("description", itemIndex); } if (this.getNodeParameter("code", itemIndex)) { productData.code = this.getNodeParameter("code", itemIndex); } if (this.getNodeParameter("xmlId", itemIndex)) { productData.xmlId = this.getNodeParameter("xmlId", itemIndex); } if (this.getNodeParameter("weight", itemIndex)) { productData.weight = this.getNodeParameter("weight", itemIndex); } if (this.getNodeParameter("width", itemIndex)) { productData.width = this.getNodeParameter("width", itemIndex); } if (this.getNodeParameter("length", itemIndex)) { productData.length = this.getNodeParameter("length", itemIndex); } if (this.getNodeParameter("height", itemIndex)) { productData.height = this.getNodeParameter("height", itemIndex); } if (this.getNodeParameter("measure", itemIndex)) { productData.measure = this.getNodeParameter("measure", itemIndex); } if (this.getNodeParameter("vatId", itemIndex)) { productData.vatId = this.getNodeParameter("vatId", itemIndex); } if (this.getNodeParameter("vatIncluded", itemIndex) !== undefined) { productData.vatIncluded = this.getNodeParameter("vatIncluded", itemIndex); } if (this.getNodeParameter("quantity", itemIndex)) { productData.quantity = this.getNodeParameter("quantity", itemIndex); } if (this.getNodeParameter("quantityTrace", itemIndex)) { productData.quantityTrace = this.getNodeParameter("quantityTrace", itemIndex); } if (this.getNodeParameter("canBuyZero", itemIndex) !== undefined) { productData.canBuyZero = this.getNodeParameter("canBuyZero", itemIndex); } if (this.getNodeParameter("subscribe", itemIndex) !== undefined) { productData.subscribe = this.getNodeParameter("subscribe", itemIndex); } // Merge with additional fields Object.assign(productData, fields); return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, productData); } /** * Handle update product operation */ async handleUpdate(endpoint, itemIndex) { const productId = this.getNodeParameter("id", itemIndex); const fields = this.getNodeParameter("fields", itemIndex, {}); const productData = { id: productId, }; // Add fields to update if (this.getNodeParameter("name", itemIndex)) { productData.name = this.getNodeParameter("name", itemIndex); } if (this.getNodeParameter("price", itemIndex)) { productData.price = this.getNodeParameter("price", itemIndex); } if (this.getNodeParameter("currency", itemIndex)) { productData.currency = this.getNodeParameter("currency", itemIndex); } if (this.getNodeParameter("catalogId", itemIndex)) { productData.catalogId = this.getNodeParameter("catalogId", itemIndex); } if (this.getNodeParameter("sectionId", itemIndex)) { productData.sectionId = this.getNodeParameter("sectionId", itemIndex); } if (this.getNodeParameter("active", itemIndex) !== undefined) { productData.active = this.getNodeParameter("active", itemIndex); } if (this.getNodeParameter("sort", itemIndex)) { productData.sort = this.getNodeParameter("sort", itemIndex); } if (this.getNodeParameter("description", itemIndex)) { productData.description = this.getNodeParameter("description", itemIndex); } if (this.getNodeParameter("code", itemIndex)) { productData.code = this.getNodeParameter("code", itemIndex); } if (this.getNodeParameter("xmlId", itemIndex)) { productData.xmlId = this.getNodeParameter("xmlId", itemIndex); } if (this.getNodeParameter("weight", itemIndex)) { productData.weight = this.getNodeParameter("weight", itemIndex); } if (this.getNodeParameter("width", itemIndex)) { productData.width = this.getNodeParameter("width", itemIndex); } if (this.getNodeParameter("length", itemIndex)) { productData.length = this.getNodeParameter("length", itemIndex); } if (this.getNodeParameter("height", itemIndex)) { productData.height = this.getNodeParameter("height", itemIndex); } if (this.getNodeParameter("measure", itemIndex)) { productData.measure = this.getNodeParameter("measure", itemIndex); } if (this.getNodeParameter("vatId", itemIndex)) { productData.vatId = this.getNodeParameter("vatId", itemIndex); } if (this.getNodeParameter("vatIncluded", itemIndex) !== undefined) { productData.vatIncluded = this.getNodeParameter("vatIncluded", itemIndex); } if (this.getNodeParameter("quantity", itemIndex)) { productData.quantity = this.getNodeParameter("quantity", itemIndex); } if (this.getNodeParameter("quantityTrace", itemIndex)) { productData.quantityTrace = this.getNodeParameter("quantityTrace", itemIndex); } if (this.getNodeParameter("canBuyZero", itemIndex) !== undefined) { productData.canBuyZero = this.getNodeParameter("canBuyZero", itemIndex); } if (this.getNodeParameter("subscribe", itemIndex) !== undefined) { productData.subscribe = this.getNodeParameter("subscribe", itemIndex); } // Merge with additional fields Object.assign(productData, fields); return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, productData); } /** * Handle delete product operation */ async handleDelete(endpoint, itemIndex) { const productId = this.getNodeParameter("id", itemIndex); return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, { id: productId }); } /** * Handle get product operation */ async handleGet(endpoint, itemIndex) { const productId = this.getNodeParameter("id", itemIndex); return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, { id: productId }); } /** * Handle get all products operation */ async handleGetAll(endpoint, itemIndex) { const returnAll = this.getNodeParameter("returnAll", itemIndex, false); const maxPages = this.getNodeParameter("maxPages", itemIndex, 5); const qs = {}; // Add filter const filter = this.getNodeParameter("filter", itemIndex, {}); if (Object.keys(filter).length > 0) { qs.filter = filter; } // Add select fields const select = this.getNodeParameter("select", itemIndex, {}); if (Object.keys(select).length > 0) { qs.select = select; } // Add order const order = this.getNodeParameter("order", itemIndex, {}); if (Object.keys(order).length > 0) { qs.order = order; } // Add limit const limit = this.getNodeParameter("limit", itemIndex, 50); qs.limit = limit; if (returnAll) { return await this.getAllItems(endpoint, qs, maxPages); } else { return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, {}, qs); } } /** * Handle get product fields operation */ async handleGetFields(endpoint, itemIndex) { return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, {}); } /** * Handle get product property operation */ async handleGetProperty(endpoint, itemIndex) { const propertyId = this.getNodeParameter("propertyId", itemIndex); return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, { id: propertyId }); } /** * Handle get product properties operation */ async handleGetProperties(endpoint, itemIndex) { const qs = {}; // Add filter const filter = this.getNodeParameter("filter", itemIndex, {}); if (Object.keys(filter).length > 0) { qs.filter = filter; } // Add select fields const select = this.getNodeParameter("select", itemIndex, {}); if (Object.keys(select).length > 0) { qs.select = select; } // Add order const order = this.getNodeParameter("order", itemIndex, {}); if (Object.keys(order).length > 0) { qs.order = order; } return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, {}, qs); } /** * Handle add product property operation */ async handleAddProperty(endpoint, itemIndex) { const fields = this.getNodeParameter("fields", itemIndex, {}); const propertyData = { name: this.getNodeParameter("propertyName", itemIndex), code: this.getNodeParameter("propertyCode", itemIndex), type: this.getNodeParameter("propertyType", itemIndex), }; // Add optional fields if (this.getNodeParameter("propertyRequired", itemIndex) !== undefined) { propertyData.required = this.getNodeParameter("propertyRequired", itemIndex); } if (this.getNodeParameter("propertySort", itemIndex)) { propertyData.sort = this.getNodeParameter("propertySort", itemIndex); } if (this.getNodeParameter("propertyMultiple", itemIndex) !== undefined) { propertyData.multiple = this.getNodeParameter("propertyMultiple", itemIndex); } if (this.getNodeParameter("propertyActive", itemIndex) !== undefined) { propertyData.active = this.getNodeParameter("propertyActive", itemIndex); } // Add property values for list type const propertyValues = this.getNodeParameter("propertyValues", itemIndex, {}); if (Object.keys(propertyValues).length > 0) { propertyData.values = propertyValues; } // Merge with additional fields Object.assign(propertyData, fields); return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, propertyData); } /** * Handle update product property operation */ async handleUpdateProperty(endpoint, itemIndex) { const propertyId = this.getNodeParameter("propertyId", itemIndex); const fields = this.getNodeParameter("fields", itemIndex, {}); const propertyData = { id: propertyId, }; // Add fields to update if (this.getNodeParameter("propertyName", itemIndex)) { propertyData.name = this.getNodeParameter("propertyName", itemIndex); } if (this.getNodeParameter("propertyCode", itemIndex)) { propertyData.code = this.getNodeParameter("propertyCode", itemIndex); } if (this.getNodeParameter("propertyType", itemIndex)) { propertyData.type = this.getNodeParameter("propertyType", itemIndex); } if (this.getNodeParameter("propertyRequired", itemIndex) !== undefined) { propertyData.required = this.getNodeParameter("propertyRequired", itemIndex); } if (this.getNodeParameter("propertySort", itemIndex)) { propertyData.sort = this.getNodeParameter("propertySort", itemIndex); } if (this.getNodeParameter("propertyMultiple", itemIndex) !== undefined) { propertyData.multiple = this.getNodeParameter("propertyMultiple", itemIndex); } if (this.getNodeParameter("propertyActive", itemIndex) !== undefined) { propertyData.active = this.getNodeParameter("propertyActive", itemIndex); } // Add property values for list type const propertyValues = this.getNodeParameter("propertyValues", itemIndex, {}); if (Object.keys(propertyValues).length > 0) { propertyData.values = propertyValues; } // Merge with additional fields Object.assign(propertyData, fields); return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, propertyData); } /** * Handle delete product property operation */ async handleDeleteProperty(endpoint, itemIndex) { const propertyId = this.getNodeParameter("propertyId", itemIndex); return await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, { id: propertyId }); } /** * Get all items with pagination */ async getAllItems(endpoint, qs, maxPages) { const allItems = []; let currentPage = 1; let hasMore = true; while (hasMore && currentPage <= maxPages) { const response = await GenericFunctions_1.makeStandardBitrix24Call.call(this.executeFunctions, endpoint, {}, { ...qs, start: (currentPage - 1) * 50 }); if (response.result && Array.isArray(response.result)) { allItems.push(...response.result); } // Check if there are more pages hasMore = response.next !== undefined; currentPage++; } return { result: allItems, total: allItems.length, }; } } exports.ProductResourceHandler = ProductResourceHandler; //# sourceMappingURL=ProductResourceHandler.js.map