diff --git a/backend/src/main/java/de/svencarstensen/muh/repository/SampleRepository.java b/backend/src/main/java/de/svencarstensen/muh/repository/SampleRepository.java index 3259eca..ac10e7e 100644 --- a/backend/src/main/java/de/svencarstensen/muh/repository/SampleRepository.java +++ b/backend/src/main/java/de/svencarstensen/muh/repository/SampleRepository.java @@ -12,6 +12,8 @@ public interface SampleRepository extends MongoRepository { Optional findTopByOrderBySampleNumberDesc(); + Optional findTopByOwnerAccountIdOrderBySampleNumberDesc(String ownerAccountId); + List findTop12ByOrderByUpdatedAtDesc(); List findByFarmerBusinessKeyOrderByCreatedAtDesc(String farmerBusinessKey); diff --git a/backend/src/main/java/de/svencarstensen/muh/service/SampleService.java b/backend/src/main/java/de/svencarstensen/muh/service/SampleService.java index 54953fd..7f72a39 100644 --- a/backend/src/main/java/de/svencarstensen/muh/service/SampleService.java +++ b/backend/src/main/java/de/svencarstensen/muh/service/SampleService.java @@ -28,6 +28,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.stream.Collectors; @Service @@ -67,7 +68,8 @@ public class SampleService { .filter(sample -> !sample.completedAt().isBefore(today.atStartOfDay())) .filter(sample -> sample.completedAt().isBefore(today.plusDays(1).atStartOfDay())) .count(); - return new DashboardOverview(nextSampleNumber(), openCount, completedToday, recent); + String ownerAccountId = authorizationService.accountId(actor); + return new DashboardOverview(nextSampleNumber(ownerAccountId), openCount, completedToday, recent); } public LookupResult lookup(String actorId, long sampleNumber) { @@ -104,9 +106,10 @@ public class SampleService { .findFirst() .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "Landwirt nicht gefunden")); + String ownerAccountId = authorizationService.accountId(actor); Sample sample = new Sample( null, - nextSampleNumber(), + nextSampleNumber(ownerAccountId), farmer.businessKey(), farmer.name(), farmer.email(), @@ -513,9 +516,19 @@ public class SampleService { } public long nextSampleNumber() { + return nextSampleNumber(null); + } + + public long nextSampleNumber(String ownerAccountId) { + if (ownerAccountId != null) { + Optional lastSample = sampleRepository.findTopByOwnerAccountIdOrderBySampleNumberDesc(ownerAccountId); + if (lastSample.isPresent()) { + return lastSample.get().sampleNumber() + 1; + } + } return sampleRepository.findTopByOrderBySampleNumberDesc() .map(sample -> sample.sampleNumber() + 1) - .orElse(100001L); + .orElse(10000L); } public Sample loadSampleEntity(String actorId, String id) {