# pratikpatel3076/embedcredit architecture

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

- Interactive diagram: https://gitdiagram.com/pratikpatel3076/embedcredit
- Repository: https://github.com/pratikpatel3076/embedcredit

## Overview

#### Purpose and entry points
EmbedCredit is a non-custodial consumption-credit marketplace: it orchestrates consumers, DLAs/LSPs, and lenders without holding disbursal or repayment funds. The React client starts at `client/src/main.jsx` and `client/src/App.jsx`; the Express API starts at `server/server.js`. Local container composition is defined in `docker-compose.yml`.

#### API and access boundary
The server exposes role-oriented REST surfaces for consumers, lenders, DLAs, integrations, engine operations, and read-only administration: `server/routes/consumer.routes.js`, `server/routes/lender.routes.js`, and `server/routes/admin.routes.js`. Authentication/RBAC, validation, security controls, idempotency, and RBI checks form the request boundary in `server/middleware/auth.js`, `server/middleware/idempotency.js`, and `server/middleware/rbiCompliance.js`.

#### Identity, profiles, and consent
Users establish borrower and credit context before marketplace actions. Persistent domain records include `server/models/User.js`, `server/models/BorrowerProfile.js`, and `server/models/CreditProfile.js`. Consent is explicitly recorded and managed through `server/models/ConsentRecord.js` and `server/services/consent.service.js`; AA and bureau adapters are separated behind `server/services/aaService.js` and `server/services/bureauService.js`.

#### Marketplace, applications, and lender routing
A consumer intent becomes an application that is matched/routed to lender products and produces offers. The core records are `server/models/LoanIntent.js`, `server/models/LoanApplication.js`, and `server/models/LoanOffer.js`. Routing and lifecycle coordination are represented by `server/models/ApplicationRoute.js`, `server/services/creditEngine.js`, and `server/services/stateMachine.js`. Lender integrations are an external-system boundary, with OCEN-facing logic in `server/services/ocenService.js`.

#### Compliance and disclosure gate
KFS generation precedes lender routing, while compliance activity is persisted for auditability. This stage centers on `server/services/kfsGenerator.js`, `server/models/ComplianceLog.js`, and `server/models/DLGPortfolio.js`. The platform enforces regulatory constraints such as consent/purpose validation, FLDG limits, no Aadhaar storage, and no fund custody at server-side middleware and domain-service boundaries.

#### Credit-facility ledger and drawdown
Approved applications create a reusable credit facility whose available, utilized, and reserved balances are managed independently of actual fund transfer. `server/models/CreditAccount.js` is supported by `server/services/credit/creditAccount.service.js`, `server/services/credit/creditBalance.service.js`, and `server/services/credit/creditReservation.service.js`. Drawdowns create controlled loans through `server/services/credit/drawdown.service.js` and persist consumption history in `server/models/CreditConsumptionEvent.js`.

#### Repayment, events, and integrations
Repayment reduces principal/utilization and restores available credit, using `server/services/credit/repayment.service.js`, `server/models/Repayment.js`, and `server/models/RepaymentSchedule.js`. Credit mutations are designed to be idempotent and event-backed via `server/services/credit/creditEvent.service.js`, `server/services/credit/eventBus.service.js`, and `server/models/CreditProfile.js`. Webhooks and notifications connect lifecycle changes to external parties through `server/services/webhookService.js`, `server/models/WebhookLog.js`, and `server/services/notificationService.js`.

#### Runtime and persistence
The runtime is Node.js/Express with MongoDB configuration at `server/config/db.js`; the frontend is React/Vite. Docker images are defined in `server/Dockerfile` and `client/Dockerfile`. MongoDB stores marketplace, consent, credit-account, loan, repayment, audit, and integration-log state; lenders, AA/bureau providers, OCEN, and webhook consumers remain external boundaries.

