crm-api-template-generator/backend/controllers/transcriptionController.js

63 lines
1.8 KiB
JavaScript
Raw Normal View History

2025-06-16 17:56:12 +00:00
const HubspotService = require('../services/hubspotService');
const { get, del } = require('../utils/redisClient')
2025-06-16 17:56:12 +00:00
const receiveTranscription = async (req, res) => {
try {
const { crmPhone, uniqueId, transcription, recordingUrl, companyId, clientTranscription, agentTranscription } = req.body;
2025-06-16 17:56:12 +00:00
2025-06-16 17:56:12 +00:00
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
2025-06-16 17:56:12 +00:00
const ticketId = await get(crmPhone);
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-16 17:56:12 +00:00
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)
2025-06-16 17:56:12 +00:00
await hubspotService.createCallNote(contact.contactId, {
transcription: `${clientTranscription || ''}\n${agentTranscription || ''}`,
summary: transcription,
recordingUrl,
2025-06-16 17:56:12 +00:00
crmPhone,
uniqueId,
ticketId
});
// await del(crmPhone)
2025-06-16 17:56:12 +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
}