n8n-nodes-tiny-erp-api-v2
Version:
Custom nodes for Tiny ERP integration with n8n, including AI tools for AI Agent workflows
428 lines • 14.3 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 Code',
name: 'codigo',
type: 'string',
default: '',
description: 'Product code (required)',
displayOptions: {
show: {
resource: ['products'],
operation: ['createProduct'],
},
},
required: true,
placeholder: 'Enter product code',
},
{
displayName: 'Product Name',
name: 'nome',
type: 'string',
default: '',
description: 'Product name (required)',
displayOptions: {
show: {
resource: ['products'],
operation: ['createProduct'],
},
},
required: true,
placeholder: 'Enter product name',
},
{
displayName: 'Unit',
name: 'unidade',
type: 'string',
default: 'UN',
description: 'Product unit (required)',
displayOptions: {
show: {
resource: ['products'],
operation: ['createProduct'],
},
},
required: true,
placeholder: 'UN, KG, M, etc.',
},
{
displayName: 'Price',
name: 'preco',
type: 'string',
default: '',
description: 'Product price (required)',
displayOptions: {
show: {
resource: ['products'],
operation: ['createProduct'],
},
},
required: true,
placeholder: '10.50',
},
{
displayName: 'Origin',
name: 'origem',
type: 'options',
default: '0',
description: 'Product origin (required)',
displayOptions: {
show: {
resource: ['products'],
operation: ['createProduct'],
},
},
required: true,
options: [
{
name: 'National',
value: '0',
description: 'National product',
},
{
name: 'Foreign - Direct Import',
value: '1',
description: 'Foreign product - direct import',
},
{
name: 'Foreign - Acquired in Domestic Market',
value: '2',
description: 'Foreign product - acquired in domestic market',
},
],
},
{
displayName: 'Status',
name: 'situacao',
type: 'options',
default: 'A',
description: 'Product status',
displayOptions: {
show: {
resource: ['products'],
operation: ['createProduct'],
},
},
options: [
{
name: 'Active',
value: 'A',
description: 'Active product',
},
{
name: 'Inactive',
value: 'I',
description: 'Inactive product',
},
],
},
{
displayName: 'Type',
name: 'tipo',
type: 'options',
default: 'P',
description: 'Product type',
displayOptions: {
show: {
resource: ['products'],
operation: ['createProduct'],
},
},
options: [
{
name: 'Product',
value: 'P',
description: 'Regular product',
},
{
name: 'Service',
value: 'S',
description: 'Service',
},
],
},
{
displayName: 'Additional Options',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: ['products'],
operation: ['createProduct'],
},
},
options: [
{
displayName: 'Promotional Price',
name: 'preco_promocional',
type: 'string',
default: '',
description: 'Promotional price',
placeholder: '8.50',
},
{
displayName: 'NCM',
name: 'ncm',
type: 'string',
default: '',
description: 'NCM (Nomenclatura Comum do Mercosul)',
placeholder: '1001.10.10',
},
{
displayName: 'GTIN/EAN',
name: 'gtin',
type: 'string',
default: '',
description: 'GTIN/EAN barcode',
placeholder: '1234567890123',
},
{
displayName: 'Package GTIN/EAN',
name: 'gtin_embalagem',
type: 'string',
default: '',
description: 'Package GTIN/EAN barcode',
placeholder: '1234567890124',
},
{
displayName: 'Net Weight',
name: 'peso_liquido',
type: 'string',
default: '',
description: 'Net weight in kg',
placeholder: '2.25',
},
{
displayName: 'Gross Weight',
name: 'peso_bruto',
type: 'string',
default: '',
description: 'Gross weight in kg',
placeholder: '2.85',
},
{
displayName: 'Minimum Stock',
name: 'estoque_minimo',
type: 'string',
default: '',
description: 'Minimum stock quantity',
placeholder: '10',
},
{
displayName: 'Maximum Stock',
name: 'estoque_maximo',
type: 'string',
default: '',
description: 'Maximum stock quantity',
placeholder: '100',
},
{
displayName: 'Cost Price',
name: 'preco_custo',
type: 'string',
default: '',
description: 'Cost price',
placeholder: '6.25',
},
{
displayName: 'Brand',
name: 'marca',
type: 'string',
default: '',
description: 'Product brand',
placeholder: 'Brand name',
},
{
displayName: 'Category',
name: 'categoria',
type: 'string',
default: '',
description: 'Product category (use >> for subcategories)',
placeholder: 'Parent Category >> Child Category',
},
{
displayName: 'Warranty',
name: 'garantia',
type: 'string',
default: '',
description: 'Product warranty',
placeholder: '12 months',
},
{
displayName: 'CEST',
name: 'cest',
type: 'string',
default: '',
description: 'CEST code',
placeholder: '01.003.00',
},
{
displayName: 'Complementary Description',
name: 'descricao_complementar',
type: 'string',
default: '',
description: 'Complementary description',
placeholder: 'Additional product details',
},
{
displayName: 'Observations',
name: 'obs',
type: 'string',
default: '',
description: 'Product observations',
placeholder: 'Internal notes',
},
{
displayName: 'Package Type',
name: 'tipo_embalagem',
type: 'options',
default: '',
description: 'Package type',
options: [
{
name: 'Box',
value: '1',
},
{
name: 'Package',
value: '2',
},
{
name: 'Envelope',
value: '3',
},
],
},
{
displayName: 'Package Height (cm)',
name: 'altura_embalagem',
type: 'string',
default: '',
description: 'Package height in centimeters',
placeholder: '26.50',
},
{
displayName: 'Package Length (cm)',
name: 'comprimento_embalagem',
type: 'string',
default: '',
description: 'Package length in centimeters',
placeholder: '27.42',
},
{
displayName: 'Package Width (cm)',
name: 'largura_embalagem',
type: 'string',
default: '',
description: 'Package width in centimeters',
placeholder: '28.00',
},
{
displayName: 'Package Diameter (cm)',
name: 'diametro_embalagem',
type: 'string',
default: '',
description: 'Package diameter in centimeters',
placeholder: '0.00',
},
],
},
];
async function execute(index) {
const credentials = await this.getCredentials('tinyErpApi');
const api = new api_1.TinyErpApi(this, credentials);
const codigo = this.getNodeParameter('codigo', index);
const nome = this.getNodeParameter('nome', index);
const unidade = this.getNodeParameter('unidade', index);
const preco = this.getNodeParameter('preco', index);
const origem = this.getNodeParameter('origem', index);
const situacao = this.getNodeParameter('situacao', index);
const tipo = this.getNodeParameter('tipo', index);
const additionalFields = this.getNodeParameter('additionalFields', index);
if (!codigo || codigo.trim() === '') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Product code is required');
}
if (!nome || nome.trim() === '') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Product name is required');
}
if (!unidade || unidade.trim() === '') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Product unit is required');
}
if (!preco || preco.trim() === '') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Product price is required');
}
try {
const productData = {
sequencia: '1',
codigo: codigo.trim(),
nome: nome.trim(),
unidade: unidade.trim(),
preco: preco.trim(),
origem: origem,
situacao: situacao,
tipo: tipo,
...additionalFields,
};
const produtosData = {
produtos: [
{
produto: productData,
},
],
};
const response = await api.createProduct(produtosData);
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.registros && retorno.registros.length > 0) {
const registro = retorno.registros[0].registro;
if (registro.status === 'Erro' && registro.erros && registro.erros.length > 0) {
const errors = registro.erros.map((e) => e.erro);
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Product creation 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 createdProduct = retorno.registros && retorno.registros.length > 0 ? retorno.registros[0].registro : {};
const responseData = {
success: true,
message: `Product created successfully with ID: ${createdProduct.id || 'Unknown'}`,
operation: 'createProduct',
resource: 'products',
productCode: codigo,
productName: nome,
timestamp: new Date().toISOString(),
createdProduct: {
id: createdProduct.id,
sequencia: createdProduct.sequencia,
status: createdProduct.status,
variacoes: createdProduct.variacoes || [],
},
originalResponse: 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 create product: ${error.message}`);
}
}
exports.execute = execute;
//# sourceMappingURL=createProduct.operation.js.map