# Database Persistence Layer - Phase 11-14 Review

## Current Status (as of 2026-03-29)

### ✅ Phases with DB Persistence (Schema + Migration + Write-Through)
- **Phase 1-7**: Core auth, accounts, ledger, compliance, risk (22 schemas, 18 migrations)
- **Phase 8**: Resilience & production (SLO, Incidents, DR Checkpoints) — **DOMAIN-ONLY, no schemas yet**
- **Phase 9**: Admin console, navigation (DevSeeds in config, no new domain schemas needed)
- **Phase 10A**: Cards (Domain only - Card, LinkedCard) — **NO SCHEMA**
- **Phase 10A**: Rewards (Domain only - Offer, RewardTransaction, PointsBalance) — **NO SCHEMA**
- **Phase 10B**: Loans (Domain only - Loan, Calculator) — **NO SCHEMA**
- **Phase 10B**: Insurance (Domain only - InsuranceProduct, InsurancePolicy) — **NO SCHEMA**
- **Phase 11A**: Branch cash-in, AFEX, Virtual IBAN (Virtual IBAN schema exists) — **PARTIAL**
- **Phase 11B**: Cash-in orchestration, profile sync, IBAN routing, PIN recovery, data governance — **DOMAIN-ONLY**
- **Phase 13A-B**: Reporting, Disputes, GL adapters, Salary credits, Notifications — **DOMAIN-ONLY**
- **Phase 14A-B-C**: Merchant lifecycle, KYB, POS, QR codes, Remittance, E2E validation — **DOMAIN-ONLY**

### Missing Database Layers
The following domains have ETS in-memory stores but NO database persistence:

#### High Priority (Business-Critical)
1. **wallet_merchant** (Phase 14)
   - Merchant, MerchantComplianceCheck, MerchantRiskProfile
   - PosTransaction, MerchantQrCode
   - 5 stores, all ETS-only

2. **wallet_integrations/remittance** (Phase 14)
   - InwardRemittanceRecord, InwardRemittanceStore
   - Critical for remittance tracking

3. **wallet_disputes** (Phase 13)
   - Dispute, RefundRequest, SlaEscalationAlert
   - SlaMonitor state
   - 5 stores, all ETS-only

4. **wallet_cards** (Phase 10A)
   - Card, LinkedCard (2 stores, both ETS-only)
   - Critical for payment flows

#### Medium Priority (Feature-Complete)
5. **wallet_rewards** (Phase 10A)
   - Offer, RewardTransaction, PointsBalance (3 stores, all ETS-only)

6. **wallet_loans** (Phase 10B)
   - Loan (1 store, ETS-only)

7. **wallet_insurance** (Phase 10B)
   - InsuranceProduct, InsurancePolicy (2 stores, ETS-only)

8. **wallet_reporting** (Phase 13)
   - RegulatoryTemplate, ReportJob, ReportRequest
   - ReportCatalog (3-4 stores, ETS-only)

9. **wallet_gl** (Phase 13)
   - GlPostingRecord, GlReconciliationRun, GlVariance
   - 3+ stores, ETS-only + AFEX adapter state

10. **wallet_wps** (Phase 13)
    - SalaryCredit, SalaryCreditException
    - WpsFile, WpsBatch (4+ stores, ETS-only)

#### Resilience & Production (Phase 8)
11. **wallet_resilience**
    - HealthCheck, SloViolation, IncidentRecord (3 stores)

12. **wallet_production**
    - DrCheckpoint, ReadinessApproval (2 stores)

## Implementation Strategy

### Phase 1: Database Foundation (Immediate)
1. Create Ecto schemas for all missing domains
2. Generate migrations (keep 20260325... sequence)
3. Wire write-through persistence for critical path
4. Update stores to call persistence after ETS writes

### Phase 2: Write-Through Layer
- Create persistence modules for each domain
- Implement get-then-insert/update pattern (MySQL compatible)
- Add error handling & logging
- Test idempotency

### Phase 3: Migration & Persistence Tests
- Verify all migrations apply cleanly
- Test write-through with concurrent access
- Validate data consistency

## Critical Path for Phase 15 Onward

**DO NOT PROCEED TO PHASE 15 WITHOUT:**
1. ✅ Merchant records persisted to DB
2. ✅ Remittance records persisted to DB
3. ✅ Dispute & refund records persisted to DB
4. ✅ Card records persisted to DB
5. ✅ At minimum: Reporting templates & GL postings queryable from DB

## Estimated Effort
- 80+ new Ecto schemas
- 80+ migrations
- 60+ persistence modules
- Comprehensive testing
- ~1-2 weeks for full implementation + testing

## Next Steps
1. Generate schema stubs for all missing domains
2. Create associated migrations
3. Implement write-through for critical domains first
4. Validate with integration tests
