# femzy123/settlehq architecture

> Architecture diagram of the GitHub repository femzy123/settlehq, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/femzy123/settlehq
- Repository: https://github.com/femzy123/settlehq

## Overview

#### Purpose and Runtime
SettleHQ is a Next.js/TypeScript payment-collection system for organizations: manage payers and collections, create payer-specific invoices, accept Nomba transfer/card payments, reconcile outcomes, and expose receipts. It runs as a Next.js App Router application, with server-rendered pages, server actions, and route handlers. Primary entry points are `src/app/layout.tsx`, `src/app/page.tsx`, and `src/proxy.ts`.

#### Identity, Workspace, and Persistence Boundary
Clerk authentication gates organization-scoped workspaces; onboarding establishes the organization context used by all business records. Database access is Neon Postgres through Drizzle, with schema/migrations defining the durable model.
- `src/app/onboarding/actions.ts`
- `src/server/current-workspace.ts`
- `src/db/schema.ts`
- `src/db/index.ts`
- `drizzle/0000_amazing_grey_gargoyle.sql`

#### Organization Operations: Payers and Collections
Authenticated operators create payers, create collections, and assign payers to a collection. This assignment is the invoice-generation boundary: a collection becomes a set of payer-specific obligations rather than a shared payment request.
- `src/app/payers/actions.ts`
- `src/server/payers.ts`
- `src/app/collections/actions.ts`
- `src/server/collections.ts`

#### Invoice Lifecycle and Public Payment Surface
Internal invoice pages provide organization visibility, while tokenized public invoice routes provide the payer-facing payment surface. A public invoice loads its obligation and supports payment initiation/status verification without exposing organization dashboard access.
- `src/app/invoices/[invoiceId]/page.tsx`
- `src/app/invoice/[token]/page.tsx`
- `src/app/invoice/[token]/actions.ts`
- `src/app/api/invoices/public/[token]/status/route.ts`
- `src/app/api/invoices/public/[token]/verify/route.ts`

#### Nomba Payment Integration
Nomba is the financial-infrastructure boundary. Invoice opening/payment actions create or retrieve virtual accounts for bank transfers and initiate checkout for card payments; payment utilities encapsulate provider-specific request/response behavior. Provider callbacks enter through a dedicated webhook endpoint, then payment verification determines authoritative status.
- `src/server/nomba.ts`
- `src/lib/nomba-virtual-account.ts`
- `src/lib/nomba-checkout.ts`
- `src/lib/nomba-payment.ts`
- `src/app/api/webhooks/nomba/route.ts`

#### Reconciliation and Receipt Flow
A Nomba event or explicit verification resolves a provider transaction to its invoice through virtual-account or checkout references. The system records actual received amount, compares it with invoice expectation, and derives paid/underpaid/overpaid/pending outcomes. Updated invoice/payment records drive dashboard, invoice, payer, and payment views; successful verified payments enable payer receipt rendering/printing.
- `src/server/invoices.ts`
- `src/server/payments.ts`
- `src/server/payment-records.ts`
- `src/app/payments/page.tsx`
- `src/components/receipt-viewer.tsx`

#### Presentation and Deployment
Dashboard pages are organized around authenticated operational views, with shared application shell/sidebar components. Public invoice pages remain a distinct payer experience. Styling uses Tailwind/shadcn conventions. Deployment targets Vercel; external dependencies are Clerk, Neon Postgres, Drizzle, and Nomba. Health checking is exposed at `src/app/api/health/route.ts`.
- `src/components/dashboard-shell.tsx`
- `src/components/app-sidebar.tsx`
- `src/components/auth-shell.tsx`
- `src/app/dashboard/page.tsx`

