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,
|
String passwordHash,
|
||||||
boolean active,
|
boolean active,
|
||||||
UserRole role,
|
UserRole role,
|
||||||
|
Long nextSampleNumber,
|
||||||
LocalDateTime createdAt,
|
LocalDateTime createdAt,
|
||||||
LocalDateTime updatedAt
|
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.atStartOfDay()))
|
||||||
.filter(sample -> sample.completedAt().isBefore(today.plusDays(1).atStartOfDay()))
|
.filter(sample -> sample.completedAt().isBefore(today.plusDays(1).atStartOfDay()))
|
||||||
.count();
|
.count();
|
||||||
String ownerAccountId = authorizationService.accountId(actor);
|
return new DashboardOverview(nextSampleNumber(actorId), openCount, completedToday, recent);
|
||||||
return new DashboardOverview(nextSampleNumber(ownerAccountId), openCount, completedToday, recent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public LookupResult lookup(String actorId, long sampleNumber) {
|
public LookupResult lookup(String actorId, long sampleNumber) {
|
||||||
@@ -106,10 +105,10 @@ public class SampleService {
|
|||||||
.findFirst()
|
.findFirst()
|
||||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "Landwirt nicht gefunden"));
|
.orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "Landwirt nicht gefunden"));
|
||||||
|
|
||||||
String ownerAccountId = authorizationService.accountId(actor);
|
long sampleNumber = reserveNextSampleNumber(actorId);
|
||||||
Sample sample = new Sample(
|
Sample sample = new Sample(
|
||||||
null,
|
null,
|
||||||
nextSampleNumber(ownerAccountId),
|
sampleNumber,
|
||||||
farmer.businessKey(),
|
farmer.businessKey(),
|
||||||
farmer.name(),
|
farmer.name(),
|
||||||
farmer.email(),
|
farmer.email(),
|
||||||
@@ -519,16 +518,41 @@ public class SampleService {
|
|||||||
return nextSampleNumber(null);
|
return nextSampleNumber(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public long nextSampleNumber(String ownerAccountId) {
|
public long nextSampleNumber(String actorId) {
|
||||||
if (ownerAccountId != null) {
|
if (actorId == null) {
|
||||||
Optional<Sample> lastSample = sampleRepository.findTopByOwnerAccountIdOrderBySampleNumberDesc(ownerAccountId);
|
return 100000L;
|
||||||
if (lastSample.isPresent()) {
|
|
||||||
return lastSample.get().sampleNumber() + 1;
|
|
||||||
}
|
}
|
||||||
|
AppUser actor = requireActor(actorId);
|
||||||
|
return actor.nextSampleNumber() != null ? actor.nextSampleNumber() : 100000L;
|
||||||
}
|
}
|
||||||
return sampleRepository.findTopByOrderBySampleNumberDesc()
|
|
||||||
.map(sample -> sample.sampleNumber() + 1)
|
public long reserveNextSampleNumber(String actorId) {
|
||||||
.orElse(10000L);
|
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) {
|
public Sample loadSampleEntity(String actorId, String id) {
|
||||||
|
|||||||
Reference in New Issue
Block a user