const HubspotService = require('../services/hubspotService'); const { get, del } = require('../utils/redisClient') const receiveTranscription = async (req, res) => { try { const { crmPhone, uniqueId, transcription, recordingUrl, companyId, clientTranscription, agentTranscription } = req.body; 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.' }); } // 1. Buscar ticketId no Redis const ticketId = await get(crmPhone); if (!ticketId) { 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 const contact = await hubspotService.createContactIfNotExists(crmPhone); // 3. Criar nota no HubSpot e associar ao contato e ao ticket (se existir) await hubspotService.createCallNote(contact.contactId, { transcription: `${clientTranscription || ''}\n${agentTranscription || ''}`, summary: transcription, recordingUrl, crmPhone, uniqueId, ticketId }); // await del(crmPhone) return res.status(200).json({ message: 'Transcrição recebida e processada com sucesso!' }); } catch (error) { console.error('Erro ao receber transcrição:', error?.response?.data || error.message || error); return res.status(500).json({ error: 'Erro ao processar a transcrição.' }); } }; module.exports ={ receiveTranscription }