# saianandmohan-cts/healthcarebe architecture

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

- Interactive diagram: https://gitdiagram.com/saianandmohan-cts/healthcarebe
- Repository: https://github.com/saianandmohan-cts/healthcarebe

## Overview

1. **Overall shape**
   - This is a Node.js/Express REST backend for a healthcare portal, centered on role-based workflows for `PATIENT` and `DOCTOR`.
   - The main runtime path is: **HTTP request → route → middleware → controller → Mongoose model → MongoDB**.

2. **Bootstrap and server boundary**
   - `server.js` and `app.js` are the entry points that likely split bootstrapping from Express app setup.
   - `db.config.js` is the database boundary, responsible for connecting the app to MongoDB/Mongoose before request handling begins.

3. **API surface**
   - Route modules define the public API boundaries:
     - `authRoutes.js` for login/session-related flows
     - `registrationRoutes.js` for onboarding/registration
     - `patientRoutes.js`, `doctorRoutes.js` for role-specific resources
     - `doc_availabilityRoutes.js` for doctor schedule management
   - These routes are the main integration points for clients.

4. **Authentication and session model**
   - The repository uses JWTs stored in **HttpOnly cookies**, so auth state is managed server-side at the transport boundary rather than in browser storage.
   - `authController.js` and `middleware/auth.js` appear to implement login/session restoration plus role checks, with the README calling out endpoints like `/login/me` and guards such as `verifyPatient` and `verifyDoctor`.

5. **Security middleware layer**
   - `src/middleware/auth.js` is the key security checkpoint for token verification, cookie creation/handling, and role authorization.
   - `logger.js` and `errorHandler.js` provide cross-cutting concerns: request tracing and centralized error response handling.

6. **Domain controllers**
   - Controllers break business logic into healthcare-specific domains:
     - `appointmentController.js` for appointment lifecycle and categorization
     - `doctorController.js` for doctor dashboard and schedule-related operations
     - `patientController.js` and `userController.js` for patient/user record operations
     - `doc_availabilityController.js` for availability slots
     - `authController.js` for authentication and session sync
   - This keeps route handlers thin and makes each domain independently understandable.

7. **Persistence models**
   - Mongoose models define the persistence layer and map directly to core business entities:
     - `patient.js`
     - `doctor.js`
     - `appointment.js`
     - `doc_availability.js`
     - `consultations.js`
   - The presence of both `appointment` and `consultations` suggests a split between scheduled visits and completed/historical clinical records.

8. **Scheduling and time-based logic**
   - `utils/decodedDate.js` supports date normalization or timezone-aware comparisons, which is important for appointment state classification.
   - The README describes logic for separating upcoming appointments from past consultations, so date handling is a meaningful architectural concern.

9. **Document/report generation**
   - `utils/pdfGenerator.js` indicates an output side channel for generating PDFs, likely for medical summaries, reports, or appointment documents.
   - This is a separate utility boundary from the core CRUD flows.

10. **Validation boundary**
   - `validators/registrationSchema.js` and `validators/registrationRules.js` enforce input constraints for onboarding flows before controller logic runs.
   - This prevents invalid patient/doctor registration payloads from reaching persistence.

11. **Data and seed-like assets**
   - `src/data/users.json` is a local data asset, likely for seed/test/mock user data or bootstrapping.
   - It is not the primary system of record; MongoDB remains the authoritative persistence layer.

12. **Important external dependencies**
   - The architecture materially depends on **Express**, **Mongoose/MongoDB Atlas**, **JWT**, **bcrypt**, and **cookie-parser**.
   - bcrypt is used for password hashing, while JWT + cookies implement the authenticated session boundary.

13. **How a request typically flows**
   - A registration or login request enters via a route, passes validation/auth middleware, reaches a controller, and then reads/writes a Mongoose model.
   - Protected doctor/patient endpoints first pass authorization checks, then execute domain logic such as availability updates, appointment filtering, or record retrieval.

14. **Architectural takeaway**
   - The repo is organized as a modular healthcare backend with clear separation between transport, security, domain logic, and persistence.
   - For diagramming, the most important boundaries are: **public routes**, **auth/role middleware**, **domain controllers**, **Mongoose models**, and **MongoDB**.

