# Specification: Robust Inventory Recovery for Order Cancellation and Deletion

## Overview
This track fixes a critical logic gap where product inventory (PostgreSQL and Redis) is not recovered when an order is cancelled or deleted. This ensures inventory consistency and prevents "lost" stock.

## Functional Requirements
1. **Order Cancellation Recovery**:
    - When an order's `order_status` is updated to `cancelled`, the system MUST recover the inventory for all items in that order.
    - **Trigger Condition**: Transitioning to `cancelled` from *any* other status (except if it was already `cancelled`).
    - **Atomic Sync**: Inventory MUST be recovered in both PostgreSQL (within a transaction) and Redis. If either fails, the entire operation (including the status update) MUST fail and be rolled back.
2. **Order Deletion Recovery**:
    - **Smart Recovery**: When an order is deleted, the system MUST recover the inventory for all items *only if* the order was NOT already in `cancelled` status (to prevent double-recovery).
    - If the order was already `cancelled`, deletion should proceed without further inventory adjustment.
    - **Guard**: 'delivered' or 'shipped' orders cannot be deleted (existing rule, must be preserved).
3. **Repository Pattern**:
    - All inventory logic MUST be encapsulated within `InventoryRepository` or called via `OrderRepository` using existing repository patterns.
    - Database operations MUST be performed within PostgreSQL transactions.

## Non-Functional Requirements
- **Consistency**: 1:1 parity between PostgreSQL `products.inventory` and Redis `product:<id>:inventory`.
- **Latency**: Inventory recovery operations should add less than 100ms to the total action time.
- **Resource Budget**:
    - **Build**: Zero errors (`tsc`, `pnpm build`)
    - **Coverage**: 100% mandatory for new/modified logic.

## Acceptance Criteria
- [ ] Updating an order to `cancelled` increases stock in Postgres.
- [ ] Updating an order to `cancelled` increases stock in Redis.
- [ ] If Redis fails during cancellation, the Postgres status change is rolled back.
- [ ] Deleting a `processing` order increases stock in Postgres and Redis.
- [ ] Deleting a `cancelled` order does NOT increase stock again.
- [ ] Deleting a `delivered` order is still blocked with an appropriate error.

## Out of Scope
- Automated re-ordering of out-of-stock products.
- Customer notifications for stock recovery.

---

**No Ambiguity Clause**: Implementers MUST provide line numbers and code snippets for all proposed changes. No speculative inference allowed.
