feat: Store next sample number on user collection
- Add nextSampleNumber field to AppUser record with default 100000 - Add compact constructor to initialize nextSampleNumber to 100000 - Add reserveNextSampleNumber method to atomically reserve and increment - Update createSample to use reserveNextSampleNumber from user - Update nextSampleNumber to read from user collection - Update dashboardOverview to use new nextSampleNumber method
This commit is contained in:
@@ -22,7 +22,13 @@ public record AppUser(
|
||||
String passwordHash,
|
||||
boolean active,
|
||||
UserRole role,
|
||||
Long nextSampleNumber,
|
||||
LocalDateTime createdAt,
|
||||
LocalDateTime updatedAt
|
||||
) {
|
||||
public AppUser {
|
||||
if (nextSampleNumber == null) {
|
||||
nextSampleNumber = 100000L;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,8 +68,7 @@ public class SampleService {
|
||||
.filter(sample -> !sample.completedAt().isBefore(today.atStartOfDay()))
|
||||
.filter(sample -> sample.completedAt().isBefore(today.plusDays(1).atStartOfDay()))
|
||||
.count();
|
||||
String ownerAccountId = authorizationService.accountId(actor);
|
||||
return new DashboardOverview(nextSampleNumber(ownerAccountId), openCount, completedToday, recent);
|
||||
return new DashboardOverview(nextSampleNumber(actorId), openCount, completedToday, recent);
|
||||
}
|
||||
|
||||
public LookupResult lookup(String actorId, long sampleNumber) {
|
||||
@@ -106,10 +105,10 @@ public class SampleService {
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "Landwirt nicht gefunden"));
|
||||
|
||||
String ownerAccountId = authorizationService.accountId(actor);
|
||||
long sampleNumber = reserveNextSampleNumber(actorId);
|
||||
Sample sample = new Sample(
|
||||
null,
|
||||
nextSampleNumber(ownerAccountId),
|
||||
sampleNumber,
|
||||
farmer.businessKey(),
|
||||
farmer.name(),
|
||||
farmer.email(),
|
||||
@@ -519,16 +518,41 @@ public class SampleService {
|
||||
return nextSampleNumber(null);
|
||||
}
|
||||
|
||||
public long nextSampleNumber(String ownerAccountId) {
|
||||
if (ownerAccountId != null) {
|
||||
Optional<Sample> lastSample = sampleRepository.findTopByOwnerAccountIdOrderBySampleNumberDesc(ownerAccountId);
|
||||
if (lastSample.isPresent()) {
|
||||
return lastSample.get().sampleNumber() + 1;
|
||||
public long nextSampleNumber(String actorId) {
|
||||
if (actorId == null) {
|
||||
return 100000L;
|
||||
}
|
||||
AppUser actor = requireActor(actorId);
|
||||
return actor.nextSampleNumber() != null ? actor.nextSampleNumber() : 100000L;
|
||||
}
|
||||
return sampleRepository.findTopByOrderBySampleNumberDesc()
|
||||
.map(sample -> sample.sampleNumber() + 1)
|
||||
.orElse(10000L);
|
||||
|
||||
public long reserveNextSampleNumber(String actorId) {
|
||||
AppUser actor = requireActor(actorId);
|
||||
long sampleNumber = actor.nextSampleNumber() != null ? actor.nextSampleNumber() : 100000L;
|
||||
|
||||
// Update user with next sample number
|
||||
appUserRepository.save(new AppUser(
|
||||
actor.id(),
|
||||
actor.accountId(),
|
||||
actor.primaryUser(),
|
||||
actor.displayName(),
|
||||
actor.companyName(),
|
||||
actor.address(),
|
||||
actor.street(),
|
||||
actor.houseNumber(),
|
||||
actor.postalCode(),
|
||||
actor.city(),
|
||||
actor.email(),
|
||||
actor.phoneNumber(),
|
||||
actor.passwordHash(),
|
||||
actor.active(),
|
||||
actor.role(),
|
||||
sampleNumber + 1,
|
||||
actor.createdAt(),
|
||||
LocalDateTime.now()
|
||||
));
|
||||
|
||||
return sampleNumber;
|
||||
}
|
||||
|
||||
public Sample loadSampleEntity(String actorId, String id) {
|
||||
|
||||
Reference in New Issue
Block a user