Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public ResponseEntity<Map<String, Object>> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ public CompletableFuture<Void> 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());
Expand All @@ -102,10 +112,10 @@ public CompletableFuture<Void> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading