Update ConversationStateService.php

This commit is contained in:
Lizandro Guarnizo
2026-01-24 01:58:05 -05:00
parent 6e89b1868b
commit d5ff1f2df9
+21 -40
View File
@@ -37,14 +37,10 @@ class ConversationStateService
*/ */
public function getState(string $phoneNumber): ?array public function getState(string $phoneNumber): ?array
{ {
$stmt = $this->db->prepare(" $state = $this->db->fetch(
SELECT * FROM user_states "SELECT * FROM user_states WHERE phone_number = :phone ORDER BY id DESC LIMIT 1",
WHERE phone_number = ? ['phone' => $phoneNumber]
ORDER BY id DESC );
LIMIT 1
");
$stmt->execute([$phoneNumber]);
$state = $stmt->fetch(PDO::FETCH_ASSOC);
if ($state && $state['state_data']) { if ($state && $state['state_data']) {
$state['state_data'] = json_decode($state['state_data'], true); $state['state_data'] = json_decode($state['state_data'], true);
@@ -62,18 +58,17 @@ class ConversationStateService
*/ */
public function setState(string $phoneNumber, string $state, array $data = []): bool public function setState(string $phoneNumber, string $state, array $data = []): bool
{ {
$stmt = $this->db->prepare(" $sql = "INSERT INTO user_states (phone_number, state, state_data, created_at, updated_at)
INSERT INTO user_states (phone_number, state, state_data, created_at, updated_at) VALUES (:phone, :state, :state_data, NOW(), NOW())
VALUES (?, ?, ?, NOW(), NOW())
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
state = VALUES(state), state = VALUES(state),
state_data = VALUES(state_data), state_data = VALUES(state_data),
updated_at = NOW() updated_at = NOW()";
");
$stateDataJson = !empty($data) ? json_encode($data, JSON_UNESCAPED_UNICODE) : null; $stateDataJson = !empty($data) ? json_encode($data, JSON_UNESCAPED_UNICODE) : null;
return $stmt->execute([$phoneNumber, $state, $stateDataJson]); $this->db->query($sql, ['phone' => $phoneNumber, 'state' => $state, 'state_data' => $stateDataJson]);
return true;
} }
/** /**
@@ -93,16 +88,11 @@ class ConversationStateService
$existingData = $currentState['state_data'] ?? []; $existingData = $currentState['state_data'] ?? [];
$mergedData = array_merge($existingData, $data); $mergedData = array_merge($existingData, $data);
$stmt = $this->db->prepare(" $this->db->query(
UPDATE user_states "UPDATE user_states SET state_data = :sd, updated_at = NOW() WHERE phone_number = :phone",
SET state_data = ?, updated_at = NOW() ['sd' => json_encode($mergedData, JSON_UNESCAPED_UNICODE), 'phone' => $phoneNumber]
WHERE phone_number = ? );
"); return true;
return $stmt->execute([
json_encode($mergedData, JSON_UNESCAPED_UNICODE),
$phoneNumber
]);
} }
/** /**
@@ -129,12 +119,7 @@ class ConversationStateService
*/ */
public function clearState(string $phoneNumber): bool public function clearState(string $phoneNumber): bool
{ {
$stmt = $this->db->prepare(" return $this->db->delete('user_states', 'phone_number = :phone', ['phone' => $phoneNumber]);
DELETE FROM user_states
WHERE phone_number = ?
");
return $stmt->execute([$phoneNumber]);
} }
/** /**
@@ -232,18 +217,14 @@ class ConversationStateService
*/ */
public function cleanExpiredStates(): int public function cleanExpiredStates(): int
{ {
$stmt = $this->db->prepare(" $sql = "DELETE FROM user_states
DELETE FROM user_states
WHERE updated_at < DATE_SUB(NOW(), INTERVAL 30 MINUTE) WHERE updated_at < DATE_SUB(NOW(), INTERVAL 30 MINUTE)
AND state NOT IN (?, ?) AND state NOT IN (?, ?)";
");
return $this->db->execute($sql, [
$stmt->execute([
self::STATE_SCHEDULED, self::STATE_SCHEDULED,
self::STATE_WITH_ADVISOR self::STATE_WITH_ADVISOR
]); ]);
return $stmt->rowCount();
} }
/** /**