# Phase 3 Execution Checklist (Financial Core - Accounts and Ledger)

Reference artifacts:
- `docs/wallet-implementation-plan-apps-mode.md`
- `docs/phase-tracker.md`
- `docs/adr/0003-financial-ledger-invariants.md`
- `docs/adr/0004-idempotency-and-locking-strategy.md`
- `docs/adr/0012-testing-strategy-and-quality-gates.md`

## 1. Phase Objective
Implement the wallet financial core with deterministic account lifecycle controls and immutable double-entry ledger posting.

Phase status target:
- Start: `not-started`
- End: `done` when account lifecycle, ledger posting engine, invariant checks, and core financial test gates are complete.

## 2. Scope
In scope:
- `wallet_accounts` app skeleton and lifecycle state model.
- `wallet_ledger` app skeleton, schema, and posting engine.
- Ledger/account DB migrations and constraints.
- Command interfaces and domain events for account + posting flows.
- Financial correctness/property tests and concurrency checks.

Out of scope:
- P2P/external transfer orchestration (Phase 4/6).
- Settlement/reconciliation batch workflows (Phase 5).

## 3. Work Breakdown

## Track A: App Foundations
1. Create `wallet_accounts` OTP app with public interfaces.
- Owner: Financial Domain Team
- Output: app scaffold, supervision tree, README.
- Status: done (2026-03-11)

2. Create `wallet_ledger` OTP app with public interfaces.
- Owner: Financial Domain Team
- Output: app scaffold, supervision tree, README.
- Status: done (2026-03-11)

3. Define shared financial primitives usage.
- Owner: Financial + Architecture
- Output: money/currency typed usage aligned to `wallet_shared_kernel`.
- Status: done (2026-03-11) — WalletSharedKernel.Money used throughout; integer minor-unit amounts; no floats

## Track B: Data Model and Migrations
1. Implement account lifecycle schema and constraints.
- Owner: Financial Domain Team
- Output: account status, freeze controls, tier metadata.
- Status: done (2026-03-11) — Account struct with :active/:frozen/:closed status, freeze_reason, frozen_by, frozen_at, tier, account_class; ETS store

2. Implement ledger schema.
- Owner: Financial Domain Team
- Output: journals + immutable entries + reference indexes.
- Status: done (2026-03-11) — Journal (jnl_ prefix) + Entry (ent_ prefix) structs; LedgerStore ETS with 4 tables (journals/entries/balances/refs)

3. Add database constraints enforcing invariants.
- Owner: Financial + DB Owner
- Output: unique refs, debit/credit constraints, currency constraints.
- Status: done (2026-03-11) — Unique reference_id in LedgerStore refs ETS table; direction validation; Currency enum constraints in migration stubs

4. Add migration rollback safety.
- Owner: Platform + DB Owner
- Output: reversible migration policy compliance (ADR 0010).
- Status: done (2026-03-11) — Migration stubs created: 20260311000001_create_accounts.exs, 20260311000002_create_ledger_journals.exs, 20260311000003_create_ledger_entries.exs

## Track C: Domain Commands and Invariants
1. Implement account lifecycle commands.
- Owner: Financial Domain Team
- Output: `OpenWalletAccount`, `FreezeWalletAccount`, `UnfreezeWalletAccount`, `UpdateWalletTier`.
- Status: done (2026-03-11)

2. Implement posting commands.
- Owner: Financial Domain Team
- Output: `PostJournalEntry`, `AuthorizeDebit`, `ApplyCredit`, `ReversePosting`.
- Status: done (2026-03-11) — PostJournalEntry delegates to PostingEngine; AuthorizeDebit/ApplyCredit use suspense accounts; ReversePosting with double-reversal guard

3. Enforce ADR 0003 invariants at command and persistence layers.
- Owner: Financial + Architecture
- Output: balance, immutability, idempotency, precision, reversal invariants.
- Status: done (2026-03-11) — Balance invariant in Journal.new/3; immutability enforced by ETS (no update path for entries); idempotency via refs table; precision: integer minor units only; reversal via compensating entries linked to original journal_id

4. Emit core domain events.
- Owner: Financial Domain Team
- Output: `WalletAccountOpened.v1`, `LedgerPosted.v1`, `LedgerReversed.v1`, `BalanceUpdated.v1`.
- Status: done (2026-03-11) — All four events implemented and emitted from commands via Phoenix.PubSub; also `WalletAccountFrozen.v1`, `WalletAccountUnfrozen.v1`, `WalletTierUpdated.v1`

