From 24abc51deab23c3740a5ccda5c422f487464a688 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Thu, 8 Oct 2026 09:39:21 +0530 Subject: [PATCH] fix(abdm): omit single top-level hi_type when linking mixed care context batches --- app/Controllers/AbdmGateway.php | 18 +++++++++++- app/Views/partials/abdm_hip_link_modal.php | 34 ++++++++++++++-------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/app/Controllers/AbdmGateway.php b/app/Controllers/AbdmGateway.php index 46dea12..49585ed 100644 --- a/app/Controllers/AbdmGateway.php +++ b/app/Controllers/AbdmGateway.php @@ -12515,7 +12515,23 @@ class AbdmGateway extends BaseController $body['display'] = $body['patient_ref']; } } - if (empty($body['hi_type'])) { + + // Do not force a single global hi_type when batch contains mixed care context types + if (! empty($body['care_contexts']) && is_array($body['care_contexts'])) { + $typesInBatch = []; + foreach ($body['care_contexts'] as $cItem) { + $t = trim((string) ($cItem['hi_type'] ?? $cItem['hiType'] ?? '')); + if ($t !== '' && ! in_array($t, $typesInBatch, true)) { + $typesInBatch[] = $t; + } + } + if (count($typesInBatch) === 1) { + $body['hi_type'] = $typesInBatch[0]; + } elseif (count($typesInBatch) > 1) { + unset($body['hi_type']); + } + } + if (empty($body['hi_type']) && empty($body['care_contexts'])) { $body['hi_type'] = 'OPConsultRecord'; } diff --git a/app/Views/partials/abdm_hip_link_modal.php b/app/Views/partials/abdm_hip_link_modal.php index 1e965dc..e7459ae 100644 --- a/app/Views/partials/abdm_hip_link_modal.php +++ b/app/Views/partials/abdm_hip_link_modal.php @@ -1014,13 +1014,33 @@ var waitMs = isReused ? 300 : 2000; await new Promise(function(r) { setTimeout(r, waitMs); }); - var hiTypeToSend = dominantHiType || 'OPConsultRecord'; + var distinctTypes = []; + selectedContexts.forEach(function(sc) { + if (sc.hi_type && distinctTypes.indexOf(sc.hi_type) === -1) { + distinctTypes.push(sc.hi_type); + } + }); + var hiTypeToSend = distinctTypes.length === 1 ? distinctTypes[0] : ''; if (hiTypeToSend === 'OPConsultation') { hiTypeToSend = 'OPConsultRecord'; } // Step 2: Link Care Contexts csrf = getCsrfData(); + var linkPayload = { + patient_id: currentPatientId, + abha_address: abhaAddress, + abha_number: abhaNumber, + link_token_id: linkTokenId, + patient_ref: abhaAddress, + display: name, + care_contexts: selectedContexts, + csrf_hms: csrf.hash + }; + if (hiTypeToSend) { + linkPayload.hi_type = hiTypeToSend; + } + var linkRes = await fetch('', { method: 'POST', headers: { @@ -1028,17 +1048,7 @@ 'X-Requested-With': 'XMLHttpRequest', 'Accept': 'application/json' }, - body: JSON.stringify({ - patient_id: currentPatientId, - abha_address: abhaAddress, - abha_number: abhaNumber, - link_token_id: linkTokenId, - patient_ref: abhaAddress, - display: name, - hi_type: hiTypeToSend, - care_contexts: selectedContexts, - csrf_hms: csrf.hash - }) + body: JSON.stringify(linkPayload) }); var linkData = await linkRes.json();