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
40 changes: 20 additions & 20 deletions src/main/java/com/iemr/flw/service/impl/BeneficiaryServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,10 @@ private String getMappingsForAddressIDs(List<RMNCHMBeneficiaryaddress> addressLi
if (benDetailsOBJ != null && benDetailsOBJ.getOccupation() != null)
benDetailsRMNCH_OBJ.setOccupation(benDetailsOBJ.getOccupation());

// anthropometry from t_phy_anthropometry, vitals from t_phy_vitals
// fallback to otherFields if exam not yet saved for this beneficiary
// anthropometry from t_phy_anthropometry, vitals from t_phy_vitals β€” latest non-null
// value per field, so a later row with some columns empty doesn't hide older values.
// otherFields fills whatever is still missing (beneficiaries registered before
// Identity-API started writing these tables).
Long benRegIdLong = m.getBenRegId() != null ? m.getBenRegId().longValue() : null;
if (benRegIdLong != null) {
List<BenAnthropometryDetail> anthroList =
Expand All @@ -545,29 +547,27 @@ private String getMappingsForAddressIDs(List<RMNCHMBeneficiaryaddress> addressLi
benPhysicalVitalRepo.findByBeneficiaryRegIDOrderByCreatedDateDesc(benRegIdLong);

Map<String, Object> anthropometry = new HashMap<>();
if (!anthroList.isEmpty()) {
BenAnthropometryDetail anthro = anthroList.get(0);
if (anthro.getHeightCm() != null) anthropometry.put("height", anthro.getHeightCm());
if (anthro.getWeightKg() != null) anthropometry.put("weight", anthro.getWeightKg());
if (anthro.getBmi() != null) anthropometry.put("bmi", anthro.getBmi());
for (BenAnthropometryDetail anthro : anthroList) {
if (anthro.getHeightCm() != null) anthropometry.putIfAbsent("height", anthro.getHeightCm());
if (anthro.getWeightKg() != null) anthropometry.putIfAbsent("weight", anthro.getWeightKg());
if (anthro.getBmi() != null) anthropometry.putIfAbsent("bmi", anthro.getBmi());
}
if (!vitalList.isEmpty()) {
BenPhysicalVitalDetail vital = vitalList.get(0);
if (vital.getTemperature() != null) anthropometry.put("temperatureValue", vital.getTemperature());
if (vital.getPulseRate() != null) anthropometry.put("pulseRate", vital.getPulseRate());
if (vital.getSystolicBP() != null) anthropometry.put("systolicBP", vital.getSystolicBP());
if (vital.getDiastolicBP() != null) anthropometry.put("diastolicBP", vital.getDiastolicBP());
if (vital.getBloodGlucoseRandom() != null) anthropometry.put("bloodGlucoseRandom", vital.getBloodGlucoseRandom());
for (BenPhysicalVitalDetail vital : vitalList) {
if (vital.getTemperature() != null) anthropometry.putIfAbsent("temperatureValue", vital.getTemperature());
if (vital.getPulseRate() != null) anthropometry.putIfAbsent("pulseRate", vital.getPulseRate());
if (vital.getSystolicBP() != null) anthropometry.putIfAbsent("systolicBP", vital.getSystolicBP());
if (vital.getDiastolicBP() != null) anthropometry.putIfAbsent("diastolicBP", vital.getDiastolicBP());
if (vital.getBloodGlucoseRandom() != null) anthropometry.putIfAbsent("bloodGlucoseRandom", vital.getBloodGlucoseRandom());
}

// fallback: if no exam saved yet, read from registration otherFields
if (anthropometry.isEmpty() && benDetailsOBJ != null && benDetailsOBJ.getOtherFields() != null) {
// fallback per field: registration otherFields
if (benDetailsOBJ != null && benDetailsOBJ.getOtherFields() != null) {
try {
Map<?, ?> extraFields = new Gson().fromJson(benDetailsOBJ.getOtherFields(), Map.class);
if (extraFields.containsKey("weight")) anthropometry.put("weight", extraFields.get("weight"));
if (extraFields.containsKey("height")) anthropometry.put("height", extraFields.get("height"));
if (extraFields.containsKey("bmi")) anthropometry.put("bmi", extraFields.get("bmi"));
if (extraFields.containsKey("temperatureValue")) anthropometry.put("temperatureValue", extraFields.get("temperatureValue"));
if (extraFields.get("weight") != null) anthropometry.putIfAbsent("weight", extraFields.get("weight"));
if (extraFields.get("height") != null) anthropometry.putIfAbsent("height", extraFields.get("height"));
if (extraFields.get("bmi") != null) anthropometry.putIfAbsent("bmi", extraFields.get("bmi"));
if (extraFields.get("temperatureValue") != null) anthropometry.putIfAbsent("temperatureValue", extraFields.get("temperatureValue"));
} catch (Exception ex) {
logger.warn("Could not parse otherFields for fallback anthropometry: " + benDetailsOBJ.getBeneficiaryDetailsId());
}
Expand Down
28 changes: 27 additions & 1 deletion src/main/java/com/iemr/flw/service/impl/StopTBServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -575,10 +575,33 @@ private Map<String, Object> getRegistrationExtras(Long beneficiaryRegID) {
return Collections.emptyMap();
}

// Latest non-null height/weight/bmi/temperatureValue for the beneficiary. Identity-API's
// syncDataToAmrit writes these to t_phy_anthropometry / t_phy_vitals at registration;
// otherFields is only a fallback for beneficiaries registered before that.
private Map<String, Object> getLatestAnthropometry(Long beneficiaryRegID) {
Map<String, Object> values = new HashMap<>();
for (BenAnthropometryDetail a : benAnthropometryRepo.findByBeneficiaryRegIDOrderByCreatedDateDesc(beneficiaryRegID)) {
if (a.getHeightCm() != null) values.putIfAbsent("height", a.getHeightCm());
if (a.getWeightKg() != null) values.putIfAbsent("weight", a.getWeightKg());
if (a.getBmi() != null) values.putIfAbsent("bmi", a.getBmi());
}
for (BenPhysicalVitalDetail v : benPhysicalVitalRepo.findByBeneficiaryRegIDOrderByCreatedDateDesc(beneficiaryRegID)) {
if (v.getTemperature() != null) {
values.put("temperatureValue", v.getTemperature());
break;
}
}
Map<String, Object> extras = getRegistrationExtras(beneficiaryRegID);
for (String key : List.of("height", "weight", "bmi", "temperatureValue")) {
if (extras.get(key) instanceof Number) values.putIfAbsent(key, extras.get(key));
}
return values;
}

private void dualWriteExamToStandardTables(StopTBGeneralExamination exam, Long beneficiaryRegID,
BenVisitDetail visit, String createdBy, Integer vanID, Integer parkingPlaceID) {
try {
Map<String, Object> extras = getRegistrationExtras(beneficiaryRegID);
Map<String, Object> extras = getLatestAnthropometry(beneficiaryRegID);
writeAnthropometry(extras, beneficiaryRegID, visit, createdBy, vanID, parkingPlaceID);
writeVitals(exam, extras, beneficiaryRegID, visit, createdBy, vanID, parkingPlaceID);
writePhyGeneralExam(exam, beneficiaryRegID, visit, createdBy, vanID, parkingPlaceID);
Expand All @@ -592,6 +615,9 @@ private void dualWriteExamToStandardTables(StopTBGeneralExamination exam, Long b

private void writeAnthropometry(Map<String, Object> extras, Long beneficiaryRegID,
BenVisitDetail visit, String createdBy, Integer vanID, Integer parkingPlaceID) {
// An all-null row would become the latest one and hide the registration values
if (!(extras.get("height") instanceof Number) && !(extras.get("weight") instanceof Number)
&& !(extras.get("bmi") instanceof Number)) return;
try {
BenAnthropometryDetail a = new BenAnthropometryDetail();
a.setBeneficiaryRegID(beneficiaryRegID);
Expand Down
Loading