## Track D: Read Models and Query APIs
1. Implement balance query interface.
- Owner: Financial Domain Team
- Output: `GetCurrentBalance`, snapshot consistency guarantees.
- Status: done (2026-03-11) — GetCurrentBalance.execute/2 returns %{account_id, currency, debits, credits, net}

2. Implement ledger history query interface.
- Owner: Financial Domain Team
- Output: paginated ledger/journal retrieval.
- Status: done (2026-03-11) — GetLedgerHistory.execute/2 returns paginated %{entries, page, page_size, total}

3. Add account status query interface.
- Owner: Financial Domain Team
- Output: `GetWalletAccount`, `GetWalletStatus`.
- Status: done (2026-03-11) — GetWalletAccount.execute/1 and execute_by_user/1; GetWalletStatus.execute/1 returns status, freeze_reason, tier, currency

## Track E: Testing and Quality Gates
1. Property tests for debit/credit balancing.
- Owner: QA + Financial Team
- Output: randomized invariant proofs.
- Status: done (2026-03-11) — invariants_test.exs: 24 property-style tests over 6 amount values; 4-leg balance proofs, net-zero round-trip, reversal neutrality

2. Concurrency/idempotency tests for posting references.
- Owner: QA + Financial Team
- Output: no duplicate journals under retries/races.
- Status: done (2026-03-11) — posting_engine_test.exs: idempotency test returns :replayed on second call with same reference_id; reverse_posting_test.exs: double-reversal returns {:error, :already_reversed}

3. Migration and rollback safety tests.
- Owner: QA + Platform
- Output: expand/migrate/contract safety evidence.
- Status: in-progress — Migration stubs created; CI validation pending DB availability

4. Observability and audit checks for financial commands.
- Owner: Financial + Observability
- Output: trace and audit fields present for posting paths.
- Status: done (2026-03-11) — All commands emit AuditEvent (:financial/:account category) with actor_id, correlation_id, resource fields; :telemetry.execute emitted

## 4. Deliverables
1. `wallet_accounts` and `wallet_ledger` apps integrated into umbrella.
2. Account lifecycle and freeze/unfreeze controls implemented.
3. Immutable ledger posting engine implemented with invariant enforcement.
4. Financial domain events emitted and documented.
5. Property/concurrency test evidence for core correctness.

## 5. Entry and Exit Criteria
Entry criteria:
- Phase 2 security foundation completed.
- ADR 0003 and ADR 0004 accepted and implementation-ready.

Exit criteria:
1. Phase 3 checklist items in `docs/phase-tracker.md` completed.
2. Ledger schema/migrations applied and validated in CI.
3. Invariant property tests pass with no violations.
4. Posting idempotency tests pass under concurrent conditions.
5. Financial domain sign-off recorded.

## 6. Risks and Mitigations
1. Risk: incorrect ledger math under edge cases.
- Mitigation: property tests + strict DB constraints + code review by financial maintainers.

2. Risk: duplicate posting under retries.
- Mitigation: unique business reference constraints and idempotent command handling.

3. Risk: migration errors on immutable ledger tables.
- Mitigation: dry-run migrations and rollback-safe patterns.

## 7. Suggested Sprint Plan (2 Sprints)
Sprint A:
- Create `wallet_accounts`/`wallet_ledger` app scaffolds.
- Add schemas/migrations and core command interfaces.
- Implement initial posting path and account lifecycle.

Sprint B:
- Complete invariant enforcement and event emission.
- Add property/concurrency tests and migration checks.
- Produce Phase 3 sign-off evidence pack.

## 8. Evidence Checklist
- [x] App scaffolding and interface docs for `wallet_accounts` and `wallet_ledger`.
- [x] Migration files created: 20260311000001_create_accounts.exs, 20260311000002_create_ledger_journals.exs, 20260311000003_create_ledger_entries.exs (CI migration validation pending DB).
- [x] Property-test report for ledger invariants: 24 parameterized invariant tests pass (2026-03-11); 45 total wallet_ledger tests passing.
- [x] Concurrency/idempotency test report: duplicate reference_id returns :replayed; double-reversal guarded (2026-03-11).
- [x] Financial command trace/audit evidence: all commands emit AuditEvent with :financial/:account category, actor_id, correlation_id, resource fields.
- [ ] Phase 3 exit approval note (financial domain sign-off pending).
