diff --git a/app/Controllers/Patient.php b/app/Controllers/Patient.php index 3dca5d3..8fe20f4 100644 --- a/app/Controllers/Patient.php +++ b/app/Controllers/Patient.php @@ -2923,12 +2923,35 @@ class Patient extends BaseController return; } - $status = strtoupper(trim((string) ( - $reconcile['consent']['status'] - ?? $reconcile['consent_status'] - ?? $reconcile['status'] - ?? '' - ))); + $status = ''; + if (isset($reconcile['consent']) && is_array($reconcile['consent'])) { + $status = trim((string) ( + $reconcile['consent']['status'] + ?? $reconcile['consent']['consent_status'] + ?? $reconcile['consent']['consentStatus'] + ?? '' + )); + } + if ($status === '' && isset($reconcile['consentDetail']) && is_array($reconcile['consentDetail'])) { + $status = trim((string) ($reconcile['consentDetail']['status'] ?? '')); + } + if ($status === '' && isset($reconcile['data']['consent']) && is_array($reconcile['data']['consent'])) { + $status = trim((string) ($reconcile['data']['consent']['status'] ?? '')); + } + if ($status === '') { + $status = trim((string) ($reconcile['consent_status'] ?? $reconcile['consentStatus'] ?? '')); + } + if ($status === '' || in_array(strtolower($status), ['success', 'ok', 'failed', 'error', 'status_checked'], true)) { + $status = trim((string) ($reconcile['workflow_state'] ?? '')); + } + if ($status === '' || in_array(strtolower($status), ['success', 'ok', 'failed', 'error', 'status_checked'], true)) { + $topStatus = trim((string) ($reconcile['status'] ?? '')); + if (! in_array(strtolower($topStatus), ['success', 'ok', 'failed', 'error', 'status_checked', '1', '0'], true)) { + $status = $topStatus; + } + } + + $status = strtoupper($status); $state = match ($status) { 'GRANTED', 'APPROVED', 'ACTIVE' => 'GRANTED', 'DENIED' => 'DENIED', @@ -4293,13 +4316,31 @@ class Patient extends BaseController $decoded = []; } - $rawConsentStatus = strtoupper(trim((string) ( - $decoded['consent']['status'] - ?? $decoded['consent_status'] - ?? $decoded['status'] - ?? $decoded['data']['consent']['status'] - ?? '' - ))); + $rawConsentStatus = ''; + if (isset($decoded['consent']) && is_array($decoded['consent'])) { + $rawConsentStatus = trim((string) ( + $decoded['consent']['status'] + ?? $decoded['consent']['consent_status'] + ?? $decoded['consent']['consentStatus'] + ?? '' + )); + } + if ($rawConsentStatus === '' && isset($decoded['consentDetail']) && is_array($decoded['consentDetail'])) { + $rawConsentStatus = trim((string) ($decoded['consentDetail']['status'] ?? '')); + } + if ($rawConsentStatus === '' && isset($decoded['data']['consent']) && is_array($decoded['data']['consent'])) { + $rawConsentStatus = trim((string) ($decoded['data']['consent']['status'] ?? '')); + } + if ($rawConsentStatus === '') { + $rawConsentStatus = trim((string) ($decoded['consent_status'] ?? $decoded['consentStatus'] ?? '')); + } + if ($rawConsentStatus === '' || in_array(strtolower($rawConsentStatus), ['success', 'ok', 'failed', 'error', 'status_checked'], true)) { + $topStatus = trim((string) ($decoded['status'] ?? '')); + if (! in_array(strtolower($topStatus), ['success', 'ok', 'failed', 'error', 'status_checked', '1', '0'], true)) { + $rawConsentStatus = $topStatus; + } + } + $rawConsentStatus = strtoupper($rawConsentStatus); $requestId = trim((string) ( $row['gateway_request_id'] @@ -4811,13 +4852,31 @@ class Patient extends BaseController $decoded = []; } - $rawConsentStatus = strtoupper(trim((string) ( - $decoded['consent']['status'] - ?? $decoded['consent_status'] - ?? $decoded['status'] - ?? $decoded['data']['consent']['status'] - ?? '' - ))); + $rawConsentStatus = ''; + if (isset($decoded['consent']) && is_array($decoded['consent'])) { + $rawConsentStatus = trim((string) ( + $decoded['consent']['status'] + ?? $decoded['consent']['consent_status'] + ?? $decoded['consent']['consentStatus'] + ?? '' + )); + } + if ($rawConsentStatus === '' && isset($decoded['consentDetail']) && is_array($decoded['consentDetail'])) { + $rawConsentStatus = trim((string) ($decoded['consentDetail']['status'] ?? '')); + } + if ($rawConsentStatus === '' && isset($decoded['data']['consent']) && is_array($decoded['data']['consent'])) { + $rawConsentStatus = trim((string) ($decoded['data']['consent']['status'] ?? '')); + } + if ($rawConsentStatus === '') { + $rawConsentStatus = trim((string) ($decoded['consent_status'] ?? $decoded['consentStatus'] ?? '')); + } + if ($rawConsentStatus === '' || in_array(strtolower($rawConsentStatus), ['success', 'ok', 'failed', 'error', 'status_checked'], true)) { + $topStatus = trim((string) ($decoded['status'] ?? '')); + if (! in_array(strtolower($topStatus), ['success', 'ok', 'failed', 'error', 'status_checked', '1', '0'], true)) { + $rawConsentStatus = $topStatus; + } + } + $rawConsentStatus = strtoupper($rawConsentStatus); $operation = strtoupper(trim((string) ($row['operation'] ?? ''))); $status = strtoupper(trim((string) ($row['status'] ?? ''))); $state = strtoupper(trim((string) ($row['workflow_state'] ?? ''))); @@ -4887,7 +4946,7 @@ class Patient extends BaseController // Phase selection above is priority-based, so a GRANTED/COMPLETED row // would otherwise outrank a LATER revocation/expiry/denial reported by - // the PHR app. A terminal outcome recorded after the winning row wins. + // the PHR app. A terminal outcome recorded in this session wins. $terminalPhase = ''; $terminalRowId = 0; foreach ($rows as $row) { @@ -4895,13 +4954,30 @@ class Patient extends BaseController if (! is_array($rowDecoded)) { $rowDecoded = []; } - $rowStatus = strtoupper(trim((string) ( - $rowDecoded['consent']['status'] - ?? $rowDecoded['consent_status'] - ?? $rowDecoded['status'] - ?? $rowDecoded['data']['consent']['status'] - ?? '' - ))); + $rowStatus = ''; + if (isset($rowDecoded['consent']) && is_array($rowDecoded['consent'])) { + $rowStatus = trim((string) ( + $rowDecoded['consent']['status'] + ?? $rowDecoded['consent']['consent_status'] + ?? '' + )); + } + if ($rowStatus === '' && isset($rowDecoded['consentDetail']) && is_array($rowDecoded['consentDetail'])) { + $rowStatus = trim((string) ($rowDecoded['consentDetail']['status'] ?? '')); + } + if ($rowStatus === '' && isset($rowDecoded['data']['consent']) && is_array($rowDecoded['data']['consent'])) { + $rowStatus = trim((string) ($rowDecoded['data']['consent']['status'] ?? '')); + } + if ($rowStatus === '') { + $rowStatus = trim((string) ($rowDecoded['consent_status'] ?? $rowDecoded['consentStatus'] ?? '')); + } + if ($rowStatus === '' || in_array(strtolower($rowStatus), ['success', 'ok', 'failed', 'error', 'status_checked'], true)) { + $rowState = strtoupper(trim((string) ($row['workflow_state'] ?? ''))); + if (in_array($rowState, ['REVOKED', 'EXPIRED', 'DENIED'], true)) { + $rowStatus = $rowState; + } + } + $rowStatus = strtoupper($rowStatus); if (! in_array($rowStatus, ['REVOKED', 'EXPIRED', 'DENIED'], true)) { continue; } @@ -4911,7 +4987,7 @@ class Patient extends BaseController $terminalRowId = $rowId; } } - if ($terminalPhase !== '' && $terminalRowId > (int) ($best['id'] ?? 0)) { + if ($terminalPhase !== '') { $phase = $terminalPhase; } diff --git a/app/Libraries/Abdm/ConsentSessionListService.php b/app/Libraries/Abdm/ConsentSessionListService.php index edc1e84..0f40760 100644 --- a/app/Libraries/Abdm/ConsentSessionListService.php +++ b/app/Libraries/Abdm/ConsentSessionListService.php @@ -232,13 +232,31 @@ class ConsentSessionListService $decoded = []; } - $rawConsentStatus = strtoupper(trim((string) ( - $decoded['consent']['status'] - ?? $decoded['consent_status'] - ?? $decoded['status'] - ?? $decoded['data']['consent']['status'] - ?? '' - ))); + $rawConsentStatus = ''; + if (isset($decoded['consent']) && is_array($decoded['consent'])) { + $rawConsentStatus = trim((string) ( + $decoded['consent']['status'] + ?? $decoded['consent']['consent_status'] + ?? $decoded['consent']['consentStatus'] + ?? '' + )); + } + if ($rawConsentStatus === '' && isset($decoded['consentDetail']) && is_array($decoded['consentDetail'])) { + $rawConsentStatus = trim((string) ($decoded['consentDetail']['status'] ?? '')); + } + if ($rawConsentStatus === '' && isset($decoded['data']['consent']) && is_array($decoded['data']['consent'])) { + $rawConsentStatus = trim((string) ($decoded['data']['consent']['status'] ?? '')); + } + if ($rawConsentStatus === '') { + $rawConsentStatus = trim((string) ($decoded['consent_status'] ?? $decoded['consentStatus'] ?? '')); + } + if ($rawConsentStatus === '' || in_array(strtolower($rawConsentStatus), ['success', 'ok', 'failed', 'error', 'status_checked'], true)) { + $topStatus = trim((string) ($decoded['status'] ?? '')); + if (! in_array(strtolower($topStatus), ['success', 'ok', 'failed', 'error', 'status_checked', '1', '0'], true)) { + $rawConsentStatus = $topStatus; + } + } + $rawConsentStatus = strtoupper($rawConsentStatus); $operation = strtoupper(trim((string) ($row['operation'] ?? ''))); $status = strtoupper(trim((string) ($row['status'] ?? ''))); $state = strtoupper(trim((string) ($row['workflow_state'] ?? ''))); @@ -297,6 +315,50 @@ class ConsentSessionListService } $phase = (string) $best['_phase']; + + $terminalPhase = ''; + $terminalRowId = 0; + foreach ($rows as $row) { + $rowDecoded = json_decode((string) ($row['response_json'] ?? ''), true); + if (! is_array($rowDecoded)) { + $rowDecoded = []; + } + $rowStatus = ''; + if (isset($rowDecoded['consent']) && is_array($rowDecoded['consent'])) { + $rowStatus = trim((string) ( + $rowDecoded['consent']['status'] + ?? $rowDecoded['consent']['consent_status'] + ?? '' + )); + } + if ($rowStatus === '' && isset($rowDecoded['consentDetail']) && is_array($rowDecoded['consentDetail'])) { + $rowStatus = trim((string) ($rowDecoded['consentDetail']['status'] ?? '')); + } + if ($rowStatus === '' && isset($rowDecoded['data']['consent']) && is_array($rowDecoded['data']['consent'])) { + $rowStatus = trim((string) ($rowDecoded['data']['consent']['status'] ?? '')); + } + if ($rowStatus === '') { + $rowStatus = trim((string) ($rowDecoded['consent_status'] ?? $rowDecoded['consentStatus'] ?? '')); + } + if ($rowStatus === '' || in_array(strtolower($rowStatus), ['success', 'ok', 'failed', 'error', 'status_checked'], true)) { + $rowState = strtoupper(trim((string) ($row['workflow_state'] ?? ''))); + if (in_array($rowState, ['REVOKED', 'EXPIRED', 'DENIED'], true)) { + $rowStatus = $rowState; + } + } + $rowStatus = strtoupper($rowStatus); + if (! in_array($rowStatus, ['REVOKED', 'EXPIRED', 'DENIED'], true)) { + continue; + } + $rowId = (int) ($row['id'] ?? 0); + if ($rowId >= $terminalRowId) { + $terminalPhase = $rowStatus; + $terminalRowId = $rowId; + } + } + if ($terminalPhase !== '') { + $phase = $terminalPhase; + } $consentId = trim((string) ($best['abdm_consent_artifact_id'] ?? $best['consent_id'] ?? $bestDecoded['consent_id'] ?? $bestDecoded['consentId'] ?? '')); $consentRequestId = trim((string) ($best['abdm_consent_request_id'] ?? $bestDecoded['abdm_consent_request_id'] ?? $bestDecoded['consent_request_id'] ?? $bestDecoded['consentRequestId'] ?? '')); diff --git a/app/Libraries/Abdm/M3HiuGatewayClient.php b/app/Libraries/Abdm/M3HiuGatewayClient.php index a5f1286..d2aae2d 100644 --- a/app/Libraries/Abdm/M3HiuGatewayClient.php +++ b/app/Libraries/Abdm/M3HiuGatewayClient.php @@ -370,8 +370,55 @@ class M3HiuGatewayClient // under `consent{}` (e.g. { consent: { status: "GRANTED", ... } }) rather // than at the top level. Flatten them so resolveState()/snapshot logic // (which reads $response['consent_status']) can see the real value — - // otherwise GRANTED consents keep getting misclassified as pending. - $abdmConsentStatus = $this->findFirstValueByKeys($response, ['status']); + // otherwise GRANTED/EXPIRED/REVOKED consents keep getting misclassified. + $abdmConsentStatus = ''; + if (isset($response['consent']) && is_array($response['consent'])) { + $abdmConsentStatus = trim((string) ( + $response['consent']['status'] + ?? $response['consent']['consent_status'] + ?? $response['consent']['consentStatus'] + ?? $response['consent']['state'] + ?? '' + )); + } + if ($abdmConsentStatus === '' && isset($response['consentDetail']) && is_array($response['consentDetail'])) { + $abdmConsentStatus = trim((string) ( + $response['consentDetail']['status'] + ?? $response['consentDetail']['consent_status'] + ?? $response['consentDetail']['consentStatus'] + ?? '' + )); + } + if ($abdmConsentStatus === '' && isset($response['data']) && is_array($response['data'])) { + if (isset($response['data']['consent']) && is_array($response['data']['consent'])) { + $abdmConsentStatus = trim((string) ( + $response['data']['consent']['status'] + ?? $response['data']['consent']['consent_status'] + ?? $response['data']['consent']['consentStatus'] + ?? '' + )); + } + if ($abdmConsentStatus === '') { + $abdmConsentStatus = trim((string) ( + $response['data']['consent_status'] + ?? $response['data']['consentStatus'] + ?? '' + )); + } + } + if ($abdmConsentStatus === '') { + $abdmConsentStatus = trim((string) ( + $response['consent_status'] + ?? $response['consentStatus'] + ?? '' + )); + } + if ($abdmConsentStatus === '') { + $potentialStatus = $this->findFirstValueByKeys($response, ['consent_status', 'consentStatus', 'status']); + if (! in_array(strtolower($potentialStatus), ['success', 'ok', 'failed', 'error', 'true', 'false', '1', '0'], true)) { + $abdmConsentStatus = $potentialStatus; + } + } if ($abdmConsentStatus !== '') { $response['consent_status'] = strtoupper($abdmConsentStatus); } diff --git a/app/Libraries/Abdm/M3HiuWorkflowService.php b/app/Libraries/Abdm/M3HiuWorkflowService.php index a2ca0c6..39dcf10 100644 --- a/app/Libraries/Abdm/M3HiuWorkflowService.php +++ b/app/Libraries/Abdm/M3HiuWorkflowService.php @@ -1427,7 +1427,42 @@ class M3HiuWorkflowService return 'FAILED'; } - $statusStr = strtolower(trim((string) ($result['status'] ?? $result['consent_status'] ?? ''))); + $rawConsent = ''; + if (isset($result['consent']) && is_array($result['consent'])) { + $rawConsent = (string) ( + $result['consent']['status'] + ?? $result['consent']['consent_status'] + ?? $result['consent']['consentStatus'] + ?? $result['consent']['state'] + ?? '' + ); + } + if ($rawConsent === '' && isset($result['consentDetail']) && is_array($result['consentDetail'])) { + $rawConsent = (string) ( + $result['consentDetail']['status'] + ?? $result['consentDetail']['consent_status'] + ?? $result['consentDetail']['consentStatus'] + ?? '' + ); + } + if ($rawConsent === '' && isset($result['data']['consent']) && is_array($result['data']['consent'])) { + $rawConsent = (string) ( + $result['data']['consent']['status'] + ?? $result['data']['consent']['consent_status'] + ?? '' + ); + } + if ($rawConsent === '') { + $rawConsent = (string) ($result['consent_status'] ?? $result['consentStatus'] ?? ''); + } + if ($rawConsent === '' || in_array(strtolower($rawConsent), ['success', 'ok', 'failed', 'error'], true)) { + $topStatus = (string) ($result['status'] ?? ''); + if (! in_array(strtolower($topStatus), ['success', 'ok', 'failed', 'error', '1', '0'], true)) { + $rawConsent = $topStatus; + } + } + + $statusStr = strtolower(trim($rawConsent)); if (in_array($statusStr, ['revoked', 'denied'], true)) { return 'REVOKED'; } @@ -1437,13 +1472,13 @@ class M3HiuWorkflowService return match ($operation) { 'consent_request' => 'REQUESTED', - 'consent_status' => 'STATUS_CHECKED', + 'consent_status' => ($statusStr === 'granted' || $statusStr === 'approved' || $statusStr === 'active' ? 'GRANTED' : ($statusStr === 'revoked' ? 'REVOKED' : ($statusStr === 'expired' ? 'EXPIRED' : ($statusStr === 'denied' ? 'DENIED' : 'STATUS_CHECKED')))), 'consent_fetch' => 'CONSENT_FETCHED', - 'consent_callback' => ($statusStr === 'granted' || $statusStr === 'approved' ? 'GRANTED' : ($statusStr === 'revoked' ? 'REVOKED' : ($statusStr === 'expired' ? 'EXPIRED' : 'STATUS_CHECKED'))), + 'consent_callback' => ($statusStr === 'granted' || $statusStr === 'approved' || $statusStr === 'active' ? 'GRANTED' : ($statusStr === 'revoked' ? 'REVOKED' : ($statusStr === 'expired' ? 'EXPIRED' : ($statusStr === 'denied' ? 'DENIED' : 'STATUS_CHECKED')))), 'hi_request' => ($statusStr === 'completed' ? 'COMPLETED' : 'DATA_REQUESTED'), 'hi_on_request_callback' => (($statusStr === 'data_received' || isset($result['fhir_bundle']) || isset($result['bundles']) || isset($result['entries'])) ? 'DATA_RECEIVED' : ($ok ? 'DATA_PENDING' : 'FAILED')), 'hi_data_push_callback' => (($statusStr === 'data_received' || isset($result['fhir_bundle']) || isset($result['bundles']) || isset($result['entries'])) ? 'DATA_RECEIVED' : ($ok ? 'DATA_PENDING' : 'FAILED')), - 'consent_reconcile' => ($statusStr === 'granted' ? 'GRANTED' : ($statusStr === 'revoked' ? 'REVOKED' : ($statusStr === 'expired' ? 'EXPIRED' : 'STATUS_CHECKED'))), + 'consent_reconcile' => ($statusStr === 'granted' || $statusStr === 'approved' || $statusStr === 'active' ? 'GRANTED' : ($statusStr === 'revoked' ? 'REVOKED' : ($statusStr === 'expired' ? 'EXPIRED' : ($statusStr === 'denied' ? 'DENIED' : 'STATUS_CHECKED')))), 'data_fetch' => (($statusStr === 'completed' || isset($result['fhir_bundle']) || isset($result['bundles'])) ? 'DATA_RECEIVED' : 'DATA_PENDING'), default => 'COMPLETED', };