This commit is contained in:
lizandrogd
2026-01-21 16:42:26 -05:00
parent ac179cec98
commit 0d96fa0029
7 changed files with 189 additions and 0 deletions
+44
View File
@@ -545,6 +545,50 @@ class BotService {
}
}
/**
* Finalizar la atención: limpiar estado 'in_service' y enviar plantilla de encuesta
*/
public function finishAttendConversation($phoneNumber, $operatorId = null) {
try {
// Limpiar estado de 'in_service'
$this->db->update('users', ['in_service' => 0, 'in_service_by' => null, 'in_service_at' => null, 'advisor_requested' => 0], 'phone_number = :phone', ['phone' => $phoneNumber]);
// Enviar plantilla de encuesta (configurable)
$template = getConfigFromDB('survey_template', 'encuesta_satisfaccin');
$lang = getConfigFromDB('survey_template_language', 'es');
try {
$this->whatsappService->sendTemplateMessage($phoneNumber, $template, $lang, []);
} catch (Exception $e) {
// Ignorar errores de envío de plantilla pero loguear
error_log('Failed to send survey template: ' . $e->getMessage());
if (function_exists('writeLog')) writeLog('WARN', 'Failed to send survey template: ' . $e->getMessage());
}
// Registrar actividad del operador
$user = $this->db->fetch('SELECT id FROM users WHERE phone_number = :phone', ['phone' => $phoneNumber]);
$userId = $user['id'] ?? null;
$now = date('Y-m-d H:i:s');
$activity = [
'user_id' => $userId,
'operator_id' => $operatorId,
'action' => 'finish',
'details' => "Finished attend and sent survey: {$template}",
'created_at' => $now
];
try {
$this->db->insert('operator_activity', $activity);
} catch (Exception $e) {
error_log('Failed to insert operator_activity on finish: ' . $e->getMessage());
}
if (function_exists('writeLog')) writeLog('INFO', "Operator {$operatorId} finished attending for {$phoneNumber}, survey={$template}");
} catch (Exception $e) {
error_log('finishAttendConversation failed: ' . $e->getMessage());
throw $e;
}
}
/**
* Actualizar estado del menú del usuario
*/