n8n-nodes-tiny-erp-api-v2
Version:
Custom nodes for Tiny ERP integration with n8n, including AI tools for AI Agent workflows
147 lines • 5.31 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: ['users'],
},
},
options: [
{
name: 'Get User Data',
value: 'getUserData',
description: 'Retrieve user information with optional filters (name, birth month, CPF)',
action: 'Get user data',
},
],
default: 'getUserData',
},
{
displayName: 'User Name',
name: 'nomeUsuario',
type: 'string',
default: '',
description: 'Name of the user to filter results',
displayOptions: {
show: {
resource: ['users'],
operation: ['getUserData'],
},
},
},
{
displayName: 'Birth Month',
name: 'mesNascimento',
type: 'number',
default: 0,
description: 'Birth month of the user (1-12) to filter results',
displayOptions: {
show: {
resource: ['users'],
operation: ['getUserData'],
},
},
},
{
displayName: 'CPF',
name: 'cpf',
type: 'string',
default: '',
description: 'CPF number of the user to filter results',
displayOptions: {
show: {
resource: ['users'],
operation: ['getUserData'],
},
},
},
{
displayName: 'Get All Users',
name: 'obterTodos',
type: 'boolean',
default: false,
description: 'Whether to return all users regardless of filters',
displayOptions: {
show: {
resource: ['users'],
operation: ['getUserData'],
},
},
},
];
async function execute(index) {
const credentials = await this.getCredentials('tinyErpApi');
const api = new api_1.TinyErpApi(this, credentials);
const nomeUsuario = this.getNodeParameter('nomeUsuario', index);
const mesNascimento = this.getNodeParameter('mesNascimento', index);
const cpf = this.getNodeParameter('cpf', index);
const obterTodos = this.getNodeParameter('obterTodos', index);
try {
const params = {};
if (nomeUsuario && nomeUsuario.trim() !== '') {
params.pesquisa = nomeUsuario.trim();
}
if (mesNascimento && mesNascimento > 0 && mesNascimento <= 12) {
params.mes_nascimento = mesNascimento;
}
if (cpf && cpf.trim() !== '') {
params.cpf = cpf.trim();
}
const response = await api.getUserData(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) {
}
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 users = retorno.contatos || [];
const responseData = {
success: true,
message: `Retrieved ${Array.isArray(users) ? users.length : 0} users from Tiny ERP`,
totalUsers: Array.isArray(users) ? users.length : 0,
filters: {
name: nomeUsuario || null,
birthMonth: mesNascimento || null,
cpf: cpf || null,
getAll: obterTodos,
},
operation: 'getUserData',
resource: 'users',
timestamp: new Date().toISOString(),
users: users,
};
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 user data: ${error.message}`);
}
}
exports.execute = execute;
//# sourceMappingURL=getUserData.operation.js.map