This commit is contained in:
Lizandro Guarnizo
2026-01-25 21:08:09 -05:00
parent efd711b1dc
commit eca0c919a4
3 changed files with 69 additions and 4 deletions
+32 -1
View File
@@ -602,7 +602,38 @@ class WhatsAppService
$messageData['reaction_emoji'] = $data['reaction']['emoji'] ?? null;
}
$this->db->insert('conversations', $messageData);
// Special handling for reactions: update the referenced message instead of creating a separate outgoing message
if ($data['type'] === 'reaction' && !empty($messageData['reaction_to_message_id'])) {
try {
$targetMid = $messageData['reaction_to_message_id'];
$update = [
'reaction_emoji' => $messageData['reaction_emoji'],
'reaction_to_message_id' => $targetMid,
'updated_at' => date('Y-m-d H:i:s')
];
$this->db->update('conversations', $update, 'message_id = :mid OR id = :mid', ['mid' => $targetMid]);
// Optionally create a small notification for the operator UI
try {
$this->db->insert('notifications', [
'user_id' => $user['id'],
'type' => 'reaction',
'message' => 'Reacción: ' . ($messageData['reaction_emoji'] ?? ''),
'data' => json_encode(['message_id' => $targetMid, 'emoji' => $messageData['reaction_emoji'] ?? '']),
'is_read' => 0,
'created_at' => date('Y-m-d H:i:s')
]);
} catch (Exception $ne) {
error_log('Failed to create reaction notification: ' . $ne->getMessage());
}
} catch (Exception $e) {
// Fallback: if update fails, insert as outgoing message
error_log('Failed to apply reaction via update, inserting outgoing as fallback: ' . $e->getMessage());
$this->db->insert('conversations', $messageData);
}
} else {
$this->db->insert('conversations', $messageData);
}
// Solo limpiar advisor_requested si el mensaje fue enviado por un asesor (operator)
try {