diff --git a/src/main/java/com/iemr/common/identity/service/rmnch/RmnchDataSyncServiceImpl.java b/src/main/java/com/iemr/common/identity/service/rmnch/RmnchDataSyncServiceImpl.java index 00162e33..6201d6a3 100644 --- a/src/main/java/com/iemr/common/identity/service/rmnch/RmnchDataSyncServiceImpl.java +++ b/src/main/java/com/iemr/common/identity/service/rmnch/RmnchDataSyncServiceImpl.java @@ -25,7 +25,6 @@ import java.sql.Date; import java.sql.Timestamp; import java.text.SimpleDateFormat; -import java.text.SimpleDateFormat; import java.time.Period; import java.util.ArrayList; import java.util.Arrays; @@ -43,13 +42,11 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.http.*; -import org.springframework.http.*; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.google.gson.Gson; import com.google.gson.JsonArray; -import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -79,8 +76,6 @@ import com.iemr.common.identity.repo.rmnch.RMNCHMBenMappingRepo; import com.iemr.common.identity.domain.MBeneficiarydetail; import com.iemr.common.identity.repo.BenDetailRepo; -import com.iemr.common.identity.domain.MBeneficiarydetail; -import com.iemr.common.identity.repo.BenDetailRepo; import com.iemr.common.identity.repo.rmnch.RMNCHMBenRegIdMapRepo; import com.iemr.common.identity.utils.config.ConfigProperties; import com.iemr.common.identity.utils.exception.IEMRException; @@ -88,8 +83,6 @@ import com.iemr.common.identity.utils.mapper.InputMapper; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; -import org.springframework.web.client.HttpClientErrorException; -import org.springframework.web.client.RestTemplate; @Service @Qualifier("rmnchServiceImpl") @@ -139,27 +132,6 @@ public class RmnchDataSyncServiceImpl implements RmnchDataSyncService { @Value("${stoptb.van.id}") private int configuredVanID; - // When true, sync fails loudly if camp is not configured instead of silently - // skipping vanID stamping. No inline default — every properties file must set this - // explicitly, so a forgotten config fails loudly at startup instead of running fail-open. - @Value("${stoptb.enforce.vanid}") - private boolean enforceVanID; - @Autowired - private BenDetailRepo benDetailRepo; - - @Value("${fhir-url}") - private String fhirUrl; - - // This deployment's van/camp ID. Previously looked up from Redis ("camp:vanID"), - // written at MMU login and deleted (globally, unscoped) on ANY user's logout — a Redis - // outage or an unrelated user's logout would silently break sync on this camp. Each - // camp/van already runs its own dedicated backend instance, so which van this is never - // actually changes at runtime; reading it from properties removes the Redis dependency - // entirely. No inline default — every properties file must set this explicitly. - // Scope: vanID only, parkingPlaceID is not part of this change. - @Value("${stoptb.van.id}") - private int configuredVanID; - // When true, sync fails loudly if camp is not configured instead of silently // skipping vanID stamping. No inline default — every properties file must set this // explicitly, so a forgotten config fails loudly at startup instead of running fail-open. @@ -167,8 +139,6 @@ public class RmnchDataSyncServiceImpl implements RmnchDataSyncService { private boolean enforceVanID; @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) @Override - public String syncDataToAmrit(String requestOBJ, String authorization) throws Exception { - public String syncDataToAmrit(String requestOBJ, String authorization) throws Exception { @@ -190,17 +160,6 @@ public String syncDataToAmrit(String requestOBJ, String authorization) throws Ex final Integer vanID = campVanID; final Integer parkingPlaceID = null; - // Configured van ID for this deployment (see configuredVanID field javadoc above). - // parkingPlaceID is out of scope for this change — kept null, same as before whenever - // Redis had no value for it. - Integer campVanID = configuredVanID > 0 ? configuredVanID : null; - if (campVanID == null && enforceVanID) { - throw new Exception( - "Camp not configured: stoptb.van.id is 0. Set stoptb.van.id in this deployment's properties file."); - } - final Integer vanID = campVanID; - final Integer parkingPlaceID = null; - try { if (requestOBJ != null && !requestOBJ.isEmpty()) { JsonObject jsnOBJ = new JsonObject(); @@ -212,8 +171,6 @@ public String syncDataToAmrit(String requestOBJ, String authorization) throws Ex // ben details RMNCH extra fields details logger.info("Request object of syncDataToAmrit: "+jsnOBJ); - logger.info("Request object of syncDataToAmrit: "+jsnOBJ); - BigInteger benRegID = null; @@ -337,26 +294,6 @@ public String syncDataToAmrit(String requestOBJ, String authorization) throws Ex // else: trust the device-supplied CreatedDate as-is — offline captures // legitimately sync well after the actual event, so a later server time // would be less accurate than the device's own timestamp. - if (isPlausibleDeviceTimestamp(temp.getCreatedDate())) { - // Already has a good CreatedDate from the first sync — a later - // re-sync must never overwrite it. - obj.setCreatedDate(temp.getCreatedDate()); - } else if (isPlausibleDeviceTimestamp(obj.getCreatedDate())) { - // Stored value was garbage but this re-sync brought a plausible - // one from the device — self-heal using it (obj already has it). - } else { - obj.setCreatedDate(new Timestamp(System.currentTimeMillis())); - } - } - } else if (!isPlausibleDeviceTimestamp(obj.getCreatedDate())) { - // Device clock is broken (unset -> 1970 epoch, or set ahead -> future - // date). We can't recover the true capture time, so fall back to the - // sync time as the least-wrong value instead of storing garbage. - obj.setCreatedDate(new Timestamp(System.currentTimeMillis())); - } - // else: trust the device-supplied CreatedDate as-is — offline captures - // legitimately sync well after the actual event, so a later server time - // would be less accurate than the device's own timestamp. @@ -378,12 +315,6 @@ public String syncDataToAmrit(String requestOBJ, String authorization) throws Ex obj.setVanID(vanID); obj.setParkingPlaceID(parkingPlaceID); } - // Mobile sends VanID=0 as a placeholder (not null) for a fresh record — - // `== null` alone never catches it, leaving the placeholder in place. - if ((obj.getVanID() == null || obj.getVanID() == 0) && vanID != null) { - obj.setVanID(vanID); - obj.setParkingPlaceID(parkingPlaceID); - } if(!rMNCHBenDetailsRepo.getByBenRegID(obj.getBenRegId()).isEmpty()){ RMNCHMBeneficiarydetail rmnchmBeneficiarydetail = rMNCHBenDetailsRepo.getByBenRegID(obj.getBenRegId()).get(0); @@ -471,10 +402,6 @@ public String syncDataToAmrit(String requestOBJ, String authorization) throws Ex .saveAll(benDetailsExtraList); // The `id`/VanSerialNo field is Gson-collision-prone (see repo javadoc) — // force it back to each row's own PK after save. - benDetailsExtraList.forEach((n) -> rMNCHBeneficiaryDetailsRmnchRepo - .updateVanSerialNo(n.getBeneficiaryDetails_RmnchId())); - // The `id`/VanSerialNo field is Gson-collision-prone (see repo javadoc) — - // force it back to each row's own PK after save. benDetailsExtraList.forEach((n) -> rMNCHBeneficiaryDetailsRmnchRepo .updateVanSerialNo(n.getBeneficiaryDetails_RmnchId())); @@ -498,22 +425,6 @@ public String syncDataToAmrit(String requestOBJ, String authorization) throws Ex } } - // Write anthropometry (height/weight/bmi/temperature) to i_beneficiarydetails.otherFields. - // i_beneficiarydetails_rmnch has no these columns; FLW-API getBeneficiaryData reads from otherFields. - for (RMNCHBeneficiaryDetailsRmnch obj : benDetailsOriginalList) { - if (obj.getBenRegId() != null && hasAnthropometryData(obj)) { - try { - MBeneficiarydetail benDetail = benDetailRepo.findByBenRegId(obj.getBenRegId()); - if (benDetail != null) { - String merged = mergeAnthropometry(benDetail.getOtherFields(), obj); - benDetailRepo.updateOtherFieldsByBenRegId(obj.getBenRegId(), merged); - } - } catch (Exception ex) { - logger.warn("Failed to update otherFields for benRegId: " + obj.getBenRegId() + " - " + ex.getMessage()); - } - } - } - // born birth details if (jsnOBJ != null && jsnOBJ.has("bornBirthDeatils")) { RMNCHBornBirthDetails[] objArr1 = InputMapper.gson() @@ -531,15 +442,6 @@ public String syncDataToAmrit(String requestOBJ, String authorization) throws Ex obj.setVanID(vanID); obj.setParkingPlaceID(parkingPlaceID); } - if(!rMNCHBornBirthDetailsRepo.getByRegID(benRegID).isEmpty()){ - RMNCHBornBirthDetails temp = rMNCHBornBirthDetailsRepo.getByRegID(benRegID).get(0); - if (temp != null) - obj.setBornBirthDeatilsId(temp.getBornBirthDeatilsId()); - } - if (obj.getVanID() == null && vanID != null) { - obj.setVanID(vanID); - obj.setParkingPlaceID(parkingPlaceID); - } } bornBirthList = (ArrayList) rMNCHBornBirthDetailsRepo .saveAll(bornBirthList); @@ -569,15 +471,6 @@ public String syncDataToAmrit(String requestOBJ, String authorization) throws Ex obj.setVanID(vanID); obj.setParkingPlaceID(parkingPlaceID); } - if(!rMNCHCBACDetailsRepo.getByRegID(benRegID).isEmpty()){ - RMNCHCBACdetails temp = rMNCHCBACDetailsRepo.getByRegID(benRegID).get(0); - if (temp != null) - obj.setCBACDetailsid(temp.getCBACDetailsid()); - } - if (obj.getVanID() == null && vanID != null) { - obj.setVanID(vanID); - obj.setParkingPlaceID(parkingPlaceID); - } } cbacList = (ArrayList) rMNCHCBACDetailsRepo.saveAll(cbacList); @@ -606,22 +499,6 @@ public String syncDataToAmrit(String requestOBJ, String authorization) throws Ex } catch (NumberFormatException ignored) {} } - // Build gpsTimestamp map (sent as string, needs manual parse) - Map hhTimestampMap = new HashMap<>(); - JsonArray hhJsonArr = jsnOBJ.getAsJsonArray("houseHoldDetails"); - for (JsonElement el : hhJsonArr) { - JsonObject hhJson = el.getAsJsonObject(); - try { - if (hhJson.has("houseoldId") && !hhJson.get("houseoldId").isJsonNull() - && hhJson.has("gpsTimestamp") - && !hhJson.get("gpsTimestamp").isJsonNull()) { - hhTimestampMap.put( - Long.parseLong(hhJson.get("houseoldId").getAsString()), - hhJson.get("gpsTimestamp").getAsLong()); - } - } catch (NumberFormatException ignored) {} - } - for (RMNCHHouseHoldDetails obj : houseHoldList) { if(!rMNCHHouseHoldDetailsRepo .getByHouseHoldID(obj.getHouseoldId()).isEmpty()){ @@ -636,17 +513,6 @@ public String syncDataToAmrit(String requestOBJ, String authorization) throws Ex // stay OUTSIDE the "household already exists" block above (it's regressed // back inside there twice already via merges), otherwise a brand-new // household never gets VanID stamped, breaking van-scoped sync. - if (obj.getVanID() == null && vanID != null) { - obj.setVanID(vanID); - obj.setParkingPlaceID(parkingPlaceID); - } - if (hhTimestampMap.containsKey(obj.getHouseoldId())) - obj.setGpsTimestamp(new Timestamp(hhTimestampMap.get(obj.getHouseoldId()))); - } - // Set VanID/ParkingPlaceID for both NEW and existing households — this must - // stay OUTSIDE the "household already exists" block above (it's regressed - // back inside there twice already via merges), otherwise a brand-new - // household never gets VanID stamped, breaking van-scoped sync. if (obj.getVanID() == null && vanID != null) { obj.setVanID(vanID); obj.setParkingPlaceID(parkingPlaceID); @@ -656,10 +522,6 @@ public String syncDataToAmrit(String requestOBJ, String authorization) throws Ex .saveAll(houseHoldList); // The `id`/VanSerialNo field is Gson-collision-prone (see repo javadoc) — // force it back to each row's own PK after save. - houseHoldList.forEach((n) -> rMNCHHouseHoldDetailsRepo - .updateVanSerialNo(n.getHouseHoldDetailsId())); - // The `id`/VanSerialNo field is Gson-collision-prone (see repo javadoc) — - // force it back to each row's own PK after save. houseHoldList.forEach((n) -> rMNCHHouseHoldDetailsRepo .updateVanSerialNo(n.getHouseHoldDetailsId())); // success response @@ -793,110 +655,6 @@ public String mapHealthIDToBeneficiary(String authorization, } - /** - * Splits a list into sub-lists (batches) of the given size. - * Last batch may contain fewer elements. - */ - private List> partitionList(List list, int batchSize) { - List> batches = new ArrayList<>(); - if (list == null || list.isEmpty()) { - return batches; - } - for (int i = 0; i < list.size(); i += batchSize) { - batches.add(new ArrayList<>(list.subList(i, Math.min(i + batchSize, list.size())))); - } - return batches; - } - - public String mapHealthIDToBeneficiary(String authorization, - Long benRegID, - Long beneficiaryID, - String abhaId, - String createdBy,String firstName,String lastName,String dob,Integer providerServiceMapId) { - try { - RestTemplate restTemplate = new RestTemplate(); - String formattedDob = dob; - - try { - if (dob != null && dob.contains(" ")) { - Timestamp timestamp = Timestamp.valueOf(dob); - formattedDob = new SimpleDateFormat("dd-MM-yyyy") - .format(timestamp); - } - } catch (Exception ex) { - logger.warn("DOB format conversion failed, sending original DOB : {}", dob); - } - logger.info("Authorization Token : {}", authorization); - - Map requestMap = new HashMap<>(); - - requestMap.put("beneficiaryRegID", benRegID); - requestMap.put("beneficiaryID", beneficiaryID); - requestMap.put("healthIdNumber", abhaId); - - requestMap.put("createdBy", createdBy); - requestMap.put("providerServiceMapId", providerServiceMapId); - requestMap.put("isNew", false); - - // ABHA Profile - Map abhaProfile = new HashMap<>(); - abhaProfile.put("ABHANumber", abhaId); - - List phrAddress = new ArrayList<>(); - phrAddress.add(abhaId + "@abdm"); - - abhaProfile.put("phrAddress", phrAddress); - abhaProfile.put("firstName", firstName); - abhaProfile.put("middleName", ""); - abhaProfile.put("lastName", lastName); - abhaProfile.put("dob", formattedDob); - - - requestMap.put("ABHAProfile", abhaProfile); - - String requestBody = new Gson().toJson(requestMap); - - String url = fhirUrl - + ConfigProperties.getPropertyByName("mapHealthIDToBeneficiary"); - - logger.info("Calling URL : {}", url); - logger.info("Request Body : {}", requestBody); - - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON); - - headers.set("Jwttoken", authorization); - - HttpEntity entity = - new HttpEntity<>(requestBody, headers); - - ResponseEntity response = restTemplate.exchange( - url, - HttpMethod.POST, - entity, - String.class - ); - - logger.info("ABHA Mapping Response : {}", response.getBody()); - - return response.getBody(); - - } catch (HttpClientErrorException e) { - - logger.error("HTTP Error Status : {}", e.getStatusCode()); - logger.error("HTTP Error Response : {}", e.getResponseBodyAsString(), e); - - return "HTTP Error : " + e.getStatusCode(); - - } catch (Exception e) { - - logger.error("Error while saving Health ID Mapping", e); - - return "Error Save Health Id : " + e.getMessage(); - } - - } - @Override @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) @@ -1283,19 +1041,12 @@ private String getMappingsForAddressIDs(List addressLi benDetailsRMNCHOBJ = rMNCHBeneficiaryDetailsRmnchRepo .getByRegID(m.getBenRegId()).get(0); } - if(!rMNCHBornBirthDetailsRepo.getByRegID(m.getBenRegId()).isEmpty()){ - benBotnBirthRMNCHROBJ = rMNCHBornBirthDetailsRepo.getByRegID(m.getBenRegId()).get(0); if(!rMNCHBornBirthDetailsRepo.getByRegID(m.getBenRegId()).isEmpty()){ benBotnBirthRMNCHROBJ = rMNCHBornBirthDetailsRepo.getByRegID(m.getBenRegId()).get(0); } if(! rMNCHCBACDetailsRepo.getByRegID(m.getBenRegId()).isEmpty()){ benCABCRMNCHROBJ = rMNCHCBACDetailsRepo.getByRegID(m.getBenRegId()).get(0); - } - if(! rMNCHCBACDetailsRepo.getByRegID(m.getBenRegId()).isEmpty()){ - benCABCRMNCHROBJ = rMNCHCBACDetailsRepo.getByRegID(m.getBenRegId()).get(0); - - } } @@ -1325,12 +1076,6 @@ private String getMappingsForAddressIDs(List addressLi .getByHouseHoldID(benDetailsRMNCHOBJ.getHouseoldId()).get(0); } - if(!rMNCHHouseHoldDetailsRepo - .getByHouseHoldID(benDetailsRMNCHOBJ.getHouseoldId()).isEmpty()){ - benHouseHoldRMNCHROBJ = rMNCHHouseHoldDetailsRepo - .getByHouseHoldID(benDetailsRMNCHOBJ.getHouseoldId()).get(0); - } - } if (benDetailsRMNCHOBJ == null) @@ -1400,8 +1145,6 @@ private String getMappingsForAddressIDs(List addressLi benDetailsRMNCHOBJ.setAddressLine3(benAddressOBJ.getPermAddrLine3()); if (benAddressOBJ.getPermPinCode() != null) benDetailsRMNCHOBJ.setPinCode(benAddressOBJ.getPermPinCode()); - if (benAddressOBJ.getPermPinCode() != null) - benDetailsRMNCHOBJ.setPinCode(benAddressOBJ.getPermPinCode()); // related benids if (benDetailsRMNCHOBJ.getRelatedBeneficiaryIdsDB() != null) {