Main entry points: [`src/server/payments.ts`](https://github.com/femzy123/settlehq/blob/master/src/server/payments.ts) (Payment reconciliation), [`src/server/invoices.ts`](https://github.com/femzy123/settlehq/blob/master/src/server/invoices.ts) (Invoice lifecycle), [`src/db/index.ts`](https://github.com/femzy123/settlehq/blob/master/src/db/index.ts) (Neon Postgres), [`src/server/collections.ts`](https://github.com/femzy123/settlehq/blob/master/src/server/collections.ts) (Collection service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["Next.js application"]
  node_entry["App entry<br/>Next.js App Router<br/>[layout.tsx]"]
  node_auth_proxy{{"Auth proxy<br/>Clerk route gate<br/>[proxy.ts]"}}
  node_dashboard["Dashboard<br/>operator page<br/>[page.tsx]"]
  node_payers_actions["Payer management<br/>server actions<br/>[actions.ts]"]
  node_collections_actions["Collection management<br/>server actions<br/>[actions.ts]"]
  node_internal_invoice["Internal invoice view<br/>operator page<br/>[page.tsx]"]
  node_public_invoice["Public invoice<br/>payer page/actions<br/>[page.tsx]"]
  node_verification_api["Public verification API<br/>route handler<br/>[route.ts]"]
  node_payments_page["Payments and receipts<br/>operator page<br/>[page.tsx]"]
  node_receipt["Receipt viewer<br/>UI component<br/>[receipt-viewer.tsx]"]
end

subgraph group_domain["Business services"]
  node_payers_service["Payer service<br/>domain service<br/>[payers.ts]"]
  node_collections_service["Collection service<br/>domain service<br/>[collections.ts]"]
  node_invoices_service["Invoice lifecycle<br/>domain service<br/>[invoices.ts]"]
  node_reconciliation["Payment reconciliation<br/>domain service<br/>[payments.ts]"]
end

subgraph group_payments["Payment integration"]
  node_nomba_checkout["Nomba payment initiation<br/>provider adapter<br/>[nomba.ts]"]
  node_nomba_webhook["Nomba webhook<br/>route handler<br/>[route.ts]"]
  node_nomba_provider{{"Nomba<br/>payment provider"}}
end

subgraph group_data["Identity and persistence"]
  node_workspace{{"Current workspace<br/>identity service"}}
  node_database[("Neon Postgres<br/>Drizzle database<br/>[index.ts]")]
  node_schema["Schema and migrations<br/>Drizzle model<br/>[schema.ts]"]
end

node_entry -->|"routes through"| node_auth_proxy
node_auth_proxy -->|"authenticates"| node_workspace
node_dashboard -->|"uses"| node_workspace
node_payers_actions -->|"calls"| node_payers_service
node_collections_actions -->|"calls"| node_collections_service
node_payers_service -->|"persists"| node_database
node_collections_service -->|"persists"| node_database
node_collections_service -->|"creates obligations"| node_invoices_service
node_internal_invoice -->|"reads"| node_invoices_service
node_public_invoice -->|"loads by token"| node_invoices_service
node_public_invoice -->|"initiates payment"| node_nomba_checkout
node_verification_api -->|"verifies transaction"| node_reconciliation
node_nomba_webhook -->|"submits event"| node_reconciliation
node_nomba_checkout -->|"creates accounts / checkout"| node_nomba_provider
node_nomba_provider -.->|"payment callback"| node_nomba_webhook
node_reconciliation -->|"updates outcome"| node_invoices_service
node_reconciliation -->|"records payment"| node_database
node_invoices_service -->|"reads and updates"| node_database
node_payments_page -->|"shows outcomes"| node_reconciliation
node_receipt -->|"renders verified payment"| node_reconciliation
node_schema -->|"defines model"| node_database

click node_entry "https://github.com/femzy123/settlehq/blob/master/src/app/layout.tsx"
click node_auth_proxy "https://github.com/femzy123/settlehq/blob/master/src/proxy.ts"
click node_dashboard "https://github.com/femzy123/settlehq/blob/master/src/app/dashboard/page.tsx"
click node_payers_actions "https://github.com/femzy123/settlehq/blob/master/src/app/payers/actions.ts"
click node_collections_actions "https://github.com/femzy123/settlehq/blob/master/src/app/collections/actions.ts"
click node_internal_invoice "https://github.com/femzy123/settlehq/blob/master/src/app/invoices/%5BinvoiceId%5D/page.tsx"
click node_public_invoice "https://github.com/femzy123/settlehq/blob/master/src/app/invoice/%5Btoken%5D/page.tsx"
click node_verification_api "https://github.com/femzy123/settlehq/blob/master/src/app/api/invoices/public/%5Btoken%5D/verify/route.ts"
click node_payments_page "https://github.com/femzy123/settlehq/blob/master/src/app/payments/page.tsx"
click node_workspace "https://github.com/femzy123/settlehq/blob/master/src/server/current-workspace.ts"
click node_database "https://github.com/femzy123/settlehq/blob/master/src/db/index.ts"
click node_schema "https://github.com/femzy123/settlehq/blob/master/src/db/schema.ts"
click node_payers_service "https://github.com/femzy123/settlehq/blob/master/src/server/payers.ts"
click node_collections_service "https://github.com/femzy123/settlehq/blob/master/src/server/collections.ts"
click node_invoices_service "https://github.com/femzy123/settlehq/blob/master/src/server/invoices.ts"
click node_reconciliation "https://github.com/femzy123/settlehq/blob/master/src/server/payments.ts"
click node_nomba_checkout "https://github.com/femzy123/settlehq/blob/master/src/server/nomba.ts"
click node_nomba_webhook "https://github.com/femzy123/settlehq/blob/master/src/app/api/webhooks/nomba/route.ts"
click node_receipt "https://github.com/femzy123/settlehq/blob/master/src/components/receipt-viewer.tsx"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry,node_auth_proxy,node_dashboard,node_payers_actions,node_collections_actions,node_internal_invoice,node_public_invoice,node_verification_api,node_payments_page,node_receipt toneBlue
class node_payers_service,node_collections_service,node_invoices_service,node_reconciliation toneAmber
class node_nomba_checkout,node_nomba_webhook,node_nomba_provider toneMint
class node_workspace,node_database,node_schema toneRose
```

## Components

### Next.js application

Authenticated operations and public payer routes.

- [App entry](https://github.com/femzy123/settlehq/blob/master/src/app/layout.tsx): Next.js App Router · `src/app/layout.tsx`
- [Auth proxy](https://github.com/femzy123/settlehq/blob/master/src/proxy.ts): Clerk route gate · Gates organization workspace access. · `src/proxy.ts`
- [Dashboard](https://github.com/femzy123/settlehq/blob/master/src/app/dashboard/page.tsx): operator page · `src/app/dashboard/page.tsx`
- [Payer management](https://github.com/femzy123/settlehq/blob/master/src/app/payers/actions.ts): server actions · `src/app/payers/actions.ts`
- [Collection management](https://github.com/femzy123/settlehq/blob/master/src/app/collections/actions.ts): server actions · Assigning payers creates invoice obligations. · `src/app/collections/actions.ts`
- [Internal invoice view](https://github.com/femzy123/settlehq/blob/master/src/app/invoices/%5BinvoiceId%5D/page.tsx): operator page · `src/app/invoices/[invoiceId]/page.tsx`
- [Public invoice](https://github.com/femzy123/settlehq/blob/master/src/app/invoice/%5Btoken%5D/page.tsx): payer page/actions · Tokenized payer payment surface. · `src/app/invoice/[token]/page.tsx`
- [Public verification API](https://github.com/femzy123/settlehq/blob/master/src/app/api/invoices/public/%5Btoken%5D/verify/route.ts): route handler · `src/app/api/invoices/public/[token]/verify/route.ts`
- [Payments and receipts](https://github.com/femzy123/settlehq/blob/master/src/app/payments/page.tsx): operator page · Shows reconciled payment outcomes and receipts. · `src/app/payments/page.tsx`
- [Receipt viewer](https://github.com/femzy123/settlehq/blob/master/src/components/receipt-viewer.tsx): UI component · Renders printable receipts for verified payments. · `src/components/receipt-viewer.tsx`

### Business services

Organization-scoped lifecycle and reconciliation logic.

- [Payer service](https://github.com/femzy123/settlehq/blob/master/src/server/payers.ts): domain service · `src/server/payers.ts`
- [Collection service](https://github.com/femzy123/settlehq/blob/master/src/server/collections.ts): domain service · `src/server/collections.ts`
- [Invoice lifecycle](https://github.com/femzy123/settlehq/blob/master/src/server/invoices.ts): domain service · Loads obligations and derives invoice state. · `src/server/invoices.ts`
- [Payment reconciliation](https://github.com/femzy123/settlehq/blob/master/src/server/payments.ts): domain service · Matches provider references and records paid, underpaid, overpaid, or pending outcomes. · `src/server/payments.ts`

### Payment integration

Nomba-facing account, checkout, and event handling.

- [Nomba payment initiation](https://github.com/femzy123/settlehq/blob/master/src/server/nomba.ts): provider adapter · Creates virtual accounts and card checkouts. · `src/server/nomba.ts`
- [Nomba webhook](https://github.com/femzy123/settlehq/blob/master/src/app/api/webhooks/nomba/route.ts): route handler · Receives provider callbacks. · `src/app/api/webhooks/nomba/route.ts`
- **Nomba**: payment provider · Transfer and card payment infrastructure.

### Identity and persistence

Workspace identity and durable records.

- [Current workspace](https://github.com/femzy123/settlehq/blob/master/src/server/current-workspace.ts): identity service · Resolves the Clerk organization context. · `src/server/current-workspace.ts`
- [Neon Postgres](https://github.com/femzy123/settlehq/blob/master/src/db/index.ts): Drizzle database · Durable organizations, invoices, and payment records. · `src/db/index.ts`
- [Schema and migrations](https://github.com/femzy123/settlehq/blob/master/src/db/schema.ts): Drizzle model · `src/db/schema.ts`

## Connections

- App entry → Auth proxy: routes through
- Auth proxy → Current workspace: authenticates
- Dashboard → Current workspace: uses
- Payer management → Payer service: calls
- Collection management → Collection service: calls
- Payer service → Neon Postgres: persists
- Collection service → Neon Postgres: persists
- Collection service → Invoice lifecycle: creates obligations
- Internal invoice view → Invoice lifecycle: reads
- Public invoice → Invoice lifecycle: loads by token
- Public invoice → Nomba payment initiation: initiates payment
- Public verification API → Payment reconciliation: verifies transaction
- Nomba webhook → Payment reconciliation: submits event
- Nomba payment initiation → Nomba: creates accounts / checkout
- Nomba → Nomba webhook: payment callback
- Payment reconciliation → Invoice lifecycle: updates outcome
- Payment reconciliation → Neon Postgres: records payment
- Invoice lifecycle → Neon Postgres: reads and updates
- Payments and receipts → Payment reconciliation: shows outcomes
- Receipt viewer → Payment reconciliation: renders verified payment
- Schema and migrations → Neon Postgres: defines model
