# adilamejoud/medisync architecture

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

- Interactive diagram: https://gitdiagram.com/adilamejoud/medisync
- Repository: https://github.com/adilamejoud/medisync

## Overview

1. **Overall shape**
MediSync is a role-based clinic management system split into three major parts: an Angular 17 frontend, a Node.js/Express backend layer, and a PostgreSQL database accessed through Prisma. The repo also includes Docker orchestration, a gateway, and a few service-oriented workers/services for specific domains.

2. **Frontend boundary**
The Angular app is organized around **roles and workflows**, not generic technical modules. Under `features/` there are separate experiences for **admin**, **doctor/medecin**, **patient**, **secretary/secretaire**, plus auth, landing, and unauthorized screens. Shared UI lives in `shared/`, while `core/` contains guards, interceptors, API-facing services, models, and cross-cutting app state.

3. **Client-side request flow**
Frontend services such as `auth.service`, `appointment.service`, `doctor.service`, `patient.service`, `billing.service`, `notification.service`, and `socket.service` are the main API consumers. Guards and interceptors enforce access control and attach JWTs before requests reach the backend. This means the frontend is not just presentation; it is the first enforcement layer for role-based navigation and session handling.

4. **Primary backend**
The main backend in `backend/server.js` and `backend/src/` is an Express application structured by domain: controllers, routes, middlewares, config, and utilities. It appears to be the core API for clinic operations such as users, admins, doctors, secretaries, appointments, patients, prescriptions, invoices, documents, reports, services, and statistics.

5. **Authentication and authorization**
Auth is split across the main backend and a dedicated `auth-service` under `backend/src/services/auth-service`. The system uses **JWT** plus role-based middleware (`authAdmin`, `authDoctor`, `authUser`) and supports **TOTP 2FA**, especially for admin accounts. Password rules are enforced through a dedicated validator utility, and the README indicates bcrypt hashing and rate limiting as part of the security boundary.

6. **Operational domains**
The business logic is centered on clinic workflows:
- **Appointments** and scheduling
- **Doctor availability** and patient files
- **Prescriptions** and medical dossiers
- **Invoices/billing**
- **Patient registration**
- **Care sheets**, reports, and documents
- **Stats/analytics** for admin and doctor views  
These are separated into their own routes/controllers, which is a strong clue for drawing the main application boundary in a diagram.

7. **Real-time and asynchronous behavior**
The repo includes `socket.js` and a frontend `socket.service`, which indicates **Socket.IO** is used for live updates such as notifications, scheduling changes, or status changes. There is also a `notification-service` worker and a publisher utility in the auth service, suggesting asynchronous event flow rather than only synchronous HTTP calls.

8. **External integrations**
Two integrations materially affect architecture: **Cloudinary** for document/image storage and an email system (`emailService.js`, plus cron-based reminder behavior described in the README). File uploads are handled with Multer middleware before assets are stored externally, so medical documents do not live only in PostgreSQL.

9. **Persistence layer**
All persistent domain data flows through Prisma, with `database/schema.prisma` as the schema source and `database/migrations/` capturing schema evolution. The backend also has a `seed.js` script for bootstrapping test accounts and initial data. This is the primary data boundary: application services manipulate Prisma models, then persist to PostgreSQL.

10. **Service decomposition**
The repo contains multiple service directories: `gateway`, `auth-service`, `booking-service`, `doctor-service`, and `notification-service`. The README shows a gateway-centered architecture where the Angular app talks to an **API Gateway**, which forwards traffic to the auth service and the main backend. In practice, the codebase looks like a hybrid: a main monolith plus specialized services and workers for selected concerns.

11. **Deployment and runtime**
Docker is a first-class part of the architecture. Each major component has its own Dockerfile, and the root `docker-compose.yml` orchestrates frontend, gateway, backend, PostgreSQL, and supporting infrastructure. The frontend is built for Angular + Nginx production serving, while the backend runs as Node 20 services.

12. **Cross-cutting concerns and observability**
Security and auditability are prominent architectural concerns. There is an `auditLogger`, an audit route, security-focused tests, and support for user/IP-tracked sensitive actions. That means the system is designed not just for CRUD workflows but for traceable clinical operations, which is important when diagramming the control plane around auth, auditing, and logging.

