n8n-nodes-tiny-erp-api-v2
Version:
Custom nodes for Tiny ERP integration with n8n, including AI tools for AI Agent workflows
104 lines • 4.29 kB
JavaScript
;
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: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: ['orders'],
},
},
options: [
{
name: 'Get Order Data',
value: 'getOrderData',
description: 'Get detailed information about a specific order by order number',
action: 'Get order data',
},
],
default: 'getOrderData',
},
{
displayName: 'Order Number',
name: 'numeroPedido',
type: 'string',
default: '',
required: true,
description: 'The specific order number to retrieve detailed information for',
displayOptions: {
show: {
resource: ['orders'],
operation: ['getOrderData'],
},
},
},
];
async function execute(index) {
const credentials = await this.getCredentials('tinyErpApi');
const api = new api_1.TinyErpApi(this, credentials);
const numeroPedido = this.getNodeParameter('numeroPedido', index);
if (!numeroPedido || numeroPedido.trim() === '') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Order number is required for this operation');
}
try {
const response = await api.getOrderData(numeroPedido.trim());
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') ||
error.includes('não foi encontrado') ||
error.includes('not found'));
if (hasNoResultsError) {
const responseData = {
success: true,
message: `No order found with number ${numeroPedido}`,
orderNumber: numeroPedido,
operation: 'getOrderData',
resource: 'orders',
timestamp: new Date().toISOString(),
orderData: null,
};
return [{ json: responseData }];
}
else {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Tiny ERP API error: ${errors.join(', ')}`);
}
}
else if (retorno.status !== 'OK') {
const errorMessage = `API returned status: ${retorno.status}`;
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Tiny ERP API error: ${errorMessage}`);
}
const responseData = {
success: true,
message: `Retrieved order data for order ${numeroPedido}`,
orderNumber: numeroPedido,
operation: 'getOrderData',
resource: 'orders',
timestamp: new Date().toISOString(),
orderData: retorno.pedido || retorno,
};
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 order data: ${error.message}. Please check the order number and try again.`);
}
}
exports.execute = execute;
//# sourceMappingURL=getOrderData.operation.js.map