Main entry points: [`server/server.js`](https://github.com/pratikpatel3076/embedcredit/blob/main/server/server.js) (Express API), [`server/services/credit/repayment.service.js`](https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/credit/repayment.service.js) (Repayment service), [`server/services/consent.service.js`](https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/consent.service.js) (Identity & consent), [`server/services/creditEngine.js`](https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/creditEngine.js) (Credit engine)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client"]
  node_react_client["React/Vite client<br/>frontend<br/>[App.jsx]"]
end

subgraph group_api["API boundary"]
  node_express_api["Express API<br/>node runtime<br/>[server.js]"]
  node_rest_routes["Role REST routes<br/>route layer<br/>[consumer.routes.js]"]
  node_request_controls["Auth, RBAC &amp; controls<br/>middleware<br/>[auth.js]"]
end

subgraph group_marketplace["Credit marketplace"]
  node_identity_consent["Identity &amp; consent<br/>domain service<br/>[consent.service.js]"]
  node_loan_intake["Intent &amp; application<br/>domain records<br/>[LoanApplication.js]"]
  node_kfs_compliance["KFS &amp; compliance gate<br/>compliance service<br/>[kfsGenerator.js]"]
  node_credit_engine["Credit engine<br/>orchestration service<br/>[creditEngine.js]"]
end

subgraph group_facility["Facility lifecycle"]
  node_credit_account["Credit account<br/>facility record<br/>[CreditAccount.js]"]
  node_balance_reservation["Balance &amp; reservation<br/>facility service"]
  node_drawdown["Drawdown service<br/>loan control"]
  node_consumption_events["Consumption history<br/>credit record"]
  node_repayment["Repayment service<br/>facility service"]
  node_repayment_records["Repayments &amp; schedules<br/>repayment records<br/>[Repayment.js]"]
  node_credit_events["Credit event bus<br/>eventing service"]
end

subgraph group_external["External parties"]
  node_aa_bureau{{"AA &amp; bureau adapters<br/>integration adapters<br/>[aaService.js]"}}
  node_lender_ocen{{"Lender &amp; OCEN integration<br/>external integration<br/>[ocenService.js]"}}
  node_webhooks_notifications{{"Webhooks &amp; notifications<br/>outbound integration<br/>[webhookService.js]"}}
end

node_mongodb[("MongoDB<br/>database<br/>[db.js]")]
node_docker_compose["Local containers<br/>runtime composition<br/>[docker-compose.yml]"]

node_react_client -->|"REST requests"| node_express_api
node_express_api -->|"mounts"| node_rest_routes
node_rest_routes -->|"passes through"| node_request_controls
node_request_controls -->|"authorized actions"| node_identity_consent
node_identity_consent -->|"consented data checks"| node_aa_bureau
node_identity_consent -->|"establishes credit context"| node_loan_intake
node_loan_intake -->|"application disclosure"| node_kfs_compliance
node_kfs_compliance -->|"approved for routing"| node_credit_engine
node_credit_engine -->|"routes applications &amp; offers"| node_lender_ocen
node_credit_engine -->|"approved facility"| node_credit_account
node_credit_account -->|"facility balances"| node_balance_reservation
node_balance_reservation -->|"reserved capacity"| node_drawdown
node_drawdown -->|"records consumption"| node_consumption_events
node_drawdown -->|"emits mutation"| node_credit_events
node_repayment -->|"persists repayment"| node_repayment_records
node_repayment -->|"restores availability"| node_balance_reservation
node_repayment -->|"emits mutation"| node_credit_events
node_credit_events -->|"lifecycle updates"| node_webhooks_notifications
node_express_api -->|"persists domain state"| node_mongodb
node_docker_compose -.->|"runs"| node_react_client
node_docker_compose -.->|"runs"| node_express_api

click node_react_client "https://github.com/pratikpatel3076/embedcredit/blob/main/client/src/App.jsx"
click node_express_api "https://github.com/pratikpatel3076/embedcredit/blob/main/server/server.js"
click node_rest_routes "https://github.com/pratikpatel3076/embedcredit/blob/main/server/routes/consumer.routes.js"
click node_request_controls "https://github.com/pratikpatel3076/embedcredit/blob/main/server/middleware/auth.js"
click node_identity_consent "https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/consent.service.js"
click node_aa_bureau "https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/aaService.js"
click node_loan_intake "https://github.com/pratikpatel3076/embedcredit/blob/main/server/models/LoanApplication.js"
click node_kfs_compliance "https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/kfsGenerator.js"
click node_credit_engine "https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/creditEngine.js"
click node_lender_ocen "https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/ocenService.js"
click node_credit_account "https://github.com/pratikpatel3076/embedcredit/blob/main/server/models/CreditAccount.js"
click node_balance_reservation "https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/credit/creditReservation.service.js"
click node_drawdown "https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/credit/drawdown.service.js"
click node_consumption_events "https://github.com/pratikpatel3076/embedcredit/blob/main/server/models/CreditConsumptionEvent.js"
click node_repayment "https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/credit/repayment.service.js"
click node_repayment_records "https://github.com/pratikpatel3076/embedcredit/blob/main/server/models/Repayment.js"
click node_credit_events "https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/credit/eventBus.service.js"
click node_webhooks_notifications "https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/webhookService.js"
click node_mongodb "https://github.com/pratikpatel3076/embedcredit/blob/main/server/config/db.js"
click node_docker_compose "https://github.com/pratikpatel3076/embedcredit/blob/main/docker-compose.yml"

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_react_client toneBlue
class node_express_api,node_rest_routes,node_request_controls toneAmber
class node_identity_consent,node_loan_intake,node_kfs_compliance,node_credit_engine toneMint
class node_credit_account,node_balance_reservation,node_drawdown,node_consumption_events,node_repayment,node_repayment_records,node_credit_events toneRose
class node_aa_bureau,node_lender_ocen,node_webhooks_notifications toneIndigo
class node_mongodb,node_docker_compose toneNeutral
```

## Components

### Client

- [React/Vite client](https://github.com/pratikpatel3076/embedcredit/blob/main/client/src/App.jsx): frontend · `client/src/App.jsx`

### API boundary

- [Express API](https://github.com/pratikpatel3076/embedcredit/blob/main/server/server.js): node runtime · `server/server.js`
- [Role REST routes](https://github.com/pratikpatel3076/embedcredit/blob/main/server/routes/consumer.routes.js): route layer · Consumer, lender, and read-only admin surfaces. · `server/routes/consumer.routes.js`
- [Auth, RBAC & controls](https://github.com/pratikpatel3076/embedcredit/blob/main/server/middleware/auth.js): middleware · Enforces access, idempotency, security, and RBI checks. · `server/middleware/auth.js`

### Credit marketplace

- [Identity & consent](https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/consent.service.js): domain service · Manages user, borrower, credit-profile, and consent records. · `server/services/consent.service.js`
- [Intent & application](https://github.com/pratikpatel3076/embedcredit/blob/main/server/models/LoanApplication.js): domain records · Captures consumption intent and loan applications. · `server/models/LoanApplication.js`
- [KFS & compliance gate](https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/kfsGenerator.js): compliance service · Generates disclosures and persists audit/FLDG activity before routing. · `server/services/kfsGenerator.js`
- [Credit engine](https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/creditEngine.js): orchestration service · Matches applications, coordinates lifecycle, and creates offers/routes. · `server/services/creditEngine.js`

### Facility lifecycle

- [Credit account](https://github.com/pratikpatel3076/embedcredit/blob/main/server/models/CreditAccount.js): facility record · Tracks available, utilized, and reserved facility balances. · `server/models/CreditAccount.js`
- [Balance & reservation](https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/credit/creditReservation.service.js): facility service · Maintains available credit and protects drawdown capacity. · `server/services/credit/creditReservation.service.js`
- [Drawdown service](https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/credit/drawdown.service.js): loan control · Creates controlled consumption loans against a facility. · `server/services/credit/drawdown.service.js`
- [Consumption history](https://github.com/pratikpatel3076/embedcredit/blob/main/server/models/CreditConsumptionEvent.js): credit record · `server/models/CreditConsumptionEvent.js`
- [Repayment service](https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/credit/repayment.service.js): facility service · Reduces utilization and restores available credit. · `server/services/credit/repayment.service.js`
- [Repayments & schedules](https://github.com/pratikpatel3076/embedcredit/blob/main/server/models/Repayment.js): repayment records · `server/models/Repayment.js`
- [Credit event bus](https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/credit/eventBus.service.js): eventing service · Publishes idempotent credit mutations and lifecycle events. · `server/services/credit/eventBus.service.js`

### External parties

- [AA & bureau adapters](https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/aaService.js): integration adapters · `server/services/aaService.js`
- [Lender & OCEN integration](https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/ocenService.js): external integration · `server/services/ocenService.js`
- [Webhooks & notifications](https://github.com/pratikpatel3076/embedcredit/blob/main/server/services/webhookService.js): outbound integration · Delivers lifecycle updates and records webhook activity. · `server/services/webhookService.js`

### Other components

- [MongoDB](https://github.com/pratikpatel3076/embedcredit/blob/main/server/config/db.js): database · Stores marketplace, consent, facility, repayment, audit, and integration state. · `server/config/db.js`
- [Local containers](https://github.com/pratikpatel3076/embedcredit/blob/main/docker-compose.yml): runtime composition · Composes client, server, and local dependencies. · `docker-compose.yml`

## Connections

- React/Vite client → Express API: REST requests
- Express API → Role REST routes: mounts
- Role REST routes → Auth, RBAC & controls: passes through
- Auth, RBAC & controls → Identity & consent: authorized actions
- Identity & consent → AA & bureau adapters: consented data checks
- Identity & consent → Intent & application: establishes credit context
- Intent & application → KFS & compliance gate: application disclosure
- KFS & compliance gate → Credit engine: approved for routing
- Credit engine → Lender & OCEN integration: routes applications & offers
- Credit engine → Credit account: approved facility
- Credit account → Balance & reservation: facility balances
- Balance & reservation → Drawdown service: reserved capacity
- Drawdown service → Consumption history: records consumption
- Drawdown service → Credit event bus: emits mutation
- Repayment service → Repayments & schedules: persists repayment
- Repayment service → Balance & reservation: restores availability
- Repayment service → Credit event bus: emits mutation
- Credit event bus → Webhooks & notifications: lifecycle updates
- Express API → MongoDB: persists domain state
- Local containers → React/Vite client: runs
- Local containers → Express API: runs
