Fix Consent ID, Consent Request ID, and date metadata resolution across ABDM client, workflow service, controller, and views

This commit is contained in:
DevSoftTech Server 2026-09-03 12:37:11 +05:30
parent 398ad3238e
commit 05622a12de
6 changed files with 413 additions and 42 deletions

View File

@ -5016,9 +5016,20 @@ class Patient extends BaseController
?? $row['consent_id']
?? $rowDecoded['consent_id']
?? $rowDecoded['consentId']
?? $rowDecoded['consent']['id']
?? $rowDecoded['consent']['consent_id']
?? $rowDecoded['consent']['consentId']
?? $rowDecoded['consentDetail']['id']
?? $rowDecoded['consentDetail']['consentId']
?? $rowDecoded['consent_artifact_id']
?? $rowDecoded['consent_artefact_id']
?? $rowDecoded['consentArtifactId']
?? $rowDecoded['consentArtefactId']
?? $rowDecoded['data']['consent']['id']
?? $rowDecoded['data']['consent_id']
?? ''
));
if ($rowConsentId !== '') {
if ($rowConsentId !== '' && ! preg_match('/^REQ-/i', $rowConsentId)) {
$consentId = $rowConsentId;
}
@ -5027,12 +5038,20 @@ class Patient extends BaseController
?? $rowDecoded['abdm_consent_request_id']
?? $rowDecoded['consent_request_id']
?? $rowDecoded['consentRequestId']
?? $rowDecoded['consent']['consent_request_id']
?? $rowDecoded['consent']['consentRequestId']
?? $rowDecoded['consentDetail']['consent_request_id']
?? $rowDecoded['consentDetail']['consentRequestId']
?? $rowDecoded['data']['consent_request_id']
?? ''
));
if ($rowConsentRequestId !== '') {
if ($rowConsentRequestId !== '' && ! preg_match('/^REQ-/i', $rowConsentRequestId)) {
$consentRequestId = $rowConsentRequestId;
}
}
if ($consentId === '' && $consentRequestId !== '') {
$consentId = $consentRequestId;
}
// The session should start with its own CONSENT_REQUEST row.
$consentRequestRow = null;
@ -5061,7 +5080,6 @@ class Patient extends BaseController
$displayId = (int) ($best['id'] ?? 0);
}
$requestedHiTypes = [];
$requestedOn = '';
$purpose = '';
@ -5080,15 +5098,81 @@ class Patient extends BaseController
$requestedHiTypes = $this->normalizeHiTypesList($consentBlock['hiTypes'] ?? $consentBlock['hi_types'] ?? []);
$requestedOn = trim((string) ($consentRequestRow['created_at'] ?? ''));
$purpose = trim((string) ($consentBlock['purpose']['text'] ?? $consentBlock['purpose']['code'] ?? ''));
$validFrom = trim((string) ($consentBlock['permission']['dateRange']['from'] ?? ''));
$validTo = trim((string) ($consentBlock['permission']['dateRange']['to'] ?? ''));
$eraseAt = trim((string) ($consentBlock['permission']['dataEraseAt'] ?? ''));
$validFrom = trim((string) ($consentBlock['permission']['dateRange']['from'] ?? $consentBlock['permission']['date_range']['from'] ?? ''));
$validTo = trim((string) ($consentBlock['permission']['dateRange']['to'] ?? $consentBlock['permission']['date_range']['to'] ?? ''));
$eraseAt = trim((string) ($consentBlock['permission']['dataEraseAt'] ?? $consentBlock['permission']['data_erase_at'] ?? ''));
$requestedBy = trim((string) ($consentBlock['requester']['name'] ?? ''));
if ($hfrId === '') {
$hfrId = trim((string) ($consentRequestRow['hfr_id'] ?? ''));
}
}
// Fallback scanning across all session rows for missing metadata (dates, purpose, requestedBy)
foreach ($rows as $row) {
$rowReq = json_decode((string) ($row['request_json'] ?? ''), true);
$rowResp = json_decode((string) ($row['response_json'] ?? ''), true);
$containers = [
is_array($rowReq) ? ($rowReq['consent'] ?? null) : null,
is_array($rowReq) ? ($rowReq['consentDetail'] ?? null) : null,
is_array($rowReq) ? $rowReq : null,
is_array($rowResp) ? ($rowResp['consent'] ?? null) : null,
is_array($rowResp) ? ($rowResp['consentDetail'] ?? null) : null,
is_array($rowResp) ? $rowResp : null,
];
foreach ($containers as $c) {
if (! is_array($c)) {
continue;
}
if ($validFrom === '') {
$validFrom = trim((string) (
$c['permission']['dateRange']['from']
?? $c['permission']['date_range']['from']
?? $c['date_range']['from']
?? $c['dateRange']['from']
?? ''
));
}
if ($validTo === '') {
$validTo = trim((string) (
$c['permission']['dateRange']['to']
?? $c['permission']['date_range']['to']
?? $c['date_range']['to']
?? $c['dateRange']['to']
?? ''
));
}
if ($eraseAt === '') {
$eraseAt = trim((string) (
$c['permission']['dataEraseAt']
?? $c['permission']['data_erase_at']
?? $c['expiry']
?? $c['dataEraseAt']
?? $c['data_erase_at']
?? ''
));
}
if ($purpose === '') {
$purpose = trim((string) (
$c['purpose']['text']
?? $c['purpose']['code']
?? $c['purpose']
?? ''
));
}
if ($requestedBy === '') {
$requestedBy = trim((string) (
$c['requester']['name']
?? $c['requester']
?? ''
));
}
if ($requestedOn === '') {
$requestedOn = trim((string) ($row['created_at'] ?? ''));
}
}
}
// Bridge flattens consent.hi_types -> top-level hi_types on consent_status /
// consent_reconcile / consent_callback responses (see M3HiuGatewayClient
// flatten logic). The "best" row for overall phase can be a data_fetch /

