Compare commits
No commits in common. "bb25ec1f091888ec44d63681fe0d624fcbd48769" and "ce94c22f0aba3ac07e51c722dedc947d9fcbfe08" have entirely different histories.
bb25ec1f09
...
ce94c22f0a
|
@ -1,33 +1,19 @@
|
||||||
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 {
|
||||||
console.log('Payload recebido:', JSON.stringify(req.body, null, 2));
|
|
||||||
|
|
||||||
const { crmPhone, uniqueId, transcription, recordingUrl, companyId, clientTranscription, agentTranscription } = req.body;
|
const { crmPhone, uniqueId, transcription, recordingUrl, companyId } = req.body;
|
||||||
|
|
||||||
console.log('=========> crmPhone: ', crmPhone, "===> uniqueId: ", uniqueId, "===> transcription: ", transcription, "===> recordingUrl: ", recordingUrl, "===> companyId: ", companyId )
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (!crmPhone || !uniqueId || !transcription || !recordingUrl) {
|
if (!crmPhone || !uniqueId || !transcription || !recordingUrl) {
|
||||||
console.log('Campos faltando:', {
|
|
||||||
crmPhone: !crmPhone,
|
|
||||||
uniqueId: !uniqueId,
|
|
||||||
transcription: !transcription,
|
|
||||||
recordingUrl: !recordingUrl
|
|
||||||
});
|
|
||||||
return res.status(400).json({ error: 'Campos obrigatórios ausentes.' });
|
return res.status(400).json({ error: 'Campos obrigatórios ausentes.' });
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Recebida transcrição para crmPhone: ${crmPhone}, uniqueId: ${uniqueId}`);
|
console.log(`Recebida transcrição para crmPhone: ${crmPhone}, uniqueId: ${uniqueId}`);
|
||||||
console.log('Transcrição Resumo:', transcription);
|
console.log('Transcrição:', transcription.summary);
|
||||||
console.log('Transcrição Cliente:', clientTranscription);
|
|
||||||
console.log('Transcrição Agente:', agentTranscription);
|
|
||||||
|
|
||||||
// 1. Buscar ticketId no Redis
|
// 1. Buscar ticketId no Redis
|
||||||
const ticketId = await get(crmPhone);
|
const ticketId = await get(crmPhone);
|
||||||
|
@ -36,22 +22,20 @@ 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(crmPhone);
|
const contact = await hubspotService.createContactIfNotExists(companyId, 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, {
|
||||||
transcription: `${clientTranscription || ''}\n${agentTranscription || ''}`,
|
transcription: `${transcription.client || ''}\n${transcription.agent || ''}`,
|
||||||
summary: transcription,
|
summary: transcription.summary,
|
||||||
recordingUrl,
|
recordingUrl,
|
||||||
crmPhone,
|
crmPhone,
|
||||||
uniqueId,
|
uniqueId,
|
||||||
ticketId
|
ticketId
|
||||||
});
|
});
|
||||||
|
|
||||||
// await del(crmPhone)
|
await del(crmPhone)
|
||||||
|
|
||||||
return res.status(200).json({ message: 'Transcrição recebida e processada com sucesso!' });
|
return res.status(200).json({ message: 'Transcrição recebida e processada com sucesso!' });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
@ -12,28 +12,17 @@ class HubspotService {
|
||||||
/**
|
/**
|
||||||
* Inicializa o serviço HubspotService com configuração da API.
|
* Inicializa o serviço HubspotService com configuração da API.
|
||||||
*/
|
*/
|
||||||
constructor(companyId) {
|
constructor() {
|
||||||
this.logger = Logger;
|
this.logger = Logger;
|
||||||
this.companyId = companyId
|
this.baseUrl = 'https://api.hubapi.com';
|
||||||
}
|
this.apiKey = process.env.HUBSPOT_API_KEY;
|
||||||
|
this.client = axios.create({
|
||||||
async init() {
|
baseURL: this.baseUrl,
|
||||||
this.crmFiles = await loadCRM(this.companyId);
|
headers: {
|
||||||
|
'Authorization': `Bearer ${this.apiKey}`,
|
||||||
if (this?.crmFiles?.length > 0) {
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -51,12 +40,12 @@ class HubspotService {
|
||||||
*/
|
*/
|
||||||
async createCallNote(contactId, callData) {
|
async createCallNote(contactId, callData) {
|
||||||
try {
|
try {
|
||||||
const { transcription, summary, recordingUrl, crmPhone, uniqueId, ticketId } = callData;
|
const { transcription, summary, recordingUrl, callerId, uniqueId, ticketId } = callData;
|
||||||
|
|
||||||
const noteContent = `
|
const noteContent = `
|
||||||
Chamada Recebida
|
Chamada Recebida
|
||||||
------------------
|
------------------
|
||||||
Número: ${crmPhone}
|
Número: ${callerId}
|
||||||
ID da Chamada: ${uniqueId}
|
ID da Chamada: ${uniqueId}
|
||||||
Data/Hora: ${new Date().toLocaleString('pt-BR', { timeZone: 'America/Sao_Paulo' })}
|
Data/Hora: ${new Date().toLocaleString('pt-BR', { timeZone: 'America/Sao_Paulo' })}
|
||||||
|
|
||||||
|
@ -101,8 +90,8 @@ Link da Gravação: ${recordingUrl}
|
||||||
/**
|
/**
|
||||||
* Busca um contato no HubSpot pelo número de telefone.
|
* Busca um contato no HubSpot pelo número de telefone.
|
||||||
*
|
*
|
||||||
* @param {string} phoneNumber
|
* @param {string} phoneNumber - Número de telefone para buscar.
|
||||||
* @returns {Promise<Object|null>}
|
* @returns {Promise<Object|null>} - Contato encontrado ou null.
|
||||||
*/
|
*/
|
||||||
async findContactByPhone(phoneNumber) {
|
async findContactByPhone(phoneNumber) {
|
||||||
try {
|
try {
|
||||||
|
@ -129,22 +118,23 @@ 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(crmPhone) {
|
async createContactIfNotExists(companyId, crmPhone) {
|
||||||
|
|
||||||
if (this?.crmFiles?.length > 0) {
|
const crmFiles = await loadCRM(companyId)
|
||||||
|
|
||||||
const { crmRest: rest, authentication } = this.crmFiles[0].crm
|
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)
|
|
||||||
return contact
|
return contact
|
||||||
}
|
}
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,9 +25,6 @@ async function get(key) {
|
||||||
|
|
||||||
// Function to delete a token
|
// Function to delete a token
|
||||||
async function del(key) {
|
async function del(key) {
|
||||||
|
|
||||||
console.log('=========> key: ', key)
|
|
||||||
|
|
||||||
const deletedCount = await redis.del(key)
|
const deletedCount = await redis.del(key)
|
||||||
if (deletedCount === 1) {
|
if (deletedCount === 1) {
|
||||||
console.log(`Token ${key} deleted successfully!`)
|
console.log(`Token ${key} deleted successfully!`)
|
||||||
|
|
Loading…
Reference in New Issue