Skip to content

Commit

Permalink
Bug fix for duplicating new patient nodes (#1081)
Browse files Browse the repository at this point in the history
Patient nodes were getting duplicated under specific circumstances:
- patient does not already exist in the database
- more than one sample in a new incoming request share the same patient

These changes and rearrangement of some of the logic handle how the patient
is resolved per incoming sample should now work for both existing and new patients.

Signed-off-by: Angelica Ochoa <[email protected]>
  • Loading branch information
ao508 authored Jan 12, 2024
1 parent e39a535 commit ae1cf12
Showing 1 changed file with 26 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public class SampleServiceImpl implements SmileSampleService {
@Autowired
private CrdbMappingService crdbMappingService;


private static final Log LOG = LogFactory.getLog(SampleServiceImpl.class);
private final ObjectMapper mapper = new ObjectMapper();

Expand Down Expand Up @@ -166,13 +165,15 @@ public SmileSample fetchAndLoadPatientDetails(SmileSample sample) throws Excepti

// handle the scenario where a patient node does not already exist in the database
// to prevent any null pointer exceptions (a situation that had arose in some test dmp sample cases)
if (patientService.getPatientByCmoPatientId(
sample.getPatient().getCmoPatientId().getValue()) == null) {
if (patientService.getPatientByCmoPatientId(patient.getCmoPatientId().getValue()) == null) {
patientService.savePatientMetadata(patient);
sample.setPatient(patient);
}

// get patient by cmo id from latest sample metadata
// note: the patient according to the cmo id in the sample's latest metadata may
// not match the patient that is linked to an existing sample in the event
// that a patient swap is needed and is the reason for the incoming sample update
SmilePatient patientByLatestCmoId = patientService.getPatientByCmoPatientId(
sampleMetadata.getCmoPatientId());

Expand All @@ -183,28 +184,8 @@ public SmileSample fetchAndLoadPatientDetails(SmileSample sample) throws Excepti
+ "- confirm whether data construction and parsing is being handled correctly");
}

// scenario that requires a patient swap and updating the sample-to-patient relationship
// in the database and removing the former sample-to-patient relationship
if (patientByLatestCmoId == null) {
SmilePatient newPatient = new SmilePatient(sampleMetadata.getCmoPatientId(), "cmoId");
//if this sample is a clinical sample, we would also need to add dmpId
if (sample.getSampleCategory().equals("clinical")) {
Matcher matcher = DMP_PATIENT_ID.matcher(sampleMetadata.getPrimaryId());
if (matcher.find()) {
newPatient.addPatientAlias(new PatientAlias(matcher.group(), "dmpId"));
}
}
patientService.savePatientMetadata(newPatient);
sample.setPatient(newPatient);
// remove sample-to-patient relationship from former patient node
sampleRepository.removeSamplePatientRelationship(sample.getSmileSampleId(),
patient.getSmilePatientId());
return sample;
}

// scenario where we are checking for an update to the existing patient, which is the same
// that already existing and is linked to the sample in the database but may contain updates
// (i.e., a new patient alias)
// scenario where we are checking for an update to the existing patient and is linked to the
// sample in the database but may contain updates (i.e., a new patient alias)
if (patient.getCmoPatientId().getValue().equals(sampleMetadata.getCmoPatientId())) {
// go through the new patient aliases and indicator for whether a
// new patient alias was added to the existing patient
Expand All @@ -223,6 +204,26 @@ public SmileSample fetchAndLoadPatientDetails(SmileSample sample) throws Excepti
return sample;
}

// scenario that requires a patient swap and updating the sample-to-patient relationship
// in the database and removing the former sample-to-patient relationship
if (patientByLatestCmoId == null) {
SmilePatient newPatient = new SmilePatient(sampleMetadata.getCmoPatientId(), "cmoId");
//if this sample is a clinical sample, we would also need to add dmpId
if (sample.getSampleCategory().equals("clinical")
&& !sample.getPatient().hasPatientAlias("dmpId")) {
Matcher matcher = DMP_PATIENT_ID.matcher(sampleMetadata.getPrimaryId());
if (matcher.find()) {
newPatient.addPatientAlias(new PatientAlias(matcher.group(), "dmpId"));
}
}
patientService.savePatientMetadata(newPatient);
sample.setPatient(newPatient);
// remove sample-to-patient relationship from former patient node
sampleRepository.removeSamplePatientRelationship(sample.getSmileSampleId(),
patient.getSmilePatientId());
return sample;
}

// scenario where the patient that the sample-to-patient relationship points to in the database
// does not match the cmo patient id referenced in the latest sample metadata updates
// and the former sample-to-patient relationship needs to be removed
Expand Down

0 comments on commit ae1cf12

Please sign in to comment.