From 210da8608da2bda5c4533125b4490433610b7bd5 Mon Sep 17 00:00:00 2001 From: Eduxavier88 Date: Fri, 20 Jun 2025 18:06:30 -0300 Subject: [PATCH] fix: changes applied after technical lead code review --- .../controllers/transcriptionController.js | 9 ++---- backend/services/hubspotService.js | 31 ++++++++++--------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/backend/controllers/transcriptionController.js b/backend/controllers/transcriptionController.js index f57c4dc..9fc4f6e 100644 --- a/backend/controllers/transcriptionController.js +++ b/backend/controllers/transcriptionController.js @@ -5,11 +5,11 @@ const { get, del } = require('../utils/redisClient') const receiveTranscription = async (req, res) => { try { - console.log('Payload recebido:', JSON.stringify(req.body, null, 2)); + const { crmPhone, uniqueId, transcription, recordingUrl, companyId, clientTranscription, agentTranscription } = req.body; - console.log('=========> crmPhone: ', crmPhone, "===> uniqueId: ", uniqueId, "===> transcription: ", transcription, "===> recordingUrl: ", recordingUrl, "===> companyId: ", companyId ) + @@ -24,10 +24,7 @@ const receiveTranscription = async (req, res) => { return res.status(400).json({ error: 'Campos obrigatórios ausentes.' }); } - console.log(`Recebida transcrição para crmPhone: ${crmPhone}, uniqueId: ${uniqueId}`); - 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); diff --git a/backend/services/hubspotService.js b/backend/services/hubspotService.js index ca6f348..6817714 100644 --- a/backend/services/hubspotService.js +++ b/backend/services/hubspotService.js @@ -130,21 +130,24 @@ Link da Gravação: ${recordingUrl} * @returns {Promise} - Contato existente ou novo contato criado. */ async createContactIfNotExists(crmPhone) { - - if (this?.crmFiles?.length > 0) { - - const { crmRest: rest, authentication } = this.crmFiles[0].crm - - let contact = await lookupContactByPhone(rest, authentication, crmPhone, companyId) - - if (!contact.exist) { - contact = await createContact(companyId, rest, authentication, crmPhone) - - } - console.log('========> contact', contact) - return contact + if (!this.crmFiles?.length) { + throw new Error(`Nenhuma configuração CRM encontrada para companyId: ${this.companyId}`); } - return null + + const { crmRest: rest, authentication } = this.crmFiles[0].crm; + + let contact = await lookupContactByPhone(rest, authentication, crmPhone, this.companyId); + + if (!contact?.exist) { + contact = await createContact(this.companyId, rest, authentication, crmPhone); + } + + if (!contact || !contact.contactId) { + throw new Error(`Falha ao obter contactId para ${crmPhone}`); + } + + console.log('========> contact', contact); + return contact; } }