diff --git a/src/main/java/com/iemr/common/identity/controller/elasticsearch/ElasticsearchSyncController.java b/src/main/java/com/iemr/common/identity/controller/elasticsearch/ElasticsearchSyncController.java index 7d8f1877..7f4debc8 100644 --- a/src/main/java/com/iemr/common/identity/controller/elasticsearch/ElasticsearchSyncController.java +++ b/src/main/java/com/iemr/common/identity/controller/elasticsearch/ElasticsearchSyncController.java @@ -347,7 +347,7 @@ public ResponseEntity> checkBeneficiaryExists( try { java.math.BigInteger benRegIdBig = new java.math.BigInteger(benRegId); - boolean exists = mappingRepo.existsByBenRegId(benRegIdBig); + boolean exists = mappingRepo.countActiveByBenRegId(benRegIdBig) > 0; response.put("benRegId", benRegId); response.put("existsInDatabase", exists); diff --git a/src/main/java/com/iemr/common/identity/repo/BenMappingRepo.java b/src/main/java/com/iemr/common/identity/repo/BenMappingRepo.java index fd781922..333f893a 100644 --- a/src/main/java/com/iemr/common/identity/repo/BenMappingRepo.java +++ b/src/main/java/com/iemr/common/identity/repo/BenMappingRepo.java @@ -237,7 +237,11 @@ MBeneficiarymapping getWithVanSerialNoVanID(@Param("vanSerialNo") BigInteger van @Query("SELECT m FROM MBeneficiarymapping m WHERE m.benRegId = :benRegId AND m.deleted = false") MBeneficiarymapping findByBenRegId(@Param("benRegId") BigInteger benRegId); - /** + + @Query(value = "SELECT COUNT(*) FROM i_beneficiarymapping WHERE BenRegId = :benRegId AND Deleted = false", nativeQuery = true) + long countActiveByBenRegId(@Param("benRegId") BigInteger benRegId); + +/** * Check if beneficiary exists */ @Query(value = "SELECT COUNT(*) > 0 FROM i_beneficiarymapping WHERE BenRegId = :benRegId AND Deleted = false", nativeQuery = true) diff --git a/src/main/java/com/iemr/common/identity/service/elasticsearch/BeneficiaryElasticsearchIndexUpdater.java b/src/main/java/com/iemr/common/identity/service/elasticsearch/BeneficiaryElasticsearchIndexUpdater.java index ff2fec5b..15adc6ad 100644 --- a/src/main/java/com/iemr/common/identity/service/elasticsearch/BeneficiaryElasticsearchIndexUpdater.java +++ b/src/main/java/com/iemr/common/identity/service/elasticsearch/BeneficiaryElasticsearchIndexUpdater.java @@ -94,6 +94,16 @@ public CompletableFuture syncBeneficiaryAsync(BigInteger benRegId) { return CompletableFuture.completedFuture(null); } + // Document id must be the beneficiary id, matching the full/bulk sync and + // the delete path. Keying on benRegId here wrote a second document per + // beneficiary instead of updating the existing one, so edits never + // surfaced in search. + final String documentId = document.getBenId(); + if (documentId == null) { + logger.warn("No beneficiary id for benRegId: {}, skipping ES sync", benRegId); + return CompletableFuture.completedFuture(null); + } + // Log ABHA for verification logger.info("Syncing benRegId={} with ABHA: healthID={}, abhaID={}", benRegId, document.getHealthID(), document.getAbhaID()); @@ -102,10 +112,10 @@ public CompletableFuture syncBeneficiaryAsync(BigInteger benRegId) { // Index to ES esClient.index(i -> i .index(beneficiaryIndex) - .id(String.valueOf(benRegId)) + .id(documentId) .document(document).refresh(Refresh.True)); - logger.info("Successfully synced benRegId: {} to ES", benRegId); + logger.info("Successfully synced benRegId: {} to ES as document {}", benRegId, documentId); } catch (Exception e) { logger.error("Error syncing beneficiary {} to Elasticsearch: {}", benRegId, e.getMessage(), e); } diff --git a/src/main/java/com/iemr/common/identity/service/elasticsearch/BeneficiaryTransactionHelper.java b/src/main/java/com/iemr/common/identity/service/elasticsearch/BeneficiaryTransactionHelper.java index a173e327..5af139bf 100644 --- a/src/main/java/com/iemr/common/identity/service/elasticsearch/BeneficiaryTransactionHelper.java +++ b/src/main/java/com/iemr/common/identity/service/elasticsearch/BeneficiaryTransactionHelper.java @@ -80,7 +80,7 @@ public long countActiveBeneficiaries() { @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = true, timeout = 10) public boolean existsByBenRegId(BigInteger benRegId) { try { - return mappingRepo.existsByBenRegId(benRegId); + return mappingRepo.countActiveByBenRegId(benRegId) > 0; } catch (Exception e) { logger.error("Error checking existence for benRegId={}: {}", benRegId, e.getMessage()); throw e;