Caricash Nova Platform
Home
Home
  1. Internal
  • Default module
    • Clients
      • PBAC for Customer Authentication
      • Create Clients
    • Internal
      • Accounts, Transactioins and Ledger Implementatioin
      • Ensure real-time balance guarantees
      • Web App Scaffold
      • Database Migrations Guide
      • Microservices
      • Service Implementation
      • TO-DO
      • Authentication & Authorization
    • Customers
      • Onboarding
  • Release Schedule
    • Agency Operations
      • Agent APIs Specs
        • auth
          • POST /v1/agent/auth/login
          • POST /v1/agent/auth/logout
          • POST /v1/agent/auth/refresh
          • POST /v1/agent/auth/device-bind
          • POST /v1/agent/auth/otp/request
          • POST /v1/agent/auth/otp/verify
        • agent
          • GET /v1/agent/me
          • PATCH /v1/agent/me
          • GET /v1/agent/outlet
          • GET /v1/agent/capabilities
        • kyc
          • POST /v1/kyc/customers
          • POST /v1/kyc/customers/{customer_id}/upgrade
          • POST /v1/kyc/customers/{customer_id}/rekcy
          • GET /v1/kyc/customers/{customer_id}/status
        • transactions
          • POST /v1/txns/cashin
          • POST /v1/txns/cashout
          • POST /v1/txns/p2p/assist
          • GET /v1/txns/{txn_id}
          • POST /v1/txns/{txn_id}/reverse
        • wallets
          • GET /v1/wallets/{wallet_id}/balance
          • GET /v1/wallets/{wallet_id}/transactions
        • float
          • GET /v1/float
          • POST /v1/float/topup
          • POST /v1/float/redeem
          • GET /v1/float/instructions/{instruction_id}
        • commissions
          • GET /v1/agents/{agent_id}/commissions
          • POST /v1/agents/{agent_id}/commissions/payouts/preview
          • POST /v1/agents/{agent_id}/commissions/payouts/accept
        • disputes
          • POST /v1/disputes
          • GET /v1/disputes/{case_id}
          • POST /v1/disputes/{case_id}/attachments
        • reports
          • GET /v1/reports/eod
          • POST /v1/reports/eod/close
          • GET /v1/reports/txns
          • GET /v1/reports/float
        • content
          • GET /v1/announcements
        • training
          • GET /v1/training/courses
          • POST /v1/training/quizzes/{quiz_id}/submit
        • ussd
          • POST /v1/ussd/session
          • POST /v1/ussd/agent/menu
        • ops
          • GET /v1/health
      • Agent Scope
        • Agent Scope
    • Customer Operations
      • Customer Scope
        • Customer & Merchant Scope
    • Schemas
      • Agent Ops APIs
  • Nova Core Banking Service API
    • core
      • Create account
      • Get account
      • Get balances
      • Create posting (double-entry)
      • Reverse posting
      • Check limits
      • Generate statement
    • Schemas
      • Schemas
        • Amount
        • Account
        • BalanceSet
        • PostingEntry
        • Posting
        • Hold
        • LimitCheckResponse
        • SavingsProduct
        • OverdraftLine
        • StatementRequest
        • Error
Home
Home
  1. Internal

Accounts, Transactioins and Ledger Implementatioin

Phase 0: Foundations & Tooling#

1.
Tech Stack & Infrastructure
PostgreSQL (13+), with plpgsql triggers
Node.js + TypeScript
TypeORM (or Prisma) for ORM + migrations
Flyway or Liquibase as an alternative for DDL versioning
decimal.js for all money fields
2.
Validation & Schema
Zod (or AJV) for JSONB schemas
class-validator + class-transformer for entity‐level checks
3.
Testing & CI
Jest + TestContainers to spin up real Postgres in CI
Supertest for HTTP/integration tests
GitHub Actions (or equivalent) for linting, migrations, tests
4.
Observability & Security
Winston / Pino for structured logs
Sentry for error tracking
Prometheus + Grafana for metrics

Phase 1: Core Account Model#

TablePurpose
account_typesDefines “kinds” (e.g. SAVINGS, CHECKING)
account_balancesReal-time running balance per account
account_type_transactionWhich transaction types each account type is allowed to perform
accountsActual account record (FK → type, country, currency, status)
Key DB Features:
Unique index on account_types.code
Trigger on ledger_entries to update account_balances
Pessimistic‐locking methods in service to avoid race conditions

Phase 2: Transaction Type Configuration#

TablePurpose
transaction_typesMaster list (code, description, requires_two_factor, JSONB)
transaction_type_postings(Optional) normalized posting templates per transaction type
Key Features:
JSONB or separate table for posting templates
Zod/AJV schema to validate that postings sum to 100% and refer to valid account types

Phase 3: Domain Transactions#

TablePurpose
domain_transactionsBusiness-level record (payer, payee, amount, status, etc.)
domain_transaction_historyAudit of status changes, attachments, idempotency keys
Key Features:
Status enum transitions (PENDING → POSTED → FAILED)
FK to transaction_types, optional idempotency_key

Phase 4: Ledger Header & Lines#

TablePurpose
ledger_transactions“Header” linking back to a domain txn, with metadata & reference
ledger_entriesIndividual debit/credit lines (FK → ledger_transaction, account, side, amount)
Key Features:
Trigger: enforce_double_entry() to ensure Σ(debits) = Σ(credits)
Trigger: adjust_account_balance() to maintain account_balances
Unique or partial‐unique constraints on reference, domain_txn_id

Phase 5: Audit, Idempotency & Outbox#

TablePurpose
ledger_transaction_historyImmutable record of every header change
idempotency_keysGlobal registry of idempotency tokens to prevent duplicates
outbox_eventsFor reliable pub/sub / eventing (e.g. notify downstream)

Phase 6: Service Layer & Orchestration#

Atomic “Post Transaction” with TypeORM QueryRunner at SERIALIZABLE
Steps:
1.
Create & validate DomainTransaction
2.
Instantiate LedgerTransaction + LedgerEntry[] via template
3.
Save header (cascades to entries) → triggers fire → balances update
4.
Record in outbox_events if needed
5.
Commit or rollback on any error (unbalanced, constraint, validation)
Repository Methods for:
Fetching balances with FOR UPDATE lock
Searching transactions by date/account/status

Phase 7: Validation, Testing & CI#

Unit Tests: JSONB schemas, service error paths
Integration Tests:
Triggers (unbalanced entries must fail)
Concurrency (two debits at once update account_balances correctly)
CI Pipeline: lint → build → migrations → tests

Phase 8: Security & Hardening#

DB roles & permissions (readonly vs write)
Audit logging at service layer
2FA enforcement for high-risk transaction types

Phase 9: Scaling & Partitioning#

Partition ledger_entries by date (e.g. monthly) for very large volumes
Archival process to move old partitions to cheaper storage

Phase 10: Monitoring & Maintenance#

Regular drift checks (Flyway)
Health checks on balances vs trial-balance reports
Alerts if any trigger failures or balance mismatches

All Tables at a Glance
account_types
account_balances
account_type_transaction
accounts

transaction_types
transaction_type_postings

domain_transactions
domain_transaction_history

ledger_transactions
ledger_entries
ledger_transaction_history
idempotency_keys
outbox_events

➡️ Next: Phase 1, implement account_types + its migration, entity, indexes, metadata schema, and triggers for balance adjustments. Shall we dive into that?
Modified at 2025-07-14 12:42:46
Previous
Create Clients
Next
Ensure real-time balance guarantees
Built with