UNPKG

n8n-nodes-tiny-erp-api-v2

Version:

Custom nodes for Tiny ERP integration with n8n, including AI tools for AI Agent workflows

136 lines 5.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.execute = exports.description = void 0; const n8n_workflow_1 = require("n8n-workflow"); const api_1 = require("../../transport/api"); exports.description = [ { displayName: 'Products Data', name: 'productsData', type: 'json', default: '[]', description: 'Array of product objects to create in batch (max 20 products per batch)', displayOptions: { show: { resource: ['products'], operation: ['createProductsBatch'], }, }, required: true, placeholder: `[ { "codigo": "PROD001", "nome": "Product 1", "unidade": "UN", "preco": "10.50", "origem": "0" }, { "codigo": "PROD002", "nome": "Product 2", "unidade": "UN", "preco": "15.75", "origem": "0" } ]`, }, { displayName: 'Batch Size', name: 'batchSize', type: 'number', default: 20, description: 'Number of products to process per batch (max 20)', displayOptions: { show: { resource: ['products'], operation: ['createProductsBatch'], }, }, typeOptions: { minValue: 1, maxValue: 20, }, }, ]; async function execute(index) { const credentials = await this.getCredentials('tinyErpApi'); const api = new api_1.TinyErpApi(this, credentials); const productsDataStr = this.getNodeParameter('productsData', index); const batchSize = this.getNodeParameter('batchSize', index); if (!productsDataStr || productsDataStr.trim() === '') { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Products data is required for batch creation'); } let productsData; try { productsData = JSON.parse(productsDataStr); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Invalid JSON in products data: ${error.message}`); } if (!Array.isArray(productsData)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Products data must be an array'); } if (productsData.length === 0) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Products data array cannot be empty'); } for (let i = 0; i < productsData.length; i++) { const product = productsData[i]; if (!product.codigo || !product.nome || !product.unidade || !product.preco || product.origem === undefined) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Product at index ${i} is missing required fields (codigo, nome, unidade, preco, origem)`); } } try { const effectiveBatchSize = Math.min(batchSize, 20); const results = []; const batches = []; for (let i = 0; i < productsData.length; i += effectiveBatchSize) { const batch = productsData.slice(i, i + effectiveBatchSize); batches.push(batch); } console.log(`🔍 [Tiny ERP API] Processing ${productsData.length} products in ${batches.length} batches`); for (let batchIndex = 0; batchIndex < batches.length; batchIndex++) { const batch = batches[batchIndex]; try { console.log(`🔍 [Tiny ERP API] Processing batch ${batchIndex + 1}/${batches.length} with ${batch.length} products`); const batchWithSequence = batch.map((product, idx) => ({ sequencia: (batchIndex * effectiveBatchSize + idx + 1).toString(), ...product, })); const batchResults = await api.createProductsBatch(batchWithSequence); results.push(...(Array.isArray(batchResults) ? batchResults : [batchResults])); } catch (batchError) { console.error(`🔍 [Tiny ERP API] Batch ${batchIndex + 1} failed:`, batchError.message); results.push({ retorno: { status: 'Erro', erros: [{ erro: `Batch ${batchIndex + 1} failed: ${batchError.message}` }], batchIndex: batchIndex + 1, batchSize: batch.length, }, }); } } const responseData = { success: true, message: `Processed ${productsData.length} products in ${batches.length} batches`, operation: 'createProductsBatch', resource: 'products', totalProducts: productsData.length, totalBatches: batches.length, batchSize: effectiveBatchSize, timestamp: new Date().toISOString(), results: results, rateLimitInfo: api.getRateLimitInfo(), }; return [{ json: responseData }]; } catch (error) { if (error instanceof n8n_workflow_1.NodeOperationError) { throw error; } throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to create products in batch: ${error.message}`); } } exports.execute = execute; //# sourceMappingURL=createProductsBatch.operation.js.map