mirror of
https://github.com/s-frick/effigenix.git
synced 2026-03-28 17:39:57 +01:00
feat(production): Produktionsauftrag freigeben (US-P14, #39)
This commit is contained in:
parent
ba37ff647b
commit
b77b209f10
12 changed files with 585 additions and 4 deletions
|
|
@ -0,0 +1,81 @@
|
|||
package de.effigenix.application.production;
|
||||
|
||||
import de.effigenix.application.production.command.ReleaseProductionOrderCommand;
|
||||
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 ReleaseProductionOrder {
|
||||
|
||||
private final ProductionOrderRepository productionOrderRepository;
|
||||
private final RecipeRepository recipeRepository;
|
||||
private final AuthorizationPort authorizationPort;
|
||||
|
||||
public ReleaseProductionOrder(
|
||||
ProductionOrderRepository productionOrderRepository,
|
||||
RecipeRepository recipeRepository,
|
||||
AuthorizationPort authorizationPort
|
||||
) {
|
||||
this.productionOrderRepository = productionOrderRepository;
|
||||
this.recipeRepository = recipeRepository;
|
||||
this.authorizationPort = authorizationPort;
|
||||
}
|
||||
|
||||
public Result<ProductionOrderError, ProductionOrder> execute(ReleaseProductionOrderCommand cmd, ActorId performedBy) {
|
||||
if (!authorizationPort.can(performedBy, ProductionAction.PRODUCTION_ORDER_WRITE)) {
|
||||
return Result.failure(new ProductionOrderError.Unauthorized("Not authorized to release 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();
|
||||
}
|
||||
}
|
||||
|
||||
// Verify recipe is still ACTIVE
|
||||
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.RecipeNotFound(order.recipeId()));
|
||||
}
|
||||
recipe = opt.get();
|
||||
}
|
||||
}
|
||||
|
||||
if (recipe.status() != RecipeStatus.ACTIVE) {
|
||||
return Result.failure(new ProductionOrderError.RecipeNotActive(recipe.id()));
|
||||
}
|
||||
|
||||
// Release
|
||||
switch (order.release()) {
|
||||
case Result.Failure(var err) -> { return Result.failure(err); }
|
||||
case Result.Success(var ignored) -> { }
|
||||
}
|
||||
|
||||
// Persist
|
||||
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(order);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
package de.effigenix.application.production.command;
|
||||
|
||||
public record ReleaseProductionOrderCommand(String productionOrderId) {}
|
||||
|
|
@ -18,10 +18,10 @@ import java.time.ZoneOffset;
|
|||
* 3. Status starts as PLANNED
|
||||
* 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: Status transitions (PLANNED → IN_PROGRESS → COMPLETED, PLANNED/IN_PROGRESS → CANCELLED)
|
||||
* must be guarded by explicit transition methods with invariant checks once those
|
||||
* use cases are implemented. Do NOT allow arbitrary setStatus().
|
||||
* TODO: Further transitions (RELEASED → IN_PROGRESS → COMPLETED, RELEASED/IN_PROGRESS → CANCELLED)
|
||||
* must be guarded by explicit transition methods once those use cases are implemented.
|
||||
*/
|
||||
public class ProductionOrder {
|
||||
|
||||
|
|
@ -146,4 +146,13 @@ public class ProductionOrder {
|
|||
public OffsetDateTime createdAt() { return createdAt; }
|
||||
public OffsetDateTime updatedAt() { return updatedAt; }
|
||||
public long version() { return version; }
|
||||
|
||||
public Result<ProductionOrderError, Void> release() {
|
||||
if (status != ProductionOrderStatus.PLANNED) {
|
||||
return Result.failure(new ProductionOrderError.InvalidStatusTransition(status, ProductionOrderStatus.RELEASED));
|
||||
}
|
||||
this.status = ProductionOrderStatus.RELEASED;
|
||||
this.updatedAt = OffsetDateTime.now(ZoneOffset.UTC);
|
||||
return Result.success(null);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,16 @@ public sealed interface ProductionOrderError {
|
|||
@Override public String message() { return "Recipe '" + recipeId.value() + "' is not in ACTIVE status"; }
|
||||
}
|
||||
|
||||
record RecipeNotFound(RecipeId recipeId) implements ProductionOrderError {
|
||||
@Override public String code() { return "PRODUCTION_ORDER_RECIPE_NOT_FOUND"; }
|
||||
@Override public String message() { return "Recipe '" + recipeId.value() + "' not found"; }
|
||||
}
|
||||
|
||||
record InvalidStatusTransition(ProductionOrderStatus current, ProductionOrderStatus target) implements ProductionOrderError {
|
||||
@Override public String code() { return "PRODUCTION_ORDER_INVALID_STATUS_TRANSITION"; }
|
||||
@Override public String message() { return "Cannot transition from " + current + " to " + target; }
|
||||
}
|
||||
|
||||
record ValidationFailure(String message) implements ProductionOrderError {
|
||||
@Override public String code() { return "PRODUCTION_ORDER_VALIDATION_ERROR"; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package de.effigenix.domain.production;
|
|||
|
||||
public enum ProductionOrderStatus {
|
||||
PLANNED,
|
||||
RELEASED,
|
||||
IN_PROGRESS,
|
||||
COMPLETED,
|
||||
CANCELLED
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
package de.effigenix.domain.production.event;
|
||||
|
||||
import de.effigenix.domain.production.ProductionOrderId;
|
||||
import de.effigenix.domain.production.RecipeId;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
public record ProductionOrderReleased(
|
||||
ProductionOrderId id,
|
||||
RecipeId recipeId,
|
||||
OffsetDateTime releasedAt
|
||||
) {}
|
||||
|
|
@ -5,6 +5,7 @@ import de.effigenix.application.production.ArchiveRecipe;
|
|||
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.CancelBatch;
|
||||
import de.effigenix.application.production.CompleteBatch;
|
||||
import de.effigenix.application.production.CreateRecipe;
|
||||
|
|
@ -129,4 +130,11 @@ public class ProductionUseCaseConfiguration {
|
|||
AuthorizationPort authorizationPort) {
|
||||
return new CreateProductionOrder(productionOrderRepository, recipeRepository, authorizationPort);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ReleaseProductionOrder releaseProductionOrder(ProductionOrderRepository productionOrderRepository,
|
||||
RecipeRepository recipeRepository,
|
||||
AuthorizationPort authorizationPort) {
|
||||
return new ReleaseProductionOrder(productionOrderRepository, recipeRepository, authorizationPort);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
package de.effigenix.infrastructure.production.web.controller;
|
||||
|
||||
import de.effigenix.application.production.CreateProductionOrder;
|
||||
import de.effigenix.application.production.ReleaseProductionOrder;
|
||||
import de.effigenix.application.production.command.CreateProductionOrderCommand;
|
||||
import de.effigenix.application.production.command.ReleaseProductionOrderCommand;
|
||||
import de.effigenix.domain.production.ProductionOrderError;
|
||||
import de.effigenix.infrastructure.production.web.dto.CreateProductionOrderRequest;
|
||||
import de.effigenix.infrastructure.production.web.dto.ProductionOrderResponse;
|
||||
|
|
@ -26,9 +28,12 @@ public class ProductionOrderController {
|
|||
private static final Logger logger = LoggerFactory.getLogger(ProductionOrderController.class);
|
||||
|
||||
private final CreateProductionOrder createProductionOrder;
|
||||
private final ReleaseProductionOrder releaseProductionOrder;
|
||||
|
||||
public ProductionOrderController(CreateProductionOrder createProductionOrder) {
|
||||
public ProductionOrderController(CreateProductionOrder createProductionOrder,
|
||||
ReleaseProductionOrder releaseProductionOrder) {
|
||||
this.createProductionOrder = createProductionOrder;
|
||||
this.releaseProductionOrder = releaseProductionOrder;
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
|
|
@ -58,6 +63,24 @@ public class ProductionOrderController {
|
|||
.body(ProductionOrderResponse.from(result.unsafeGetValue()));
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/release")
|
||||
@PreAuthorize("hasAuthority('PRODUCTION_ORDER_WRITE')")
|
||||
public ResponseEntity<ProductionOrderResponse> releaseProductionOrder(
|
||||
@PathVariable String id,
|
||||
Authentication authentication
|
||||
) {
|
||||
logger.info("Releasing production order: {} by actor: {}", id, authentication.getName());
|
||||
|
||||
var cmd = new ReleaseProductionOrderCommand(id);
|
||||
var result = releaseProductionOrder.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;
|
||||
|
||||
|
|
|
|||
|
|
@ -55,7 +55,9 @@ public final class ProductionErrorHttpStatusMapper {
|
|||
case ProductionOrderError.InvalidPlannedQuantity e -> 400;
|
||||
case ProductionOrderError.PlannedDateInPast e -> 400;
|
||||
case ProductionOrderError.InvalidPriority e -> 400;
|
||||
case ProductionOrderError.RecipeNotFound e -> 404;
|
||||
case ProductionOrderError.RecipeNotActive e -> 409;
|
||||
case ProductionOrderError.InvalidStatusTransition e -> 409;
|
||||
case ProductionOrderError.ValidationFailure e -> 400;
|
||||
case ProductionOrderError.Unauthorized e -> 403;
|
||||
case ProductionOrderError.RepositoryFailure e -> 500;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue