Fix duplicate ABDM document rows: dedup on bundle_id + care_context_reference, not transaction_id/consent_ref

Confirmed empirically: the SAME clinical document (identical FHIR Bundle.id) gets re-delivered by the bridge under a DIFFERENT transaction_id/consent_ref every time it is re-fetched (e.g. via the by-ABHA-address historical fetch, which returns every past consent session for the patient). The old dedup hash included transaction_id+consent_ref, so every re-fetch created a brand-new duplicate row instead of updating the existing one -- observed as literal duplicate rows in the OPD ABDM Fetched Data list (same date/title appearing 2-3x). bundle_id is set once by the HIP and is stable across re-fetches/consents, so pair it with care_context_reference to uniquely identify a document instead.
This commit is contained in:
DevSoftTech Server 2026-07-29 02:44:14 +05:30
parent ad98afbe57
commit 016c0fdf2f

View File

@ -60,18 +60,27 @@ class M3HiuDocumentRepository
$resolvedAbha = $abhaAddress !== '' ? $abhaAddress : trim((string) ($summary['abha_address'] ?? '')); $resolvedAbha = $abhaAddress !== '' ? $abhaAddress : trim((string) ($summary['abha_address'] ?? ''));
$patientMap = $this->mapPatientByAbha($resolvedAbha, (string) ($summary['abha_number'] ?? '')); $patientMap = $this->mapPatientByAbha($resolvedAbha, (string) ($summary['abha_number'] ?? ''));
// NOTE: request_id is intentionally excluded from the dedup key. // NOTE: transaction_id and consent_ref are intentionally EXCLUDED
// It is unique per polling/fetch call, so including it here would // from the dedup key. Confirmed empirically: the SAME clinical
// create a brand-new duplicate row every time the same document is // document (identical bundle_id) gets re-delivered by the bridge
// re-fetched (e.g. repeated "Fetch ABDM Records" clicks or retries). // under a DIFFERENT transaction_id/consent_ref every time it is
// transaction_id + consent_ref + care_context_reference + bundle_id // re-fetched (e.g. via the by-ABHA-address historical fetch, which
// identify the same underlying document across repeated fetches. // returns every past consent session), so including them created a
$docHash = sha1(implode('|', [ // brand-new duplicate row per fetch instead of updating the
$transactionId, // existing one. bundle_id is the FHIR Bundle resource's own `id`
$consentRef, // (set once by the HIP) and is stable across re-fetches/consents,
$careContextRef, // so pair it with care_context_reference (the clinical encounter
(string) ($summary['bundle_id'] ?? ''), // reference) to uniquely identify a document. request_id is also
])); // excluded since it is unique per polling/fetch call.
$bundleId = trim((string) ($summary['bundle_id'] ?? ''));
$docHash = $bundleId !== ''
? sha1('bundle:' . $careContextRef . '|' . $bundleId)
: sha1(implode('|', [
$transactionId,
$consentRef,
$careContextRef,
(string) ($summary['bundle_id'] ?? ''),
]));
$row = [ $row = [
'workflow_id' => $workflowId > 0 ? $workflowId : null, 'workflow_id' => $workflowId > 0 ? $workflowId : null,