Skip to content
Merged
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 @@ -219,8 +219,26 @@ public String syncDataToAmrit(String requestOBJ, String authorization) throws Ex
.getByRegID(benRegID).get(0);
if (temp != null) {
obj.setBeneficiaryDetails_RmnchId(temp.getBeneficiaryDetails_RmnchId());
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.



Expand Down Expand Up @@ -655,6 +673,22 @@ private Integer getInt(JsonObject obj, String key, Integer defaultVal) {
: defaultVal;
}

/**
* A device-supplied CreatedDate is trustworthy only if it is non-null,
* not the classic unset-clock epoch default, and not after the current
* server time (an offline capture can only have happened before the sync
* request that reports it, so a future date means the device's clock is
* wrong, not that the record is legitimately from the future).
*/
private boolean isPlausibleDeviceTimestamp(Timestamp createdDate) {
if (createdDate == null) {
return false;
}
long epochDayZero = 24L * 60 * 60 * 1000; // guard band around 1970-01-01 for epoch defaults
long now = System.currentTimeMillis();
return createdDate.getTime() > epochDayZero && createdDate.getTime() <= now;
}

private boolean hasAnthropometryData(RMNCHBeneficiaryDetailsRmnch obj) {
return obj.getHeight() != null || obj.getWeight() != null
|| obj.getBmi() != null || obj.getTemperature() != null;
Expand Down
Loading