View File

@ -359,8 +359,55 @@ class ConsentSessionListService
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'] ?? ''));
$consentId = '';
$consentRequestId = '';
foreach ($rows as $row) {
$rowDecoded = json_decode((string) ($row['response_json'] ?? ''), true);
if (! is_array($rowDecoded)) {
$rowDecoded = [];
}
$rowConsentId = trim((string) (
$row['abdm_consent_artifact_id']
?? $row['consent_id']
?? $rowDecoded['consent_id']
?? $rowDecoded['consentId']
?? $rowDecoded['consent']['id']
?? $rowDecoded['consent']['consent_id']
?? $rowDecoded['consent']['consentId']
?? $rowDecoded['consentDetail']['id']
?? $rowDecoded['consentDetail']['consentId']
?? $rowDecoded['consent_artifact_id']
?? $rowDecoded['consent_artefact_id']
?? $rowDecoded['consentArtifactId']
?? $rowDecoded['consentArtefactId']
?? $rowDecoded['data']['consent']['id']
?? $rowDecoded['data']['consent_id']
?? ''
));
if ($rowConsentId !== '' && ! preg_match('/^REQ-/i', $rowConsentId)) {
$consentId = $rowConsentId;
}
$rowConsentRequestId = trim((string) (
$row['abdm_consent_request_id']
?? $rowDecoded['abdm_consent_request_id']
?? $rowDecoded['consent_request_id']
?? $rowDecoded['consentRequestId']
?? $rowDecoded['consent']['consent_request_id']
?? $rowDecoded['consent']['consentRequestId']
?? $rowDecoded['consentDetail']['consent_request_id']
?? $rowDecoded['consentDetail']['consentRequestId']
?? $rowDecoded['data']['consent_request_id']
?? ''
));
if ($rowConsentRequestId !== '' && ! preg_match('/^REQ-/i', $rowConsentRequestId)) {
$consentRequestId = $rowConsentRequestId;
}
}
if ($consentId === '' && $consentRequestId !== '') {
$consentId = $consentRequestId;
}
// The session should start with its own CONSENT_REQUEST row.
$consentRequestRow = null;
@ -389,15 +436,81 @@ class ConsentSessionListService
$requestedHiTypes = $this->normalizeHiTypesList($consentBlock['hiTypes'] ?? $consentBlock['hi_types'] ?? []);
$requestedOn = trim((string) ($consentRequestRow['created_at'] ?? ''));
$purpose = trim((string) ($consentBlock['purpose']['text'] ?? $consentBlock['purpose']['code'] ?? ''));
$validFrom = trim((string) ($consentBlock['permission']['dateRange']['from'] ?? ''));
$validTo = trim((string) ($consentBlock['permission']['dateRange']['to'] ?? ''));
$eraseAt = trim((string) ($consentBlock['permission']['dataEraseAt'] ?? ''));
$validFrom = trim((string) ($consentBlock['permission']['dateRange']['from'] ?? $consentBlock['permission']['date_range']['from'] ?? ''));
$validTo = trim((string) ($consentBlock['permission']['dateRange']['to'] ?? $consentBlock['permission']['date_range']['to'] ?? ''));
$eraseAt = trim((string) ($consentBlock['permission']['dataEraseAt'] ?? $consentBlock['permission']['data_erase_at'] ?? ''));
$requestedBy = trim((string) ($consentBlock['requester']['name'] ?? ''));
if ($hfrId === '') {
$hfrId = trim((string) ($consentRequestRow['hfr_id'] ?? ''));
}
}
// Fallback scanning across all session rows for missing metadata (dates, purpose, requestedBy)
foreach ($rows as $row) {
$rowReq = json_decode((string) ($row['request_json'] ?? ''), true);
$rowResp = json_decode((string) ($row['response_json'] ?? ''), true);
$containers = [
is_array($rowReq) ? ($rowReq['consent'] ?? null) : null,
is_array($rowReq) ? ($rowReq['consentDetail'] ?? null) : null,
is_array($rowReq) ? $rowReq : null,
is_array($rowResp) ? ($rowResp['consent'] ?? null) : null,
is_array($rowResp) ? ($rowResp['consentDetail'] ?? null) : null,
is_array($rowResp) ? $rowResp : null,
];
foreach ($containers as $c) {
if (! is_array($c)) {
continue;
}
if ($validFrom === '') {
$validFrom = trim((string) (
$c['permission']['dateRange']['from']
?? $c['permission']['date_range']['from']
?? $c['date_range']['from']
?? $c['dateRange']['from']
?? ''
));
}
if ($validTo === '') {
$validTo = trim((string) (
$c['permission']['dateRange']['to']
?? $c['permission']['date_range']['to']
?? $c['date_range']['to']
?? $c['dateRange']['to']
?? ''
));
}
if ($eraseAt === '') {
$eraseAt = trim((string) (
$c['permission']['dataEraseAt']
?? $c['permission']['data_erase_at']
?? $c['expiry']
?? $c['dataEraseAt']
?? $c['data_erase_at']
?? ''
));
}
if ($purpose === '') {
$purpose = trim((string) (
$c['purpose']['text']
?? $c['purpose']['code']
?? $c['purpose']
?? ''
));
}
if ($requestedBy === '') {
$requestedBy = trim((string) (
$c['requester']['name']
?? $c['requester']
?? ''
));
}
if ($requestedOn === '') {
$requestedOn = trim((string) ($row['created_at'] ?? ''));
}
}
}
$grantedHiTypes = [];
$grantedOn = '';
foreach ($rows as $row) {

View File

@ -170,27 +170,100 @@ class M3HiuGatewayClient
}
$response['gateway_request_id'] = $gatewayRequestId;
$abdmConsentRequestId = $this->findFirstValueByKeys($response, [
'consentRequestId',
'consent_request_id',
'abdm_consent_request_id',
]);
if ($abdmConsentRequestId !== '' && $this->isGatewayRefId($abdmConsentRequestId)) {
$abdmConsentRequestId = '';
$abdmConsentRequestId = '';
if (isset($response['consent']) && is_array($response['consent'])) {
$abdmConsentRequestId = trim((string) (
$response['consent']['consent_request_id']
?? $response['consent']['consentRequestId']
?? $response['consent']['abdm_consent_request_id']
?? ''
));
}
if ($abdmConsentRequestId !== '') {
if ($abdmConsentRequestId === '' && isset($response['consentDetail']) && is_array($response['consentDetail'])) {
$abdmConsentRequestId = trim((string) (
$response['consentDetail']['consent_request_id']
?? $response['consentDetail']['consentRequestId']
?? ''
));
}
if ($abdmConsentRequestId === '' && isset($response['data']) && is_array($response['data'])) {
if (isset($response['data']['consent']) && is_array($response['data']['consent'])) {
$abdmConsentRequestId = trim((string) (
$response['data']['consent']['consent_request_id']
?? $response['data']['consent']['consentRequestId']
?? ''
));
}
if ($abdmConsentRequestId === '') {
$abdmConsentRequestId = trim((string) (
$response['data']['consent_request_id']
?? $response['data']['consentRequestId']
?? $response['data']['abdm_consent_request_id']
?? ''
));
}
}
if ($abdmConsentRequestId === '') {
$abdmConsentRequestId = $this->findFirstValueByKeys($response, [
'consentRequestId',
'consent_request_id',
'abdm_consent_request_id',
]);
}
if ($abdmConsentRequestId !== '' && ! $this->isGatewayRefId($abdmConsentRequestId)) {
$response['abdm_consent_request_id'] = $abdmConsentRequestId;
$response['consent_request_id'] = $abdmConsentRequestId;
}
$abdmConsentId = $this->findFirstValueByKeys($response, [
'consentId',
'consent_id',
]);
if ($abdmConsentId !== '' && $this->isGatewayRefId($abdmConsentId)) {
$abdmConsentId = '';
$abdmConsentId = '';
if (isset($response['consent']) && is_array($response['consent'])) {
$abdmConsentId = trim((string) (
$response['consent']['id']
?? $response['consent']['consent_id']
?? $response['consent']['consentId']
?? $response['consent']['consent_artifact_id']
?? $response['consent']['consent_artefact_id']
?? ''
));
}
if ($abdmConsentId !== '') {
if ($abdmConsentId === '' && isset($response['consentDetail']) && is_array($response['consentDetail'])) {
$abdmConsentId = trim((string) (
$response['consentDetail']['id']
?? $response['consentDetail']['consent_id']
?? $response['consentDetail']['consentId']
?? ''
));
}
if ($abdmConsentId === '' && isset($response['data']) && is_array($response['data'])) {
if (isset($response['data']['consent']) && is_array($response['data']['consent'])) {
$abdmConsentId = trim((string) (
$response['data']['consent']['id']
?? $response['data']['consent']['consent_id']
?? $response['data']['consent']['consentId']
?? ''
));
}
if ($abdmConsentId === '') {
$abdmConsentId = trim((string) (
$response['data']['consent_id']
?? $response['data']['consentId']
?? $response['data']['consent_artifact_id']
?? ''
));
}
}
if ($abdmConsentId === '') {
$abdmConsentId = $this->findFirstValueByKeys($response, [
'consent_id',
'consentId',
'abdm_consent_artifact_id',
'consent_artifact_id',
'consent_artefact_id',
'consentArtifactId',
'consentArtefactId',
]);
}
if ($abdmConsentId !== '' && ! $this->isGatewayRefId($abdmConsentId)) {
$response['consent_id'] = $abdmConsentId;
$response['abdm_consent_artifact_id'] = $abdmConsentId;
}
@ -346,21 +419,99 @@ class M3HiuGatewayClient
$gatewayRequestId = trim((string) ($response['request_id'] ?? $response['requestId'] ?? $cleanQuery['request_id'] ?? ''));
$response['gateway_request_id'] = $gatewayRequestId;
$abdmConsentRequestId = $this->findFirstValueByKeys($response, [
'consentRequestId',
'consent_request_id',
'abdm_consent_request_id',
]);
$abdmConsentRequestId = '';
if (isset($response['consent']) && is_array($response['consent'])) {
$abdmConsentRequestId = trim((string) (
$response['consent']['consent_request_id']
?? $response['consent']['consentRequestId']
?? $response['consent']['abdm_consent_request_id']
?? ''
));
}
if ($abdmConsentRequestId === '' && isset($response['consentDetail']) && is_array($response['consentDetail'])) {
$abdmConsentRequestId = trim((string) (
$response['consentDetail']['consent_request_id']
?? $response['consentDetail']['consentRequestId']
?? ''
));
}
if ($abdmConsentRequestId === '' && isset($response['data']) && is_array($response['data'])) {
if (isset($response['data']['consent']) && is_array($response['data']['consent'])) {
$abdmConsentRequestId = trim((string) (
$response['data']['consent']['consent_request_id']
?? $response['data']['consent']['consentRequestId']
?? ''
));
}
if ($abdmConsentRequestId === '') {
$abdmConsentRequestId = trim((string) (
$response['data']['consent_request_id']
?? $response['data']['consentRequestId']
?? $response['data']['abdm_consent_request_id']
?? ''
));
}
}
if ($abdmConsentRequestId === '') {
$abdmConsentRequestId = $this->findFirstValueByKeys($response, [
'consentRequestId',
'consent_request_id',
'abdm_consent_request_id',
]);
}
if ($abdmConsentRequestId !== '' && ! $this->isGatewayRefId($abdmConsentRequestId)) {
$response['abdm_consent_request_id'] = $abdmConsentRequestId;
$response['consent_request_id'] = $abdmConsentRequestId;
}
$abdmConsentId = $this->findFirstValueByKeys($response, [
'consentId',
'consent_id',
'abdm_consent_artifact_id',
]);
$abdmConsentId = '';
if (isset($response['consent']) && is_array($response['consent'])) {
$abdmConsentId = trim((string) (
$response['consent']['id']
?? $response['consent']['consent_id']
?? $response['consent']['consentId']
?? $response['consent']['consent_artifact_id']
?? $response['consent']['consent_artefact_id']
?? ''
));
}
if ($abdmConsentId === '' && isset($response['consentDetail']) && is_array($response['consentDetail'])) {
$abdmConsentId = trim((string) (
$response['consentDetail']['id']
?? $response['consentDetail']['consent_id']
?? $response['consentDetail']['consentId']
?? ''
));
}
if ($abdmConsentId === '' && isset($response['data']) && is_array($response['data'])) {
if (isset($response['data']['consent']) && is_array($response['data']['consent'])) {
$abdmConsentId = trim((string) (
$response['data']['consent']['id']
?? $response['data']['consent']['consent_id']
?? $response['data']['consent']['consentId']
?? ''
));
}
if ($abdmConsentId === '') {
$abdmConsentId = trim((string) (
$response['data']['consent_id']
?? $response['data']['consentId']
?? $response['data']['consent_artifact_id']
?? ''
));
}
}
if ($abdmConsentId === '') {
$abdmConsentId = $this->findFirstValueByKeys($response, [
'consent_id',
'consentId',
'abdm_consent_artifact_id',
'consent_artifact_id',
'consent_artefact_id',
'consentArtifactId',
'consentArtefactId',
]);
}
if ($abdmConsentId !== '' && ! $this->isGatewayRefId($abdmConsentId)) {
$response['consent_id'] = $abdmConsentId;
$response['abdm_consent_artifact_id'] = $abdmConsentId;

View File

@ -1591,8 +1591,18 @@ class M3HiuWorkflowService
$abdmConsentRequestId = $this->firstNonEmptyString([
$result['abdm_consent_request_id'] ?? null,
$result['consent_request_id'] ?? null,
$result['consentRequestId'] ?? null,
$result['consent']['consent_request_id'] ?? null,
$result['consent']['consentRequestId'] ?? null,
$result['consentDetail']['consent_request_id'] ?? null,
$result['consentDetail']['consentRequestId'] ?? null,
$result['data']['consent_request_id'] ?? null,
$result['data']['consent']['consent_request_id'] ?? null,
$payload['consentRequestId'] ?? null,
$payload['consent_request_id'] ?? null,
$payload['abdm_consent_request_id'] ?? null,
$payload['consent']['consent_request_id'] ?? null,
$payload['consentDetail']['consent_request_id'] ?? null,
]);
$transactionId = $this->firstNonEmptyString([
$payload['transaction_id'] ?? null,
@ -1601,13 +1611,26 @@ class M3HiuWorkflowService
$result['transactionId'] ?? null,
]);
$consentId = $this->firstNonEmptyString([
$payload['abdm_consent_artifact_id'] ?? null,
$payload['consent_id'] ?? null,
$payload['consentId'] ?? null,
$payload['hiRequest']['consent']['id'] ?? null,
$result['abdm_consent_artifact_id'] ?? null,
$result['consent_id'] ?? null,
$result['consentId'] ?? null,
$result['consent']['id'] ?? null,
$result['consent']['consent_id'] ?? null,
$result['consent']['consentId'] ?? null,
$result['consentDetail']['id'] ?? null,
$result['consentDetail']['consentId'] ?? null,
$result['consentDetail']['consent_id'] ?? null,
$result['consent_artifact_id'] ?? null,
$result['consent_artefact_id'] ?? null,
$result['data']['consent']['id'] ?? null,
$result['data']['consent_id'] ?? null,
$result['data']['consentId'] ?? null,
$payload['abdm_consent_artifact_id'] ?? null,
$payload['consent_id'] ?? null,
$payload['consentId'] ?? null,
$payload['consent']['id'] ?? null,
$payload['consentDetail']['id'] ?? null,
$payload['hiRequest']['consent']['id'] ?? null,
$result['hiRequest']['consent']['id'] ?? null,
]);
if ($consentId === '' && $operation !== 'consent_fetch' && $operation !== 'hi_request') {

View File

@ -136,7 +136,7 @@
var metaHtml = '<div class="row small text-muted mb-3">'
+ '<div class="col-md-6">Patient: <strong>' + escHtml(consent.patient_name || '-') + '</strong></div>'
+ '<div class="col-md-6">UHID: <strong>' + escHtml(consent.patient_code || '-') + '</strong></div>'
+ '<div class="col-md-6">Consent ID: <strong>' + escHtml(consent.consent_id || '-') + '</strong></div>'
+ '<div class="col-md-6">Consent ID: <strong>' + escHtml(consent.consent_id || consent.abdm_consent_artifact_id || consent.consent_request_id || consent.request_id || '-') + '</strong></div>'
+ '<div class="col-md-6">ABHA: <strong>' + escHtml(consent.abha_address || '-') + '</strong></div>'
+ '<div class="col-md-6">Purpose: <strong>' + escHtml(consent.purpose || '-') + '</strong></div>'
+ '<div class="col-md-6">Status: <strong>' + escHtml(consent.status || '-') + '</strong></div>'

View File

@ -1145,7 +1145,7 @@ $(function() {
stepperHtml += '</div>';
var metaHtml = '<div class="row small text-muted mb-3">'
+ '<div class="col-md-6">Consent ID: <strong>' + escHtml(consent.consent_id || '-') + '</strong></div>'
+ '<div class="col-md-6">Consent ID: <strong>' + escHtml(consent.consent_id || consent.abdm_consent_artifact_id || consent.consent_request_id || consent.request_id || '-') + '</strong></div>'
+ '<div class="col-md-6">ABHA: <strong>' + escHtml(consent.abha_address || '-') + '</strong></div>'
+ '<div class="col-md-6">Purpose: <strong>' + escHtml(consent.purpose || '-') + '</strong></div>'
+ '<div class="col-md-6">Status: <strong>' + escHtml(consent.status || '-') + '</strong></div>'