From 8b83c7613ca535e32557cc359bdd0c7e49d038a8 Mon Sep 17 00:00:00 2001 From: Eduxavier88 Date: Fri, 20 Jun 2025 08:57:01 -0300 Subject: [PATCH] refactor: apply feedback and finalize PR #2 --- .../controllers/transcriptionController.js | 24 +++++++++++++++---- backend/services/hubspotService.js | 17 ++++++++----- backend/utils/redisClient.js | 3 +++ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/backend/controllers/transcriptionController.js b/backend/controllers/transcriptionController.js index ecbff8e..a98a25d 100644 --- a/backend/controllers/transcriptionController.js +++ b/backend/controllers/transcriptionController.js @@ -5,15 +5,29 @@ const hubspotService = new HubspotService(); const receiveTranscription = async (req, res) => { try { + console.log('Payload recebido:', JSON.stringify(req.body, null, 2)); - const { crmPhone, uniqueId, transcription, recordingUrl, companyId } = req.body; + const { crmPhone, uniqueId, transcription, recordingUrl, companyId, clientTranscription, agentTranscription } = req.body; + + console.log('=========> crmPhone: ', crmPhone, "===> uniqueId: ", uniqueId, "===> transcription: ", transcription, "===> recordingUrl: ", recordingUrl, "===> companyId: ", companyId ) + + + 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.' }); } console.log(`Recebida transcrição para crmPhone: ${crmPhone}, uniqueId: ${uniqueId}`); - console.log('Transcrição:', transcription.summary); + console.log('Transcrição Resumo:', transcription); + console.log('Transcrição Cliente:', clientTranscription); + console.log('Transcrição Agente:', agentTranscription); // 1. Buscar ticketId no Redis const ticketId = await get(crmPhone); @@ -27,15 +41,15 @@ const receiveTranscription = async (req, res) => { // 3. Criar nota no HubSpot e associar ao contato e ao ticket (se existir) await hubspotService.createCallNote(contact.contactId, { - transcription: `${transcription.client || ''}\n${transcription.agent || ''}`, - summary: transcription.summary, + transcription: `${clientTranscription || ''}\n${agentTranscription || ''}`, + summary: transcription, recordingUrl, crmPhone, uniqueId, ticketId }); - await del(crmPhone) + // await del(crmPhone) return res.status(200).json({ message: 'Transcrição recebida e processada com sucesso!' }); } catch (error) { diff --git a/backend/services/hubspotService.js b/backend/services/hubspotService.js index d5189ef..6ee2616 100644 --- a/backend/services/hubspotService.js +++ b/backend/services/hubspotService.js @@ -40,12 +40,12 @@ class HubspotService { */ async createCallNote(contactId, callData) { try { - const { transcription, summary, recordingUrl, callerId, uniqueId, ticketId } = callData; + const { transcription, summary, recordingUrl, crmPhone, uniqueId, ticketId } = callData; const noteContent = ` Chamada Recebida ------------------ -Número: ${callerId} +Número: ${crmPhone} ID da Chamada: ${uniqueId} Data/Hora: ${new Date().toLocaleString('pt-BR', { timeZone: 'America/Sao_Paulo' })} @@ -90,8 +90,8 @@ Link da Gravação: ${recordingUrl} /** * Busca um contato no HubSpot pelo número de telefone. * - * @param {string} phoneNumber - Número de telefone para buscar. - * @returns {Promise} - Contato encontrado ou null. + * @param {string} phoneNumber + * @returns {Promise} */ async findContactByPhone(phoneNumber) { try { @@ -120,8 +120,12 @@ Link da Gravação: ${recordingUrl} */ async createContactIfNotExists(companyId, crmPhone) { + console.log('=========> companyId: ', companyId) + const crmFiles = await loadCRM(companyId) + console.log('=========> crmFiles: ', crmFiles) + if (crmFiles.length > 0) { const { crmRest: rest, authentication } = crmFiles[0].crm @@ -130,11 +134,12 @@ Link da Gravação: ${recordingUrl} if (!contact.exist) { contact = await createContact(companyId, rest, authentication, crmPhone) + } - + console.log('========> contact', contact) return contact } - + return null } } diff --git a/backend/utils/redisClient.js b/backend/utils/redisClient.js index 1c93942..22149ac 100644 --- a/backend/utils/redisClient.js +++ b/backend/utils/redisClient.js @@ -25,6 +25,9 @@ async function get(key) { // Function to delete a token async function del(key) { + + console.log('=========> key: ', key) + const deletedCount = await redis.del(key) if (deletedCount === 1) { console.log(`Token ${key} deleted successfully!`)