From acc4e5c287cb92b15809ca1d2957964cdcee017e Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Tue, 28 Jul 2026 02:28:57 +0530 Subject: [PATCH] Prefer recent granted/completed over pending 404 snapshot --- app/Controllers/Patient.php | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/app/Controllers/Patient.php b/app/Controllers/Patient.php index 9dcb772..ff269ba 100644 --- a/app/Controllers/Patient.php +++ b/app/Controllers/Patient.php @@ -3027,7 +3027,7 @@ class Patient extends BaseController } $fields = $this->db->getFieldNames('abdm_hiu_workflows') ?? []; - $select = ['operation', 'workflow_state', 'status', 'request_id', 'consent_id', 'updated_at', 'last_error', 'http_code', 'response_json']; + $select = ['id', 'operation', 'workflow_state', 'status', 'request_id', 'consent_id', 'updated_at', 'last_error', 'http_code', 'response_json']; if (in_array('gateway_request_id', $fields, true)) { $select[] = 'gateway_request_id'; } @@ -3060,6 +3060,8 @@ class Patient extends BaseController $best = null; $bestPriority = -1; + $bestGrantedOrCompleted = null; + $bestGrantedOrCompletedId = -1; foreach ($rows as $row) { $decoded = json_decode((string) ($row['response_json'] ?? ''), true); @@ -3152,6 +3154,7 @@ class Patient extends BaseController if ($best === null || $priority > $bestPriority) { $best = [ + 'id' => (int) ($row['id'] ?? 0), 'phase' => $phase, 'request_id' => $requestId, 'consent_request_id' => $consentRequestId, @@ -3163,12 +3166,41 @@ class Patient extends BaseController ]; $bestPriority = $priority; } + + if (in_array($phase, ['GRANTED', 'COMPLETED'], true)) { + $currentId = (int) ($row['id'] ?? 0); + if ($currentId > $bestGrantedOrCompletedId) { + $bestGrantedOrCompleted = [ + 'id' => $currentId, + 'phase' => $phase, + 'request_id' => $requestId, + 'consent_request_id' => $consentRequestId, + 'consent_id' => $consentId, + 'message' => $message, + 'updated_at' => trim((string) ($row['updated_at'] ?? '')), + 'operation' => strtolower(trim((string) ($row['operation'] ?? ''))), + 'status' => strtolower(trim((string) ($row['status'] ?? ''))), + ]; + $bestGrantedOrCompletedId = $currentId; + } + } } if (! is_array($best)) { return $snapshot; } + // Guardrail: if top-ranked row is transient pending/not-found, but we already + // have a more recent granted/completed record, prefer the success state. + if ( + is_array($bestGrantedOrCompleted) + && in_array((string) ($best['phase'] ?? ''), ['REQUESTED', 'PENDING'], true) + && (string) ($best['status'] ?? '') === 'failed' + && stripos((string) ($best['message'] ?? ''), 'processed by bridge') !== false + ) { + $best = $bestGrantedOrCompleted; + } + if (($best['request_id'] ?? '') === '') { foreach ($rows as $row) { $decoded = json_decode((string) ($row['response_json'] ?? ''), true);