# rtharindu/adminbackendnew architecture

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

- Interactive diagram: https://gitdiagram.com/rtharindu/adminbackendnew
- Repository: https://github.com/rtharindu/adminbackendnew

## Overview

1. **Overall shape**
   - This is a TypeScript REST API for the eChannelling hospital/doctor platform.
   - The codebase follows a layered **routes → controllers → services → repositories** structure, with Prisma handling database access.

2. **Application entry points**
   - The main runtime bootstrap appears to be `src/server.ts` and `src/app.ts`.
   - There is also `api/index.ts`, which suggests an alternate entry wrapper or deployment target in addition to the standard Express server startup.

3. **HTTP/API layer**
   - API endpoints are organized under `src/routes/*` and mounted under `/api` per the README.
   - Controllers in `src/controllers/*` map HTTP requests to domain actions for auth, users, branches, invoices, dashboard, notifications, doctors, hospitals, agents, customers, and appointments.
   - The route set is narrower than the controller set, so some controllers are likely internal, shared, or partially wired.

4. **Middleware boundary**
   - Request handling is protected and normalized by middleware for:
     - JWT authentication
     - role-based authorization
     - request validation
     - CORS
     - error handling
   - This is the main boundary between untrusted HTTP traffic and the domain layer.

5. **Domain/service layer**
   - Core business logic lives in `src/services/*`.
   - The key service domains are:
     - `AuthService` for login, token refresh, registration, password flows
     - `BranchService` for branch lifecycle and filtering
     - `InvoiceService` for invoice lifecycle, overdue/payment handling, notifications
     - `DashboardService` for aggregate stats and analytics
     - `DoctorService` and `HospitalService` for provider-related operations
     - `KafkaService`, `EmailService`, and `AuditService` as cross-cutting service adapters

6. **Persistence layer**
   - Prisma is the ORM boundary, with schema defined in `prisma/schema.prisma`.
   - Repositories in `src/repositories/*` encapsulate database operations for the main persisted aggregates: users, branches, invoices, and audit logs.
   - The data store is PostgreSQL, with the README indicating Neon serverless as the production target.

7. **Security and identity**
   - Authentication is JWT-based with access and refresh tokens.
   - Authorization is role-driven; the README calls out ADMIN, DOCTOR, HOSPITAL, and PATIENT roles, and the code includes a role middleware plus permissions utilities.
   - Password handling is hashed, and auth flows include login, registration, logout, refresh, password reset, and password change.

8. **Asynchronous and external integrations**
   - Kafka (`src/config/kafka.ts`, `KafkaService`) provides an event/messaging integration point.
   - Nodemailer (`src/config/mailer.ts`, `EmailService`) handles email notifications such as welcome and password-reset messages.
   - These are external side-effect boundaries separate from the primary HTTP/database flow.

9. **Observability and auditing**
   - Winston logging is configured in `src/config/logger.ts`.
   - Audit logging is a first-class subsystem, with `AuditService` and `AuditRepository` persisting operational history such as user actions, resources, metadata, and request context.
   - The dashboard module exposes stats and recent activity views that likely consume these audit and domain records.

10. **Configuration and environment**
    - Runtime configuration is centralized in `src/config/*` for database, env, JWT, Kafka, logger, and mailer settings.
    - Environment variables drive secrets, port, CORS origin, rate limits, SMTP, Kafka brokers, and database connection details.

11. **Schema and operational scripts**
    - `scripts/*` contains database/bootstrap utilities: admin creation, DB initialization SQL, password reset, and seed data.
    - `prisma` migrations/schema plus the scripts imply the repo is designed for repeatable environment setup and deterministic data seeding.

12. **Testing and quality gates**
    - Tests are split into unit and integration layers under `src/tests`.
    - Authentication has both unit and integration coverage, and Kafka is mocked for tests, which indicates the system isolates external dependencies during verification.
    - Tooling includes Jest, Supertest, ESLint, and Prettier, with Docker Compose used to reproduce the runtime stack locally.

