Skip to content

Commit

Permalink
Merge pull request #13161 from SORMAS-Foundation/change-#13154-automa…
Browse files Browse the repository at this point in the history
…tic-processing-new-case-if-there-is-a-too-old-one

#13154 [Automatic lab message processing] Create new case if the exis…
  • Loading branch information
leventegal-she authored Oct 10, 2024
2 parents 4fdea77 + f4e63f8 commit bde6b27
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2334,13 +2334,7 @@ protected String getDeleteReferenceField(DeletionReference deletionReference) {
return super.getDeleteReferenceField(deletionReference);
}

public String getCaseUuidForAutomaticSampleAssignment(Set<String> uuids, Disease disease) {
Integer automaticSampleAssignmentThreshold = diseaseConfigurationFacade.getAutomaticSampleAssignmentThreshold(disease);

if (automaticSampleAssignmentThreshold == null) {
return null;
}

public String getCaseUuidForAutomaticSampleAssignment(Set<String> uuids, Disease disease, int threshold) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<String> cq = cb.createQuery(String.class);
Root<Case> caseRoot = cq.from(Case.class);
Expand All @@ -2362,9 +2356,10 @@ public String getCaseUuidForAutomaticSampleAssignment(Set<String> uuids, Disease
Date.class,
CriteriaBuilderHelper.coalesce(cb, Date.class, earliestSampleSq, caseRoot.get(Case.REPORT_DATE))),
cb.function(ExtendedPostgreSQL94Dialect.DATE, Date.class, cb.literal(new Date()))),
Long.valueOf(TimeUnit.DAYS.toSeconds(automaticSampleAssignmentThreshold)).doubleValue()));
Long.valueOf(TimeUnit.DAYS.toSeconds(threshold)).doubleValue()));
cq.orderBy(cb.desc(caseRoot.get(Case.REPORT_DATE)));

