From dbc8c2a2a20c139a91440cbd69509af7c1547893 Mon Sep 17 00:00:00 2001 From: Sven Carstensen Date: Tue, 17 Mar 2026 09:04:41 +0100 Subject: [PATCH] feat: Start sample numbering at 10000 for each veterinarian - Add findTopByOwnerAccountIdOrderBySampleNumberDesc to SampleRepository - Modify nextSampleNumber to accept ownerAccountId parameter - Calculate next sample number per user based on ownerAccountId - Update dashboardOverview and createSample to use user-specific numbering --- .../muh/repository/SampleRepository.java | 2 ++ .../muh/service/SampleService.java | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) 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) {