FEAT: Implementar criação de prospecto no Hubsoft e adicionar variáveis de ambiente necessárias.
This commit is contained in:
parent
e2f6915a50
commit
5adc1dba38
@ -1,14 +1,34 @@
|
|||||||
const dotenv = require("dotenv");
|
const dotenv = require("dotenv");
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
|
// Google API Key
|
||||||
const googleApiKey = process.env.GOOGLE_API_KEY;
|
const googleApiKey = process.env.GOOGLE_API_KEY;
|
||||||
|
|
||||||
|
// Geogrid API Configs
|
||||||
const geogridApiUrl = process.env.GEOGRID_API_URL;
|
const geogridApiUrl = process.env.GEOGRID_API_URL;
|
||||||
const geogridApiKey = process.env.GEOGRID_API_KEY;
|
const geogridApiKey = process.env.GEOGRID_API_KEY;
|
||||||
const geogridApiCookie = process.env.GEOGRID_API_COOKIE;
|
const geogridApiCookie = process.env.GEOGRID_API_COOKIE;
|
||||||
|
|
||||||
|
// Hubsoft API Configs
|
||||||
|
const hubsoftUrl = process.env.HUBSOFT_URL;
|
||||||
|
const hubsoftAuthUrl = `${process.env.HUBSOFT_URL}oauth/token`;
|
||||||
|
const hubsoftClientId = process.env.HUBSOFT_CLIENT_ID;
|
||||||
|
const hubsoftClientSecret = process.env.HUBSOFT_CLIENT_SECRET;
|
||||||
|
const hubsoftUsername = process.env.HUBSOFT_USERNAME;
|
||||||
|
const hubsoftPassword = process.env.HUBSOFT_PASSWORD;
|
||||||
|
const hubsoftGrantType = process.env.HUBSOFT_GRANT_TYPE;
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
googleApiKey: googleApiKey,
|
googleApiKey: googleApiKey,
|
||||||
geogridApiUrl: geogridApiUrl,
|
geogridApiUrl: geogridApiUrl,
|
||||||
geogridApiKey: geogridApiKey,
|
geogridApiKey: geogridApiKey,
|
||||||
geogridApiCookie: geogridApiCookie,
|
geogridApiCookie: geogridApiCookie,
|
||||||
|
hubsoftUrl: hubsoftUrl,
|
||||||
|
|
||||||
|
hubsoftAuthUrl: hubsoftAuthUrl,
|
||||||
|
hubsoftClientId: hubsoftClientId,
|
||||||
|
hubsoftClientSecret: hubsoftClientSecret,
|
||||||
|
hubsoftUsername: hubsoftUsername,
|
||||||
|
hubsoftPassword: hubsoftPassword,
|
||||||
|
hubsoftGrantType: hubsoftGrantType
|
||||||
};
|
};
|
||||||
@ -1,6 +1,7 @@
|
|||||||
const geogridService = require("../services/geogridService.js");
|
const geogridService = require("../services/geogridService.js");
|
||||||
const googleService = require("../services/googleService.js");
|
const googleService = require("../services/googleService.js");
|
||||||
const cepRestService = require("../services/cepRestService.js");
|
const cepRestService = require("../services/cepRestService.js");
|
||||||
|
const hubsoftService = require("../services/hubsoftService.js");
|
||||||
|
|
||||||
async function handleViabilidade(req, res) {
|
async function handleViabilidade(req, res) {
|
||||||
const rawCep = req.body.cep;
|
const rawCep = req.body.cep;
|
||||||
@ -70,8 +71,26 @@ async function handleViabilidade(req, res) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleCriarProspecto(req, res) {
|
||||||
|
|
||||||
|
const prospectData = req.body;
|
||||||
|
console.log("Dados recebidos para criação de prospecto:", prospectData);
|
||||||
|
try {
|
||||||
|
const resultado = await hubsoftService.criarProspectHubsoft(prospectData);
|
||||||
|
return res.json({ message: 'Prospecto criado com sucesso', data: resultado });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Erro ao criar prospecto:", error);
|
||||||
|
return res.status(500).json({ error: "Erro ao criar prospecto" });
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// async function handleTeste(req, res) {
|
||||||
|
// console.log("Rota de teste acessada com sucesso.");
|
||||||
|
// return res.json({ message: "Rota de teste funcionando corretamente!" });
|
||||||
|
// }
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
handleViabilidade,
|
handleViabilidade,
|
||||||
};
|
handleCriarProspecto
|
||||||
|
};
|
||||||
|
|
||||||
@ -5,4 +5,10 @@ const controller = require('../controllers/controller.js');
|
|||||||
// Rota para consulta de viabilidade
|
// Rota para consulta de viabilidade
|
||||||
router.post('/viabilidade', controller.handleViabilidade);
|
router.post('/viabilidade', controller.handleViabilidade);
|
||||||
|
|
||||||
|
// Rota para criação de prospecto no Hubsoft
|
||||||
|
router.post('/prospecto', controller.handleCriarProspecto);
|
||||||
|
|
||||||
|
// router.post('/teste', controller.handleTeste);
|
||||||
|
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
67
src/services/hubsoftService.js
Normal file
67
src/services/hubsoftService.js
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
const apiConfig = require('../config/apiConfig');
|
||||||
|
const axios = require('axios');
|
||||||
|
const qs = require('qs');
|
||||||
|
|
||||||
|
// Função para autenticar e obter o token de acesso do Hubsoft
|
||||||
|
const hubsoftToken = async () => {
|
||||||
|
try {
|
||||||
|
const authBody = {
|
||||||
|
client_id: apiConfig.hubsoftClientId,
|
||||||
|
client_secret: apiConfig.hubsoftClientSecret,
|
||||||
|
username: apiConfig.hubsoftUsername,
|
||||||
|
password: apiConfig.hubsoftPassword,
|
||||||
|
grant_type: apiConfig.hubsoftGrantType
|
||||||
|
};
|
||||||
|
|
||||||
|
// Envia como application/x-www-form-urlencoded (padrão OAuth)
|
||||||
|
const response = await axios.post(apiConfig.hubsoftAuthUrl, qs.stringify(authBody), {
|
||||||
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
|
||||||
|
});
|
||||||
|
|
||||||
|
return response.data && response.data.access_token;
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error('Erro ao autenticar no Hubsoft:', error.response?.data || error.message);
|
||||||
|
throw new Error('Erro ao autenticar no Hubsoft');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Função para criar prospects no Hubsoft
|
||||||
|
const criarProspectHubsoft = async (prospectData) => {
|
||||||
|
try {
|
||||||
|
const token = await hubsoftToken();
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
cep: prospectData.cep,
|
||||||
|
servico: {
|
||||||
|
id_servico: prospectData.servicoId,
|
||||||
|
valor: prospectData.servicoValor
|
||||||
|
},
|
||||||
|
numero: prospectData.numero,
|
||||||
|
endereco: prospectData.endereco,
|
||||||
|
bairro: prospectData.bairro,
|
||||||
|
tipo_pessoa: prospectData.tipoPessoa,
|
||||||
|
nome_razaosocial: prospectData.nomeRazaoSocial,
|
||||||
|
cpf_cnpj: prospectData.cpfCnpj,
|
||||||
|
email: prospectData.email,
|
||||||
|
telefone: prospectData.telefone || null
|
||||||
|
};
|
||||||
|
|
||||||
|
// Envia o body JSON corretamente (sem "params" aninhado)
|
||||||
|
const response = await axios.post(`${apiConfig.hubsoftUrl}/api/v1/integracao/prospecto`, payload, {
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`,
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('Resposta do Hubsoft:', response.data);
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error('Erro ao criar prospecto no Hubsoft:', error.response?.data || error.message);
|
||||||
|
throw new Error('Erro ao criar prospecto no Hubsoft');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { criarProspectHubsoft };
|
||||||
Loading…
Reference in New Issue
Block a user