Repository navigation
Throw when an order status change is not allowed from the current status - #1077
blurman-ai wants to merge 1 commit into
Conversation
SetPaidStatus, SetStockConfirmedStatus and SetAwaitingValidationStatus silently did nothing when the order was not in the expected status, so a payment confirmation for a cancelled order was dropped without any trace. Throw OrderingDomainException, as SetShippedStatus already does.
|
@dotnet-policy-service agree |
|
@DamianEdwards — ready for review. An order status change that is not allowed from the current status now throws instead of being written: |
|
|
||
| OrderStatus = OrderStatus.Paid; | ||
| Description = "The payment was performed at a simulated \"American Bank checking bank account ending on XX35071\""; | ||
| if (OrderStatus != OrderStatus.StockConfirmed) |
There was a problem hiding this comment.
This check only validates the status read into this aggregate. If payment and cancellation load StockConfirmed concurrently, both pass; whichever saves last overwrites the other, so the order can still end up Paid after cancellation. Can we add optimistic concurrency or a conditional database update so the transition is checked against the persisted status?
| public void SetPaidStatus() | ||
| { | ||
| if (OrderStatus == OrderStatus.StockConfirmed) | ||
| if (OrderStatus == OrderStatus.Paid) |
There was a problem hiding this comment.
The duplicate-event no-op only applies while the order is exactly Paid. A delayed or redelivered payment event after the order has advanced to Shipped now throws; RabbitMQEventBus catches handler exceptions and ACKs the message, so this becomes a logged-and-discarded replay. Could duplicate integration events be made idempotent across later valid statuses (or deduplicated by event ID)?
What happens to a user
An order can be cancelled while it is in
StockConfirmed(SetCancelledStatusallows it). If the payment succeeds around the same time,SetPaidOrderStatusCommandarrives for an already cancelled order.Order.SetPaidStatus()does nothing in that case, the handler returns success, and the event is consumed without a trace. The customer is charged, the order staysCancelled, and nothing in the logs or in the order says that a payment was dropped.SetStockConfirmedStatusandSetAwaitingValidationStatusbehave the same way for their own source statuses.SetShippedStatusandSetCancelledStatusalready throwOrderingDomainExceptionthroughStatusChangeExceptionin this situation.The fix
SetAwaitingValidationStatus,SetStockConfirmedStatusandSetPaidStatusnow:StatusChangeExceptionwhen the current status is not the one the transition is allowed from, the same waySetShippedStatusdoes.Tests
tests/Ordering.UnitTests/Domain/OrderAggregateTest.cs:AwaitingValidation→StockConfirmed→Cancelled) throwsOrderingDomainExceptiononSetPaidStatusand keeps itsCancelledstatus; the same forSetStockConfirmedStatusandSetAwaitingValidationStatus;SetPaidStatustwice keeps the orderPaidand adds no second domain event.Without the change the three "cancelled" tests fail ("no exception was thrown"). With it,
Ordering.UnitTestspasses (47/47). I did not run the functional tests, which need Aspire and containers.Open question
This makes the dropped payment visible; it does not return the money. Would you want an explicit status for "paid after cancellation" (for example
RefundRequired) and an integration event a payment service could react to? I did not add one, because that changes the order model and the public event contract. Related: #1056 changes the same paid/cancelled flow on the Catalog side.