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

feat(production): Produktion über Auftrag starten (US-P15)

RELEASED ProductionOrder kann mit einer PLANNED Batch verknüpft und
in Produktion gestartet werden. Dabei wechselt der Order auf IN_PROGRESS
und die Batch auf IN_PRODUCTION. Neuer REST-Endpoint POST /{id}/start,
StartOrderProduction Use Case, BatchAlreadyAssigned Error, Liquibase-
Migration für batch_id FK auf production_orders.
This commit is contained in:
Sebastian Frick 2026-02-24 22:46:23 +01:00
parent a8bbe3a951
commit bfae3eff73
19 changed files with 985 additions and 17 deletions

View file

@ -0,0 +1,105 @@
package de.effigenix.application.production;
import de.effigenix.application.production.command.StartProductionOrderCommand;
import de.effigenix.domain.production.*;
import de.effigenix.shared.common.Result;
import de.effigenix.shared.security.ActorId;
import de.effigenix.shared.security.AuthorizationPort;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class StartProductionOrder {
private final ProductionOrderRepository productionOrderRepository;
private final BatchRepository batchRepository;
private final AuthorizationPort authorizationPort;
public StartProductionOrder(
ProductionOrderRepository productionOrderRepository,
BatchRepository batchRepository,
AuthorizationPort authorizationPort
) {
this.productionOrderRepository = productionOrderRepository;
this.batchRepository = batchRepository;
this.authorizationPort = authorizationPort;
}
public Result<ProductionOrderError, ProductionOrder> 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"));
}
// Load production order
var orderId = ProductionOrderId.of(cmd.productionOrderId());
ProductionOrder order;
switch (productionOrderRepository.findById(orderId)) {
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.ProductionOrderNotFound(orderId));
}
order = opt.get();
}
}
// Load batch
var batchId = BatchId.of(cmd.batchId());
Batch batch;
switch (batchRepository.findById(batchId)) {
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"));
}
batch = opt.get();
}
}
// Batch must be PLANNED
if (batch.status() != BatchStatus.PLANNED) {
return Result.failure(new ProductionOrderError.ValidationFailure(
"Batch '" + cmd.batchId() + "' is not in PLANNED status (current: " + batch.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() + "'"));
}
// 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) -> { }
}
// Start production on batch (PLANNED IN_PRODUCTION)
switch (batch.startProduction()) {
case Result.Failure(var err) -> {
return Result.failure(new ProductionOrderError.ValidationFailure(err.message()));
}
case Result.Success(var ignored) -> { }
}
// Persist both
switch (productionOrderRepository.save(order)) {
case Result.Failure(var err) -> {
return Result.failure(new ProductionOrderError.RepositoryFailure(err.message()));
}
case Result.Success(var ignored) -> { }
}
switch (batchRepository.save(batch)) {
case Result.Failure(var err) -> {
return Result.failure(new ProductionOrderError.RepositoryFailure(err.message()));
}
case Result.Success(var ignored) -> { }
}
return Result.success(order);
}
}

View file

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

View file

@ -19,15 +19,16 @@ import java.time.ZoneOffset;
* 4. RecipeId must be set (not blank)
* 5. Priority must be valid (LOW, NORMAL, HIGH, URGENT)
* 6. Only PLANNED RELEASED transition allowed via release()
*
* TODO: Further transitions (RELEASED IN_PROGRESS COMPLETED, RELEASED/IN_PROGRESS CANCELLED)
* must be guarded by explicit transition methods once those use cases are implemented.
* 7. Only RELEASED IN_PROGRESS transition allowed via startProduction(BatchId)
* 8. BatchId is set exactly once (null non-null) during startProduction()
* 9. BatchId must not already be assigned (BatchAlreadyAssigned)
*/
public class ProductionOrder {
private final ProductionOrderId id;
private final RecipeId recipeId;
private ProductionOrderStatus status;
private BatchId batchId;
private final Quantity plannedQuantity;
private final LocalDate plannedDate;
private final Priority priority;
@ -40,6 +41,7 @@ public class ProductionOrder {
ProductionOrderId id,
RecipeId recipeId,
ProductionOrderStatus status,
BatchId batchId,
Quantity plannedQuantity,
LocalDate plannedDate,
Priority priority,
@ -51,6 +53,7 @@ public class ProductionOrder {
this.id = id;
this.recipeId = recipeId;
this.status = status;
this.batchId = batchId;
this.plannedQuantity = plannedQuantity;
this.plannedDate = plannedDate;
this.priority = priority;
@ -110,6 +113,7 @@ public class ProductionOrder {
ProductionOrderId.generate(),
RecipeId.of(draft.recipeId()),
ProductionOrderStatus.PLANNED,
null,
plannedQuantity,
draft.plannedDate(),
priority,
@ -124,6 +128,7 @@ public class ProductionOrder {
ProductionOrderId id,
RecipeId recipeId,
ProductionOrderStatus status,
BatchId batchId,
Quantity plannedQuantity,
LocalDate plannedDate,
Priority priority,
@ -132,13 +137,14 @@ public class ProductionOrder {
OffsetDateTime updatedAt,
long version
) {
return new ProductionOrder(id, recipeId, status, plannedQuantity, plannedDate,
return new ProductionOrder(id, recipeId, status, batchId, plannedQuantity, plannedDate,
priority, notes, createdAt, updatedAt, version);
}
public ProductionOrderId id() { return id; }
public RecipeId recipeId() { return recipeId; }
public ProductionOrderStatus status() { return status; }
public BatchId batchId() { return batchId; }
public Quantity plannedQuantity() { return plannedQuantity; }
public LocalDate plannedDate() { return plannedDate; }
public Priority priority() { return priority; }
@ -155,4 +161,17 @@ public class ProductionOrder {
this.updatedAt = OffsetDateTime.now(ZoneOffset.UTC);
return Result.success(null);
}
public Result<ProductionOrderError, Void> startProduction(BatchId batchId) {
if (status != ProductionOrderStatus.RELEASED) {
return Result.failure(new ProductionOrderError.InvalidStatusTransition(status, ProductionOrderStatus.IN_PROGRESS));
}
if (this.batchId != null) {
return Result.failure(new ProductionOrderError.BatchAlreadyAssigned(this.batchId));
}
this.batchId = batchId;
this.status = ProductionOrderStatus.IN_PROGRESS;
this.updatedAt = OffsetDateTime.now(ZoneOffset.UTC);
return Result.success(null);
}
}

View file

@ -40,6 +40,11 @@ public sealed interface ProductionOrderError {
@Override public String message() { return "Cannot transition from " + current + " to " + target; }
}
record BatchAlreadyAssigned(BatchId batchId) implements ProductionOrderError {
@Override public String code() { return "PRODUCTION_ORDER_BATCH_ALREADY_ASSIGNED"; }
@Override public String message() { return "Production order already has batch '" + batchId.value() + "' assigned"; }
}
record ValidationFailure(String message) implements ProductionOrderError {
@Override public String code() { return "PRODUCTION_ORDER_VALIDATION_ERROR"; }
}

View file

@ -6,6 +6,7 @@ import de.effigenix.application.production.AddProductionStep;
import de.effigenix.application.production.AddRecipeIngredient;
import de.effigenix.application.production.CreateProductionOrder;
import de.effigenix.application.production.ReleaseProductionOrder;
import de.effigenix.application.production.StartProductionOrder;
import de.effigenix.application.production.CancelBatch;
import de.effigenix.application.production.CompleteBatch;
import de.effigenix.application.production.CreateRecipe;
@ -137,4 +138,11 @@ public class ProductionUseCaseConfiguration {
AuthorizationPort authorizationPort) {
return new ReleaseProductionOrder(productionOrderRepository, recipeRepository, authorizationPort);
}
@Bean
public StartProductionOrder startProductionOrder(ProductionOrderRepository productionOrderRepository,
BatchRepository batchRepository,
AuthorizationPort authorizationPort) {
return new StartProductionOrder(productionOrderRepository, batchRepository, authorizationPort);
}
}

View file

@ -36,6 +36,9 @@ public class ProductionOrderEntity {
@Column(name = "priority", nullable = false, length = 10)
private String priority;
@Column(name = "batch_id", length = 36)
private String batchId;
@Column(name = "notes", length = 1000)
private String notes;
@ -79,11 +82,13 @@ public class ProductionOrderEntity {
public String getPlannedQuantityUnit() { return plannedQuantityUnit; }
public LocalDate getPlannedDate() { return plannedDate; }
public String getPriority() { return priority; }
public String getBatchId() { return batchId; }
public String getNotes() { return notes; }
public OffsetDateTime getCreatedAt() { return createdAt; }
public OffsetDateTime getUpdatedAt() { return updatedAt; }
public void setStatus(String status) { this.status = status; }
public void setBatchId(String batchId) { this.batchId = batchId; }
public void setUpdatedAt(OffsetDateTime updatedAt) { this.updatedAt = updatedAt; }
public void setNotes(String notes) { this.notes = notes; }
public void setPriority(String priority) { this.priority = priority; }

View file

@ -26,6 +26,7 @@ public class ProductionOrderMapper {
public void updateEntity(ProductionOrderEntity entity, ProductionOrder order) {
entity.setStatus(order.status().name());
entity.setBatchId(order.batchId() != null ? order.batchId().value() : null);
entity.setPriority(order.priority().name());
entity.setNotes(order.notes());
entity.setUpdatedAt(order.updatedAt());
@ -36,6 +37,7 @@ public class ProductionOrderMapper {
ProductionOrderId.of(entity.getId()),
RecipeId.of(entity.getRecipeId()),
ProductionOrderStatus.valueOf(entity.getStatus()),
entity.getBatchId() != null ? BatchId.of(entity.getBatchId()) : null,
Quantity.reconstitute(
entity.getPlannedQuantityAmount(),
UnitOfMeasure.valueOf(entity.getPlannedQuantityUnit())

View file

@ -2,11 +2,14 @@ package de.effigenix.infrastructure.production.web.controller;
import de.effigenix.application.production.CreateProductionOrder;
import de.effigenix.application.production.ReleaseProductionOrder;
import de.effigenix.application.production.StartProductionOrder;
import de.effigenix.application.production.command.CreateProductionOrderCommand;
import de.effigenix.application.production.command.ReleaseProductionOrderCommand;
import de.effigenix.application.production.command.StartProductionOrderCommand;
import de.effigenix.domain.production.ProductionOrderError;
import de.effigenix.infrastructure.production.web.dto.CreateProductionOrderRequest;
import de.effigenix.infrastructure.production.web.dto.ProductionOrderResponse;
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;
@ -29,11 +32,14 @@ public class ProductionOrderController {
private final CreateProductionOrder createProductionOrder;
private final ReleaseProductionOrder releaseProductionOrder;
private final StartProductionOrder startProductionOrder;
public ProductionOrderController(CreateProductionOrder createProductionOrder,
ReleaseProductionOrder releaseProductionOrder) {
ReleaseProductionOrder releaseProductionOrder,
StartProductionOrder startProductionOrder) {
this.createProductionOrder = createProductionOrder;
this.releaseProductionOrder = releaseProductionOrder;
this.startProductionOrder = startProductionOrder;
}
@PostMapping
@ -81,6 +87,25 @@ public class ProductionOrderController {
return ResponseEntity.ok(ProductionOrderResponse.from(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());
var cmd = new StartProductionOrderCommand(id, request.batchId());
var result = startProductionOrder.execute(cmd, ActorId.of(authentication.getName()));
if (result.isFailure()) {
throw new ProductionOrderDomainErrorException(result.unsafeGetError());
}
return ResponseEntity.ok(ProductionOrderResponse.from(result.unsafeGetValue()));
}
public static class ProductionOrderDomainErrorException extends RuntimeException {
private final ProductionOrderError error;

View file

@ -9,6 +9,7 @@ public record ProductionOrderResponse(
String id,
String recipeId,
String status,
String batchId,
String plannedQuantity,
String plannedQuantityUnit,
LocalDate plannedDate,
@ -22,6 +23,7 @@ public record ProductionOrderResponse(
order.id().value(),
order.recipeId().value(),
order.status().name(),
order.batchId() != null ? order.batchId().value() : null,
order.plannedQuantity().amount().toPlainString(),
order.plannedQuantity().uom().name(),
order.plannedDate(),

View file

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

View file

@ -58,6 +58,7 @@ public final class ProductionErrorHttpStatusMapper {
case ProductionOrderError.RecipeNotFound e -> 404;
case ProductionOrderError.RecipeNotActive e -> 409;
case ProductionOrderError.InvalidStatusTransition e -> 409;
case ProductionOrderError.BatchAlreadyAssigned e -> 409;
case ProductionOrderError.ValidationFailure e -> 400;
case ProductionOrderError.Unauthorized e -> 403;
case ProductionOrderError.RepositoryFailure e -> 500;

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
<changeSet id="028-add-batch-id-to-production-orders" author="effigenix">
<addColumn tableName="production_orders">
<column name="batch_id" type="VARCHAR(36)"/>
</addColumn>
<addForeignKeyConstraint
baseTableName="production_orders"
baseColumnNames="batch_id"
constraintName="fk_production_orders_batch_id"
referencedTableName="batches"
referencedColumnNames="id"/>
<addUniqueConstraint tableName="production_orders"
columnNames="batch_id"
constraintName="uq_production_orders_batch_id"/>
</changeSet>
</databaseChangeLog>

View file

@ -36,5 +36,6 @@
<include file="db/changelog/changes/029-seed-stock-movement-permissions.xml"/>
<include file="db/changelog/changes/030-add-batch-id-index-to-stock-movements.xml"/>
<include file="db/changelog/changes/031-add-performed-at-index-to-stock-movements.xml"/>
<include file="db/changelog/changes/032-add-batch-id-to-production-orders.xml"/>
</databaseChangeLog>