Skip to content

Commit bde6b27

Browse files
Merge pull request #13161 from SORMAS-Foundation/change-#13154-automatic-processing-new-case-if-there-is-a-too-old-one
#13154 [Automatic lab message processing] Create new case if the exis…
2 parents 4fdea77 + f4e63f8 commit bde6b27

File tree

3 files changed

+48
-35
lines changed

3 files changed

+48
-35
lines changed

sormas-backend/src/main/java/de/symeda/sormas/backend/caze/CaseService.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,13 +2334,7 @@ protected String getDeleteReferenceField(DeletionReference deletionReference) {
23342334
return super.getDeleteReferenceField(deletionReference);
23352335
}
23362336

2337-
public String getCaseUuidForAutomaticSampleAssignment(Set<String> uuids, Disease disease) {
2338-
Integer automaticSampleAssignmentThreshold = diseaseConfigurationFacade.getAutomaticSampleAssignmentThreshold(disease);
2339-
2340-
if (automaticSampleAssignmentThreshold == null) {
2341-
return null;
2342-
}
2343-
2337+
public String getCaseUuidForAutomaticSampleAssignment(Set<String> uuids, Disease disease, int threshold) {
23442338
CriteriaBuilder cb = em.getCriteriaBuilder();
23452339
CriteriaQuery<String> cq = cb.createQuery(String.class);
23462340
Root<Case> caseRoot = cq.from(Case.class);
@@ -2362,9 +2356,10 @@ public String getCaseUuidForAutomaticSampleAssignment(Set<String> uuids, Disease
23622356
Date.class,
23632357
CriteriaBuilderHelper.coalesce(cb, Date.class, earliestSampleSq, caseRoot.get(Case.REPORT_DATE))),
23642358
cb.function(ExtendedPostgreSQL94Dialect.DATE, Date.class, cb.literal(new Date()))),
2365-
Long.valueOf(TimeUnit.DAYS.toSeconds(automaticSampleAssignmentThreshold)).doubleValue()));
2359+
Long.valueOf(TimeUnit.DAYS.toSeconds(threshold)).doubleValue()));
2360+
cq.orderBy(cb.desc(caseRoot.get(Case.REPORT_DATE)));
23662361

23672362
List<String> caseUuids = em.createQuery(cq).getResultList();
2368-
return caseUuids.size() == 1 ? caseUuids.get(0) : null;
2363+
return caseUuids.isEmpty() ? null : caseUuids.get(0);
23692364
}
23702365
}

sormas-backend/src/main/java/de/symeda/sormas/backend/externalmessage/labmessage/AutomaticLabMessageProcessor.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,20 +171,31 @@ protected void handlePickOrCreateEntry(
171171
result.setNewCase(true);
172172
callback.done(result);
173173
} else {
174+
Disease disease = externalMessageDto.getDisease();
175+
Integer automaticSampleAssignmentThreshold = diseaseConfigurationFacade.getAutomaticSampleAssignmentThreshold(disease);
176+
if (automaticSampleAssignmentThreshold == null) {
177+
logger.debug(
178+
"[MESSAGE PROCESSING] No automatic sample assignment threshold configured for disease {}. Canceling processing.",
179+
disease);
180+
callback.cancel();
181+
return;
182+
}
183+
174184
Set<String> similarCaseUuids = similarCases.stream().map(CaseSelectionDto::getUuid).collect(Collectors.toSet());
175-
String caseUuid = caseService.getCaseUuidForAutomaticSampleAssignment(similarCaseUuids, similarCases.get(0).getDisease());
185+
String caseUuid = caseService.getCaseUuidForAutomaticSampleAssignment(similarCaseUuids, disease, automaticSampleAssignmentThreshold);
176186

177-
if (caseUuid != null) {
187+
if (caseUuid == null) {
188+
logger.debug(
189+
"[MESSAGE PROCESSING] None of the similar cases {} is usable for automatic sample assignment. Continue with case creation.",
190+
similarCaseUuids);
191+
result.setNewCase(true);
192+
} else {
178193
CaseSelectionDto caseToAssignTo =
179194
similarCases.stream().filter(c -> c.getUuid().equals(caseUuid)).findFirst().orElseThrow(IllegalStateException::new);
180195
result.setCaze(caseToAssignTo);
181-
callback.done(result);
182-
} else {
183-
logger.debug(
184-
"[MESSAGE PROCESSING] None of the similar cases {} is usable for automatic sample assignment. Canceling processing.",
185-
similarCaseUuids);
186-
callback.cancel();
187196
}
197+
198+
callback.done(result);
188199
}
189200
}
190201

sormas-backend/src/test/java/de/symeda/sormas/backend/externalmessage/labmessage/AutomaticLabMessageProcessorTest.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,13 @@ public void testProcessWithExistingPersonAndCase() throws ExecutionException, In
186186
getCaseFacade().save(caze);
187187

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

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

213215
@Test
214-
public void testProcessWithExistingPersonAndCaseWithSample() throws ExecutionException, InterruptedException {
216+
public void testProcessWithExistingPersonAndCaseWithBySampleDate() throws ExecutionException, InterruptedException {
215217
ExternalMessageDto externalMessage = createExternalMessage(null);
216218

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

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

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

242245
result = runFlow(externalMessage);
243-
assertThat(result.getStatus(), is(CANCELED));
244-
assertThat(externalMessage.getStatus(), is(ExternalMessageStatus.UNPROCESSED));
246+
assertThat(result.getStatus(), is(DONE));
247+
assertThat(externalMessage.getStatus(), is(ExternalMessageStatus.PROCESSED));
248+
249+
List<PersonDto> persons = getPersonFacade().getAllAfter(new Date(0));
250+
List<CaseDataDto> cases = getCaseFacade().getByPersonUuids(persons.stream().map(PersonDto::getUuid).collect(Collectors.toList()));
251+
assertThat(cases, hasSize(2));
252+
CaseDataDto newCase = cases.stream().filter(c -> !DataHelper.isSame(c, caze)).findFirst().get();
245253

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

254-
List<PersonDto> persons = getPersonFacade().getAllAfter(new Date(0));
262+
persons = getPersonFacade().getAllAfter(new Date(0));
255263
assertThat(persons, hasSize(1));
256264
assertThat(persons.get(0).getUuid(), is(person.getUuid()));
257-
List<CaseDataDto> cases = getCaseFacade().getByPersonUuids(persons.stream().map(PersonDto::getUuid).collect(Collectors.toList()));
258-
assertThat(cases, hasSize(1));
259-
assertThat(cases.get(0).getUuid(), is(caze.getUuid()));
260-
List<SampleDto> samples = getSampleFacade().getByCaseUuids(cases.stream().map(CaseDataDto::getUuid).collect(Collectors.toList()));
265+
cases = getCaseFacade().getByPersonUuids(persons.stream().map(PersonDto::getUuid).collect(Collectors.toList()));
266+
assertThat(cases, hasSize(2));
267+
List<SampleDto> samples = getSampleFacade().getByCaseUuids(Collections.singletonList(newCase.getUuid()));
261268
assertThat(samples, hasSize(2));
262269
SampleDto processedSample = samples.stream().filter(s -> !DataHelper.isSame(s, sample)).findFirst().get();
263270
List<PathogenTestDto> pathogenTests = getPathogenTestFacade().getAllBySample(processedSample.toReference());

0 commit comments

Comments
 (0)