---
id: order
name: Order
version: 1.0.0
identifier: id
aggregateRoot: true
summary: Durable purchase record owned by a Buyer, capturing immutable purchase-time line items and governing order lifecycle transitions.
owners:
  - commerce-fulfillment-team
badges:
  - content: Aggregate Root
    backgroundColor: blue
    textColor: blue
properties:
  - name: id
    type: UUID
    required: true
    description: Globally unique UUIDv7 identifier for the order.
  - name: buyerId
    type: UUID
    required: false
    description: Identifier of the Buyer who placed the order (nullable if the buyer profile is later deleted).
    references: buyer
    referencesIdentifier: id
    relationType: placedBy
  - name: status
    type: string
    required: true
    description: Authoritative business lifecycle status owned by Ordering.
    enum:
      - New
      - Completed
      - Cancelled
  - name: note
    type: string
    required: false
    description: Optional customer delivery or order note (up to 500 characters).
  - name: orderItems
    type: array
    required: true
    description: Purchase-time line items composing the order.
    references: order-item
    referencesIdentifier: id
    relationType: contains
    items:
      type: order-item
  - name: totalPrice
    type: decimal
    required: true
    description: Computed sum of unit price multiplied by quantity across all order items.
  - name: isDeleted
    type: boolean
    required: true
    description: Soft-delete marker independent of the order's business status.
  - name: createdAt
    type: datetime
    required: true
    description: UTC timestamp when the order was placed.
  - name: lastModifiedAt
    type: datetime
    required: false
    description: UTC timestamp of the most recent modification.
  - name: rowVersion
    type: string
    required: true
    description: Persistence-managed optimistic concurrency token.
---

## Domain Role & Boundary

**Order** is the central Aggregate Root in the [[domain|ordering]] bounded context, representing an accepted customer purchase with its locked purchase-time [[entity|order-item]] lines and authoritative lifecycle status. Owned by [[service|order-api]] (with post-checkout saga coordination by [[service|order-orchestration]]), it records what was bought and at what price. It explicitly does **not** own pre-purchase mutable baskets ([[domain|shopping]]), live catalog metadata ([[domain|catalog]]), payment settlement, or email transport ([[domain|platform]]).

## Entity Profile

| Property                      | Value                                                                                                   |
| :---------------------------- | :------------------------------------------------------------------------------------------------------ |
| **Bounded Context / Domain**  | [[domain\|ordering]]                                                                                    |
| **Owning Service**            | [[service\|order-api]]                                                                                  |
| **DDD Role**                  | Aggregate Root                                                                                          |
| **Identifier Strategy**       | UUIDv7 generated at persistence                                                                         |
| **Persistence Store**         | Ordering relational database (`orders`) and event-sourced summary stream                                |
| **Deletion Semantics**        | Soft-delete (`isDeleted` flag excludes deleted orders and items without altering `status`)              |
| **Concurrency Control**       | Optimistic concurrency via row version, paired with buyer-scoped distributed locks and idempotency keys |
| **PII / Data Classification** | Internal transactional record (`note` may contain customer-provided free text)                          |

## Attributes & Constraints

<EntityPropertiesTable />

| Attribute    | Business Rule / Constraint                                                                                      | Sensitivity / Notes           |
| :----------- | :-------------------------------------------------------------------------------------------------------------- | :---------------------------- |
| `buyerId`    | Links to [[entity\|buyer]]; becomes null if the buyer profile is hard-deleted so accepted orders remain intact. | Customer reference            |
| `status`     | Starts as `New`; transitions once to `Completed` or `Cancelled`.                                                | Authoritative lifecycle state |
| `note`       | Optional free-text customer note, max 500 characters.                                                           | May contain user-entered PII  |
| `totalPrice` | Computed dynamically as the sum of `price * quantity` across `orderItems`; not stored separately.               | Derived monetary total        |
| `isDeleted`  | Defaults to `false`; hides the order and its items from standard queries when `true`.                           | Independent of `status`       |

## Domain Invariants & Lifecycle

- **Single terminal transition:** Every new order begins in `New`. Only `New` orders may transition to `Completed` or `Cancelled`; repeated identical transitions are idempotent, while cross-transitions (`Completed` ↔ `Cancelled`) are rejected.
- **Price snapshot isolation:** Line-item prices are locked from [[domain|catalog]] at checkout time so future catalog price changes never alter accepted orders.
- **Soft-delete vs. business status:** Soft-deleting an order (for example, during failed basket-cleanup compensation) hides it from standard queries without changing its `status`.
- **Saga separation:** Post-checkout orchestration progress in [[service|order-orchestration]] and email delivery in [[service|notification-api]] never directly mutate an order's business `status`.

:::warning
If post-checkout basket cleanup fails after bounded retries, [[service|order-orchestration]] issues a compensation command so [[service|order-api]] soft-deletes the order and suppresses confirmation emails.
:::

### Order Status Lifecycle

<MermaidFileLoader file="mermaids/order-lifecycle.mmd" />

## Relationships & Associated Operations

| Category                     | Resource                                                                                                                                                  | Role / Interaction                                            |
| :--------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------ |
| **Related Entities**         | [[entity\|buyer]], [[entity\|order-item]]                                                                                                                 | Placed by a buyer; owns one or more purchase-time order items |
| **Cross-Context References** | [[entity\|book]]                                                                                                                                          | Referenced indirectly via [[entity\|order-item]] `bookId`     |
| **Mutating Commands**        | [[command\|CreateOrder]], [[command\|CompleteOrder]], [[command\|CancelOrder]], [[command\|DeleteOrder]], [[command\|DeleteBasketFailedCommand]]          | Place, complete, cancel, or soft-delete/compensate an order   |
| **Read Queries**             | [[query\|GetOrder]], [[query\|ListOrders]]                                                                                                                | Retrieve order details or filtered order history              |
| **Emitted Events**           | [[event\|UserCheckedOutIntegrationEvent]], [[event\|OrderStatusChangedToCompleteIntegrationEvent]], [[event\|OrderStatusChangedToCancelIntegrationEvent]] | Trigger post-checkout saga and lifecycle email notifications  |
