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

56 lines
1.8 KiB
JavaScript
Raw Normal View History

const redis = require('redis');
const HubspotService = require('../services/hubspotService');
require('dotenv').config();
const redisClient = redis.createClient({
url: process.env.REDIS_URI
});
redisClient.on('error', (err) => console.log('Redis Client Error', err));
(async () => {
if (!redisClient.isOpen) {
await redisClient.connect();
}
})();
const hubspotService = new HubspotService();
exports.receiveTranscription = async (req, res) => {
try {
const { callerId, uniqueId, transcription, recordingUrl } = req.body;
if (!callerId || !uniqueId || !transcription || !recordingUrl) {
return res.status(400).json({ error: 'Campos obrigatórios ausentes.' });
}
console.log(`Recebida transcrição para callerId: ${callerId}, uniqueId: ${uniqueId}`);
console.log('Transcrição:', transcription.summary);
// 1. Buscar ticketId no Redis
const ticketId = await redisClient.get(callerId);
if (!ticketId) {
console.warn(`Nenhum ticketId encontrado no Redis para o callerId: ${callerId}. A transcrição será salva como uma nota sem associação ao ticket.`);
}
// 2. Buscar ou criar contato no HubSpot
const contact = await hubspotService.createContactIfNotExists(callerId);
// 3. Criar nota no HubSpot e associar ao contato e ao ticket (se existir)
await hubspotService.createCallNote(contact.id, {
transcription: `${transcription.client || ''}\n${transcription.agent || ''}`,
summary: transcription.summary,
recordingUrl,
callerId,
uniqueId,
ticketId
});
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.' });
}
};