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

feat(production): Batch bei Produktionsstart automatisch erstellen (#73)

- BatchNumber in allen ProductionOrder-Endpoints via BatchRepository auflösen
- BatchCreationFailed Error-Variante statt generischem ValidationFailure
- bestBeforeDate-Berechnung als Recipe.calculateBestBeforeDate() in die Domain verschoben
This commit is contained in:
Sebastian Frick 2026-02-26 09:13:51 +01:00
parent 26adf21162
commit 600d0f9f06
20 changed files with 356 additions and 397 deletions

View file

@ -11,22 +11,28 @@ public class StartProductionOrder {
private final ProductionOrderRepository productionOrderRepository;
private final BatchRepository batchRepository;
private final RecipeRepository recipeRepository;
private final BatchNumberGenerator batchNumberGenerator;
private final AuthorizationPort authorizationPort;
private final UnitOfWork unitOfWork;
public StartProductionOrder(
ProductionOrderRepository productionOrderRepository,
BatchRepository batchRepository,
RecipeRepository recipeRepository,
BatchNumberGenerator batchNumberGenerator,
AuthorizationPort authorizationPort,
UnitOfWork unitOfWork
) {
this.productionOrderRepository = productionOrderRepository;
this.batchRepository = batchRepository;
this.recipeRepository = recipeRepository;
this.batchNumberGenerator = batchNumberGenerator;
this.authorizationPort = authorizationPort;
this.unitOfWork = unitOfWork;
}
public Result<ProductionOrderError, ProductionOrder> execute(StartProductionOrderCommand cmd, ActorId performedBy) {
public Result<ProductionOrderError, StartProductionResult> execute(StartProductionOrderCommand cmd, ActorId performedBy) {
if (!authorizationPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)) {
return Result.failure(new ProductionOrderError.Unauthorized("Not authorized to start production orders"));
}
@ -46,56 +52,80 @@ public class StartProductionOrder {
}
}
// Load batch
var batchId = BatchId.of(cmd.batchId());
Batch batch;
switch (batchRepository.findById(batchId)) {
// Load recipe
Recipe recipe;
switch (recipeRepository.findById(order.recipeId())) {
case Result.Failure(var err) -> {
return Result.failure(new ProductionOrderError.RepositoryFailure(err.message()));
}
case Result.Success(var opt) -> {
if (opt.isEmpty()) {
return Result.failure(new ProductionOrderError.ValidationFailure("Batch '" + cmd.batchId() + "' not found"));
return Result.failure(new ProductionOrderError.ValidationFailure(
"Recipe '" + order.recipeId().value() + "' not found"));
}
batch = opt.get();
recipe = opt.get();
}
}
// Batch must be PLANNED
if (batch.status() != BatchStatus.PLANNED) {
// Recipe must be ACTIVE
if (recipe.status() != RecipeStatus.ACTIVE) {
return Result.failure(new ProductionOrderError.ValidationFailure(
"Batch '" + cmd.batchId() + "' is not in PLANNED status (current: " + batch.status() + ")"));
"Recipe '" + recipe.id().value() + "' is not ACTIVE (current: " + recipe.status() + ")"));
}
// Batch must reference the same recipe as the order
if (!batch.recipeId().equals(order.recipeId())) {
return Result.failure(new ProductionOrderError.ValidationFailure(
"Batch recipe '" + batch.recipeId().value() + "' does not match order recipe '" + order.recipeId().value() + "'"));
// Calculate bestBeforeDate (validates shelfLifeDays internally)
java.time.LocalDate bestBeforeDate;
switch (recipe.calculateBestBeforeDate(order.plannedDate())) {
case Result.Failure(var err) -> {
return Result.failure(new ProductionOrderError.ValidationFailure(
"Recipe '" + recipe.id().value() + "' has no valid shelf life configured: " + err.message()));
}
case Result.Success(var val) -> bestBeforeDate = val;
}
// Start production on order (RELEASED -> IN_PROGRESS, assigns batchId)
switch (order.startProduction(batchId)) {
case Result.Failure(var err) -> { return Result.failure(err); }
case Result.Success(var ignored) -> { }
// Generate batch number
BatchNumber batchNumber;
switch (batchNumberGenerator.generateNext(order.plannedDate())) {
case Result.Failure(var err) -> {
return Result.failure(new ProductionOrderError.RepositoryFailure(err.message()));
}
case Result.Success(var val) -> batchNumber = val;
}
// Build BatchDraft from order data
var batchDraft = new BatchDraft(
order.recipeId().value(),
order.plannedQuantity().amount().toPlainString(),
order.plannedQuantity().uom().name(),
order.plannedDate(),
bestBeforeDate
);
// Create batch in PLANNED status
Batch batch;
switch (Batch.plan(batchDraft, batchNumber)) {
case Result.Failure(var err) -> {
return Result.failure(new ProductionOrderError.BatchCreationFailed(err.code(), err.message()));
}
case Result.Success(var val) -> batch = val;
}
// Start production on batch (PLANNED -> IN_PRODUCTION)
switch (batch.startProduction()) {
case Result.Failure(var err) -> {
return Result.failure(new ProductionOrderError.ValidationFailure(err.message()));
return Result.failure(new ProductionOrderError.BatchCreationFailed(err.code(), err.message()));
}
case Result.Success(var ignored) -> { }
}
// Persist both atomically
return unitOfWork.executeAtomically(() -> {
switch (productionOrderRepository.save(order)) {
case Result.Failure(var err) -> {
return Result.failure(new ProductionOrderError.RepositoryFailure(err.message()));
}
case Result.Success(var ignored) -> { }
}
// Start production on order (RELEASED -> IN_PROGRESS, assigns batchId)
switch (order.startProduction(batch.id())) {
case Result.Failure(var err) -> { return Result.failure(err); }
case Result.Success(var ignored) -> { }
}
// Persist both atomically (batch first due to FK constraint on production_orders.batch_id)
return unitOfWork.executeAtomically(() -> {
switch (batchRepository.save(batch)) {
case Result.Failure(var err) -> {
return Result.failure(new ProductionOrderError.RepositoryFailure(err.message()));
@ -103,7 +133,14 @@ public class StartProductionOrder {
case Result.Success(var ignored) -> { }
}
return Result.success(order);
switch (productionOrderRepository.save(order)) {
case Result.Failure(var err) -> {
return Result.failure(new ProductionOrderError.RepositoryFailure(err.message()));
}
case Result.Success(var ignored) -> { }
}
return Result.success(new StartProductionResult(order, batch));
});
}
}

View file

@ -0,0 +1,7 @@
package de.effigenix.application.production;
import de.effigenix.domain.production.Batch;
import de.effigenix.domain.production.ProductionOrder;
public record StartProductionResult(ProductionOrder order, Batch batch) {
}

View file

@ -1,4 +1,4 @@
package de.effigenix.application.production.command;
public record StartProductionOrderCommand(String productionOrderId, String batchId) {
public record StartProductionOrderCommand(String productionOrderId) {
}

View file

@ -55,6 +55,10 @@ public sealed interface ProductionOrderError {
@Override public String message() { return "Cannot reschedule production order in status " + current; }
}
record BatchCreationFailed(String batchErrorCode, String message) implements ProductionOrderError {
@Override public String code() { return "PRODUCTION_ORDER_BATCH_CREATION_FAILED"; }
}
record ValidationFailure(String message) implements ProductionOrderError {
@Override public String code() { return "PRODUCTION_ORDER_VALIDATION_ERROR"; }
}

View file

@ -253,6 +253,16 @@ public class Recipe {
return Result.success(null);
}
// ==================== Business Logic ====================
public Result<RecipeError, java.time.LocalDate> calculateBestBeforeDate(java.time.LocalDate productionDate) {
if (shelfLifeDays == null || shelfLifeDays <= 0) {
return Result.failure(new RecipeError.InvalidShelfLife(
"ShelfLifeDays must be > 0, was: " + shelfLifeDays));
}
return Result.success(productionDate.plusDays(shelfLifeDays));
}
// ==================== Getters ====================
public RecipeId id() { return id; }

View file

@ -161,9 +161,12 @@ public class ProductionUseCaseConfiguration {
@Bean
public StartProductionOrder startProductionOrder(ProductionOrderRepository productionOrderRepository,
BatchRepository batchRepository,
RecipeRepository recipeRepository,
BatchNumberGenerator batchNumberGenerator,
AuthorizationPort authorizationPort,
UnitOfWork unitOfWork) {
return new StartProductionOrder(productionOrderRepository, batchRepository, authorizationPort, unitOfWork);
return new StartProductionOrder(productionOrderRepository, batchRepository, recipeRepository,
batchNumberGenerator, authorizationPort, unitOfWork);
}
@Bean

View file

@ -14,13 +14,15 @@ import de.effigenix.application.production.command.CreateProductionOrderCommand;
import de.effigenix.application.production.command.ReleaseProductionOrderCommand;
import de.effigenix.application.production.command.RescheduleProductionOrderCommand;
import de.effigenix.application.production.command.StartProductionOrderCommand;
import de.effigenix.domain.production.BatchRepository;
import de.effigenix.domain.production.ProductionOrder;
import de.effigenix.domain.production.ProductionOrderError;
import de.effigenix.domain.production.ProductionOrderStatus;
import de.effigenix.infrastructure.production.web.dto.CancelProductionOrderRequest;
import de.effigenix.infrastructure.production.web.dto.CreateProductionOrderRequest;
import de.effigenix.infrastructure.production.web.dto.ProductionOrderResponse;
import de.effigenix.infrastructure.production.web.dto.RescheduleProductionOrderRequest;
import de.effigenix.infrastructure.production.web.dto.StartProductionOrderRequest;
import de.effigenix.shared.security.ActorId;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
@ -53,6 +55,7 @@ public class ProductionOrderController {
private final CompleteProductionOrder completeProductionOrder;
private final CancelProductionOrder cancelProductionOrder;
private final ListProductionOrders listProductionOrders;
private final BatchRepository batchRepository;
public ProductionOrderController(CreateProductionOrder createProductionOrder,
GetProductionOrder getProductionOrder,
@ -61,7 +64,8 @@ public class ProductionOrderController {
StartProductionOrder startProductionOrder,
CompleteProductionOrder completeProductionOrder,
CancelProductionOrder cancelProductionOrder,
ListProductionOrders listProductionOrders) {
ListProductionOrders listProductionOrders,
BatchRepository batchRepository) {
this.createProductionOrder = createProductionOrder;
this.getProductionOrder = getProductionOrder;
this.releaseProductionOrder = releaseProductionOrder;
@ -70,6 +74,7 @@ public class ProductionOrderController {
this.completeProductionOrder = completeProductionOrder;
this.cancelProductionOrder = cancelProductionOrder;
this.listProductionOrders = listProductionOrders;
this.batchRepository = batchRepository;
}
@GetMapping
@ -104,7 +109,7 @@ public class ProductionOrderController {
}
var responses = result.unsafeGetValue().stream()
.map(ProductionOrderResponse::from)
.map(order -> ProductionOrderResponse.from(order, resolveBatchNumber(order)))
.toList();
return ResponseEntity.ok(responses);
}
@ -120,7 +125,7 @@ public class ProductionOrderController {
throw new ProductionOrderDomainErrorException(result.unsafeGetError());
}
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue()));
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue(), resolveBatchNumber(result.unsafeGetValue())));
}
@PostMapping
@ -147,7 +152,7 @@ public class ProductionOrderController {
}
return ResponseEntity.status(HttpStatus.CREATED)
.body(ProductionOrderResponse.from(result.unsafeGetValue()));
.body(ProductionOrderResponse.from(result.unsafeGetValue(), resolveBatchNumber(result.unsafeGetValue())));
}
@PostMapping("/{id}/reschedule")
@ -166,7 +171,7 @@ public class ProductionOrderController {
throw new ProductionOrderDomainErrorException(result.unsafeGetError());
}
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue()));
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue(), resolveBatchNumber(result.unsafeGetValue())));
}
@PostMapping("/{id}/release")
@ -184,26 +189,36 @@ public class ProductionOrderController {
throw new ProductionOrderDomainErrorException(result.unsafeGetError());
}
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue()));
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue(), resolveBatchNumber(result.unsafeGetValue())));
}
@PostMapping("/{id}/start")
@PreAuthorize("hasAuthority('PRODUCTION_ORDER_WRITE')")
public ResponseEntity<ProductionOrderResponse> startProductionOrder(
@PathVariable String id,
@Valid @RequestBody StartProductionOrderRequest request,
Authentication authentication
) {
logger.info("Starting production for order: {} with batch: {} by actor: {}", id, request.batchId(), authentication.getName());
logger.info("Starting production for order: {} by actor: {}", id, authentication.getName());
var cmd = new StartProductionOrderCommand(id, request.batchId());
var cmd = new StartProductionOrderCommand(id);
var result = startProductionOrder.execute(cmd, ActorId.of(authentication.getName()));
if (result.isFailure()) {
throw new ProductionOrderDomainErrorException(result.unsafeGetError());
}
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue()));
var startResult = result.unsafeGetValue();
return ResponseEntity.ok(ProductionOrderResponse.from(
startResult.order(),
startResult.batch().batchNumber().value()));
}
private String resolveBatchNumber(ProductionOrder order) {
if (order.batchId() == null) {
return null;
}
return batchRepository.findById(order.batchId())
.fold(err -> null, opt -> opt.map(batch -> batch.batchNumber().value()).orElse(null));
}
@PostMapping("/{id}/complete")
@ -221,7 +236,7 @@ public class ProductionOrderController {
throw new ProductionOrderDomainErrorException(result.unsafeGetError());
}
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue()));
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue(), resolveBatchNumber(result.unsafeGetValue())));
}
@PostMapping("/{id}/cancel")
@ -240,7 +255,7 @@ public class ProductionOrderController {
throw new ProductionOrderDomainErrorException(result.unsafeGetError());
}
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue()));
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue(), resolveBatchNumber(result.unsafeGetValue())));
}
public static class ProductionOrderDomainErrorException extends RuntimeException {

View file

@ -10,6 +10,7 @@ public record ProductionOrderResponse(
String recipeId,
String status,
String batchId,
String batchNumber,
String plannedQuantity,
String plannedQuantityUnit,
LocalDate plannedDate,
@ -19,12 +20,13 @@ public record ProductionOrderResponse(
OffsetDateTime createdAt,
OffsetDateTime updatedAt
) {
public static ProductionOrderResponse from(ProductionOrder order) {
public static ProductionOrderResponse from(ProductionOrder order, String batchNumber) {
return new ProductionOrderResponse(
order.id().value(),
order.recipeId().value(),
order.status().name(),
order.batchId() != null ? order.batchId().value() : null,
batchNumber,
order.plannedQuantity().amount().toPlainString(),
order.plannedQuantity().uom().name(),
order.plannedDate(),

View file

@ -1,6 +0,0 @@
package de.effigenix.infrastructure.production.web.dto;
import jakarta.validation.constraints.NotBlank;
public record StartProductionOrderRequest(@NotBlank String batchId) {
}

View file

@ -61,6 +61,7 @@ public final class ProductionErrorHttpStatusMapper {
case ProductionOrderError.RescheduleNotAllowed e -> 409;
case ProductionOrderError.BatchAlreadyAssigned e -> 409;
case ProductionOrderError.BatchNotCompleted e -> 409;
case ProductionOrderError.BatchCreationFailed e -> 400;
case ProductionOrderError.ValidationFailure e -> 400;
case ProductionOrderError.Unauthorized e -> 403;
case ProductionOrderError.RepositoryFailure e -> 500;