How rtharindu/adminbackendnew works
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: app.ts App · schema.prisma Prisma schema · AuthService.ts Auth service · InvoiceService.ts Invoice service
Components 30
Entry points
Application bootstrap and alternate deployment wrapper
HTTP API
Routing, controllers, and middleware boundaries
- Middleware · Auth, role checks, validation, CORS, and error handling
src/middlewares - Auth routes · Authentication endpoints
src/routes/auth.routes.ts - User routes · User management endpoints
src/routes/users.routes.ts - Branch routes · Branch endpoints
src/routes/branches.routes.ts - Invoice routes · Invoice endpoints
src/routes/invoices.routes.ts - Dashboard routes · Stats and activity endpoints
src/routes/dashboard.routes.ts - Provider routes · Doctor and hospital endpoints
src/routes/doctors.routes.ts - Notifications · Notification endpoints
src/routes/notifications.routes.ts - Auth controller · Login, refresh, registration, password flows
src/controllers/AuthController.ts - User controller · User administration actions
src/controllers/UserController.ts - Branch controller · Branch lifecycle and filtering
src/controllers/BranchController.ts - Invoice controller · Invoice lifecycle and payment actions
src/controllers/InvoiceController.ts - Dashboard controller · Aggregates stats and activity views
src/controllers/DashboardController.ts - Provider controller · Doctor and hospital operations
src/controllers/DoctorController.ts
Domain services
Core business logic and external service adapters
- Auth service · Authentication, tokens, and password flows
src/services/AuthService.ts - Branch service · Branch operations
src/services/BranchService.ts - Invoice service · Invoice handling and notifications
src/services/InvoiceService.ts - Dashboard service · Stats and recent activity queries
src/services/DashboardService.ts - Provider service · Doctor and hospital operations
src/services/DoctorService.ts - Integrations · Email, Kafka, and audit side effects
src/services/EmailService.ts
Infrastructure
Configuration and runtime integrations
- Config · Environment, database, JWT, mail, Kafka, and logging setup
src/config
Persistence
Database schema and repositories
- Prisma schema · Database model and ORM boundary
prisma/schema.prisma - User repo · User persistence
src/repositories/UserRepository.ts - Branch repo · Branch persistence
src/repositories/BranchRepository.ts - Invoice repo · Invoice persistence
src/repositories/InvoiceRepository.ts - Audit repo · Operational history persistence
src/repositories/AuditRepository.ts
Connections 34
- 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