13. **Test architecture**
The test suite is broad and should be treated as part of the repo architecture: unit tests for controllers/middlewares, integration tests for API behavior, security tests for auth/OWASP/rate limiting/uploads, end-to-end Playwright journeys, and UX completeness/flow tests. This implies the team validates both functional boundaries and role-specific user journeys across frontend and backend.

14. **How to diagram it**
A good architecture diagram for this repo should show:
- Angular role-based client at the edge
- API Gateway as the first backend hop
- Main Express backend as the core domain API
- Auth service and async notification/service workers as specialized side services
- Prisma as the ORM boundary to PostgreSQL
- External services for email and document storage
- Socket.IO as the real-time channel between backend and clients  
The main conceptual split is between **presentation**, **request routing**, **domain services**, **persistent data**, and **external integrations**.

Main entry points: [`backend/server.js`](https://github.com/adilamejoud/medisync/blob/main/backend/server.js) (Clinic API), [`tests`](https://github.com/adilamejoud/medisync/tree/main/tests) (Test suite), [`frontend/src/app`](https://github.com/adilamejoud/medisync/tree/main/frontend/src/app) (Angular app), [`frontend/src/app/core`](https://github.com/adilamejoud/medisync/tree/main/frontend/src/app/core) (Core services)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["Angular app<br/>SPA client"]
  node_frontend_core["Core services<br/>client core"]
  node_frontend_shared["Shared UI<br/>component library"]
  node_frontend_features["Role screens<br/>workflow UI"]
end

subgraph group_backend["Backend"]
  node_gateway["API gateway<br/>routing service<br/>[server.js]"]
  node_backend_api["Clinic API<br/>Express app<br/>[server.js]"]
  node_backend_routes["Domain routes<br/>HTTP boundary"]
  node_backend_controllers["Controllers<br/>domain logic"]
  node_backend_middleware["Auth middleware<br/>policy layer"]
  node_auth_service["Auth service<br/>specialized service<br/>[server.js]"]
  node_booking_service["Booking service<br/>specialized service<br/>[server.js]"]
  node_doctor_service["Doctor service<br/>specialized service<br/>[server.js]"]
  node_notification_worker["Notification worker<br/>async worker<br/>[worker.js]"]
end

subgraph group_data["Data"]
  node_prisma["Prisma schema<br/>ORM schema<br/>[schema.prisma]"]
  node_postgres[("PostgreSQL<br/>database")]
end

subgraph group_external["External"]
  node_cloudinary["Cloudinary<br/>document store<br/>[cloudinary.js]"]
  node_email_service["Email service<br/>messaging integration<br/>[emailService.js]"]
  node_socket_channel(("Socket.IO<br/>realtime channel<br/>[socket.js]"))
end

subgraph group_tests["Tests"]
  node_tests_suite["Test suite<br/>validation stack"]
end

node_frontend_app -->|"uses"| node_frontend_core
node_frontend_app -->|"renders"| node_frontend_shared
node_frontend_app -->|"navigates"| node_frontend_features
node_frontend_core -->|"API calls"| node_gateway
node_frontend_core -->|"subscribes"| node_socket_channel
node_gateway -->|"routes auth"| node_auth_service
node_gateway -->|"routes domain"| node_backend_api
node_backend_api -->|"exposes"| node_backend_routes
node_backend_routes -->|"dispatches"| node_backend_controllers
node_backend_controllers -->|"protected by"| node_backend_middleware
node_backend_api -->|"persists via"| node_prisma
node_prisma -->|"stores"| node_postgres
node_backend_api -->|"uploads"| node_cloudinary
node_backend_api -->|"sends mail"| node_email_service
node_backend_api -->|"publishes"| node_socket_channel
node_auth_service -.->|"publishes events"| node_notification_worker
node_booking_service -.->|"supports"| node_backend_api
node_doctor_service -.->|"supports"| node_backend_api
node_tests_suite -.->|"covers"| node_frontend_app
node_tests_suite -.->|"covers"| node_backend_api
node_tests_suite -.->|"covers"| node_auth_service
node_tests_suite -.->|"covers"| node_prisma

click node_frontend_app "https://github.com/adilamejoud/medisync/tree/main/frontend/src/app"
click node_frontend_core "https://github.com/adilamejoud/medisync/tree/main/frontend/src/app/core"
click node_frontend_shared "https://github.com/adilamejoud/medisync/tree/main/frontend/src/app/shared"
click node_frontend_features "https://github.com/adilamejoud/medisync/tree/main/frontend/src/app/features"
click node_gateway "https://github.com/adilamejoud/medisync/blob/main/backend/src/services/gateway/server.js"
click node_backend_api "https://github.com/adilamejoud/medisync/blob/main/backend/server.js"
click node_backend_routes "https://github.com/adilamejoud/medisync/tree/main/backend/src/routes"
click node_backend_controllers "https://github.com/adilamejoud/medisync/tree/main/backend/src/controllers"
click node_backend_middleware "https://github.com/adilamejoud/medisync/tree/main/backend/src/middlewares"
click node_auth_service "https://github.com/adilamejoud/medisync/blob/main/backend/src/services/auth-service/server.js"
click node_booking_service "https://github.com/adilamejoud/medisync/blob/main/backend/src/services/booking-service/server.js"
click node_doctor_service "https://github.com/adilamejoud/medisync/blob/main/backend/src/services/doctor-service/server.js"
click node_notification_worker "https://github.com/adilamejoud/medisync/blob/main/backend/src/services/notification-service/worker.js"
click node_prisma "https://github.com/adilamejoud/medisync/blob/main/database/schema.prisma"
click node_cloudinary "https://github.com/adilamejoud/medisync/blob/main/backend/src/config/cloudinary.js"
click node_email_service "https://github.com/adilamejoud/medisync/blob/main/backend/src/services/emailService.js"
click node_socket_channel "https://github.com/adilamejoud/medisync/blob/main/backend/socket.js"
click node_tests_suite "https://github.com/adilamejoud/medisync/tree/main/tests"

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_frontend_app,node_frontend_core,node_frontend_shared,node_frontend_features toneBlue
class node_gateway,node_backend_api,node_backend_routes,node_backend_controllers,node_backend_middleware,node_auth_service,node_booking_service,node_doctor_service,node_notification_worker toneAmber
class node_prisma,node_postgres toneMint
class node_cloudinary,node_email_service,node_socket_channel toneRose
class node_tests_suite toneIndigo
```

## Components

### Frontend

Angular client with role-based workflows and shared UI.

- [Angular app](https://github.com/adilamejoud/medisync/tree/main/frontend/src/app): SPA client · Angular 17 application shell and route entrypoints. · `frontend/src/app`
- [Core services](https://github.com/adilamejoud/medisync/tree/main/frontend/src/app/core): client core · Guards, interceptors, models, and API-facing services. · `frontend/src/app/core`
- [Shared UI](https://github.com/adilamejoud/medisync/tree/main/frontend/src/app/shared): component library · Reusable components, directives, and pipes. · `frontend/src/app/shared`
- [Role screens](https://github.com/adilamejoud/medisync/tree/main/frontend/src/app/features): workflow UI · Admin, doctor, patient, secretary, auth, landing, and unauthorized experiences. · `frontend/src/app/features`

### Backend

Express API, gateway, and specialized services.

- [API gateway](https://github.com/adilamejoud/medisync/blob/main/backend/src/services/gateway/server.js): routing service · First backend hop that fronts the service stack. · `backend/src/services/gateway/server.js`
- [Clinic API](https://github.com/adilamejoud/medisync/blob/main/backend/server.js): Express app · Main domain API for clinic operations. · `backend/server.js`
- [Domain routes](https://github.com/adilamejoud/medisync/tree/main/backend/src/routes): HTTP boundary · Routes for auth, users, appointments, patients, prescriptions, billing, reports, stats, and audit. · `backend/src/routes`
- [Controllers](https://github.com/adilamejoud/medisync/tree/main/backend/src/controllers): domain logic · Admin, doctor, secretary, user, auth, appointment, config, and stats controllers. · `backend/src/controllers`
- [Auth middleware](https://github.com/adilamejoud/medisync/tree/main/backend/src/middlewares): policy layer · Role-based access control and upload/session middleware. · `backend/src/middlewares`
- [Auth service](https://github.com/adilamejoud/medisync/blob/main/backend/src/services/auth-service/server.js): specialized service · Dedicated auth service with JWT and 2FA support. · `backend/src/services/auth-service/server.js`
- [Booking service](https://github.com/adilamejoud/medisync/blob/main/backend/src/services/booking-service/server.js): specialized service · Separate runtime for appointment-related workflows. · `backend/src/services/booking-service/server.js`
- [Doctor service](https://github.com/adilamejoud/medisync/blob/main/backend/src/services/doctor-service/server.js): specialized service · Separate runtime for doctor-focused operations. · `backend/src/services/doctor-service/server.js`
- [Notification worker](https://github.com/adilamejoud/medisync/blob/main/backend/src/services/notification-service/worker.js): async worker · Background notification processing and event handling. · `backend/src/services/notification-service/worker.js`

### Data

ORM boundary and PostgreSQL persistence.

- [Prisma schema](https://github.com/adilamejoud/medisync/blob/main/database/schema.prisma): ORM schema · Schema and migrations for persistence models. · `database/schema.prisma`
- **PostgreSQL**: database · Primary relational data store for clinic records.

### External

Real-time, storage, and messaging integrations.

- [Cloudinary](https://github.com/adilamejoud/medisync/blob/main/backend/src/config/cloudinary.js): document store · External storage for uploaded documents and images. · `backend/src/config/cloudinary.js`
- [Email service](https://github.com/adilamejoud/medisync/blob/main/backend/src/services/emailService.js): messaging integration · Outbound email delivery for reminders and account flows. · `backend/src/services/emailService.js`
- [Socket.IO](https://github.com/adilamejoud/medisync/blob/main/backend/socket.js): realtime channel · Live updates between backend and clients. · `backend/socket.js`

### Tests

Cross-layer validation for API, security, UX, and journeys.

- [Test suite](https://github.com/adilamejoud/medisync/tree/main/tests): validation stack · Integration, security, E2E, UX, and unit coverage across the repo. · `tests`

## Connections

- Angular app → Core services: uses · Client UI relies on guards, interceptors, and API services.
- Angular app → Shared UI: renders · Role screens compose reusable UI building blocks.
- Angular app → Role screens: navigates · Route structure is organized around role-specific workflows.
- Core services → API gateway: API calls · Angular services send authenticated requests to the backend entrypoint.
- Core services → Socket.IO: subscribes · Client listens for live notifications and updates.
- API gateway → Auth service: routes auth · Gateway forwards authentication traffic to the auth service.
- API gateway → Clinic API: routes domain · Gateway forwards clinic domain traffic to the main API.
- Clinic API → Domain routes: exposes · Express app mounts domain routes.
- Domain routes → Controllers: dispatches · Routes delegate to domain controllers.
- Controllers → Auth middleware: protected by · Controllers depend on role checks, uploads, and request policy.
- Clinic API → Prisma schema: persists via · Core API reads and writes through Prisma.
- Prisma schema → PostgreSQL: stores · Prisma migrations and schema map to PostgreSQL.
- Clinic API → Cloudinary: uploads · Medical documents and images are stored externally.
- Clinic API → Email service: sends mail · Reminders and notifications flow through email delivery.
- Clinic API → Socket.IO: publishes · Backend emits live events to connected clients.
- Auth service → Notification worker: publishes events · Auth flows feed asynchronous notification handling.
- Booking service → Clinic API: supports · Specialized booking runtime complements the main API.
- Doctor service → Clinic API: supports · Specialized doctor runtime complements the main API.
- Test suite → Angular app: covers · E2E and UX tests validate user journeys.
- Test suite → Clinic API: covers · Unit, integration, and security tests validate backend behavior.
- Test suite → Auth service: covers · Security and auth tests exercise the dedicated auth path.
- Test suite → Prisma schema: covers · Test helpers seed and verify data persistence behavior.
