1
0
Fork 0
mirror of https://github.com/s-frick/effigenix.git synced 2026-03-28 15:49:35 +01:00

fix(inventory): confirmReservation – Layering, Error-Semantik und FK-Bug

- MovementType-Ableitung via ReferenceType.toMovementType() in Domain Layer
- Doppelten Batch-Lookup in Stock.confirmReservation() eliminiert
- StockError.MovementCreationFailed statt RepositoryFailure für Domain-Fehler
- 204 No Content statt 200 OK (konsistent mit releaseReservation)
- Batches mit Menge 0 nicht mehr entfernen (FK stock_movements → stock_batches)
This commit is contained in:
Sebastian Frick 2026-02-25 23:13:04 +01:00
parent 0b6028b967
commit 74dc9a6981
9 changed files with 43 additions and 44 deletions

View file

@ -44,10 +44,7 @@ public class ConfirmReservation {
}
// 3. MovementType aus ReferenceType ableiten
String movementType = switch (confirmed.referenceType()) {
case PRODUCTION_ORDER -> "PRODUCTION_CONSUMPTION";
case SALE_ORDER -> "SALE";
};
String movementType = confirmed.referenceType().toMovementType().name();
// 4. StockMovements erzeugen
List<StockMovement> movements = new ArrayList<>();
@ -68,7 +65,7 @@ public class ConfirmReservation {
);
switch (StockMovement.record(draft)) {
case Result.Failure(var err) ->
{ return Result.failure(new StockError.RepositoryFailure(err.message())); }
{ return Result.failure(new StockError.MovementCreationFailed(err.message())); }
case Result.Success(var val) -> movements.add(val);
}
}

View file

@ -2,5 +2,12 @@ package de.effigenix.domain.inventory;
public enum ReferenceType {
PRODUCTION_ORDER,
SALE_ORDER
SALE_ORDER;
public MovementType toMovementType() {
return switch (this) {
case PRODUCTION_ORDER -> MovementType.PRODUCTION_CONSUMPTION;
case SALE_ORDER -> MovementType.SALE;
};
}
}

View file

@ -35,7 +35,7 @@ import java.util.stream.Collectors;
* - reserve: FEFO allocation across AVAILABLE/EXPIRING_SOON batches sorted by expiryDate ASC
* - releaseReservation: removes reservation by ID, implicitly freeing allocated quantities
* - confirmReservation: deducts allocated quantities from batches, removes reservation, returns ConfirmedReservation
* - batches with quantity 0 after deduction are removed
* - batches with quantity 0 after deduction are kept (FK from stock_movements requires batch persistence)
* - reservations track allocated quantities per batch; no over-reservation possible
*/
public class Stock {
@ -366,7 +366,7 @@ public class Stock {
return Result.failure(new StockError.ReservationNotFound(reservationId.value()));
}
// 2. Für jede Allocation: Batch finden und BatchReference merken
// 2. Für jede Allocation: Batch finden, Menge abziehen, ConfirmedAllocation erzeugen
List<ConfirmedAllocation> confirmedAllocations = new ArrayList<>();
for (StockBatchAllocation alloc : reservation.allocations()) {
StockBatch batch = batches.stream()
@ -376,35 +376,21 @@ public class Stock {
if (batch == null) {
return Result.failure(new StockError.BatchNotFound(alloc.stockBatchId().value()));
}
confirmedAllocations.add(new ConfirmedAllocation(
batch.id(), batch.batchReference(), alloc.allocatedQuantity()));
}
// 3. Für jede Allocation: Menge abziehen, bei 0 Batch entfernen
for (ConfirmedAllocation confirmed : confirmedAllocations) {
StockBatch batch = batches.stream()
.filter(b -> b.id().equals(confirmed.stockBatchId()))
.findFirst()
.orElse(null);
if (batch == null) {
return Result.failure(new StockError.BatchNotFound(confirmed.stockBatchId().value()));
}
Quantity remaining;
switch (batch.removeQuantity(confirmed.allocatedQuantity())) {
switch (batch.removeQuantity(alloc.allocatedQuantity())) {
case Result.Failure(var err) -> { return Result.failure(err); }
case Result.Success(var val) -> remaining = val;
}
if (remaining.amount().signum() == 0) {
this.batches.remove(batch);
} else {
int index = this.batches.indexOf(batch);
this.batches.set(index, batch.withQuantity(remaining));
}
int index = this.batches.indexOf(batch);
this.batches.set(index, batch.withQuantity(remaining));
}
// 4. Reservation entfernen
// 3. Reservation entfernen
this.reservations.remove(reservation);
return Result.success(new ConfirmedReservation(

View file

@ -113,6 +113,10 @@ public sealed interface StockError {
@Override public String code() { return "UNAUTHORIZED"; }
}
record MovementCreationFailed(String message) implements StockError {
@Override public String code() { return "MOVEMENT_CREATION_FAILED"; }
}
record RepositoryFailure(String message) implements StockError {
@Override public String code() { return "REPOSITORY_ERROR"; }
}

View file

@ -332,7 +332,7 @@ public class StockController {
}
logger.info("Reservation {} of stock {} confirmed", reservationId, stockId);
return ResponseEntity.ok().build();
return ResponseEntity.noContent().build();
}
public static class StockDomainErrorException extends RuntimeException {

View file

@ -47,6 +47,7 @@ public final class InventoryErrorHttpStatusMapper {
case StockError.InvalidReferenceId e -> 400;
case StockError.InvalidReservationPriority e -> 400;
case StockError.Unauthorized e -> 403;
case StockError.MovementCreationFailed e -> 500;
case StockError.RepositoryFailure e -> 500;
};
}