UNPKG

n8n-nodes-tiny-erp-api-v2

Version:

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

77 lines 3.4 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: 'Search Term', name: 'pesquisa', type: 'string', default: '', description: 'Name or code (or part) of the product you want to search for (required)', displayOptions: { show: { resource: ['products'], operation: ['getProducts'], }, }, required: true, placeholder: 'Product name or code', }, ]; async function execute(index) { const credentials = await this.getCredentials('tinyErpApi'); const api = new api_1.TinyErpApi(this, credentials); const pesquisa = this.getNodeParameter('pesquisa', index); if (!pesquisa || pesquisa.trim() === '') { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Search term (pesquisa) is required for product search'); } try { const params = { pesquisa: pesquisa.trim(), }; const response = await api.getProducts(params); if (!response || typeof response !== 'object') { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Invalid response from Tiny ERP API - expected object, got ${typeof response}: ${JSON.stringify(response)}`); } if (!response.retorno) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Invalid response structure from Tiny ERP API - missing 'retorno' field: ${JSON.stringify(response)}`); } const retorno = response.retorno; if (retorno.erros && retorno.erros.length > 0) { const errors = retorno.erros.map((e) => e.erro); const hasNoResultsError = errors.some((error) => error.includes('não retornou registros') || error.includes('no records found') || error.includes('nenhum registro encontrado')); if (!hasNoResultsError) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Tiny ERP API error: ${errors.join(', ')}`); } } else if (retorno.status !== 'OK') { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Tiny ERP API error: API returned status: ${retorno.status}`); } const products = retorno.produtos && Array.isArray(retorno.produtos) ? retorno.produtos : []; const responseData = { success: true, message: products.length > 0 ? `Retrieved ${products.length} products from Tiny ERP` : `No products found matching search criteria`, totalProducts: products.length, operation: 'getProducts', resource: 'products', searchTerm: pesquisa, timestamp: new Date().toISOString(), products: products, }; return [{ json: responseData }]; } catch (error) { if (error instanceof n8n_workflow_1.NodeOperationError) { throw error; } throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to retrieve products: ${error.message}`); } } exports.execute = execute; //# sourceMappingURL=getProducts.operation.js.map