Main entry points: [`app.js`](https://github.com/saianandmohan-cts/healthcarebe/blob/main/app.js) (app.js), [`src/controllers/authController.js`](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/controllers/authController.js) (Auth controller), [`src/routes/registrationRoutes.js`](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/routes/registrationRoutes.js) (Registration routes), [`src/controllers/doctorController.js`](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/controllers/doctorController.js) (Doctor controller)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_boot["Bootstrap"]
  node_n_server["server.js<br/>entrypoint<br/>[server.js]"]
  node_n_app["app.js<br/>express app<br/>[app.js]"]
  node_n_db[("db.config.js<br/>mongo config<br/>[db.config.js]")]
end

subgraph group_g_api["API Routes"]
  node_n_auth_routes["Auth routes<br/>route module<br/>[authRoutes.js]"]
  node_n_reg_routes["Registration routes<br/>route module"]
  node_n_patient_routes["Patient routes<br/>route module<br/>[patientRoutes.js]"]
  node_n_doctor_routes["Doctor routes<br/>route module<br/>[doctorRoutes.js]"]
  node_n_avail_routes["Availability routes<br/>route module"]
end

subgraph group_g_sec["Security & Middleware"]
  node_n_auth_mw["Auth middleware<br/>security middleware<br/>[auth.js]"]
  node_n_logger["Logger<br/>middleware<br/>[logger.js]"]
  node_n_errors["Error handler<br/>middleware<br/>[errorHandler.js]"]
end

subgraph group_g_dom["Domain Logic"]
  node_n_auth_ctrl["Auth controller<br/>[authController.js]"]
  node_n_appt_ctrl["Appointments<br/>controller"]
  node_n_doctor_ctrl["Doctor controller"]
  node_n_patient_ctrl["Patient controller"]
  node_n_user_ctrl["User controller<br/>[userController.js]"]
  node_n_avail_ctrl["Availability controller"]
  node_n_patient_model["Patient<br/>mongoose model<br/>[patient.js]"]
  node_n_doctor_model["Doctor<br/>mongoose model<br/>[doctor.js]"]
  node_n_appt_model["Appointment<br/>mongoose model<br/>[appointment.js]"]
  node_n_consult_model["Consultations<br/>mongoose model<br/>[consultations.js]"]
  node_n_avail_model["Availability<br/>mongoose model"]
  node_n_valid_schema["Registration schema<br/>validator"]
  node_n_valid_rules["Registration rules<br/>validator"]
  node_n_date_util["Date utils<br/>[decodedDate.js]"]
  node_n_pdf_util["PDF generator<br/>[pdfGenerator.js]"]
  node_n_seed["users.json<br/>data asset<br/>[users.json]"]
end

node_n_server -->|"starts"| node_n_app
node_n_app -->|"connects"| node_n_db
node_n_app -->|"uses"| node_n_logger
node_n_app -->|"uses"| node_n_errors
node_n_app -->|"mounts"| node_n_auth_routes
node_n_app -->|"mounts"| node_n_reg_routes
node_n_app -->|"mounts"| node_n_patient_routes
node_n_app -->|"mounts"| node_n_doctor_routes
node_n_app -->|"mounts"| node_n_avail_routes
node_n_auth_routes -->|"calls"| node_n_auth_ctrl
node_n_reg_routes -->|"validates"| node_n_valid_schema
node_n_reg_routes -->|"validates"| node_n_valid_rules
node_n_reg_routes -->|"calls"| node_n_auth_ctrl
node_n_patient_routes -->|"guards"| node_n_auth_mw
node_n_patient_routes -->|"calls"| node_n_patient_ctrl
node_n_doctor_routes -->|"guards"| node_n_auth_mw
node_n_doctor_routes -->|"calls"| node_n_doctor_ctrl
node_n_avail_routes -->|"guards"| node_n_auth_mw
node_n_avail_routes -->|"calls"| node_n_avail_ctrl
node_n_auth_ctrl -->|"reads/writes"| node_n_patient_model
node_n_auth_ctrl -->|"reads/writes"| node_n_doctor_model
node_n_auth_ctrl -->|"reads/writes"| node_n_appt_model
node_n_patient_ctrl -->|"reads/writes"| node_n_patient_model
node_n_patient_ctrl -->|"reads/writes"| node_n_appt_model
node_n_doctor_ctrl -->|"reads/writes"| node_n_doctor_model
node_n_doctor_ctrl -->|"reads/writes"| node_n_appt_model
node_n_doctor_ctrl -->|"reads/writes"| node_n_consult_model
node_n_avail_ctrl -->|"reads/writes"| node_n_avail_model
node_n_avail_ctrl -->|"reads/writes"| node_n_doctor_model
node_n_appt_ctrl -->|"reads/writes"| node_n_appt_model
node_n_appt_ctrl -->|"reads/writes"| node_n_consult_model
node_n_appt_ctrl -->|"uses"| node_n_date_util
node_n_auth_ctrl -->|"uses"| node_n_pdf_util
node_n_reg_routes -.->|"loads"| node_n_seed

click node_n_server "https://github.com/saianandmohan-cts/healthcarebe/blob/main/server.js"
click node_n_app "https://github.com/saianandmohan-cts/healthcarebe/blob/main/app.js"
click node_n_db "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/config/db.config.js"
click node_n_auth_routes "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/routes/authRoutes.js"
click node_n_reg_routes "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/routes/registrationRoutes.js"
click node_n_patient_routes "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/routes/patientRoutes.js"
click node_n_doctor_routes "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/routes/doctorRoutes.js"
click node_n_avail_routes "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/routes/doc_availabilityRoutes.js"
click node_n_auth_mw "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/middleware/auth.js"
click node_n_logger "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/middleware/logger.js"
click node_n_errors "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/middleware/errorHandler.js"
click node_n_auth_ctrl "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/controllers/authController.js"
click node_n_appt_ctrl "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/controllers/appointmentController.js"
click node_n_doctor_ctrl "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/controllers/doctorController.js"
click node_n_patient_ctrl "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/controllers/patientController.js"
click node_n_user_ctrl "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/controllers/userController.js"
click node_n_avail_ctrl "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/controllers/doc_availabilityController.js"
click node_n_patient_model "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/models/patient.js"
click node_n_doctor_model "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/models/doctor.js"
click node_n_appt_model "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/models/appointment.js"
click node_n_consult_model "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/models/consultations.js"
click node_n_avail_model "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/models/doc_availability.js"
click node_n_valid_schema "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/validators/registrationSchema.js"
click node_n_valid_rules "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/validators/registrationRules.js"
click node_n_date_util "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/utils/decodedDate.js"
click node_n_pdf_util "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/utils/pdfGenerator.js"
click node_n_seed "https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/data/users.json"

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_n_server,node_n_app,node_n_db toneBlue
class node_n_auth_routes,node_n_reg_routes,node_n_patient_routes,node_n_doctor_routes,node_n_avail_routes toneAmber
class node_n_auth_mw,node_n_logger,node_n_errors toneMint
class node_n_auth_ctrl,node_n_appt_ctrl,node_n_doctor_ctrl,node_n_patient_ctrl,node_n_user_ctrl,node_n_avail_ctrl,node_n_patient_model,node_n_doctor_model,node_n_appt_model,node_n_consult_model,node_n_avail_model,node_n_valid_schema,node_n_valid_rules,node_n_date_util,node_n_pdf_util,node_n_seed toneRose
```

## Components

### Bootstrap

App startup, server wiring, and database connection.

- [server.js](https://github.com/saianandmohan-cts/healthcarebe/blob/main/server.js): entrypoint · Process bootstrap and listener startup. · `server.js`
- [app.js](https://github.com/saianandmohan-cts/healthcarebe/blob/main/app.js): express app · Express application setup and route/middleware wiring. · `app.js`
- [db.config.js](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/config/db.config.js): mongo config · Database connection boundary for Mongoose/MongoDB. · `src/config/db.config.js`

### API Routes

Public HTTP boundaries for auth, registration, and role-specific resources.

- [Auth routes](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/routes/authRoutes.js): route module · Login and session-related endpoints. · `src/routes/authRoutes.js`
- [Registration routes](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/routes/registrationRoutes.js): route module · Onboarding and registration endpoints. · `src/routes/registrationRoutes.js`
- [Patient routes](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/routes/patientRoutes.js): route module · Patient-facing resource endpoints. · `src/routes/patientRoutes.js`
- [Doctor routes](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/routes/doctorRoutes.js): route module · Doctor-facing resource endpoints. · `src/routes/doctorRoutes.js`
- [Availability routes](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/routes/doc_availabilityRoutes.js): route module · Doctor schedule and availability endpoints. · `src/routes/doc_availabilityRoutes.js`

### Security & Middleware

Cross-cutting request processing for auth, logging, and errors.

- [Auth middleware](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/middleware/auth.js): security middleware · JWT verification, cookie handling, and role guards. · `src/middleware/auth.js`
- [Logger](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/middleware/logger.js): middleware · Request tracing and logging. · `src/middleware/logger.js`
- [Error handler](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/middleware/errorHandler.js): middleware · Centralized error response handling. · `src/middleware/errorHandler.js`

### Domain Logic

Controllers, models, validation, and supporting utilities.

- [Auth controller](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/controllers/authController.js): controller · Login, session sync, and auth flow coordination. · `src/controllers/authController.js`
- [Appointments](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/controllers/appointmentController.js): controller · Appointment lifecycle and visit classification. · `src/controllers/appointmentController.js`
- [Doctor controller](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/controllers/doctorController.js): controller · Doctor dashboard and schedule operations. · `src/controllers/doctorController.js`
- [Patient controller](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/controllers/patientController.js): controller · Patient record and profile operations. · `src/controllers/patientController.js`
- [User controller](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/controllers/userController.js): controller · General user record operations. · `src/controllers/userController.js`
- [Availability controller](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/controllers/doc_availabilityController.js): controller · Availability slot management. · `src/controllers/doc_availabilityController.js`
- [Patient](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/models/patient.js): mongoose model · Patient persistence model. · `src/models/patient.js`
- [Doctor](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/models/doctor.js): mongoose model · Doctor persistence model. · `src/models/doctor.js`
- [Appointment](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/models/appointment.js): mongoose model · Scheduled visit persistence model. · `src/models/appointment.js`
- [Consultations](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/models/consultations.js): mongoose model · Completed or historical clinical records. · `src/models/consultations.js`
- [Availability](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/models/doc_availability.js): mongoose model · Doctor schedule slot persistence model. · `src/models/doc_availability.js`
- [Registration schema](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/validators/registrationSchema.js): validator · Input schema for registration payloads. · `src/validators/registrationSchema.js`
- [Registration rules](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/validators/registrationRules.js): validator · Registration constraints and checks. · `src/validators/registrationRules.js`
- [Date utils](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/utils/decodedDate.js): utility · Date normalization and comparison helpers. · `src/utils/decodedDate.js`
- [PDF generator](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/utils/pdfGenerator.js): utility · Document/report generation boundary. · `src/utils/pdfGenerator.js`
- [users.json](https://github.com/saianandmohan-cts/healthcarebe/blob/main/src/data/users.json): data asset · Local user data for seeding or mock data. · `src/data/users.json`

## Connections

- server.js → app.js: starts · Server bootstraps the Express application.
- app.js → db.config.js: connects · Application initializes database connectivity.
- app.js → Logger: uses · Global request logging is applied in the app.
- app.js → Error handler: uses · Central error handling is mounted in the app.
- app.js → Auth routes: mounts · Auth endpoints are exposed through the Express app.
- app.js → Registration routes: mounts · Registration endpoints are exposed through the Express app.
- app.js → Patient routes: mounts · Patient endpoints are exposed through the Express app.
- app.js → Doctor routes: mounts · Doctor endpoints are exposed through the Express app.
- app.js → Availability routes: mounts · Availability endpoints are exposed through the Express app.
- Auth routes → Auth controller: calls · Authentication requests are handled by the auth controller.
- Registration routes → Registration schema: validates · Registration payloads are checked against schema rules.
- Registration routes → Registration rules: validates · Registration payloads are checked against business rules.
- Registration routes → Auth controller: calls · Onboarding flows reuse auth/session logic.
- Patient routes → Auth middleware: guards · Patient routes require token and role verification.
- Patient routes → Patient controller: calls · Patient endpoints delegate to patient logic.
- Doctor routes → Auth middleware: guards · Doctor routes require token and role verification.
- Doctor routes → Doctor controller: calls · Doctor endpoints delegate to doctor logic.
- Availability routes → Auth middleware: guards · Availability routes are protected by auth middleware.
- Availability routes → Availability controller: calls · Availability endpoints delegate to schedule logic.
- Auth controller → Patient: reads/writes · Auth flow may touch patient records.
- Auth controller → Doctor: reads/writes · Auth flow may touch doctor records.
- Auth controller → Appointment: reads/writes · Session-aware flows may inspect appointments.
- Patient controller → Patient: reads/writes · Patient operations persist to patient model.
- Patient controller → Appointment: reads/writes · Patient views can include appointment data.
- Doctor controller → Doctor: reads/writes · Doctor operations persist to doctor model.
- Doctor controller → Appointment: reads/writes · Doctor dashboard logic uses appointment data.
- Doctor controller → Consultations: reads/writes · Completed visits are tracked as consultations.
- Availability controller → Availability: reads/writes · Availability management persists schedule slots.
- Availability controller → Doctor: reads/writes · Availability updates are tied to doctors.
- Appointments → Appointment: reads/writes · Appointment lifecycle is stored in appointment records.
- Appointments → Consultations: reads/writes · Past appointments can be promoted to consultations.
- Appointments → Date utils: uses · Date helpers support appointment classification.
- Auth controller → PDF generator: uses · Auth or account flows may generate documents.
- Registration routes → users.json: loads · Local user data can support bootstrap or mock flows.
