fix: removed api key hubspot loaded from .env

analisando_gabriel-poc
adriano 2025-06-20 10:48:48 -03:00
parent 8b83c7613c
commit bb25ec1f09
2 changed files with 29 additions and 22 deletions

View File

@ -1,7 +1,7 @@
const HubspotService = require('../services/hubspotService'); const HubspotService = require('../services/hubspotService');
const { get, del } = require('../utils/redisClient') const { get, del } = require('../utils/redisClient')
const hubspotService = new HubspotService();
const receiveTranscription = async (req, res) => { const receiveTranscription = async (req, res) => {
try { try {
@ -36,8 +36,10 @@ const receiveTranscription = async (req, res) => {
console.warn(`Nenhum ticketId encontrado no Redis para o crmPhone: ${crmPhone}. A transcrição será salva como uma nota sem associação ao ticket.`); console.warn(`Nenhum ticketId encontrado no Redis para o crmPhone: ${crmPhone}. A transcrição será salva como uma nota sem associação ao ticket.`);
} }
const hubspotService = await new HubspotService(companyId).init();
// 2. Buscar ou criar contato no HubSpot // 2. Buscar ou criar contato no HubSpot
const contact = await hubspotService.createContactIfNotExists(companyId, crmPhone); const contact = await hubspotService.createContactIfNotExists(crmPhone);
// 3. Criar nota no HubSpot e associar ao contato e ao ticket (se existir) // 3. Criar nota no HubSpot e associar ao contato e ao ticket (se existir)
await hubspotService.createCallNote(contact.contactId, { await hubspotService.createCallNote(contact.contactId, {

View File

@ -12,19 +12,30 @@ class HubspotService {
/** /**
* Inicializa o serviço HubspotService com configuração da API. * Inicializa o serviço HubspotService com configuração da API.
*/ */
constructor() { constructor(companyId) {
this.logger = Logger; this.logger = Logger;
this.baseUrl = 'https://api.hubapi.com'; this.companyId = companyId
this.apiKey = process.env.HUBSPOT_API_KEY; }
async init() {
this.crmFiles = await loadCRM(this.companyId);
if (this?.crmFiles?.length > 0) {
const { authentication } = this.crmFiles[0].crm
this.client = axios.create({ this.client = axios.create({
baseURL: this.baseUrl, baseURL: 'https://api.hubapi.com',
headers: { headers: {
'Authorization': `Bearer ${this.apiKey}`, 'Authorization': `Bearer ${authentication.token}`,
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
}); });
} }
return this;
}
/** /**
* Cria uma nota de chamada no HubSpot e associa ao contato e opcionalmente a um ticket. * Cria uma nota de chamada no HubSpot e associa ao contato e opcionalmente a um ticket.
* *
@ -118,17 +129,11 @@ Link da Gravação: ${recordingUrl}
* @param {string} crmPhone - Número de telefone do contato. * @param {string} crmPhone - Número de telefone do contato.
* @returns {Promise<Object>} - Contato existente ou novo contato criado. * @returns {Promise<Object>} - Contato existente ou novo contato criado.
*/ */
async createContactIfNotExists(companyId, crmPhone) { async createContactIfNotExists(crmPhone) {
console.log('=========> companyId: ', companyId) if (this?.crmFiles?.length > 0) {
const crmFiles = await loadCRM(companyId) const { crmRest: rest, authentication } = this.crmFiles[0].crm
console.log('=========> crmFiles: ', crmFiles)
if (crmFiles.length > 0) {
const { crmRest: rest, authentication } = crmFiles[0].crm
let contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId) let contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId)