UNPKG

@contaazul/n8n-nodes-contaazul

Version:

Node for integration with Conta Azul API in n8n

94 lines 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPersonsByFilter = getPersonsByFilter; exports.getPersonById = getPersonById; exports.createPerson = createPerson; const n8n_workflow_1 = require("n8n-workflow"); async function getPersonsByFilter() { const termo_busca = this.getNodeParameter('personSearchAdditionalFields.termo_busca', 0, ''); const pagina = this.getNodeParameter('personSearchAdditionalFields.pagina_pessoa', 0, 1); const tamanho_pagina = this.getNodeParameter('personSearchAdditionalFields.tamanho_pagina_pessoa', 0, 10); const qs = { pagina, tamanho_pagina, }; if (termo_busca) { qs.busca = termo_busca; } const responseData = await this.helpers.httpRequestWithAuthentication.call(this, 'contaAzulOAuth2Api', { method: 'GET', url: 'https://api-v2.contaazul.com/v1/pessoas', qs, json: true, }); const dataArray = Array.isArray(responseData) ? responseData : [responseData]; const items = dataArray.map((item) => ({ json: item, pairedItem: { item: 0, }, })); return items; } async function getPersonById() { const personId = this.getNodeParameter('personId', 0); const responseData = await this.helpers.httpRequestWithAuthentication.call(this, 'contaAzulOAuth2Api', { method: 'GET', url: `https://api-v2.contaazul.com/v1/pessoas/${personId}`, json: true, }); return [ { json: responseData, pairedItem: { item: 0, }, }, ]; } async function createPerson() { const tipoPessoa = this.getNodeParameter('tipo_pessoa', 0); const tipoPerfilParam = this.getNodeParameter('tipo_perfil', 0); const perfisArray = Array.isArray(tipoPerfilParam) ? tipoPerfilParam : [tipoPerfilParam]; const estado = this.getNodeParameter('estado', 0); const email = this.getNodeParameter('personAdditionalFields.email', 0, ''); const telefone = this.getNodeParameter('personAdditionalFields.telefone', 0, ''); if (estado.length !== 2 || !/^[A-Z]{2}$/.test(estado)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'State must be exactly 2 uppercase letters (example: SC)'); } const body = { tipo_pessoa: tipoPessoa, nome: this.getNodeParameter('nome', 0), enderecos: [ { cep: this.getNodeParameter('cep', 0), logradouro: this.getNodeParameter('logradouro', 0), numero: this.getNodeParameter('numero', 0), complemento: this.getNodeParameter('personAdditionalFields.complemento', 0, ''), bairro: this.getNodeParameter('bairro', 0), cidade: this.getNodeParameter('cidade', 0), estado: this.getNodeParameter('estado', 0), pais: this.getNodeParameter('pais', 0), }, ], perfis: perfisArray.map(perfil => ({ tipo_perfil: perfil })), }; if (email) body.email = email; if (telefone) body.telefone_celular = telefone; if (tipoPessoa === 'Física') { body.cpf = this.getNodeParameter('cpf', 0, null); } if (tipoPessoa === 'Jurídica') { body.cnpj = this.getNodeParameter('cnpj', 0, null); } const response = await this.helpers.httpRequestWithAuthentication.call(this, 'contaAzulOAuth2Api', { method: 'POST', url: 'https://api-v2.contaazul.com/v1/pessoas', body, json: true, }); return [{ json: response }]; } //# sourceMappingURL=pessoas.js.map