Order States
The system defines two order states in theOrderStatus enum:
OrderStatus.java
CREATED
The initial state when an order is created:- Order has been submitted with items and delivery address
- Total price has been calculated automatically
- Order can be modified (change quantities, update address)
- Payment has not been processed
PAID
The final state after successful payment:- Payment has been processed and verified
- Order is immutable - no further modifications allowed
- Order is ready for fulfillment
The current implementation has two states. Future versions may add additional states like SHIPPED, DELIVERED, or CANCELLED.
State Transitions
Business Rules
Order Creation
When an order is created viaPOST /orders:
Order.java
- Order ID is generated automatically using timestamp-based format
- Total price is calculated from item prices and quantities
- Order starts in CREATED state
- OrderCreatedEvent is published to RabbitMQ
- Items list must not be empty (validated by controller)
- Address must be complete (province, city, detail required)
Changing Product Quantities
While in CREATED state, quantities can be modified viaPOST /orders/{id}/products:
Order.java
- Only allowed when status is CREATED
- Product must exist in the order
- Total price is recalculated automatically
- OrderProductChangedEvent is published
- Count must be positive (validated by controller)
OrderCannotBeModifiedException(409) - Order is already PAIDProductNotInOrderException(409) - Product not found in orderOrderNotFoundException(404) - Order ID doesn’t exist
Changing Address
Delivery address detail can be updated viaPOST /orders/{id}/address/detail:
Order.java
- Only allowed when status is CREATED
- Only the address detail field can be changed (not province/city)
- OrderAddressChangedEvent is published
OrderCannotBeModifiedException(409) - Order is already PAIDOrderNotFoundException(404) - Order ID doesn’t exist
Payment
Payment is processed viaPOST /orders/{id}/payment:
Order.java
- Paid price must exactly match order total price
- Order transitions from CREATED to PAID
- OrderPaidEvent is published
- Order becomes immutable after this transition
PaidPriceNotSameWithOrderPriceException(409) - Price mismatchOrderNotFoundException(404) - Order ID doesn’t exist
Immutability After Payment
Once an order is PAID, it becomes immutable. All modification attempts will fail:Event Timeline
For a typical order lifecycle, the following events are published:Modifications (optional)
OrderProductChangedEvent and/or OrderAddressChangedEvent published for any changes
- ecommerce-order-query-service - Updates read-optimized views
- ecommerce-inventory-service - Reserves inventory when order is paid
- Other microservices in the platform
Price Calculation
Total price is always calculated automatically:Order.java