n8n-nodes-tiny-erp-api-v2
Version:
Custom nodes for Tiny ERP integration with n8n, including AI tools for AI Agent workflows
74 lines • 3.14 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: 'Product ID',
name: 'productId',
type: 'string',
default: '',
description: 'The ID of the product to get stock information for (required)',
displayOptions: {
show: {
resource: ['products'],
operation: ['getStock'],
},
},
required: true,
placeholder: 'Enter product ID',
},
];
async function execute(index) {
const credentials = await this.getCredentials('tinyErpApi');
const api = new api_1.TinyErpApi(this, credentials);
const productId = this.getNodeParameter('productId', index);
if (!productId || productId.trim() === '') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Product ID is required to get stock information');
}
try {
const response = await api.getProductStock(productId.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);
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Tiny ERP API error: ${errors.join(', ')}`);
}
if (retorno.status !== 'OK') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Tiny ERP API error: API returned status: ${retorno.status}`);
}
const produto = retorno.produto || {};
const responseData = {
success: true,
message: `Retrieved stock information for product ID: ${productId}`,
operation: 'getStock',
resource: 'products',
productId: productId,
timestamp: new Date().toISOString(),
product: {
id: produto.id,
nome: produto.nome,
codigo: produto.codigo,
unidade: produto.unidade,
saldo: produto.saldo,
saldoReservado: produto.saldoReservado,
depositos: produto.depositos || []
},
};
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 product stock: ${error.message}`);
}
}
exports.execute = execute;
//# sourceMappingURL=getStock.operation.js.map