Skip to content
Open
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 @@ -455,12 +455,10 @@ private List<Condition> makeMedicalHistoryList(
private List<AllergyIntolerance> makeAllergiesList(
Patient patient,
List<Practitioner> practitionerList,
OPConsultationRequest opConsultationRequest)
throws ParseException {
return Optional.ofNullable(opConsultationRequest.getAllergies())
.orElse(Collections.emptyList())
.stream()
.filter(allergy -> allergy.isBlank() && allergy.isEmpty())
OPConsultationRequest opConsultationRequest) {

return Optional.ofNullable(opConsultationRequest.getAllergies()).orElse(List.of()).stream()
.filter(allergy -> allergy != null && !allergy.isBlank())
.map(
StreamUtils.wrapException(
allergy ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private List<Composition.SectionComponent> makeCompositionSection(
List<Observation> otherObservationList,
List<DocumentReference> documentReferenceList) {
List<Composition.SectionComponent> sectionComponentList = new ArrayList<>();
if (Objects.nonNull(chiefComplaintList)) {
if (chiefComplaintList != null && !chiefComplaintList.isEmpty()) {
Composition.SectionComponent sectionComponent = new Composition.SectionComponent();
sectionComponent.setCode(
new CodeableConcept()
Expand All @@ -129,7 +129,7 @@ private List<Composition.SectionComponent> makeCompositionSection(
}
sectionComponentList.add(sectionComponent);
}
if (Objects.nonNull(physicalObservationList)) {
if (Objects.nonNull(physicalObservationList) && !physicalObservationList.isEmpty()) {
Composition.SectionComponent sectionComponent = new Composition.SectionComponent();
sectionComponent.setCode(
new CodeableConcept()
Expand All @@ -149,7 +149,7 @@ private List<Composition.SectionComponent> makeCompositionSection(
}
sectionComponentList.add(sectionComponent);
}
if (Objects.nonNull(allergieList)) {
if (Objects.nonNull(allergieList) && !allergieList.isEmpty()) {
Composition.SectionComponent sectionComponent = new Composition.SectionComponent();
sectionComponent.setCode(
new CodeableConcept()
Expand All @@ -169,7 +169,7 @@ private List<Composition.SectionComponent> makeCompositionSection(
}
sectionComponentList.add(sectionComponent);
}
if (Objects.nonNull(medicalHistoryList)) {
if (Objects.nonNull(medicalHistoryList) && !medicalHistoryList.isEmpty()) {
Composition.SectionComponent sectionComponent = new Composition.SectionComponent();
sectionComponent.setCode(
new CodeableConcept()
Expand All @@ -187,7 +187,7 @@ private List<Composition.SectionComponent> makeCompositionSection(
}
sectionComponentList.add(sectionComponent);
}
if (Objects.nonNull(familyMemberHistoryList)) {
if (Objects.nonNull(familyMemberHistoryList) && !familyMemberHistoryList.isEmpty()) {
Composition.SectionComponent sectionComponent = new Composition.SectionComponent();
sectionComponent.setCode(
new CodeableConcept()
Expand All @@ -205,7 +205,7 @@ private List<Composition.SectionComponent> makeCompositionSection(
}
sectionComponentList.add(sectionComponent);
}
if (Objects.nonNull(investigationAdviceList)) {
if (Objects.nonNull(investigationAdviceList) && !investigationAdviceList.isEmpty()) {
Composition.SectionComponent sectionComponent = new Composition.SectionComponent();
sectionComponent.setCode(
new CodeableConcept()
Expand All @@ -223,7 +223,7 @@ private List<Composition.SectionComponent> makeCompositionSection(
}
sectionComponentList.add(sectionComponent);
}
if (Objects.nonNull(medicationList)) {
if (Objects.nonNull(medicationList) && !medicationList.isEmpty()) {
Composition.SectionComponent sectionComponent = new Composition.SectionComponent();
sectionComponent.setCode(
new CodeableConcept()
Expand All @@ -236,11 +236,12 @@ private List<Composition.SectionComponent> makeCompositionSection(
for (MedicationRequest medication : medicationList) {
sectionComponent.addEntry(
new Reference()
.setReference(BundleResourceIdentifier.FAMILY_HISTORY + "/" + medication.getId()));
.setReference(
BundleResourceIdentifier.MEDICATION_REQUEST + "/" + medication.getId()));
}
sectionComponentList.add(sectionComponent);
}
if (Objects.nonNull(followupList)) {
if (Objects.nonNull(followupList) && !followupList.isEmpty()) {
Composition.SectionComponent sectionComponent = new Composition.SectionComponent();
sectionComponent.setCode(
new CodeableConcept()
Expand All @@ -257,7 +258,7 @@ private List<Composition.SectionComponent> makeCompositionSection(
}
sectionComponentList.add(sectionComponent);
}
if (Objects.nonNull(procedureList)) {
if (Objects.nonNull(procedureList) && !procedureList.isEmpty()) {
Composition.SectionComponent sectionComponent = new Composition.SectionComponent();
sectionComponent.setCode(
new CodeableConcept()
Expand All @@ -274,7 +275,7 @@ private List<Composition.SectionComponent> makeCompositionSection(
}
sectionComponentList.add(sectionComponent);
}
if (Objects.nonNull(referralList)) {
if (Objects.nonNull(referralList) && !referralList.isEmpty()) {
Composition.SectionComponent sectionComponent = new Composition.SectionComponent();
sectionComponent.setCode(
new CodeableConcept()
Expand All @@ -291,7 +292,7 @@ private List<Composition.SectionComponent> makeCompositionSection(
}
sectionComponentList.add(sectionComponent);
}
if (Objects.nonNull(otherObservationList)) {
if (Objects.nonNull(otherObservationList) && !otherObservationList.isEmpty()) {
Composition.SectionComponent sectionComponent = new Composition.SectionComponent();
sectionComponent.setCode(
new CodeableConcept()
Expand All @@ -309,7 +310,7 @@ private List<Composition.SectionComponent> makeCompositionSection(
}
sectionComponentList.add(sectionComponent);
}
if (Objects.nonNull(documentReferenceList)) {
if (Objects.nonNull(documentReferenceList) && !documentReferenceList.isEmpty()) {
Composition.SectionComponent sectionComponent = new Composition.SectionComponent();
sectionComponent.setCode(
new CodeableConcept()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class FieldIdentifiers {
public static final String ENTITY = "entity";
public static final String ENTITY_TYPE = "entityType";
public static final String LAST_UPDATED = "lastUpdated";
public static final String CREATED_ON = "createdOn";
public static final String MODULE = "module";
public static final String LINK_TOKEN_REQUEST_ID = "linkTokenRequestId";

// Patient table.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ public enum RequestStatus {
ENCRYPTED_HEALTH_INFORMATION_RECEIVED("Encrypted Health Information received by HIU from HIP"),
ENCRYPTED_HEALTH_INFORMATION_ERROR(
"Error while receiving encrypted Health Information by HIU from HIP"),
DECRYPTION_ERROR("Unable to decrypt the data sent by HIP");
DECRYPTION_ERROR("Unable to decrypt the data sent by HIP"),
FETCHING_BUNDLE("Fetching bundle from HIP"),
FETCHING_BUNDLE_ERROR("Error while fetching bundle from HIP"),
FETCHING_BUNDLE_SUCCESS("Bundle fetched successfully from HIP"),
ENCRYPTION_ERROR("Error while encrypting the data"),
ENCRYPTION_SUCCESS("Data encrypted successfully"),
DATA_TRANSFER_INITIATED("Data transfer initiated successfully"),
DATA_TRANSFER_ERROR("Error while initiating data transfer"),
DATA_TRANSFER_SUCCESS("Data transfer completed successfully");

private String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
import in.nha.abdm.wrapper.v3.hip.hrp.link.userInitiated.responses.InitV3Response;
import in.nha.abdm.wrapper.v3.hip.hrp.share.requests.OnShareV3Request;
import in.nha.abdm.wrapper.v3.hip.hrp.share.requests.ProfileShareV3Request;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
Expand Down Expand Up @@ -107,6 +109,17 @@ public void updateError(String requestId, Object errors, RequestStatus requestSt
mongoTemplate.updateFirst(query, update, RequestLog.class);
}

public void updateConsentStatus(String consentId, Object errors, RequestStatus requestStatus) {
Query query = new Query(Criteria.where(FieldIdentifiers.CONSENT_ID).is(consentId));
Update update = new Update();
if (errors != null) {
update.set(FieldIdentifiers.ERROR, errors);
}
update.set(FieldIdentifiers.STATUS, requestStatus);
update.set(FieldIdentifiers.LAST_UPDATED, Utils.getCurrentDateTime());
mongoTemplate.updateFirst(query, update, RequestLog.class);
}

/**
* If any error encountered in the generation of LinkToken API this method is used to update in
* requestLogs
Expand Down Expand Up @@ -241,43 +254,44 @@ public RequestLog findRequestLogByTransactionId(String transactionId) {
*/
public void persistHipLinkRequest(
LinkRecordsV3Request linkRecordsV3Request, RequestStatus status, Object errors) {

if (Objects.isNull(linkRecordsV3Request)) {
return;
}
RequestLog existingLog = logsRepo.findByClientRequestId(linkRecordsV3Request.getRequestId());
if (Objects.isNull(existingLog)) {
RequestLog requestLog = new RequestLog();
requestLog.setAbhaAddress(linkRecordsV3Request.getAbhaAddress());
requestLog.setModule(FieldIdentifiers.HIP_INITIATED_LINKING);
requestLog.setCreatedOn(Utils.getCurrentDateTime());
requestLog.setLastUpdated(Utils.getCurrentDateTime());
requestLog.setClientRequestId(linkRecordsV3Request.getRequestId());
requestLog.setGatewayRequestId(linkRecordsV3Request.getRequestId());
requestLog.setHipId(linkRecordsV3Request.getRequesterId());
requestLog.setStatus(status);
HashMap<String, Object> map = new HashMap<>();
map.put(FieldIdentifiers.LINK_RECORDS_REQUEST, linkRecordsV3Request);
requestLog.setRequestDetails(map);
if (Objects.nonNull(errors)) {
requestLog.setError(errors);
}
mongoTemplate.save(requestLog);
return;
String requestId = linkRecordsV3Request.getRequestId();
LocalDateTime now = Utils.getCurrentDateTime();

RequestLog existingLog = logsRepo.findByClientRequestId(requestId);

Map<String, Object> requestDetails =
Optional.ofNullable(existingLog != null ? existingLog.getRequestDetails() : null)
.orElse(new HashMap<>());

requestDetails.put(FieldIdentifiers.LINK_RECORDS_REQUEST, linkRecordsV3Request);

Query query = Query.query(Criteria.where(FieldIdentifiers.CLIENT_REQUEST_ID).is(requestId));

Update update =
new Update()
.set(FieldIdentifiers.LAST_UPDATED, now)
.set(FieldIdentifiers.STATUS, status)
.set(FieldIdentifiers.REQUEST_DETAILS, requestDetails);

if (existingLog == null) {
update
.setOnInsert(FieldIdentifiers.ABHA_ADDRESS, linkRecordsV3Request.getAbhaAddress())
.setOnInsert(FieldIdentifiers.MODULE, FieldIdentifiers.HIP_INITIATED_LINKING)
.setOnInsert(FieldIdentifiers.CREATED_ON, now)
.setOnInsert(FieldIdentifiers.CLIENT_REQUEST_ID, requestId)
.setOnInsert(FieldIdentifiers.GATEWAY_REQUEST_ID, requestId)
.setOnInsert(FieldIdentifiers.HIP_ID, linkRecordsV3Request.getRequesterId());
}
Query query =
new Query(
Criteria.where(FieldIdentifiers.CLIENT_REQUEST_ID)
.is(linkRecordsV3Request.getRequestId()));
Map<String, Object> map = existingLog.getRequestDetails();
if (Objects.isNull(map)) {
map = new HashMap<>();

if (Objects.nonNull(errors)) {
update.set(FieldIdentifiers.ERROR, errors);
}
map.replace(FieldIdentifiers.LINK_RECORDS_REQUEST, linkRecordsV3Request);
Update update = new Update();
update.set(FieldIdentifiers.REQUEST_DETAILS, map);
update.set(FieldIdentifiers.STATUS, status);
update.set(FieldIdentifiers.LAST_UPDATED, Utils.getCurrentDateTime());
mongoTemplate.updateFirst(query, update, RequestLog.class);

mongoTemplate.upsert(query, update, RequestLog.class);
}

/**
Expand Down Expand Up @@ -602,38 +616,57 @@ public void saveLinkTokenRequest(
*
* <p>Adding linkOnAddCareContextsResponse dump into db.
*
* @param linkOnAddCareContextsV3Response Acknowledgement from ABDM gateway for HipLinking.
* @param response Acknowledgement from ABDM gateway for HipLinking.
*/
public void setHipOnAddCareContextResponse(
LinkOnAddCareContextsV3Response linkOnAddCareContextsV3Response)
public void setHipOnAddCareContextResponse(LinkOnAddCareContextsV3Response response)
throws IllegalDataStateException {
RequestLog RequestLog =
logsRepo.findByGatewayRequestId(
linkOnAddCareContextsV3Response.getResponse().getRequestId());

if (RequestLog == null) {
throw new IllegalDataStateException(
"Request not found in database for: "
+ linkOnAddCareContextsV3Response.getResponse().getRequestId());
}
HashMap<String, Object> map = RequestLog.getRequestDetails();
map.put(FieldIdentifiers.HIP_ON_ADD_CARE_CONTEXT_RESPONSE, linkOnAddCareContextsV3Response);
Query query =
new Query(
Criteria.where(FieldIdentifiers.GATEWAY_REQUEST_ID)
.is(linkOnAddCareContextsV3Response.getResponse().getRequestId()));
Update update = new Update();
if ((Objects.nonNull(linkOnAddCareContextsV3Response.getError()))) {
update.set(FieldIdentifiers.ERROR, linkOnAddCareContextsV3Response.getError());
String requestId = response.getResponse().getRequestId();
log.info("Processing HIP OnAddCareContext response for requestId: {}", requestId);

Query query = Query.query(Criteria.where(FieldIdentifiers.GATEWAY_REQUEST_ID).is(requestId));
Update update =
new Update()
.set(
FieldIdentifiers.REQUEST_DETAILS
+ "."
+ FieldIdentifiers.HIP_ON_ADD_CARE_CONTEXT_RESPONSE,
response)
.set(FieldIdentifiers.LAST_UPDATED, Utils.getCurrentDateTime());

if (response.getError() != null) {
update.set(FieldIdentifiers.ERROR, response.getError());
} else {
update.set(FieldIdentifiers.STATUS, RequestStatus.CARE_CONTEXT_LINKED);
LinkRecordsV3Request linkRecordsV3Request =
(LinkRecordsV3Request)
RequestLog.getRequestDetails().get(FieldIdentifiers.LINK_RECORDS_REQUEST);
patientService.addPatientCareContexts(linkRecordsV3Request);
}
update.set(FieldIdentifiers.REQUEST_DETAILS, map);
mongoTemplate.updateFirst(query, update, RequestLog.class);
log.info("Querying gatewayRequestId = {}", requestId);
log.info("Query for requestId: {} {}", query, requestId);
log.info("Update for requestId: {} {}", update, requestId);
RequestLog updatedLog =
mongoTemplate.findAndModify(
query, update, FindAndModifyOptions.options().returnNew(true), RequestLog.class);

if (updatedLog == null) {
log.warn(
"findAndModify returned null, fallback to fetch manually for requestId: {}", requestId);
updatedLog = logsRepo.findByGatewayRequestId(requestId);

if (updatedLog == null) {
throw new IllegalDataStateException("Request not found in database for: " + requestId);
}
}

if (response.getError() == null) {
Object raw = updatedLog.getRequestDetails().get(FieldIdentifiers.LINK_RECORDS_REQUEST);
if (raw instanceof LinkRecordsV3Request) {
patientService.addPatientCareContexts((LinkRecordsV3Request) raw);
} else {
log.warn(
"Expected LinkRecordsV3Request but got: {}",
raw == null ? "null" : raw.getClass().getName());
}
}
log.info("Successfully updated requestId: {}", requestId);
}

public RequestLog getLogsByAbhaAddress(String abhaAddress, String hipId) {
Expand Down
Loading