List<String> caseUuids = em.createQuery(cq).getResultList();
return caseUuids.size() == 1 ? caseUuids.get(0) : null;
return caseUuids.isEmpty() ? null : caseUuids.get(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,31 @@ protected void handlePickOrCreateEntry(
result.setNewCase(true);
callback.done(result);
} else {
Disease disease = externalMessageDto.getDisease();
Integer automaticSampleAssignmentThreshold = diseaseConfigurationFacade.getAutomaticSampleAssignmentThreshold(disease);
if (automaticSampleAssignmentThreshold == null) {
logger.debug(
"[MESSAGE PROCESSING] No automatic sample assignment threshold configured for disease {}. Canceling processing.",
disease);
callback.cancel();
return;
}

Set<String> similarCaseUuids = similarCases.stream().map(CaseSelectionDto::getUuid).collect(Collectors.toSet());
String caseUuid = caseService.getCaseUuidForAutomaticSampleAssignment(similarCaseUuids, similarCases.get(0).getDisease());
String caseUuid = caseService.getCaseUuidForAutomaticSampleAssignment(similarCaseUuids, disease, automaticSampleAssignmentThreshold);

if (caseUuid != null) {
if (caseUuid == null) {
logger.debug(
"[MESSAGE PROCESSING] None of the similar cases {} is usable for automatic sample assignment. Continue with case creation.",
similarCaseUuids);
result.setNewCase(true);
} else {
CaseSelectionDto caseToAssignTo =
similarCases.stream().filter(c -> c.getUuid().equals(caseUuid)).findFirst().orElseThrow(IllegalStateException::new);
result.setCaze(caseToAssignTo);
callback.done(result);
} else {
logger.debug(
"[MESSAGE PROCESSING] None of the similar cases {} is usable for automatic sample assignment. Canceling processing.",
similarCaseUuids);
callback.cancel();
}

callback.done(result);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,13 @@ public void testProcessWithExistingPersonAndCase() throws ExecutionException, In
getCaseFacade().save(caze);

result = runFlow(externalMessage);
assertThat(result.getStatus(), is(CANCELED));
assertThat(externalMessage.getStatus(), is(ExternalMessageStatus.UNPROCESSED));
assertThat(getExternalMessageFacade().getByUuid(externalMessage.getUuid()).getStatus(), is(ExternalMessageStatus.UNPROCESSED));
assertThat(result.getStatus(), is(DONE));
assertThat(externalMessage.getStatus(), is(ExternalMessageStatus.PROCESSED));
assertThat(getExternalMessageFacade().getByUuid(externalMessage.getUuid()).getStatus(), is(ExternalMessageStatus.PROCESSED));
List<CaseDataDto> cases = getCaseFacade().getAllAfter(new Date(0));
assertThat(cases, hasSize(2));
CaseDataDto newCase = cases.stream().filter(c -> !DataHelper.isSame(c, caze)).findFirst().get();
assertThat(newCase.getPerson(), is(caze.getPerson()));

// set the case report after the threshold
caze.setReportDate(DateHelper.subtractDays(new Date(), 5));
Expand All @@ -201,17 +205,15 @@ public void testProcessWithExistingPersonAndCase() throws ExecutionException, In
List<PersonDto> persons = getPersonFacade().getAllAfter(new Date(0));
assertThat(persons, hasSize(1));
assertThat(persons.get(0).getUuid(), is(person.getUuid()));
List<CaseDataDto> cases = getCaseFacade().getByPersonUuids(persons.stream().map(PersonDto::getUuid).collect(Collectors.toList()));
assertThat(cases, hasSize(1));
assertThat(cases.get(0).getUuid(), is(caze.getUuid()));
List<SampleDto> samples = getSampleFacade().getByCaseUuids(cases.stream().map(CaseDataDto::getUuid).collect(Collectors.toList()));
assertThat(samples, hasSize(1));
List<PathogenTestDto> pathogenTests = getPathogenTestFacade().getAllBySample(samples.get(0).toReference());
assertThat(pathogenTests, hasSize(1));
cases = getCaseFacade().getByPersonUuids(persons.stream().map(PersonDto::getUuid).collect(Collectors.toList()));
assertThat(cases, hasSize(2));
// the sample should be added on the new case
List<SampleDto> samples = getSampleFacade().getByCaseUuids(Collections.singletonList(newCase.getUuid()));
assertThat(samples, hasSize(2));
}

@Test
public void testProcessWithExistingPersonAndCaseWithSample() throws ExecutionException, InterruptedException {
public void testProcessWithExistingPersonAndCaseWithBySampleDate() throws ExecutionException, InterruptedException {
ExternalMessageDto externalMessage = createExternalMessage(null);

PersonDto person =
Expand All @@ -220,6 +222,7 @@ public void testProcessWithExistingPersonAndCaseWithSample() throws ExecutionExc
});

CaseDataDto caze = creator.createCase(reportingUser.toReference(), person.toReference(), rdcf, c -> {
c.setReportDate(DateHelper.subtractDays(new Date(), 1));
c.setDisease(externalMessage.getDisease());
});

Expand All @@ -240,8 +243,13 @@ public void testProcessWithExistingPersonAndCaseWithSample() throws ExecutionExc
getSampleFacade().saveSample(sample);

result = runFlow(externalMessage);
assertThat(result.getStatus(), is(CANCELED));
assertThat(externalMessage.getStatus(), is(ExternalMessageStatus.UNPROCESSED));
assertThat(result.getStatus(), is(DONE));
assertThat(externalMessage.getStatus(), is(ExternalMessageStatus.PROCESSED));

List<PersonDto> persons = getPersonFacade().getAllAfter(new Date(0));
List<CaseDataDto> cases = getCaseFacade().getByPersonUuids(persons.stream().map(PersonDto::getUuid).collect(Collectors.toList()));
assertThat(cases, hasSize(2));
CaseDataDto newCase = cases.stream().filter(c -> !DataHelper.isSame(c, caze)).findFirst().get();

// set the sample date time after the threshold
sample.setSampleDateTime(DateHelper.subtractDays(new Date(), 5));
Expand All @@ -251,13 +259,12 @@ public void testProcessWithExistingPersonAndCaseWithSample() throws ExecutionExc
assertThat(result.getStatus(), is(DONE));
assertThat(externalMessage.getStatus(), is(ExternalMessageStatus.PROCESSED));

List<PersonDto> persons = getPersonFacade().getAllAfter(new Date(0));
persons = getPersonFacade().getAllAfter(new Date(0));
assertThat(persons, hasSize(1));
assertThat(persons.get(0).getUuid(), is(person.getUuid()));
List<CaseDataDto> cases = getCaseFacade().getByPersonUuids(persons.stream().map(PersonDto::getUuid).collect(Collectors.toList()));
assertThat(cases, hasSize(1));
assertThat(cases.get(0).getUuid(), is(caze.getUuid()));
List<SampleDto> samples = getSampleFacade().getByCaseUuids(cases.stream().map(CaseDataDto::getUuid).collect(Collectors.toList()));
cases = getCaseFacade().getByPersonUuids(persons.stream().map(PersonDto::getUuid).collect(Collectors.toList()));
assertThat(cases, hasSize(2));
List<SampleDto> samples = getSampleFacade().getByCaseUuids(Collections.singletonList(newCase.getUuid()));
assertThat(samples, hasSize(2));
SampleDto processedSample = samples.stream().filter(s -> !DataHelper.isSame(s, sample)).findFirst().get();
List<PathogenTestDto> pathogenTests = getPathogenTestFacade().getAllBySample(processedSample.toReference());
Expand Down

0 comments on commit bde6b27

Please sign in to comment.