1
0
Fork 0
mirror of https://github.com/s-frick/effigenix.git synced 2026-03-28 10:39: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 ProductionOrderRepository productionOrderRepository;
private final BatchRepository batchRepository; private final BatchRepository batchRepository;
private final RecipeRepository recipeRepository;
private final BatchNumberGenerator batchNumberGenerator;
private final AuthorizationPort authorizationPort; private final AuthorizationPort authorizationPort;
private final UnitOfWork unitOfWork; private final UnitOfWork unitOfWork;
public StartProductionOrder( public StartProductionOrder(
ProductionOrderRepository productionOrderRepository, ProductionOrderRepository productionOrderRepository,
BatchRepository batchRepository, BatchRepository batchRepository,
RecipeRepository recipeRepository,
BatchNumberGenerator batchNumberGenerator,
AuthorizationPort authorizationPort, AuthorizationPort authorizationPort,
UnitOfWork unitOfWork UnitOfWork unitOfWork
) { ) {
this.productionOrderRepository = productionOrderRepository; this.productionOrderRepository = productionOrderRepository;
this.batchRepository = batchRepository; this.batchRepository = batchRepository;
this.recipeRepository = recipeRepository;
this.batchNumberGenerator = batchNumberGenerator;
this.authorizationPort = authorizationPort; this.authorizationPort = authorizationPort;
this.unitOfWork = unitOfWork; 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)) { if (!authorizationPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)) {
return Result.failure(new ProductionOrderError.Unauthorized("Not authorized to start production orders")); return Result.failure(new ProductionOrderError.Unauthorized("Not authorized to start production orders"));
} }
@ -46,56 +52,80 @@ public class StartProductionOrder {
} }
} }
// Load batch // Load recipe
var batchId = BatchId.of(cmd.batchId()); Recipe recipe;
Batch batch; switch (recipeRepository.findById(order.recipeId())) {
switch (batchRepository.findById(batchId)) {
case Result.Failure(var err) -> { case Result.Failure(var err) -> {
return Result.failure(new ProductionOrderError.RepositoryFailure(err.message())); return Result.failure(new ProductionOrderError.RepositoryFailure(err.message()));
} }
case Result.Success(var opt) -> { case Result.Success(var opt) -> {
if (opt.isEmpty()) { 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 // Recipe must be ACTIVE
if (batch.status() != BatchStatus.PLANNED) { if (recipe.status() != RecipeStatus.ACTIVE) {
return Result.failure(new ProductionOrderError.ValidationFailure( 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 // Calculate bestBeforeDate (validates shelfLifeDays internally)
if (!batch.recipeId().equals(order.recipeId())) { java.time.LocalDate bestBeforeDate;
return Result.failure(new ProductionOrderError.ValidationFailure( switch (recipe.calculateBestBeforeDate(order.plannedDate())) {
"Batch recipe '" + batch.recipeId().value() + "' does not match order recipe '" + order.recipeId().value() + "'")); 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) // Generate batch number
switch (order.startProduction(batchId)) { BatchNumber batchNumber;
case Result.Failure(var err) -> { return Result.failure(err); } switch (batchNumberGenerator.generateNext(order.plannedDate())) {
case Result.Success(var ignored) -> { } 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) // Start production on batch (PLANNED -> IN_PRODUCTION)
switch (batch.startProduction()) { switch (batch.startProduction()) {
case Result.Failure(var err) -> { 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) -> { } case Result.Success(var ignored) -> { }
} }
// Persist both atomically // Start production on order (RELEASED -> IN_PROGRESS, assigns batchId)
return unitOfWork.executeAtomically(() -> { switch (order.startProduction(batch.id())) {
switch (productionOrderRepository.save(order)) { case Result.Failure(var err) -> { return Result.failure(err); }
case Result.Failure(var err) -> { case Result.Success(var ignored) -> { }
return Result.failure(new ProductionOrderError.RepositoryFailure(err.message())); }
}
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)) { switch (batchRepository.save(batch)) {
case Result.Failure(var err) -> { case Result.Failure(var err) -> {
return Result.failure(new ProductionOrderError.RepositoryFailure(err.message())); return Result.failure(new ProductionOrderError.RepositoryFailure(err.message()));
@ -103,7 +133,14 @@ public class StartProductionOrder {
case Result.Success(var ignored) -> { } 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; 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; } @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 { record ValidationFailure(String message) implements ProductionOrderError {
@Override public String code() { return "PRODUCTION_ORDER_VALIDATION_ERROR"; } @Override public String code() { return "PRODUCTION_ORDER_VALIDATION_ERROR"; }
} }

View file

@ -253,6 +253,16 @@ public class Recipe {
return Result.success(null); 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 ==================== // ==================== Getters ====================
public RecipeId id() { return id; } public RecipeId id() { return id; }

View file

@ -161,9 +161,12 @@ public class ProductionUseCaseConfiguration {
@Bean @Bean
public StartProductionOrder startProductionOrder(ProductionOrderRepository productionOrderRepository, public StartProductionOrder startProductionOrder(ProductionOrderRepository productionOrderRepository,
BatchRepository batchRepository, BatchRepository batchRepository,
RecipeRepository recipeRepository,
BatchNumberGenerator batchNumberGenerator,
AuthorizationPort authorizationPort, AuthorizationPort authorizationPort,
UnitOfWork unitOfWork) { UnitOfWork unitOfWork) {
return new StartProductionOrder(productionOrderRepository, batchRepository, authorizationPort, unitOfWork); return new StartProductionOrder(productionOrderRepository, batchRepository, recipeRepository,
batchNumberGenerator, authorizationPort, unitOfWork);
} }
@Bean @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.ReleaseProductionOrderCommand;
import de.effigenix.application.production.command.RescheduleProductionOrderCommand; import de.effigenix.application.production.command.RescheduleProductionOrderCommand;
import de.effigenix.application.production.command.StartProductionOrderCommand; 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.ProductionOrderError;
import de.effigenix.domain.production.ProductionOrderStatus; import de.effigenix.domain.production.ProductionOrderStatus;
import de.effigenix.infrastructure.production.web.dto.CancelProductionOrderRequest; import de.effigenix.infrastructure.production.web.dto.CancelProductionOrderRequest;
import de.effigenix.infrastructure.production.web.dto.CreateProductionOrderRequest; import de.effigenix.infrastructure.production.web.dto.CreateProductionOrderRequest;
import de.effigenix.infrastructure.production.web.dto.ProductionOrderResponse; import de.effigenix.infrastructure.production.web.dto.ProductionOrderResponse;
import de.effigenix.infrastructure.production.web.dto.RescheduleProductionOrderRequest; import de.effigenix.infrastructure.production.web.dto.RescheduleProductionOrderRequest;
import de.effigenix.infrastructure.production.web.dto.StartProductionOrderRequest;
import de.effigenix.shared.security.ActorId; import de.effigenix.shared.security.ActorId;
import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
@ -53,6 +55,7 @@ public class ProductionOrderController {
private final CompleteProductionOrder completeProductionOrder; private final CompleteProductionOrder completeProductionOrder;
private final CancelProductionOrder cancelProductionOrder; private final CancelProductionOrder cancelProductionOrder;
private final ListProductionOrders listProductionOrders; private final ListProductionOrders listProductionOrders;
private final BatchRepository batchRepository;
public ProductionOrderController(CreateProductionOrder createProductionOrder, public ProductionOrderController(CreateProductionOrder createProductionOrder,
GetProductionOrder getProductionOrder, GetProductionOrder getProductionOrder,
@ -61,7 +64,8 @@ public class ProductionOrderController {
StartProductionOrder startProductionOrder, StartProductionOrder startProductionOrder,
CompleteProductionOrder completeProductionOrder, CompleteProductionOrder completeProductionOrder,
CancelProductionOrder cancelProductionOrder, CancelProductionOrder cancelProductionOrder,
ListProductionOrders listProductionOrders) { ListProductionOrders listProductionOrders,
BatchRepository batchRepository) {
this.createProductionOrder = createProductionOrder; this.createProductionOrder = createProductionOrder;
this.getProductionOrder = getProductionOrder; this.getProductionOrder = getProductionOrder;
this.releaseProductionOrder = releaseProductionOrder; this.releaseProductionOrder = releaseProductionOrder;
@ -70,6 +74,7 @@ public class ProductionOrderController {
this.completeProductionOrder = completeProductionOrder; this.completeProductionOrder = completeProductionOrder;
this.cancelProductionOrder = cancelProductionOrder; this.cancelProductionOrder = cancelProductionOrder;
this.listProductionOrders = listProductionOrders; this.listProductionOrders = listProductionOrders;
this.batchRepository = batchRepository;
} }
@GetMapping @GetMapping
@ -104,7 +109,7 @@ public class ProductionOrderController {
} }
var responses = result.unsafeGetValue().stream() var responses = result.unsafeGetValue().stream()
.map(ProductionOrderResponse::from) .map(order -> ProductionOrderResponse.from(order, resolveBatchNumber(order)))
.toList(); .toList();
return ResponseEntity.ok(responses); return ResponseEntity.ok(responses);
} }
@ -120,7 +125,7 @@ public class ProductionOrderController {
throw new ProductionOrderDomainErrorException(result.unsafeGetError()); throw new ProductionOrderDomainErrorException(result.unsafeGetError());
} }
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue())); return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue(), resolveBatchNumber(result.unsafeGetValue())));
} }
@PostMapping @PostMapping
@ -147,7 +152,7 @@ public class ProductionOrderController {
} }
return ResponseEntity.status(HttpStatus.CREATED) return ResponseEntity.status(HttpStatus.CREATED)
.body(ProductionOrderResponse.from(result.unsafeGetValue())); .body(ProductionOrderResponse.from(result.unsafeGetValue(), resolveBatchNumber(result.unsafeGetValue())));
} }
@PostMapping("/{id}/reschedule") @PostMapping("/{id}/reschedule")
@ -166,7 +171,7 @@ public class ProductionOrderController {
throw new ProductionOrderDomainErrorException(result.unsafeGetError()); 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") @PostMapping("/{id}/release")
@ -184,26 +189,36 @@ public class ProductionOrderController {
throw new ProductionOrderDomainErrorException(result.unsafeGetError()); 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") @PostMapping("/{id}/start")
@PreAuthorize("hasAuthority('PRODUCTION_ORDER_WRITE')") @PreAuthorize("hasAuthority('PRODUCTION_ORDER_WRITE')")
public ResponseEntity<ProductionOrderResponse> startProductionOrder( public ResponseEntity<ProductionOrderResponse> startProductionOrder(
@PathVariable String id, @PathVariable String id,
@Valid @RequestBody StartProductionOrderRequest request,
Authentication authentication 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())); var result = startProductionOrder.execute(cmd, ActorId.of(authentication.getName()));
if (result.isFailure()) { if (result.isFailure()) {
throw new ProductionOrderDomainErrorException(result.unsafeGetError()); 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") @PostMapping("/{id}/complete")
@ -221,7 +236,7 @@ public class ProductionOrderController {
throw new ProductionOrderDomainErrorException(result.unsafeGetError()); 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") @PostMapping("/{id}/cancel")
@ -240,7 +255,7 @@ public class ProductionOrderController {
throw new ProductionOrderDomainErrorException(result.unsafeGetError()); 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 { public static class ProductionOrderDomainErrorException extends RuntimeException {

View file

@ -10,6 +10,7 @@ public record ProductionOrderResponse(
String recipeId, String recipeId,
String status, String status,
String batchId, String batchId,
String batchNumber,
String plannedQuantity, String plannedQuantity,
String plannedQuantityUnit, String plannedQuantityUnit,
LocalDate plannedDate, LocalDate plannedDate,
@ -19,12 +20,13 @@ public record ProductionOrderResponse(
OffsetDateTime createdAt, OffsetDateTime createdAt,
OffsetDateTime updatedAt OffsetDateTime updatedAt
) { ) {
public static ProductionOrderResponse from(ProductionOrder order) { public static ProductionOrderResponse from(ProductionOrder order, String batchNumber) {
return new ProductionOrderResponse( return new ProductionOrderResponse(
order.id().value(), order.id().value(),
order.recipeId().value(), order.recipeId().value(),
order.status().name(), order.status().name(),
order.batchId() != null ? order.batchId().value() : null, order.batchId() != null ? order.batchId().value() : null,
batchNumber,
order.plannedQuantity().amount().toPlainString(), order.plannedQuantity().amount().toPlainString(),
order.plannedQuantity().uom().name(), order.plannedQuantity().uom().name(),
order.plannedDate(), 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.RescheduleNotAllowed e -> 409;
case ProductionOrderError.BatchAlreadyAssigned e -> 409; case ProductionOrderError.BatchAlreadyAssigned e -> 409;
case ProductionOrderError.BatchNotCompleted e -> 409; case ProductionOrderError.BatchNotCompleted e -> 409;
case ProductionOrderError.BatchCreationFailed e -> 400;
case ProductionOrderError.ValidationFailure e -> 400; case ProductionOrderError.ValidationFailure e -> 400;
case ProductionOrderError.Unauthorized e -> 403; case ProductionOrderError.Unauthorized e -> 403;
case ProductionOrderError.RepositoryFailure e -> 500; case ProductionOrderError.RepositoryFailure e -> 500;

View file

@ -34,6 +34,8 @@ class StartProductionOrderTest {
@Mock private ProductionOrderRepository productionOrderRepository; @Mock private ProductionOrderRepository productionOrderRepository;
@Mock private BatchRepository batchRepository; @Mock private BatchRepository batchRepository;
@Mock private RecipeRepository recipeRepository;
@Mock private BatchNumberGenerator batchNumberGenerator;
@Mock private AuthorizationPort authPort; @Mock private AuthorizationPort authPort;
@Mock private UnitOfWork unitOfWork; @Mock private UnitOfWork unitOfWork;
@ -41,16 +43,19 @@ class StartProductionOrderTest {
private ActorId performedBy; private ActorId performedBy;
private static final LocalDate PLANNED_DATE = LocalDate.now().plusDays(7); private static final LocalDate PLANNED_DATE = LocalDate.now().plusDays(7);
private static final int SHELF_LIFE_DAYS = 14;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
startProductionOrder = new StartProductionOrder(productionOrderRepository, batchRepository, authPort, unitOfWork); startProductionOrder = new StartProductionOrder(
productionOrderRepository, batchRepository, recipeRepository,
batchNumberGenerator, authPort, unitOfWork);
performedBy = ActorId.of("admin-user"); performedBy = ActorId.of("admin-user");
lenient().when(unitOfWork.executeAtomically(any())).thenAnswer(inv -> ((Supplier<?>) inv.getArgument(0)).get()); lenient().when(unitOfWork.executeAtomically(any())).thenAnswer(inv -> ((Supplier<?>) inv.getArgument(0)).get());
} }
private StartProductionOrderCommand validCommand() { private StartProductionOrderCommand validCommand() {
return new StartProductionOrderCommand("order-1", "batch-1"); return new StartProductionOrderCommand("order-1");
} }
private ProductionOrder releasedOrder() { private ProductionOrder releasedOrder() {
@ -87,78 +92,90 @@ class StartProductionOrderTest {
); );
} }
private Batch plannedBatch() { private Recipe activeRecipe() {
return Batch.reconstitute( return Recipe.reconstitute(
BatchId.of("batch-1"),
new BatchNumber("P-2026-02-24-001"),
RecipeId.of("recipe-1"), RecipeId.of("recipe-1"),
BatchStatus.PLANNED, RecipeName.of("Test Recipe").unsafeGetValue(),
1,
RecipeType.FINISHED_PRODUCT,
"Test",
YieldPercentage.of(100).unsafeGetValue(),
SHELF_LIFE_DAYS,
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(), Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
null, null, null, "ART-001",
PLANNED_DATE, PLANNED_DATE.plusDays(14), RecipeStatus.ACTIVE,
List.of(),
List.of(),
OffsetDateTime.now(ZoneOffset.UTC), OffsetDateTime.now(ZoneOffset.UTC),
OffsetDateTime.now(ZoneOffset.UTC), OffsetDateTime.now(ZoneOffset.UTC)
null, null, null,
1L,
List.of()
); );
} }
private Batch plannedBatchWithDifferentRecipe() { private Recipe draftRecipe() {
return Batch.reconstitute( return Recipe.reconstitute(
BatchId.of("batch-1"),
new BatchNumber("P-2026-02-24-001"),
RecipeId.of("recipe-other"),
BatchStatus.PLANNED,
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
null, null, null,
PLANNED_DATE, PLANNED_DATE.plusDays(14),
OffsetDateTime.now(ZoneOffset.UTC),
OffsetDateTime.now(ZoneOffset.UTC),
null, null, null,
1L,
List.of()
);
}
private Batch inProductionBatch() {
return Batch.reconstitute(
BatchId.of("batch-1"),
new BatchNumber("P-2026-02-24-001"),
RecipeId.of("recipe-1"), RecipeId.of("recipe-1"),
BatchStatus.IN_PRODUCTION, RecipeName.of("Test Recipe").unsafeGetValue(),
1,
RecipeType.FINISHED_PRODUCT,
"Test",
YieldPercentage.of(100).unsafeGetValue(),
SHELF_LIFE_DAYS,
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(), Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
null, null, null, "ART-001",
PLANNED_DATE, PLANNED_DATE.plusDays(14), RecipeStatus.DRAFT,
List.of(),
List.of(),
OffsetDateTime.now(ZoneOffset.UTC), OffsetDateTime.now(ZoneOffset.UTC),
OffsetDateTime.now(ZoneOffset.UTC), OffsetDateTime.now(ZoneOffset.UTC)
null, null, null,
1L,
List.of()
); );
} }
@Test private BatchNumber batchNumber() {
@DisplayName("should start production when order is RELEASED and batch is PLANNED") return new BatchNumber("P-" + PLANNED_DATE + "-001");
void should_StartProduction_When_ValidCommand() { }
private void setupHappyPath() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true); when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1"))) when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder()))); .thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1"))) when(recipeRepository.findById(RecipeId.of("recipe-1")))
.thenReturn(Result.success(Optional.of(plannedBatch()))); .thenReturn(Result.success(Optional.of(activeRecipe())));
when(batchNumberGenerator.generateNext(PLANNED_DATE))
.thenReturn(Result.success(batchNumber()));
when(productionOrderRepository.save(any())).thenReturn(Result.success(null)); when(productionOrderRepository.save(any())).thenReturn(Result.success(null));
when(batchRepository.save(any())).thenReturn(Result.success(null)); when(batchRepository.save(any())).thenReturn(Result.success(null));
}
@Test
@DisplayName("should auto-create batch and start production when order is RELEASED")
void should_StartProduction_When_ValidCommand() {
setupHappyPath();
var result = startProductionOrder.execute(validCommand(), performedBy); var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isSuccess()).isTrue(); assertThat(result.isSuccess()).isTrue();
var order = result.unsafeGetValue(); var startResult = result.unsafeGetValue();
assertThat(order.status()).isEqualTo(ProductionOrderStatus.IN_PROGRESS); assertThat(startResult.order().status()).isEqualTo(ProductionOrderStatus.IN_PROGRESS);
assertThat(order.batchId()).isEqualTo(BatchId.of("batch-1")); assertThat(startResult.order().batchId()).isNotNull();
assertThat(startResult.batch().status()).isEqualTo(BatchStatus.IN_PRODUCTION);
assertThat(startResult.batch().batchNumber()).isEqualTo(batchNumber());
assertThat(startResult.batch().recipeId()).isEqualTo(RecipeId.of("recipe-1"));
verify(productionOrderRepository).save(any(ProductionOrder.class)); verify(productionOrderRepository).save(any(ProductionOrder.class));
verify(batchRepository).save(any(Batch.class)); verify(batchRepository).save(any(Batch.class));
} }
@Test
@DisplayName("should calculate bestBeforeDate from plannedDate + shelfLifeDays")
void should_CalculateBestBeforeDate() {
setupHappyPath();
var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isSuccess()).isTrue();
var batch = result.unsafeGetValue().batch();
assertThat(batch.bestBeforeDate()).isEqualTo(PLANNED_DATE.plusDays(SHELF_LIFE_DAYS));
}
@Test @Test
@DisplayName("should fail when actor lacks PRODUCTION_ORDER_WRITE permission") @DisplayName("should fail when actor lacks PRODUCTION_ORDER_WRITE permission")
void should_Fail_When_Unauthorized() { void should_Fail_When_Unauthorized() {
@ -186,52 +203,36 @@ class StartProductionOrderTest {
} }
@Test @Test
@DisplayName("should fail when batch not found") @DisplayName("should fail when recipe not found")
void should_Fail_When_BatchNotFound() { void should_Fail_When_RecipeNotFound() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true); when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1"))) when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder()))); .thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1"))) when(recipeRepository.findById(RecipeId.of("recipe-1")))
.thenReturn(Result.success(Optional.empty())); .thenReturn(Result.success(Optional.empty()));
var result = startProductionOrder.execute(validCommand(), performedBy); var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue(); assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class); assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class);
verify(productionOrderRepository, never()).save(any()); assertThat(result.unsafeGetError().message()).contains("Recipe");
assertThat(result.unsafeGetError().message()).contains("not found");
} }
@Test @Test
@DisplayName("should fail when batch is not PLANNED") @DisplayName("should fail when recipe is not ACTIVE")
void should_Fail_When_BatchNotPlanned() { void should_Fail_When_RecipeNotActive() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true); when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1"))) when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder()))); .thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1"))) when(recipeRepository.findById(RecipeId.of("recipe-1")))
.thenReturn(Result.success(Optional.of(inProductionBatch()))); .thenReturn(Result.success(Optional.of(draftRecipe())));
var result = startProductionOrder.execute(validCommand(), performedBy); var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue(); assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class); assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class);
verify(productionOrderRepository, never()).save(any()); assertThat(result.unsafeGetError().message()).contains("not ACTIVE");
}
@Test
@DisplayName("should fail when batch recipe does not match order recipe")
void should_Fail_When_RecipeMismatch() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1")))
.thenReturn(Result.success(Optional.of(plannedBatchWithDifferentRecipe())));
var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class);
assertThat(result.unsafeGetError().message()).contains("does not match order recipe");
verify(productionOrderRepository, never()).save(any());
} }
@Test @Test
@ -240,8 +241,10 @@ class StartProductionOrderTest {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true); when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1"))) when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(plannedOrder()))); .thenReturn(Result.success(Optional.of(plannedOrder())));
when(batchRepository.findById(BatchId.of("batch-1"))) when(recipeRepository.findById(RecipeId.of("recipe-1")))
.thenReturn(Result.success(Optional.of(plannedBatch()))); .thenReturn(Result.success(Optional.of(activeRecipe())));
when(batchNumberGenerator.generateNext(PLANNED_DATE))
.thenReturn(Result.success(batchNumber()));
var result = startProductionOrder.execute(validCommand(), performedBy); var result = startProductionOrder.execute(validCommand(), performedBy);
@ -265,12 +268,12 @@ class StartProductionOrderTest {
} }
@Test @Test
@DisplayName("should fail when batch repository findById returns error") @DisplayName("should fail when recipe repository returns error")
void should_Fail_When_BatchRepositoryError() { void should_Fail_When_RecipeRepositoryError() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true); when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1"))) when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder()))); .thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1"))) when(recipeRepository.findById(RecipeId.of("recipe-1")))
.thenReturn(Result.failure(new RepositoryError.DatabaseError("connection lost"))); .thenReturn(Result.failure(new RepositoryError.DatabaseError("connection lost")));
var result = startProductionOrder.execute(validCommand(), performedBy); var result = startProductionOrder.execute(validCommand(), performedBy);
@ -280,32 +283,16 @@ class StartProductionOrderTest {
verify(productionOrderRepository, never()).save(any()); verify(productionOrderRepository, never()).save(any());
} }
@Test
@DisplayName("should fail when order save fails")
void should_Fail_When_OrderSaveFails() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1")))
.thenReturn(Result.success(Optional.of(plannedBatch())));
when(productionOrderRepository.save(any()))
.thenReturn(Result.failure(new RepositoryError.DatabaseError("disk full")));
var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.RepositoryFailure.class);
}
@Test @Test
@DisplayName("should fail when batch save fails") @DisplayName("should fail when batch save fails")
void should_Fail_When_BatchSaveFails() { void should_Fail_When_BatchSaveFails() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true); when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1"))) when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder()))); .thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1"))) when(recipeRepository.findById(RecipeId.of("recipe-1")))
.thenReturn(Result.success(Optional.of(plannedBatch()))); .thenReturn(Result.success(Optional.of(activeRecipe())));
when(productionOrderRepository.save(any())).thenReturn(Result.success(null)); when(batchNumberGenerator.generateNext(PLANNED_DATE))
.thenReturn(Result.success(batchNumber()));
when(batchRepository.save(any())) when(batchRepository.save(any()))
.thenReturn(Result.failure(new RepositoryError.DatabaseError("disk full"))); .thenReturn(Result.failure(new RepositoryError.DatabaseError("disk full")));
@ -316,68 +303,126 @@ class StartProductionOrderTest {
} }
@Test @Test
@DisplayName("should fail when batch is COMPLETED") @DisplayName("should fail when order save fails")
void should_Fail_When_BatchCompleted() { void should_Fail_When_OrderSaveFails() {
var completedBatch = Batch.reconstitute( setupHappyPath();
BatchId.of("batch-1"), when(productionOrderRepository.save(any()))
new BatchNumber("P-2026-02-24-001"), .thenReturn(Result.failure(new RepositoryError.DatabaseError("disk full")));
var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.RepositoryFailure.class);
}
@Test
@DisplayName("should fail when batch number generation fails")
void should_Fail_When_BatchNumberGenerationFails() {
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder())));
when(recipeRepository.findById(RecipeId.of("recipe-1")))
.thenReturn(Result.success(Optional.of(activeRecipe())));
when(batchNumberGenerator.generateNext(PLANNED_DATE))
.thenReturn(Result.failure(new BatchError.RepositoryFailure("Sequence corrupted")));
var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.RepositoryFailure.class);
verify(productionOrderRepository, never()).save(any());
verify(batchRepository, never()).save(any());
}
@Test
@DisplayName("should fail when recipe has null shelfLifeDays")
void should_Fail_When_ShelfLifeDaysNull() {
var recipeWithoutShelfLife = Recipe.reconstitute(
RecipeId.of("recipe-1"), RecipeId.of("recipe-1"),
BatchStatus.COMPLETED, RecipeName.of("Test Recipe").unsafeGetValue(),
1,
RecipeType.FINISHED_PRODUCT,
"Test",
YieldPercentage.of(100).unsafeGetValue(),
null,
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(), Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
Quantity.of(new BigDecimal("95"), UnitOfMeasure.KILOGRAM).unsafeGetValue(), "ART-001",
Quantity.of(new BigDecimal("5"), UnitOfMeasure.KILOGRAM).unsafeGetValue(), RecipeStatus.ACTIVE,
"done", List.of(),
PLANNED_DATE, PLANNED_DATE.plusDays(14), List.of(),
OffsetDateTime.now(ZoneOffset.UTC), OffsetDateTime.now(ZoneOffset.UTC),
OffsetDateTime.now(ZoneOffset.UTC), OffsetDateTime.now(ZoneOffset.UTC)
OffsetDateTime.now(ZoneOffset.UTC),
null, null,
1L,
List.of()
); );
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true); when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1"))) when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder()))); .thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1"))) when(recipeRepository.findById(RecipeId.of("recipe-1")))
.thenReturn(Result.success(Optional.of(completedBatch))); .thenReturn(Result.success(Optional.of(recipeWithoutShelfLife)));
var result = startProductionOrder.execute(validCommand(), performedBy); var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue(); assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class); assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class);
assertThat(result.unsafeGetError().message()).contains("shelf life");
verify(productionOrderRepository, never()).save(any()); verify(productionOrderRepository, never()).save(any());
} }
@Test @Test
@DisplayName("should fail when batch is CANCELLED") @DisplayName("should fail when recipe has zero shelfLifeDays")
void should_Fail_When_BatchCancelled() { void should_Fail_When_ShelfLifeDaysZero() {
var cancelledBatch = Batch.reconstitute( var recipeWithZeroShelfLife = Recipe.reconstitute(
BatchId.of("batch-1"),
new BatchNumber("P-2026-02-24-001"),
RecipeId.of("recipe-1"), RecipeId.of("recipe-1"),
BatchStatus.CANCELLED, RecipeName.of("Test Recipe").unsafeGetValue(),
1,
RecipeType.FINISHED_PRODUCT,
"Test",
YieldPercentage.of(100).unsafeGetValue(),
0,
Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(), Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
null, null, null, "ART-001",
PLANNED_DATE, PLANNED_DATE.plusDays(14), RecipeStatus.ACTIVE,
List.of(),
List.of(),
OffsetDateTime.now(ZoneOffset.UTC), OffsetDateTime.now(ZoneOffset.UTC),
OffsetDateTime.now(ZoneOffset.UTC), OffsetDateTime.now(ZoneOffset.UTC)
null,
"Storniert", OffsetDateTime.now(ZoneOffset.UTC),
1L,
List.of()
); );
when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true); when(authPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)).thenReturn(true);
when(productionOrderRepository.findById(ProductionOrderId.of("order-1"))) when(productionOrderRepository.findById(ProductionOrderId.of("order-1")))
.thenReturn(Result.success(Optional.of(releasedOrder()))); .thenReturn(Result.success(Optional.of(releasedOrder())));
when(batchRepository.findById(BatchId.of("batch-1"))) when(recipeRepository.findById(RecipeId.of("recipe-1")))
.thenReturn(Result.success(Optional.of(cancelledBatch))); .thenReturn(Result.success(Optional.of(recipeWithZeroShelfLife)));
var result = startProductionOrder.execute(validCommand(), performedBy); var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isFailure()).isTrue(); assertThat(result.isFailure()).isTrue();
assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class); assertThat(result.unsafeGetError()).isInstanceOf(ProductionOrderError.ValidationFailure.class);
verify(productionOrderRepository, never()).save(any()); assertThat(result.unsafeGetError().message()).contains("shelf life");
}
@Test
@DisplayName("should set batch plannedQuantity and UOM from order")
void should_PropagateQuantityFromOrderToBatch() {
setupHappyPath();
var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isSuccess()).isTrue();
var batch = result.unsafeGetValue().batch();
assertThat(batch.plannedQuantity().amount()).isEqualByComparingTo(new BigDecimal("100"));
assertThat(batch.plannedQuantity().uom()).isEqualTo(UnitOfMeasure.KILOGRAM);
}
@Test
@DisplayName("should assign generated batch id to the order")
void should_AssignBatchIdToOrder() {
setupHappyPath();
var result = startProductionOrder.execute(validCommand(), performedBy);
assertThat(result.isSuccess()).isTrue();
var startResult = result.unsafeGetValue();
assertThat(startResult.order().batchId()).isEqualTo(startResult.batch().id());
} }
} }

