# isahilmishra/userpayout-affiliate architecture

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

- Interactive diagram: https://gitdiagram.com/isahilmishra/userpayout-affiliate
- Repository: https://github.com/isahilmishra/userpayout-affiliate

## Overview

This is an Express backend for affiliate sales payouts. Its API accepts and lists sales, reconciles pending sales as approved or rejected, issues advances, and serves ledger-derived balances and withdrawal operations. Financial events are recorded in an append-only ledger backed by SQLite transactions and constraints; the README also documents race-safe advance issuance and the atomic 24-hour withdrawal rule. The route, service, and database call paths are visible in sampled files; no separate advance-job HTTP trigger is shown.

Main entry points: [`src/db/database.js`](https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/db/database.js) (SQLite Database), [`src/app.js`](https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/app.js) (Express App), [`src/routes/sales.js`](https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/routes/sales.js) (Sales API), [`src/services/AdvancePayoutService.js`](https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/services/AdvancePayoutService.js) (Advance Payout)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["HTTP API"]
  node_app["Express App<br/>[app.js]"]
  node_sales["Sales API<br/>[sales.js]"]
  node_payouts["Payout API<br/>[payouts.js]"]
  node_balance["Balance API<br/>[balance.js]"]
  node_withdrawals["Withdrawal API<br/>[withdrawals.js]"]
end

subgraph group_money["Payout Lifecycle"]
  node_advance["Advance Payout"]
  node_reconciliation["Sale Reconciliation"]
  node_ledger["Ledger Queries<br/>[LedgerService.js]"]
  node_withdrawalservice["Withdrawal Rules"]
end

subgraph group_storage["Financial Storage"]
  node_database[("SQLite Database<br/>[database.js]")]
  node_schema["Financial Schema<br/>[schema.sql]"]
  node_constants["Payout Constants<br/>[constants.js]"]
end

node_client(("Calling System"))

node_client -->|"sends requests"| node_app
node_app -->|"mounts routes"| node_sales
node_app -->|"mounts routes"| node_payouts
node_app -->|"mounts routes"| node_balance
node_app -->|"mounts routes"| node_withdrawals
node_sales -->|"writes sales"| node_database
node_sales -->|"reconciles sales"| node_reconciliation
node_payouts -->|"runs advance job"| node_advance
node_balance -->|"queries balances"| node_ledger
node_withdrawals -->|"handles requests"| node_withdrawalservice
node_advance -->|"records advances"| node_database
node_advance -->|"uses advance rate"| node_constants
node_reconciliation -->|"updates sales and ledger"| node_database
node_ledger -->|"reads ledger"| node_database
node_withdrawalservice -->|"records withdrawals"| node_database
node_withdrawalservice -->|"uses cooldown"| node_constants
node_database -->|"initializes schema"| node_schema

click node_app "https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/app.js"
click node_sales "https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/routes/sales.js"
click node_payouts "https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/routes/payouts.js"
click node_balance "https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/routes/balance.js"
click node_withdrawals "https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/routes/withdrawals.js"
click node_advance "https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/services/AdvancePayoutService.js"
click node_reconciliation "https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/services/ReconciliationService.js"
click node_ledger "https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/services/LedgerService.js"
click node_withdrawalservice "https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/services/WithdrawalService.js"
click node_database "https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/db/database.js"
click node_schema "https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/db/schema.sql"
click node_constants "https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/config/constants.js"

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_app,node_sales,node_payouts,node_balance,node_withdrawals toneBlue
class node_advance,node_reconciliation,node_ledger,node_withdrawalservice toneAmber
class node_database,node_schema,node_constants toneMint
class node_client toneIndigo
```

## Components

### HTTP API

- [Express App](https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/app.js): `src/app.js`
- [Sales API](https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/routes/sales.js): `src/routes/sales.js`
- [Payout API](https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/routes/payouts.js): `src/routes/payouts.js`
- [Balance API](https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/routes/balance.js): `src/routes/balance.js`
- [Withdrawal API](https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/routes/withdrawals.js): `src/routes/withdrawals.js`

### Payout Lifecycle

- [Advance Payout](https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/services/AdvancePayoutService.js): `src/services/AdvancePayoutService.js`
- [Sale Reconciliation](https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/services/ReconciliationService.js): `src/services/ReconciliationService.js`
- [Ledger Queries](https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/services/LedgerService.js): `src/services/LedgerService.js`
- [Withdrawal Rules](https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/services/WithdrawalService.js): `src/services/WithdrawalService.js`

### Financial Storage

- [SQLite Database](https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/db/database.js): `src/db/database.js`
- [Financial Schema](https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/db/schema.sql): `src/db/schema.sql`
- [Payout Constants](https://github.com/isahilmishra/userpayout-affiliate/blob/main/src/config/constants.js): `src/config/constants.js`

### Other components

- **Calling System**

## Connections

- Calling System → Express App: sends requests (evidence: `README.md`)
- Express App → Sales API: mounts routes (evidence: `src/app.js`)
- Express App → Payout API: mounts routes (evidence: `src/app.js`)
- Express App → Balance API: mounts routes (evidence: `src/app.js`)
- Express App → Withdrawal API: mounts routes (evidence: `src/app.js`)
- Sales API → SQLite Database: writes sales (evidence: `src/routes/sales.js`)
- Sales API → Sale Reconciliation: reconciles sales (evidence: `src/routes/sales.js`)
- Payout API → Advance Payout: runs advance job (evidence: `src/routes/payouts.js`)
- Balance API → Ledger Queries: queries balances (evidence: `src/routes/balance.js`)
- Withdrawal API → Withdrawal Rules: handles requests (evidence: `src/routes/withdrawals.js`)
- Advance Payout → SQLite Database: records advances (evidence: `src/services/AdvancePayoutService.js`)
- Advance Payout → Payout Constants: uses advance rate (evidence: `src/services/AdvancePayoutService.js`)
- Sale Reconciliation → SQLite Database: updates sales and ledger (evidence: `src/services/ReconciliationService.js`)
- Ledger Queries → SQLite Database: reads ledger (evidence: `src/services/LedgerService.js`)
- Withdrawal Rules → SQLite Database: records withdrawals (evidence: `src/services/WithdrawalService.js`)
- Withdrawal Rules → Payout Constants: uses cooldown (evidence: `src/services/WithdrawalService.js`)
- SQLite Database → Financial Schema: initializes schema (evidence: `src/db/database.js`)
