Skip to content

Commit 785faaa

Browse files
committed
fix:optimize the api by optimize healthid existence check using existByHealthIdNumber()
1 parent 737c197 commit 785faaa

3 files changed

Lines changed: 82 additions & 64 deletions

File tree

src/main/java/com/wipro/fhir/repo/healthID/BenHealthIDMappingRepo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,7 @@ public interface BenHealthIDMappingRepo extends CrudRepository<BenHealthIDMappin
8888
@Query(value = "SELECT HealthIdNumber, isNewAbha FROM t_healthid WHERE HealthIdNumber IN :healthIdNumbers ORDER BY HealthIdNumber, isNewAbha DESC", nativeQuery = true)
8989
List<Object[]> getIsNewAbhaBatch(@Param("healthIdNumbers") List<String> healthIdNumbers);
9090

91+
boolean existsByHealthIdNumber(String healthIdNumber);
92+
9193

9294
}

src/main/java/com/wipro/fhir/repo/healthID/HealthIDRepo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ public ArrayList<HealthIDResponse> getHealthIDDetailsUsingHealthNumber(
2727
@Query("SELECT COUNT(*) FROM HealthIDResponse HIDR WHERE HIDR.healthIdNumber = :healthId")
2828
public Integer getCountOfHealthIdNumber(@Param("healthId") String healthId);
2929

30+
boolean existsByHealthIdNumber(String healthIdNumber);
31+
3032
}

src/main/java/com/wipro/fhir/service/healthID/HealthIDServiceImpl.java

Lines changed: 78 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
import com.wipro.fhir.utils.exception.FHIRException;
5050
import com.wipro.fhir.utils.http.HttpUtils;
5151
import com.wipro.fhir.utils.mapper.InputMapper;
52+
import java.time.LocalDate;
53+
import java.time.format.DateTimeFormatter;
54+
import java.util.stream.IntStream;
55+
import java.util.stream.Stream;
5256

