From f23283124a0b12c71a86bf0685b2d0600c218301 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Thu, 30 Jul 2026 17:58:10 -0500 Subject: [PATCH] fix: preservar finca_id al resetear contexto tras generar reporte MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- services/NormalBot.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/services/NormalBot.php b/services/NormalBot.php index 6f70044..cfbcd78 100644 --- a/services/NormalBot.php +++ b/services/NormalBot.php @@ -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); }