# Implementation Plan: Robust Inventory Recovery for Order Cancellation and Deletion

## Phase 1: Database and Repository Logic
### Goals: Update PostgreSQL logic and implement Atomic Sync helpers.

- [x] **Task: Modify `InventoryRepository.updateStock` [Skills: Repository-Pattern-Enforcer] [MCP: FileSystem]**
  - [Scout] Check `lib/db/repositories/inventory.repository.ts` (L14-L40) for current `updateStock` implementation.
  - [x] Write Failing Tests: Create `tests/integration/inventory.repository.test.ts` to test `updateStock` with transactions.
  - [x] Implement: Ensure `updateStock` correctly handles `client` for transactions.
  - [x] Verify: Run tests.

- [x] **Task: Update `OrderRepository.deleteOrder` [Skills: Repository-Pattern-Enforcer] [MCP: FileSystem]**
  - [Scout] Check `lib/db/repositories/order.repository.ts` (L382-L407) for `deleteOrder` logic.
  - [x] Write Failing Tests: `tests/integration/order.repository.test.ts` to verify stock recovery on deletion of non-cancelled orders.
  - [x] Implement: Add logic to fetch items and increment stock if `order_status !== 'cancelled'`.
  - [x] Verify: Run tests.

- [x] **Task: Implement `OrderRepository.cancelOrder` [Skills: Repository-Pattern-Enforcer] [MCP: FileSystem]**
  - [Scout] Check `lib/db/repositories/order.repository.ts` for existing cancellation guards in `updateOrder` (L331-L377).
  - [x] Write Failing Tests: `tests/integration/order.repository.test.ts` to verify stock recovery when status is set to `cancelled`.
  - [x] Implement: Ensure `updateOrder` recovers stock within the same transaction if transitioning to `cancelled`.
  - [x] Verify: Run tests.

- [x] **Phase 1 Meta-Task: User Manual Verification**
  - [x] Verify Phase 1 completion and follow checkpointing protocol.

## Phase 2: Action Layer and Redis Sync
### Goals: Implement Atomic Sync in Server Actions and Redis recovery.

- [x] **Task: Update `actions/orders.ts` to handle Atomic Sync [Skills: Atomic-Sync-Enforcer] [MCP: FileSystem]**
  - [Scout] Check `actions/orders.ts` (L86-L103) for `updateOrder` action and Redis recovery pattern in `deleteOrder` (L43-L64).
  - [x] Write Failing Tests: `tests/regression/inventory-recovery.test.ts` to test Redis failure rollback.
  - [x] Implement: Modify `updateOrder` and `deleteOrder` actions to perform Redis updates within the same logical unit as the DB update. If Redis fails, roll back or throw to trigger failure.
  - [x] Verify: Run tests.

- [x] **Phase 2 Meta-Task: User Manual Verification**
  - [x] Verify Phase 2 completion and follow checkpointing protocol.

## Phase 3: Final Verification and Quality Gates
### Goals: Ensure 100% coverage and zero regression.

- [x] **Task: Final Regression Tests [Skills: Quality-Auditor] [MCP: FileSystem]**
  - [Scout] Check `tests/regression/order-invariants.test.ts`.
  - [x] Write/Run: Complete regression suite covering all acceptance criteria in `spec.md`.
  - [x] Verify: Ensure 100% coverage for new logic.

- [x] **Task: Strictness Audit [Skills: Strictness-Auditor] [MCP: FileSystem]**
  - [x] Run `pnpm run lint` and `tsc --noEmit`.
  - [x] Verify: Zero errors.

- [x] **Phase 3 Meta-Task: User Manual Verification**
  - [x] Verify Phase 3 completion and follow checkpointing protocol.
