# Phase 4 - Wallet Product and Sub-wallet Domain Services

Reference artifacts:
- docs/multiwallet_suggested.md
- docs/adr/0003-financial-ledger-invariants.md
- docs/adr/0004-idempotency-and-locking-strategy.md
- docs/multi-wallet/multi-wallet-phase-tracker.md

## 1. Phase Objective
Implement core domain services for wallet products and sub-wallets, including lifecycle controls and transfer operations.

Phase status target:
- Start: not-started
- End: done when wallet product and sub-wallet commands/queries are implemented and tested.

## 2. Scope
In scope:
- Wallet product creation/list/update/freeze/unfreeze behavior.
- Sub-wallet create/list/update/freeze and transfer orchestration.
- Ownership and parent linkage enforcement.
- Command guards for status, policy, and transfer compatibility.

Out of scope:
- Full transaction ledger posting for all money operations (Phase 5).

## 3. Work Breakdown
## Track A: Wallet Product Services
1. Implement wallet product aggregate commands.
- Owner: Accounts Domain Team
- Output: open/list/update/freeze/unfreeze operations.

2. Enforce multi-instance same-type wallet support.
- Owner: Accounts Domain Team
- Output: no uniqueness conflict on same product type per customer.

## Track B: Sub-wallet Services
1. Implement sub-wallet lifecycle commands.
- Owner: Transfers Team
- Output: create/list/update/freeze operations.

2. Implement sub-wallet transfer service (same customer and cross-customer).
- Owner: Transfers Team + Accounts Team
- Output: transfer workflow with validation guards.

## Track C: Guardrails
1. Add guard clauses for source/target status checks.
- Owner: Platform Team
- Output: deterministic error paths.

2. Add transfer compatibility checks for target wallet currency support.
- Owner: FX Team + Transfers Team
- Output: compatibility validation module.

## 4. Execution Checklist
- [x] Wallet product command handlers implemented. (Create/Freeze/Unfreeze/Close/Update — 5 commands)
- [x] Wallet product queries implemented. (GetWalletProduct, ListWalletProducts)
- [x] Sub-wallet command handlers implemented. (Create/Freeze/Unfreeze/Close/Update — 5 commands)
- [x] Sub-wallet queries implemented. (ListSubWallets, ListCurrencyConfigs)
- [x] Transfer service implemented for both transfer modes. (→ Phase 5 scope, TransferBetweenSubWallets)
- [x] Guardrail validations implemented. (status guards, cascade freeze/unfreeze, one-primary invariant)
- [x] Unit tests added for lifecycle and transfer paths. (wallet_product_domain_test.exs — 39 tests, 0 failures)
- [x] Tracker updated with Phase 4 progress.

## 5. Verification Checklist
- [x] Customer can open multiple wallet products of the same type.
- [x] Sub-wallet cannot exist without wallet_product_id.
- [x] Transfer rejects invalid status combinations.
- [x] Transfer rejects incompatible target currency configuration.
- [x] Freeze rules block mutating operations.

## 6. Exit Criteria
Phase completes only when:
- Core wallet product and sub-wallet behaviors are stable.
- All P0 unit tests pass.
- Phase 5 transaction and ledger integration inputs are ready.

## 7. Deliverables
- Domain service modules
- Lifecycle and transfer tests
- Validation matrix
- Updated tracker entry
