fix: preservar finca_id al resetear contexto tras generar reporte

El reset post-reporte borraba meta['finca']['finca_id'] causando que
todos los informes siguientes se generaran sin filtro de finca.

Nuevo helper preservingReset() restaura el grupo 'finca' después del
reset para que el usuario solo seleccione su finca una vez por sesión.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-30 17:58:10 -05:00
co-authored by Claude Sonnet 4.6
parent 5b0bd3b8b8
commit f23283124a
+21 -7
View File
@@ -1084,6 +1084,20 @@ class NormalBot
};
}
// Reset manteniendo grupos de sesión (finca, etc.) para que el usuario
// no tenga que re-seleccionar su finca después de cada reporte.
private static function preservingReset(int $ctxId, array $meta): void
{
$keep = [];
foreach (['finca'] as $group) {
if (isset($meta[$group])) $keep[$group] = $meta[$group];
}
ConversationContext::reset($ctxId);
if (!empty($keep)) {
ConversationContext::updateMetadata($ctxId, $keep);
}
}
// ── Resolves fixed-mode var values ───────────────────────────────────────
private static function resolveFixedVar(string $value): string
{
@@ -1141,7 +1155,7 @@ class NormalBot
$nav = "\n\nEscribe *menu* para ver más opciones o *salir* para terminar.";
if ($endpointKey === '') {
ConversationContext::reset($ctxId);
self::preservingReset($ctxId, ConversationContext::getMetadata($ctxId));
return self::sendText('Error: endpoint no configurado.' . $nav, $context['from'], $company);
}
@@ -1151,7 +1165,7 @@ class NormalBot
if (!$ep || empty($ep['url'])) {
self::log("API report: endpoint_key '{$endpointKey}' no configurado para company {$company['id']}");
ConversationContext::reset($ctxId);
self::preservingReset($ctxId, ConversationContext::getMetadata($ctxId));
return self::sendText('El informe solicitado no está configurado. Contacta al administrador.' . $nav, $context['from'], $company);
}
@@ -1261,13 +1275,13 @@ class NormalBot
if ($error) {
self::log("API report error [{$endpointKey}]: {$error}");
ConversationContext::reset($ctxId);
self::preservingReset($ctxId, $meta);
return self::sendText('Error al obtener el reporte. Intenta de nuevo.' . $nav, $context['from'], $company);
}
if ($httpCode >= 400) {
self::log("API report HTTP {$httpCode} [{$endpointKey}]: " . mb_substr($content, 0, 200));
ConversationContext::reset($ctxId);
self::preservingReset($ctxId, $meta);
return self::sendText('Error al obtener el reporte. Intenta de nuevo.' . $nav, $context['from'], $company);
}
@@ -1288,7 +1302,7 @@ class NormalBot
unset($nextParams['next_endpoint_key'], $nextParams['next_caption'], $nextParams['next_filename']);
return self::executeApiReport($nextParams, $context, $company, $ctxId);
}
ConversationContext::reset($ctxId);
self::preservingReset($ctxId, $meta);
return self::sendText('️ ' . $msg . $nav, $context['from'], $company);
}
@@ -1321,7 +1335,7 @@ class NormalBot
if (!$upload['success'] || !$upload['media_id']) {
self::log("API report upload failed [{$endpointKey}]: " . ($upload['error'] ?? 'unknown'));
ConversationContext::reset($ctxId);
self::preservingReset($ctxId, $meta);
return self::sendText('Error al enviar el reporte. Intenta de nuevo.' . $nav, $context['from'], $company);
}
@@ -1329,7 +1343,7 @@ class NormalBot
$caption = $params['caption'] ?? 'Aquí tienes el reporte solicitado.';
WhatsAppSender::sendDocument($context['from'], $upload['media_id'], $phoneNumberId, $caption, $reportName);
ConversationContext::reset($ctxId);
self::preservingReset($ctxId, $meta);
return self::sendText('✅ Reporte generado.' . $nav, $context['from'], $company);
}