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,17 +12,28 @@ 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; }
this.client = axios.create({
baseURL: this.baseUrl, async init() {
headers: { this.crmFiles = await loadCRM(this.companyId);
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json' if (this?.crmFiles?.length > 0) {
}
}); const { authentication } = this.crmFiles[0].crm
this.client = axios.create({
baseURL: 'https://api.hubapi.com',
headers: {
'Authorization': `Bearer ${authentication.token}`,
'Content-Type': 'application/json'
}
});
}
return this;
} }
/** /**
@ -118,23 +129,17 @@ 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)
if (!contact.exist) { if (!contact.exist) {
contact = await createContact(companyId, rest, authentication, crmPhone) contact = await createContact(companyId, rest, authentication, crmPhone)
} }
console.log('========> contact', contact) console.log('========> contact', contact)
return contact return contact