View file

@ -39,11 +39,7 @@ class ProductionOrderFuzzTest {
int op = data.consumeInt(0, 4); int op = data.consumeInt(0, 4);
switch (op) { switch (op) {
case 0 -> order.release(); case 0 -> order.release();
case 1 -> { case 1 -> order.startProduction(BatchId.of(data.consumeString(50)));
try {
order.startProduction(BatchId.of(data.consumeString(50)));
} catch (Exception ignored) { }
}
case 2 -> order.complete(); case 2 -> order.complete();
case 3 -> order.cancel(data.consumeString(50)); case 3 -> order.cancel(data.consumeString(50));
case 4 -> order.reschedule(consumeLocalDate(data)); case 4 -> order.reschedule(consumeLocalDate(data));

View file

@ -389,41 +389,26 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
class StartProductionOrderEndpoint { class StartProductionOrderEndpoint {
@Test @Test
@DisplayName("RELEASED Order mit PLANNED Batch starten → 200, Status IN_PROGRESS") @DisplayName("RELEASED Order starten → 200, Batch automatisch erstellt, Status IN_PROGRESS")
void startOrder_releasedWithPlannedBatch_returns200() throws Exception { void startOrder_released_returns200() throws Exception {
String[] orderAndRecipe = createReleasedOrderWithRecipe(); String orderId = createReleasedOrder();
String orderId = orderAndRecipe[0];
String batchId = createPlannedBatch(orderAndRecipe[1]);
String json = """
{"batchId": "%s"}
""".formatted(batchId);
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId) mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
.header("Authorization", "Bearer " + adminToken) .header("Authorization", "Bearer " + adminToken))
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(orderId)) .andExpect(jsonPath("$.id").value(orderId))
.andExpect(jsonPath("$.status").value("IN_PROGRESS")) .andExpect(jsonPath("$.status").value("IN_PROGRESS"))
.andExpect(jsonPath("$.batchId").value(batchId)); .andExpect(jsonPath("$.batchId").isNotEmpty())
.andExpect(jsonPath("$.batchNumber").isNotEmpty());
} }
@Test @Test
@DisplayName("PLANNED Order starten → 409 (InvalidStatusTransition)") @DisplayName("PLANNED Order starten → 409 (InvalidStatusTransition)")
void startOrder_plannedOrder_returns409() throws Exception { void startOrder_plannedOrder_returns409() throws Exception {
String[] orderAndRecipe = createPlannedOrderWithRecipe(); String orderId = createPlannedOrder();
String orderId = orderAndRecipe[0];
String batchId = createPlannedBatch(orderAndRecipe[1]);
String json = """
{"batchId": "%s"}
""".formatted(batchId);
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId) mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
.header("Authorization", "Bearer " + adminToken) .header("Authorization", "Bearer " + adminToken))
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isConflict()) .andExpect(status().isConflict())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_INVALID_STATUS_TRANSITION")); .andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_INVALID_STATUS_TRANSITION"));
} }
@ -431,145 +416,54 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
@Test @Test
@DisplayName("Order nicht gefunden → 404") @DisplayName("Order nicht gefunden → 404")
void startOrder_notFound_returns404() throws Exception { void startOrder_notFound_returns404() throws Exception {
String json = """
{"batchId": "non-existent-batch"}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/start", "non-existent-id") mockMvc.perform(post("/api/production/production-orders/{id}/start", "non-existent-id")
.header("Authorization", "Bearer " + adminToken) .header("Authorization", "Bearer " + adminToken))
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isNotFound()) .andExpect(status().isNotFound())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_NOT_FOUND")); .andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_NOT_FOUND"));
} }
@Test
@DisplayName("Batch nicht gefunden → 400")
void startOrder_batchNotFound_returns400() throws Exception {
String orderId = createReleasedOrder();
String json = """
{"batchId": "non-existent-batch"}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_VALIDATION_ERROR"));
}
@Test @Test
@DisplayName("Ohne PRODUCTION_ORDER_WRITE → 403") @DisplayName("Ohne PRODUCTION_ORDER_WRITE → 403")
void startOrder_withViewerToken_returns403() throws Exception { void startOrder_withViewerToken_returns403() throws Exception {
String json = """
{"batchId": "any-batch"}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/start", "any-id") mockMvc.perform(post("/api/production/production-orders/{id}/start", "any-id")
.header("Authorization", "Bearer " + viewerToken) .header("Authorization", "Bearer " + viewerToken))
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isForbidden()); .andExpect(status().isForbidden());
} }
@Test @Test
@DisplayName("Ohne Token → 401") @DisplayName("Ohne Token → 401")
void startOrder_withoutToken_returns401() throws Exception { void startOrder_withoutToken_returns401() throws Exception {
String json = """ mockMvc.perform(post("/api/production/production-orders/{id}/start", "any-id"))
{"batchId": "any-batch"}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/start", "any-id")
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isUnauthorized()); .andExpect(status().isUnauthorized());
} }
@Test
@DisplayName("Batch nicht PLANNED (bereits gestartet) → 400")
void startOrder_batchNotPlanned_returns400() throws Exception {
String[] orderAndRecipe = createReleasedOrderWithRecipe();
String orderId = orderAndRecipe[0];
String batchId = createStartedBatch(orderAndRecipe[1]);
String json = """
{"batchId": "%s"}
""".formatted(batchId);
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_VALIDATION_ERROR"));
}
@Test @Test
@DisplayName("Bereits gestartete Order erneut starten → 409") @DisplayName("Bereits gestartete Order erneut starten → 409")
void startOrder_alreadyStarted_returns409() throws Exception { void startOrder_alreadyStarted_returns409() throws Exception {
String[] orderAndRecipe = createReleasedOrderWithRecipe(); String orderId = createReleasedOrder();
String orderId = orderAndRecipe[0];
String recipeId = orderAndRecipe[1];
String batchId1 = createPlannedBatch(recipeId);
String batchId2 = createPlannedBatch(recipeId);
String json1 = """
{"batchId": "%s"}
""".formatted(batchId1);
// First start // First start
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId) mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
.header("Authorization", "Bearer " + adminToken) .header("Authorization", "Bearer " + adminToken))
.contentType(MediaType.APPLICATION_JSON)
.content(json1))
.andExpect(status().isOk()); .andExpect(status().isOk());
// Second start // Second start
String json2 = """
{"batchId": "%s"}
""".formatted(batchId2);
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId) mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
.header("Authorization", "Bearer " + adminToken) .header("Authorization", "Bearer " + adminToken))
.contentType(MediaType.APPLICATION_JSON)
.content(json2))
.andExpect(status().isConflict()) .andExpect(status().isConflict())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_INVALID_STATUS_TRANSITION")); .andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_INVALID_STATUS_TRANSITION"));
} }
@Test @Test
@DisplayName("Batch mit anderem Rezept → 400 (RecipeMismatch)") @DisplayName("Order mit archiviertem Rezept starten → 400")
void startOrder_recipeMismatch_returns400() throws Exception { void startOrder_archivedRecipe_returns400() throws Exception {
String orderId = createReleasedOrder(); String orderId = createReleasedOrderThenArchiveRecipe();
String batchId = createPlannedBatch(); // creates batch with different recipe
String json = """
{"batchId": "%s"}
""".formatted(batchId);
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId) mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
.header("Authorization", "Bearer " + adminToken) .header("Authorization", "Bearer " + adminToken))
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isBadRequest()) .andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_VALIDATION_ERROR")) .andExpect(jsonPath("$.code").value("PRODUCTION_ORDER_VALIDATION_ERROR"))
.andExpect(jsonPath("$.message").value(org.hamcrest.Matchers.containsString("does not match order recipe"))); .andExpect(jsonPath("$.message").value(org.hamcrest.Matchers.containsString("not ACTIVE")));
}
@Test
@DisplayName("batchId leer → 400 (Bean Validation)")
void startOrder_blankBatchId_returns400() throws Exception {
String json = """
{"batchId": ""}
""";
mockMvc.perform(post("/api/production/production-orders/{id}/start", "any-id")
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isBadRequest());
} }
} }
@ -1178,21 +1072,12 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
return orderAndRecipe; return orderAndRecipe;
} }
/** Creates an IN_PROGRESS order (with a started batch). Returns orderId. */ /** Creates an IN_PROGRESS order (batch auto-created by start). Returns orderId. */
private String createInProgressOrder() throws Exception { private String createInProgressOrder() throws Exception {
String[] orderAndRecipe = createReleasedOrderWithRecipe(); String orderId = createReleasedOrder();
String orderId = orderAndRecipe[0];
String recipeId = orderAndRecipe[1];
String batchId = createPlannedBatch(recipeId);
String json = """
{"batchId": "%s"}
""".formatted(batchId);
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId) mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
.header("Authorization", "Bearer " + adminToken) .header("Authorization", "Bearer " + adminToken))
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isOk()); .andExpect(status().isOk());
return orderId; return orderId;
@ -1203,17 +1088,13 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
String[] orderAndRecipe = createReleasedOrderWithRecipe(); String[] orderAndRecipe = createReleasedOrderWithRecipe();
String orderId = orderAndRecipe[0]; String orderId = orderAndRecipe[0];
String recipeId = orderAndRecipe[1]; String recipeId = orderAndRecipe[1];
String batchId = createPlannedBatch(recipeId);
String startJson = """ var startResult = mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId)
{"batchId": "%s"} .header("Authorization", "Bearer " + adminToken))
""".formatted(batchId); .andExpect(status().isOk())
.andReturn();
mockMvc.perform(post("/api/production/production-orders/{id}/start", orderId) String batchId = objectMapper.readTree(startResult.getResponse().getContentAsString()).get("batchId").asText();
.header("Authorization", "Bearer " + adminToken)
.contentType(MediaType.APPLICATION_JSON)
.content(startJson))
.andExpect(status().isOk());
// Record a consumption (required to complete batch) // Record a consumption (required to complete batch)
String inputBatchId = createPlannedBatch(recipeId); String inputBatchId = createPlannedBatch(recipeId);
@ -1236,8 +1117,18 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
return new String[]{orderId, batchId}; return new String[]{orderId, batchId};
} }
private String createPlannedBatch() throws Exception { /** Creates a RELEASED order, then archives its recipe. Returns orderId. */
return createPlannedBatch(createActiveRecipe()); private String createReleasedOrderThenArchiveRecipe() throws Exception {
String[] orderAndRecipe = createReleasedOrderWithRecipe();
String orderId = orderAndRecipe[0];
String recipeId = orderAndRecipe[1];
// Archive the recipe
mockMvc.perform(post("/api/recipes/{id}/archive", recipeId)
.header("Authorization", "Bearer " + adminToken))
.andExpect(status().isOk());
return orderId;
} }
private String createPlannedBatch(String recipeId) throws Exception { private String createPlannedBatch(String recipeId) throws Exception {
@ -1252,18 +1143,6 @@ class ProductionOrderControllerIntegrationTest extends AbstractIntegrationTest {
return objectMapper.readTree(planResult.getResponse().getContentAsString()).get("id").asText(); return objectMapper.readTree(planResult.getResponse().getContentAsString()).get("id").asText();
} }
private String createStartedBatch() throws Exception {
return createStartedBatch(createActiveRecipe());
}
private String createStartedBatch(String recipeId) throws Exception {
String batchId = createPlannedBatch(recipeId);
mockMvc.perform(post("/api/production/batches/{id}/start", batchId)
.header("Authorization", "Bearer " + adminToken))
.andExpect(status().isOk());
return batchId;
}
private String createDraftRecipe() throws Exception { private String createDraftRecipe() throws Exception {
String json = """ String json = """
{ {

View file

@ -20,7 +20,7 @@ const STATUS_COLORS: Record<string, string> = {
CANCELLED: 'red', CANCELLED: 'red',
}; };
type Mode = 'view' | 'menu' | 'start-batch-input' | 'reschedule-input'; type Mode = 'view' | 'menu' | 'reschedule-input';
export function ProductionOrderDetailScreen() { export function ProductionOrderDetailScreen() {
const { params, back } = useNavigation(); const { params, back } = useNavigation();
@ -31,7 +31,6 @@ export function ProductionOrderDetailScreen() {
const { recipeName } = useRecipeNameLookup(); const { recipeName } = useRecipeNameLookup();
const [mode, setMode] = useState<Mode>('view'); const [mode, setMode] = useState<Mode>('view');
const [menuIndex, setMenuIndex] = useState(0); const [menuIndex, setMenuIndex] = useState(0);
const [batchId, setBatchId] = useState('');
const [newDate, setNewDate] = useState(''); const [newDate, setNewDate] = useState('');
const [success, setSuccess] = useState<string | null>(null); const [success, setSuccess] = useState<string | null>(null);
const [batch, setBatch] = useState<BatchDTO | null>(null); const [batch, setBatch] = useState<BatchDTO | null>(null);
@ -80,12 +79,11 @@ export function ProductionOrderDetailScreen() {
}; };
const handleStart = async () => { const handleStart = async () => {
if (!batchId.trim()) return; const result = await startProductionOrder(orderId);
const result = await startProductionOrder(orderId, { batchId: batchId.trim() });
if (result) { if (result) {
setSuccess('Produktion gestartet.'); const bn = result.batchNumber ? ` Charge: ${result.batchNumber}` : '';
setSuccess(`Produktion gestartet.${bn}`);
setMode('view'); setMode('view');
setBatchId('');
} }
}; };
@ -107,11 +105,6 @@ export function ProductionOrderDetailScreen() {
return; return;
} }
if (mode === 'start-batch-input') {
if (key.escape) setMode('menu');
return;
}
if (mode === 'menu') { if (mode === 'menu') {
if (key.upArrow) setMenuIndex((i) => Math.max(0, i - 1)); if (key.upArrow) setMenuIndex((i) => Math.max(0, i - 1));
if (key.downArrow) setMenuIndex((i) => Math.min(menuItems.length - 1, i + 1)); if (key.downArrow) setMenuIndex((i) => Math.min(menuItems.length - 1, i + 1));
@ -122,10 +115,7 @@ export function ProductionOrderDetailScreen() {
setMode('reschedule-input'); setMode('reschedule-input');
setNewDate(''); setNewDate('');
} }
if (action === 'start') { if (action === 'start') void handleStart();
setMode('start-batch-input');
setBatchId('');
}
} }
if (key.escape) setMode('view'); if (key.escape) setMode('view');
return; return;
@ -218,22 +208,6 @@ export function ProductionOrderDetailScreen() {
</Box> </Box>
)} )}
{mode === 'start-batch-input' && (
<Box flexDirection="column" borderStyle="round" borderColor="yellow" paddingX={1}>
<Text color="yellow" bold>Chargen-ID eingeben:</Text>
<Box>
<Text color="gray"> </Text>
<TextInput
value={batchId}
onChange={setBatchId}
onSubmit={() => void handleStart()}
focus={true}
/>
</Box>
<Text color="gray" dimColor>Enter bestätigen · Escape abbrechen</Text>
</Box>
)}
{mode === 'reschedule-input' && ( {mode === 'reschedule-input' && (
<Box flexDirection="column" borderStyle="round" borderColor="yellow" paddingX={1}> <Box flexDirection="column" borderStyle="round" borderColor="yellow" paddingX={1}>
<Text color="yellow" bold>Neues Datum (YYYY-MM-DD):</Text> <Text color="yellow" bold>Neues Datum (YYYY-MM-DD):</Text>

View file

@ -1,5 +1,5 @@
import { useState, useCallback } from 'react'; import { useState, useCallback } from 'react';
import type { ProductionOrderDTO, CreateProductionOrderRequest, StartProductionOrderRequest, ProductionOrderFilter } from '@effigenix/api-client'; import type { ProductionOrderDTO, CreateProductionOrderRequest, ProductionOrderFilter } from '@effigenix/api-client';
import { client } from '../utils/api-client.js'; import { client } from '../utils/api-client.js';
interface ProductionOrdersState { interface ProductionOrdersState {
@ -77,10 +77,10 @@ export function useProductionOrders() {
} }
}, []); }, []);
const startProductionOrder = useCallback(async (id: string, request: StartProductionOrderRequest) => { const startProductionOrder = useCallback(async (id: string) => {
setState((s) => ({ ...s, loading: true, error: null })); setState((s) => ({ ...s, loading: true, error: null }));
try { try {
const productionOrder = await client.productionOrders.start(id, request); const productionOrder = await client.productionOrders.start(id);
setState((s) => ({ ...s, productionOrder, loading: false, error: null })); setState((s) => ({ ...s, productionOrder, loading: false, error: null }));
return productionOrder; return productionOrder;
} catch (err) { } catch (err) {

File diff suppressed because one or more lines are too long

View file

@ -115,7 +115,6 @@ export type {
ReserveStockRequest, ReserveStockRequest,
StockMovementDTO, StockMovementDTO,
RecordStockMovementRequest, RecordStockMovementRequest,
StartProductionOrderRequest,
CountryDTO, CountryDTO,
} from '@effigenix/types'; } from '@effigenix/types';

View file

@ -1,7 +1,7 @@
/** Production Orders resource Production BC. */ /** Production Orders resource Production BC. */
import type { AxiosInstance } from 'axios'; import type { AxiosInstance } from 'axios';
import type { ProductionOrderDTO, CreateProductionOrderRequest, StartProductionOrderRequest, RescheduleProductionOrderRequest } from '@effigenix/types'; import type { ProductionOrderDTO, CreateProductionOrderRequest, RescheduleProductionOrderRequest } from '@effigenix/types';
export type Priority = 'LOW' | 'NORMAL' | 'HIGH' | 'URGENT'; export type Priority = 'LOW' | 'NORMAL' | 'HIGH' | 'URGENT';
@ -28,7 +28,7 @@ export interface ProductionOrderFilter {
dateTo?: string; dateTo?: string;
} }
export type { ProductionOrderDTO, CreateProductionOrderRequest, StartProductionOrderRequest, RescheduleProductionOrderRequest }; export type { ProductionOrderDTO, CreateProductionOrderRequest, RescheduleProductionOrderRequest };
const BASE = '/api/production/production-orders'; const BASE = '/api/production/production-orders';
@ -58,8 +58,8 @@ export function createProductionOrdersResource(client: AxiosInstance) {
return res.data; return res.data;
}, },
async start(id: string, request: StartProductionOrderRequest): Promise<ProductionOrderDTO> { async start(id: string): Promise<ProductionOrderDTO> {
const res = await client.post<ProductionOrderDTO>(`${BASE}/${id}/start`, request); const res = await client.post<ProductionOrderDTO>(`${BASE}/${id}/start`);
return res.data; return res.data;
}, },

View file

@ -1702,6 +1702,7 @@ export interface components {
recipeId?: string; recipeId?: string;
status?: string; status?: string;
batchId?: string; batchId?: string;
batchNumber?: string;
plannedQuantity?: string; plannedQuantity?: string;
plannedQuantityUnit?: string; plannedQuantityUnit?: string;
/** Format: date */ /** Format: date */
@ -1714,9 +1715,6 @@ export interface components {
/** Format: date-time */ /** Format: date-time */
updatedAt?: string; updatedAt?: string;
}; };
StartProductionOrderRequest: {
batchId: string;
};
RescheduleProductionOrderRequest: { RescheduleProductionOrderRequest: {
/** Format: date */ /** Format: date */
newPlannedDate: string; newPlannedDate: string;
@ -3118,11 +3116,7 @@ export interface operations {
}; };
cookie?: never; cookie?: never;
}; };
requestBody: { requestBody?: never;
content: {
"application/json": components["schemas"]["StartProductionOrderRequest"];
};
};
responses: { responses: {
/** @description OK */ /** @description OK */
200: { 200: {

View file

@ -30,6 +30,5 @@ export type CancelBatchRequest = components['schemas']['CancelBatchRequest'];
// Production Order types // Production Order types
export type ProductionOrderDTO = components['schemas']['ProductionOrderResponse']; export type ProductionOrderDTO = components['schemas']['ProductionOrderResponse'];
export type CreateProductionOrderRequest = components['schemas']['CreateProductionOrderRequest']; export type CreateProductionOrderRequest = components['schemas']['CreateProductionOrderRequest'];
export type StartProductionOrderRequest = components['schemas']['StartProductionOrderRequest'];
export type RescheduleProductionOrderRequest = components['schemas']['RescheduleProductionOrderRequest']; export type RescheduleProductionOrderRequest = components['schemas']['RescheduleProductionOrderRequest'];
export type CancelProductionOrderRequest = components['schemas']['CancelProductionOrderRequest']; export type CancelProductionOrderRequest = components['schemas']['CancelProductionOrderRequest'];