5357
@Service
5458
public class HealthIDServiceImpl implements HealthIDService {
@@ -61,73 +65,83 @@ public class HealthIDServiceImpl implements HealthIDService {
6165
@Autowired
6266
HealthIDRepo healthIDRepo;
6367

68+
6469
@Override
6570
public String mapHealthIDToBeneficiary(String request) throws FHIRException {
66-
BenHealthIDMapping health = InputMapper.gson().fromJson(request, BenHealthIDMapping.class);
67-
String[] beneficiaryIdsList = null;
68-
try {
69-
if (health.getBeneficiaryRegID() == null && health.getBeneficiaryID() == null)
70-
throw new FHIRException("Error in mapping request");
71-
else {
72-
if (health.getHealthIdNumber() != null) {
73-
beneficiaryIdsList = benHealthIDMappingRepo.getBenIdForHealthId(health.getHealthIdNumber());
74-
75-
if (beneficiaryIdsList != null && beneficiaryIdsList.length > 0) {
76-
return "HealthId is already linked to other beneficiary ID";
77-
} else {
78-
if (health.getBeneficiaryRegID() != null)
79-
health = benHealthIDMappingRepo.save(health);
80-
else {
81-
if (health.getBeneficiaryID() != null) {
82-
Long benRegId = benHealthIDMappingRepo.getBenRegID(health.getBeneficiaryID());
83-
health.setBeneficiaryRegID(benRegId);
84-
health = benHealthIDMappingRepo.save(health);
85-
}
86-
}
87-
// Adding the code to check if the received healthId is present in t_healthId
88-
// table and add if missing
89-
Integer healthIdCount = healthIDRepo.getCountOfHealthIdNumber(health.getHealthIdNumber());
90-
if (healthIdCount < 1) {
91-
JsonObject jsonRequest = JsonParser.parseString(request).getAsJsonObject();
92-
JsonObject abhaProfileJson = jsonRequest.getAsJsonObject("ABHAProfile");
93-
HealthIDResponse healthID = InputMapper.gson().fromJson(abhaProfileJson,
94-
HealthIDResponse.class);
95-
96-
healthID.setHealthIdNumber(abhaProfileJson.get("ABHANumber").getAsString());
97-
JsonArray phrAddressArray = abhaProfileJson.getAsJsonArray("phrAddress");
98-
StringBuilder abhaAddressBuilder = new StringBuilder();
99-
100-
for (int i = 0; i < phrAddressArray.size(); i++) {
101-
abhaAddressBuilder.append(phrAddressArray.get(i).getAsString());
102-
if (i < phrAddressArray.size() - 1) {
103-
abhaAddressBuilder.append(", ");
104-
}
105-
}
106-
healthID.setHealthId(abhaAddressBuilder.toString());
107-
healthID.setName(abhaProfileJson.get("firstName").getAsString() + " "
108-
+ abhaProfileJson.get("middleName").getAsString() + " "
109-
+ abhaProfileJson.get("lastName").getAsString());
110-
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
111-
Date date = simpleDateFormat.parse(abhaProfileJson.get("dob").getAsString());
112-
SimpleDateFormat year = new SimpleDateFormat("yyyy");
113-
SimpleDateFormat month = new SimpleDateFormat("MM");
114-
SimpleDateFormat day = new SimpleDateFormat("dd");
115-
healthID.setYearOfBirth(year.format(date));
116-
healthID.setMonthOfBirth(month.format(date));
117-
healthID.setDayOfBirth(day.format(date));
118-
healthID.setCreatedBy(jsonRequest.get("createdBy").getAsString());
119-
healthID.setProviderServiceMapID(jsonRequest.get("providerServiceMapId").getAsInt());
120-
healthID.setIsNewAbha(jsonRequest.get("isNew").getAsBoolean());
121-
healthIDRepo.save(healthID);
122-
}
123-
}
124-
}
71+
BenHealthIDMapping health = null;
72+
try {
73+
JsonObject jsonRequest = JsonParser.parseString(request).getAsJsonObject();
74+
JsonObject abhaProfileJson = jsonRequest.getAsJsonObject("ABHAProfile");
75+
76+
health = InputMapper.gson().fromJson(request, BenHealthIDMapping.class);
77+
78+
if (health.getBeneficiaryRegID() == null && health.getBeneficiaryID() == null) {
79+
throw new FHIRException("BeneficiaryRegID or BeneficiaryID must be provided");
12580
}
126-
} catch (Exception e) {
127-
throw new FHIRException("Error in saving data");
128-
}
129-
return new Gson().toJson(health);
130-
}
81+
82+
String healthIdNumber = health.getHealthIdNumber();
83+
if (healthIdNumber != null && !healthIdNumber.trim().isEmpty()) {
84+
85+
// Avoid fetching entire list - use exists check
86+
boolean alreadyLinked = benHealthIDMappingRepo.existsByHealthIdNumber(healthIdNumber);
87+
if (alreadyLinked) {
88+
return "HealthId is already linked to another beneficiary ID";
89+
}
90+
91+
// Save mapping
92+
if (health.getBeneficiaryRegID() != null) {
93+
health = benHealthIDMappingRepo.save(health);
94+
} else if (health.getBeneficiaryID() != null) {
95+
Long benRegId = benHealthIDMappingRepo.getBenRegID(health.getBeneficiaryID());
96+
health.setBeneficiaryRegID(benRegId);
97+
health = benHealthIDMappingRepo.save(health);
98+
}
99+
100+
// Add to healthId table if missing
101+
boolean healthIdExists = healthIDRepo.existsByHealthIdNumber(healthIdNumber);
102+
if (!healthIdExists) {
103+
HealthIDResponse healthID = InputMapper.gson().fromJson(abhaProfileJson, HealthIDResponse.class);
104+
healthID.setHealthIdNumber(abhaProfileJson.get("ABHANumber").getAsString());
105+
106+
// phrAddress as comma-separated
107+
JsonArray phrAddressArray = abhaProfileJson.getAsJsonArray("phrAddress");
108+
String abhaAddress = IntStream.range(0, phrAddressArray.size())
109+
.mapToObj(i -> phrAddressArray.get(i).getAsString())
110+
.collect(Collectors.joining(", "));
111+
healthID.setHealthId(abhaAddress);
112+
113+
// Full name
114+
String fullName = Stream.of("firstName", "middleName", "lastName")
115+
.map(field -> abhaProfileJson.has(field) ? abhaProfileJson.get(field).getAsString() : "")
116+
.filter(s -> !s.isEmpty())
117+
.collect(Collectors.joining(" "));
118+
healthID.setName(fullName.trim());
119+
120+
// Parse and split DOB
121+
LocalDate dob = LocalDate.parse(abhaProfileJson.get("dob").getAsString(), DateTimeFormatter.ofPattern("dd-MM-yyyy"));
122+
healthID.setYearOfBirth(String.valueOf(dob.getYear()));
123+
healthID.setMonthOfBirth(String.format("%02d", dob.getMonthValue()));
124+
healthID.setDayOfBirth(String.format("%02d", dob.getDayOfMonth()));
125+
126+
// Other fields
127+
healthID.setCreatedBy(jsonRequest.get("createdBy").getAsString());
128+
healthID.setProviderServiceMapID(jsonRequest.get("providerServiceMapId").getAsInt());
129+
healthID.setIsNewAbha(jsonRequest.get("isNew").getAsBoolean());
130+
131+
healthIDRepo.save(healthID);
132+
}
133+
}
134+
135+
} catch (FHIRException e) {
136+
throw e; // already custom
137+
} catch (Exception e) {
138+
logger.error("Unexpected error while mapping HealthID", e);
139+
throw new FHIRException("Unexpected error: " + e.getMessage());
140+
}
141+
142+
return new Gson().toJson(health);
143+
}
144+
131145

132146
public String getBenHealthID(Long benRegID) {
133147
List<BenHealthIDMapping> healthDetailsList = benHealthIDMappingRepo.getHealthDetails(benRegID);

0 commit comments

Comments
 (0)