Main entry points: [`src/app.ts`](https://github.com/rtharindu/adminbackendnew/blob/master/src/app.ts) (App), [`prisma/schema.prisma`](https://github.com/rtharindu/adminbackendnew/blob/master/prisma/schema.prisma) (Prisma schema), [`src/services/AuthService.ts`](https://github.com/rtharindu/adminbackendnew/blob/master/src/services/AuthService.ts) (Auth service), [`src/services/InvoiceService.ts`](https://github.com/rtharindu/adminbackendnew/blob/master/src/services/InvoiceService.ts) (Invoice service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry points"]
  node_api_index["API index<br/>wrapper<br/>[index.ts]"]
  node_src_server(("Server<br/>bootstrap<br/>[server.ts]"))
  node_src_app["App<br/>[app.ts]"]
end

subgraph group_http["HTTP API"]
  node_middleware["Middleware<br/>request boundary"]
  node_auth_routes["Auth routes<br/>route set<br/>[auth.routes.ts]"]
  node_user_routes["User routes<br/>route set<br/>[users.routes.ts]"]
  node_branch_routes["Branch routes<br/>route set<br/>[branches.routes.ts]"]
  node_invoice_routes["Invoice routes<br/>route set<br/>[invoices.routes.ts]"]
  node_dashboard_routes["Dashboard routes<br/>route set"]
  node_provider_routes["Provider routes<br/>route set<br/>[doctors.routes.ts]"]
  node_notification_routes["Notifications<br/>route set"]
  node_auth_controller["Auth controller<br/>http controller<br/>[AuthController.ts]"]
  node_user_controller["User controller<br/>http controller<br/>[UserController.ts]"]
  node_branch_controller["Branch controller<br/>http controller"]
  node_invoice_controller["Invoice controller<br/>http controller"]
  node_dashboard_controller["Dashboard controller<br/>http controller"]
  node_provider_controller["Provider controller<br/>http controller"]
end

subgraph group_domain["Domain services"]
  node_auth_service["Auth service<br/>business logic<br/>[AuthService.ts]"]
  node_branch_service["Branch service<br/>business logic<br/>[BranchService.ts]"]
  node_invoice_service["Invoice service<br/>business logic<br/>[InvoiceService.ts]"]
  node_dashboard_service["Dashboard service<br/>analytics"]
  node_provider_service["Provider service<br/>business logic<br/>[DoctorService.ts]"]
  node_integration_services{{"Integrations<br/>adapters<br/>[EmailService.ts]"}}
end

subgraph group_infra["Infrastructure"]
  node_config["Config"]
end

subgraph group_persistence["Persistence"]
  node_prisma_schema["Prisma schema<br/>db schema<br/>[schema.prisma]"]
  node_user_repo["User repo<br/>[UserRepository.ts]"]
  node_branch_repo["Branch repo"]
  node_invoice_repo["Invoice repo"]
  node_audit_repo["Audit repo<br/>[AuditRepository.ts]"]
end

subgraph group_ops["Ops"]
  node_scripts["Scripts<br/>ops utilities"]
end

node_api_index -->|"boots"| node_src_app
node_src_server -->|"starts"| node_src_app
node_src_app -->|"uses"| node_middleware
node_src_app -->|"mounts"| node_auth_routes
node_src_app -->|"mounts"| node_user_routes
node_src_app -->|"mounts"| node_branch_routes
node_src_app -->|"mounts"| node_invoice_routes
node_src_app -->|"mounts"| node_dashboard_routes
node_src_app -->|"mounts"| node_provider_routes
node_src_app -->|"mounts"| node_notification_routes
node_auth_routes -->|"dispatches to"| node_auth_controller
node_user_routes -->|"dispatches to"| node_user_controller
node_branch_routes -->|"dispatches to"| node_branch_controller
node_invoice_routes -->|"dispatches to"| node_invoice_controller
node_dashboard_routes -->|"dispatches to"| node_dashboard_controller
node_provider_routes -->|"dispatches to"| node_provider_controller
node_auth_controller -->|"calls"| node_auth_service
node_user_controller -->|"calls"| node_user_repo
node_branch_controller -->|"calls"| node_branch_service
node_invoice_controller -->|"calls"| node_invoice_service
node_dashboard_controller -->|"calls"| node_dashboard_service
node_provider_controller -->|"calls"| node_provider_service
node_auth_service -->|"reads/writes"| node_user_repo
node_branch_service -->|"reads/writes"| node_branch_repo
node_invoice_service -->|"reads/writes"| node_invoice_repo
node_dashboard_service -->|"reads"| node_audit_repo
node_auth_service -->|"notifies"| node_integration_services
node_invoice_service -->|"notifies"| node_integration_services
node_integration_services -->|"uses"| node_config
node_user_repo -->|"maps to"| node_prisma_schema
node_branch_repo -->|"maps to"| node_prisma_schema
node_invoice_repo -->|"maps to"| node_prisma_schema
node_audit_repo -->|"maps to"| node_prisma_schema
node_scripts -->|"initializes"| node_prisma_schema

click node_api_index "https://github.com/rtharindu/adminbackendnew/blob/master/api/index.ts"
click node_src_server "https://github.com/rtharindu/adminbackendnew/blob/master/src/server.ts"
click node_src_app "https://github.com/rtharindu/adminbackendnew/blob/master/src/app.ts"
click node_middleware "https://github.com/rtharindu/adminbackendnew/tree/master/src/middlewares"
click node_auth_routes "https://github.com/rtharindu/adminbackendnew/blob/master/src/routes/auth.routes.ts"
click node_user_routes "https://github.com/rtharindu/adminbackendnew/blob/master/src/routes/users.routes.ts"
click node_branch_routes "https://github.com/rtharindu/adminbackendnew/blob/master/src/routes/branches.routes.ts"
click node_invoice_routes "https://github.com/rtharindu/adminbackendnew/blob/master/src/routes/invoices.routes.ts"
click node_dashboard_routes "https://github.com/rtharindu/adminbackendnew/blob/master/src/routes/dashboard.routes.ts"
click node_provider_routes "https://github.com/rtharindu/adminbackendnew/blob/master/src/routes/doctors.routes.ts"
click node_notification_routes "https://github.com/rtharindu/adminbackendnew/blob/master/src/routes/notifications.routes.ts"
click node_auth_controller "https://github.com/rtharindu/adminbackendnew/blob/master/src/controllers/AuthController.ts"
click node_user_controller "https://github.com/rtharindu/adminbackendnew/blob/master/src/controllers/UserController.ts"
click node_branch_controller "https://github.com/rtharindu/adminbackendnew/blob/master/src/controllers/BranchController.ts"
click node_invoice_controller "https://github.com/rtharindu/adminbackendnew/blob/master/src/controllers/InvoiceController.ts"
click node_dashboard_controller "https://github.com/rtharindu/adminbackendnew/blob/master/src/controllers/DashboardController.ts"
click node_provider_controller "https://github.com/rtharindu/adminbackendnew/blob/master/src/controllers/DoctorController.ts"
click node_auth_service "https://github.com/rtharindu/adminbackendnew/blob/master/src/services/AuthService.ts"
click node_branch_service "https://github.com/rtharindu/adminbackendnew/blob/master/src/services/BranchService.ts"
click node_invoice_service "https://github.com/rtharindu/adminbackendnew/blob/master/src/services/InvoiceService.ts"
click node_dashboard_service "https://github.com/rtharindu/adminbackendnew/blob/master/src/services/DashboardService.ts"
click node_provider_service "https://github.com/rtharindu/adminbackendnew/blob/master/src/services/DoctorService.ts"
click node_integration_services "https://github.com/rtharindu/adminbackendnew/blob/master/src/services/EmailService.ts"
click node_config "https://github.com/rtharindu/adminbackendnew/tree/master/src/config"
click node_prisma_schema "https://github.com/rtharindu/adminbackendnew/blob/master/prisma/schema.prisma"
click node_user_repo "https://github.com/rtharindu/adminbackendnew/blob/master/src/repositories/UserRepository.ts"
click node_branch_repo "https://github.com/rtharindu/adminbackendnew/blob/master/src/repositories/BranchRepository.ts"
click node_invoice_repo "https://github.com/rtharindu/adminbackendnew/blob/master/src/repositories/InvoiceRepository.ts"
click node_audit_repo "https://github.com/rtharindu/adminbackendnew/blob/master/src/repositories/AuditRepository.ts"
click node_scripts "https://github.com/rtharindu/adminbackendnew/tree/master/scripts"

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_api_index,node_src_server,node_src_app toneBlue
class node_middleware,node_auth_routes,node_user_routes,node_branch_routes,node_invoice_routes,node_dashboard_routes,node_provider_routes,node_notification_routes,node_auth_controller,node_user_controller,node_branch_controller,node_invoice_controller,node_dashboard_controller,node_provider_controller toneAmber
class node_auth_service,node_branch_service,node_invoice_service,node_dashboard_service,node_provider_service,node_integration_services toneMint
class node_config toneRose
class node_prisma_schema,node_user_repo,node_branch_repo,node_invoice_repo,node_audit_repo toneIndigo
class node_scripts toneTeal
```

## Components

### Entry points

Application bootstrap and alternate deployment wrapper

- [API index](https://github.com/rtharindu/adminbackendnew/blob/master/api/index.ts): wrapper · Alternate runtime entry wrapper · `api/index.ts`
- [Server](https://github.com/rtharindu/adminbackendnew/blob/master/src/server.ts): bootstrap · Starts the HTTP server · `src/server.ts`
- [App](https://github.com/rtharindu/adminbackendnew/blob/master/src/app.ts): express app · Assembles middleware and routes · `src/app.ts`

### HTTP API

Routing, controllers, and middleware boundaries

- [Middleware](https://github.com/rtharindu/adminbackendnew/tree/master/src/middlewares): request boundary · Auth, role checks, validation, CORS, and error handling · `src/middlewares`
- [Auth routes](https://github.com/rtharindu/adminbackendnew/blob/master/src/routes/auth.routes.ts): route set · Authentication endpoints · `src/routes/auth.routes.ts`
- [User routes](https://github.com/rtharindu/adminbackendnew/blob/master/src/routes/users.routes.ts): route set · User management endpoints · `src/routes/users.routes.ts`
- [Branch routes](https://github.com/rtharindu/adminbackendnew/blob/master/src/routes/branches.routes.ts): route set · Branch endpoints · `src/routes/branches.routes.ts`
- [Invoice routes](https://github.com/rtharindu/adminbackendnew/blob/master/src/routes/invoices.routes.ts): route set · Invoice endpoints · `src/routes/invoices.routes.ts`
- [Dashboard routes](https://github.com/rtharindu/adminbackendnew/blob/master/src/routes/dashboard.routes.ts): route set · Stats and activity endpoints · `src/routes/dashboard.routes.ts`
- [Provider routes](https://github.com/rtharindu/adminbackendnew/blob/master/src/routes/doctors.routes.ts): route set · Doctor and hospital endpoints · `src/routes/doctors.routes.ts`
- [Notifications](https://github.com/rtharindu/adminbackendnew/blob/master/src/routes/notifications.routes.ts): route set · Notification endpoints · `src/routes/notifications.routes.ts`
- [Auth controller](https://github.com/rtharindu/adminbackendnew/blob/master/src/controllers/AuthController.ts): http controller · Login, refresh, registration, password flows · `src/controllers/AuthController.ts`
- [User controller](https://github.com/rtharindu/adminbackendnew/blob/master/src/controllers/UserController.ts): http controller · User administration actions · `src/controllers/UserController.ts`
- [Branch controller](https://github.com/rtharindu/adminbackendnew/blob/master/src/controllers/BranchController.ts): http controller · Branch lifecycle and filtering · `src/controllers/BranchController.ts`
- [Invoice controller](https://github.com/rtharindu/adminbackendnew/blob/master/src/controllers/InvoiceController.ts): http controller · Invoice lifecycle and payment actions · `src/controllers/InvoiceController.ts`
- [Dashboard controller](https://github.com/rtharindu/adminbackendnew/blob/master/src/controllers/DashboardController.ts): http controller · Aggregates stats and activity views · `src/controllers/DashboardController.ts`
- [Provider controller](https://github.com/rtharindu/adminbackendnew/blob/master/src/controllers/DoctorController.ts): http controller · Doctor and hospital operations · `src/controllers/DoctorController.ts`

### Domain services

Core business logic and external service adapters

- [Auth service](https://github.com/rtharindu/adminbackendnew/blob/master/src/services/AuthService.ts): business logic · Authentication, tokens, and password flows · `src/services/AuthService.ts`
- [Branch service](https://github.com/rtharindu/adminbackendnew/blob/master/src/services/BranchService.ts): business logic · Branch operations · `src/services/BranchService.ts`
- [Invoice service](https://github.com/rtharindu/adminbackendnew/blob/master/src/services/InvoiceService.ts): business logic · Invoice handling and notifications · `src/services/InvoiceService.ts`
- [Dashboard service](https://github.com/rtharindu/adminbackendnew/blob/master/src/services/DashboardService.ts): analytics · Stats and recent activity queries · `src/services/DashboardService.ts`
- [Provider service](https://github.com/rtharindu/adminbackendnew/blob/master/src/services/DoctorService.ts): business logic · Doctor and hospital operations · `src/services/DoctorService.ts`
- [Integrations](https://github.com/rtharindu/adminbackendnew/blob/master/src/services/EmailService.ts): adapters · Email, Kafka, and audit side effects · `src/services/EmailService.ts`

### Infrastructure

Configuration and runtime integrations

- [Config](https://github.com/rtharindu/adminbackendnew/tree/master/src/config): runtime config · Environment, database, JWT, mail, Kafka, and logging setup · `src/config`

### Persistence

Database schema and repositories

- [Prisma schema](https://github.com/rtharindu/adminbackendnew/blob/master/prisma/schema.prisma): db schema · Database model and ORM boundary · `prisma/schema.prisma`
- [User repo](https://github.com/rtharindu/adminbackendnew/blob/master/src/repositories/UserRepository.ts): repository · User persistence · `src/repositories/UserRepository.ts`
- [Branch repo](https://github.com/rtharindu/adminbackendnew/blob/master/src/repositories/BranchRepository.ts): repository · Branch persistence · `src/repositories/BranchRepository.ts`
- [Invoice repo](https://github.com/rtharindu/adminbackendnew/blob/master/src/repositories/InvoiceRepository.ts): repository · Invoice persistence · `src/repositories/InvoiceRepository.ts`
- [Audit repo](https://github.com/rtharindu/adminbackendnew/blob/master/src/repositories/AuditRepository.ts): repository · Operational history persistence · `src/repositories/AuditRepository.ts`

### Ops

Database/bootstrap scripts

- [Scripts](https://github.com/rtharindu/adminbackendnew/tree/master/scripts): ops utilities · Admin creation, seeding, reset, and DB bootstrap · `scripts`

## Connections

- API index → App: boots · Alternate wrapper loads the application
- Server → App: starts · Server starts from the assembled app
- App → Middleware: uses · Applies request middleware stack
- App → Auth routes: mounts · Registers API route set
- App → User routes: mounts · Registers API route set
- App → Branch routes: mounts · Registers API route set
- App → Invoice routes: mounts · Registers API route set
- App → Dashboard routes: mounts · Registers API route set
- App → Provider routes: mounts · Registers API route set
- App → Notifications: mounts · Registers API route set
- Auth routes → Auth controller: dispatches to · Maps HTTP actions to auth handlers
- User routes → User controller: dispatches to · Maps HTTP actions to user handlers
- Branch routes → Branch controller: dispatches to · Maps HTTP actions to branch handlers
- Invoice routes → Invoice controller: dispatches to · Maps HTTP actions to invoice handlers
- Dashboard routes → Dashboard controller: dispatches to · Maps HTTP actions to dashboard handlers
- Provider routes → Provider controller: dispatches to · Maps HTTP actions to provider handlers
- Auth controller → Auth service: calls · Delegates auth business logic
- User controller → User repo: calls · User operations are persisted through the repository
- Branch controller → Branch service: calls · Delegates branch logic
- Invoice controller → Invoice service: calls · Delegates invoice logic
- Dashboard controller → Dashboard service: calls · Delegates analytics queries
- Provider controller → Provider service: calls · Delegates provider logic
- Auth service → User repo: reads/writes · Uses user persistence for credentials and profiles
- Branch service → Branch repo: reads/writes · Uses branch persistence
- Invoice service → Invoice repo: reads/writes · Uses invoice persistence
- Dashboard service → Audit repo: reads · Consumes audit history for recent activity
- Auth service → Integrations: notifies · Triggers email, Kafka, and audit side effects
- Invoice service → Integrations: notifies · Triggers notifications and audit side effects
- Integrations → Config: uses · Reads runtime settings for external systems
- User repo → Prisma schema: maps to · Repository access is backed by Prisma models
- Branch repo → Prisma schema: maps to · Repository access is backed by Prisma models
- Invoice repo → Prisma schema: maps to · Repository access is backed by Prisma models
- Audit repo → Prisma schema: maps to · Repository access is backed by Prisma models
- Scripts → Prisma schema: initializes · Bootstraps and seeds the database schema
