From c2a3e3054ff514e8e650fca14ffc461192fc48ee Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Tue, 28 Jul 2026 01:53:44 +0530 Subject: [PATCH] Resolve fallback consent status in OPD flow --- app/Controllers/Patient.php | 68 +++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/app/Controllers/Patient.php b/app/Controllers/Patient.php index 1504756..3bf3d02 100644 --- a/app/Controllers/Patient.php +++ b/app/Controllers/Patient.php @@ -1547,6 +1547,74 @@ class Patient extends BaseController || stripos($errorText, 'NOT_FOUND') !== false); if ($isNonFatalPending) { + $fallbackLookup = [ + 'abha_address' => $abhaContext['abha_address'], + ]; + if ($consentRequestRef !== '') { + $fallbackLookup['consentRequestId'] = $consentRequestRef; + } + if ($consentArtifactRef !== '') { + $fallbackLookup['consentId'] = $consentArtifactRef; + } + if ($flowRefId !== '') { + $fallbackLookup['request_id'] = $flowRefId; + } + + if ($consentRequestRef !== '' || $consentArtifactRef !== '') { + try { + $fallbackReconcile = $service->runOperation('consent_reconcile', $fallbackLookup); + } catch (\Throwable $e) { + $fallbackReconcile = [ + 'ok' => 0, + 'http_code' => 500, + 'error_text' => 'Unable to reconcile consent status using fallback reference: ' . $e->getMessage(), + ]; + } + + if ((int) ($fallbackReconcile['ok'] ?? 0) === 1) { + $reconcileResult = $fallbackReconcile; + $state = $this->deriveAutoFlowState($reconcileResult); + if ($state === 'GRANTED') { + $fetchPayload = [ + 'abha_address' => $abhaContext['abha_address'], + 'consentId' => $consentArtifactRef, + ]; + if ($fetchPayload['consentId'] === '') { + unset($fetchPayload['consentId']); + if ($consentRequestRef !== '') { + $fetchPayload['consentRequestId'] = $consentRequestRef; + } + } + + try { + $fetchResult = $service->runOperation('data_fetch', $fetchPayload); + } catch (\Throwable $e) { + return $this->response->setStatusCode(500)->setJSON([ + 'ok' => 0, + 'phase' => 'FETCH_FAILED', + 'error' => 'Unable to fetch ABDM content after fallback reconcile: ' . $e->getMessage(), + 'request_id' => $flowRefId, + ]); + } + + if ((int) ($fetchResult['ok'] ?? 0) === 1) { + return $this->response->setJSON([ + 'ok' => 1, + 'phase' => 'COMPLETED', + 'poll_again' => 0, + 'request_id' => $flowRefId, + 'message' => 'Consent status was resolved using fallback bridge reference and data fetched successfully.', + 'data' => [ + 'consent_request' => $consentResult, + 'consent_reconcile' => $reconcileResult, + 'data_fetch' => $fetchResult, + ], + ]); + } + } + } + } + $altConsentRequestRef = trim((string) ($consentRequestRef !== '' ? $consentRequestRef : ($lastSync['consent_request_id'] ?? ''))); $altConsentArtifactRef = trim((string) ($consentArtifactRef !== '' ? $consentArtifactRef : ($lastSync['consent_id'] ?? ''))); $canDirectFetch = ($altConsentArtifactRef !== '' && preg_match('/^REQ-/i', $altConsentArtifactRef) !== 1)