2025-06-16 17:56:12 +00:00
|
|
|
const HubspotService = require('../services/hubspotService');
|
|
|
|
const { get, del } = require('../utils/redisClient')
|
2025-06-12 20:30:46 +00:00
|
|
|
|
2025-06-20 13:48:48 +00:00
|
|
|
|
2025-06-12 20:30:46 +00:00
|
|
|
|
2025-06-16 17:56:12 +00:00
|
|
|
const receiveTranscription = async (req, res) => {
|
|
|
|
try {
|
2025-06-20 21:06:30 +00:00
|
|
|
|
2025-06-12 20:30:46 +00:00
|
|
|
|
2025-06-20 11:57:01 +00:00
|
|
|
const { crmPhone, uniqueId, transcription, recordingUrl, companyId, clientTranscription, agentTranscription } = req.body;
|
|
|
|
|
2025-06-20 21:06:30 +00:00
|
|
|
|
2025-06-16 17:56:12 +00:00
|
|
|
|
2025-06-20 11:57:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-16 17:56:12 +00:00
|
|
|
if (!crmPhone || !uniqueId || !transcription || !recordingUrl) {
|
2025-06-20 11:57:01 +00:00
|
|
|
console.log('Campos faltando:', {
|
|
|
|
crmPhone: !crmPhone,
|
|
|
|
uniqueId: !uniqueId,
|
|
|
|
transcription: !transcription,
|
|
|
|
recordingUrl: !recordingUrl
|
|
|
|
});
|
2025-06-12 20:30:46 +00:00
|
|
|
return res.status(400).json({ error: 'Campos obrigatórios ausentes.' });
|
|
|
|
}
|
|
|
|
|
2025-06-20 21:06:30 +00:00
|
|
|
|
2025-06-12 20:30:46 +00:00
|
|
|
|
|
|
|
// 1. Buscar ticketId no Redis
|
2025-06-16 17:56:12 +00:00
|
|
|
const ticketId = await get(crmPhone);
|
|
|
|
|
2025-06-12 20:30:46 +00:00
|
|
|
if (!ticketId) {
|
2025-06-16 17:56:12 +00:00
|
|
|
console.warn(`Nenhum ticketId encontrado no Redis para o crmPhone: ${crmPhone}. A transcrição será salva como uma nota sem associação ao ticket.`);
|
2025-06-12 20:30:46 +00:00
|
|
|
}
|
2025-06-16 17:56:12 +00:00
|
|
|
|
2025-06-20 13:48:48 +00:00
|
|
|
const hubspotService = await new HubspotService(companyId).init();
|
|
|
|
|
2025-06-12 20:30:46 +00:00
|
|
|
// 2. Buscar ou criar contato no HubSpot
|
2025-06-20 13:48:48 +00:00
|
|
|
const contact = await hubspotService.createContactIfNotExists(crmPhone);
|
2025-06-12 20:30:46 +00:00
|
|
|
|
|
|
|
// 3. Criar nota no HubSpot e associar ao contato e ao ticket (se existir)
|
2025-06-16 17:56:12 +00:00
|
|
|
await hubspotService.createCallNote(contact.contactId, {
|
2025-06-20 11:57:01 +00:00
|
|
|
transcription: `${clientTranscription || ''}\n${agentTranscription || ''}`,
|
|
|
|
summary: transcription,
|
2025-06-12 20:30:46 +00:00
|
|
|
recordingUrl,
|
2025-06-16 17:56:12 +00:00
|
|
|
crmPhone,
|
2025-06-12 20:30:46 +00:00
|
|
|
uniqueId,
|
|
|
|
ticketId
|
|
|
|
});
|
|
|
|
|
2025-06-20 11:57:01 +00:00
|
|
|
// await del(crmPhone)
|
2025-06-16 17:56:12 +00:00
|
|
|
|
2025-06-12 20:30:46 +00:00
|
|
|
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.' });
|
|
|
|
}
|
|
|
|
};
|
2025-06-16 17:56:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
module.exports ={
|
|
|
|
receiveTranscription
|
|
|
|
}
|