# moshahat1/medify4u architecture

> Architecture diagram of the GitHub repository moshahat1/medify4u, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/moshahat1/medify4u
- Repository: https://github.com/moshahat1/medify4u

## Overview

##### Project Type and Purpose
- Medify Backend is a monolithic, full‐stack application’s backend API, built with Node.js/Express, exposing REST endpoints to support a medical assistance platform. It connects clients (web/mobile) to core services (appointments, messaging, AI analysis, file storage, push notifications) and persists data in MongoDB via Prisma.

##### File Structure Analysis
- Top‐level:
- api/index.js: Entry point for Vercel serverless deployment
- src/: Application code (app.js, config, controllers, middleware, models, routes, services, utils, validators, scripts)
- prisma/: Database schema (Prisma ORM)
- package.json, vercel.json, Postman docs
- Layers identified:
- Configuration (firebase.js, prisma.js, logging.js, upload.js)
- Routing & Controllers (routes/* → controllers/*)
- Business Logic (services/*)
- Data Layer (models/* + Prisma ORM)
- Cross‐cutting concerns (middleware, validators, utils)

##### Key Components
a. Clients
- Web / Mobile Frontend (React, Flutter, etc.)
b. API Gateway / Load Balancer
- Vercel Serverless Functions / API Gateway
c. Application Server (Node.js/Express)
- Routing Layer (src/routes)
- Controllers Layer (src/controllers)
- Middleware (Auth, Error Handling, Rate Limiter, CORS, Helmet)
- Services Layer (Business logic, external integrations)
d. Data Persistence
- MongoDB (hosted externally)
- Prisma ORM (prisma/schema.prisma + generated client)
e. External Services
- Firebase Admin SDK (Push Notifications)
- Vercel Blob Storage (File Uploads)
- Nodemailer/SMTP (Email Notifications)
- AI Service API (Health data analysis)
f. Ancillary Components
- Redis or in‐memory store (optional for rate limiting or sessions)
- Postman Collection for API documentation

##### Relationships & Data Flows
- Client → API Gateway (HTTPS Request) → Express Router → Controller → Service
- Service → Prisma Client → MongoDB (CRUD operations)
- Service → External API (Firebase, AI Service, SMTP, Blob Storage)
- Middleware intercepts requests for authentication (JWT), validation (Yup), rate limiting, error handling.
- Logging layer (Winston) captures request/response and errors.

##### Architectural Patterns & Principles
- Layered (Onion) Architecture: controllers → services → data layer
- MVC‐ish separation for Request Handling vs Business Logic vs Data Access
- Single responsibility: each service handles one domain (e.g., appointment.service, message.service)
- Dependency Injection via module imports (config modules injected into services/controllers)
- Infrastructure as Code: Vercel deployment, Prisma migrations
- Secure by default: CORS, Helmet, rate limiting, input validation

- Clients (Web/Mobile)
- API Gateway / Vercel
- Node.js/Express App (group into Routing, Controllers, Middleware, Services, ORM)
- MongoDB
- External Services (Firebase, AI, Email SMTP, Blob Storage)
- From Clients → API Gateway → Express Router → Controllers → Services → Data Store / External Service
- “Auth Middleware” interceptor
- “appointment.controller.js” → “appointment.service.js”
- “Prisma Client” → “MongoDB Atlas”
e. Protocols & tech:
- HTTPS / REST
- JWT Auth token
- WebSocket (if real‐time messaging uses it) or HTTP Polling
- File upload via Vercel Blob API

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients and Gateway
    Clients["Web/Mobile Clients"]:::client
    Gateway["Vercel Serverless API\n(api/index.js)"]:::gateway
    click Gateway "https://github.com/moshahat1/medify4u/blob/main/api/index.js"

    %% Express Application Core
    AppCore["Express App Core\n(src/app.js)"]:::core
    click AppCore "https://github.com/moshahat1/medify4u/blob/main/src/app.js"

    %% Application Layer
    subgraph "Configuration Layer"
        direction TB
        Firebase["Firebase Admin SDK\n(src/config/firebase.js)"]:::config
        click Firebase "https://github.com/moshahat1/medify4u/blob/main/src/config/firebase.js"
        PrismaConfig["Prisma ORM Config\n(src/config/prisma.js)"]:::config
        click PrismaConfig "https://github.com/moshahat1/medify4u/blob/main/src/config/prisma.js"
        UploadConfig["File Upload Config\n(src/config/upload.js)"]:::config
        click UploadConfig "https://github.com/moshahat1/medify4u/blob/main/src/config/upload.js"
        LoggingConfig["Logging (Winston)\n(src/config/logging.js)"]:::config
        click LoggingConfig "https://github.com/moshahat1/medify4u/blob/main/src/config/logging.js"
    end

    subgraph "Routing & Controllers"
        direction TB
        Routes["Routes\n(src/routes)"]:::core
        click Routes "https://github.com/moshahat1/medify4u/tree/main/src/routes"
        Controllers["Controllers\n(src/controllers)"]:::core
        click Controllers "https://github.com/moshahat1/medify4u/tree/main/src/controllers"
    end

    subgraph "Middleware & Validation"
        direction TB
        AuthMW["Auth Middleware\n(src/middleware/auth.middleware.js)"]:::core
        click AuthMW "https://github.com/moshahat1/medify4u/blob/main/src/middleware/auth.middleware.js"
        ErrorMW["Error Handler\n(src/middleware/errorHandler.js)"]:::core
        click ErrorMW "https://github.com/moshahat1/medify4u/blob/main/src/middleware/errorHandler.js"
        Validators["Validators\n(src/validators)"]:::core
        click Validators "https://github.com/moshahat1/medify4u/tree/main/src/validators"
    end

    subgraph "Services Layer"
        direction TB
        AppointmentSvc["Appointment Service\n(src/services/appointment.service.js)"]:::core
        click AppointmentSvc "https://github.com/moshahat1/medify4u/blob/main/src/services/appointment.service.js"
        MessageSvc["Message Service\n(src/services/message.service.js)"]:::core
        click MessageSvc "https://github.com/moshahat1/medify4u/blob/main/src/services/message.service.js"
        AiSvc["AI Analysis Service\n(src/services/ai.service.js)"]:::core
        click AiSvc "https://github.com/moshahat1/medify4u/blob/main/src/services/ai.service.js"
        NotificationSvc["Notification Service\n(src/services/notification.service.js)"]:::core
        click NotificationSvc "https://github.com/moshahat1/medify4u/blob/main/src/services/notification.service.js"
        UserSvc["User Service\n(src/services/user.service.js)"]:::core
        click UserSvc "https://github.com/moshahat1/medify4u/blob/main/src/services/user.service.js"
        MedicalSvc["Medical Record Service\n(src/services/medical.service.js)"]:::core
        click MedicalSvc "https://github.com/moshahat1/medify4u/blob/main/src/services/medical.service.js"
    end

    subgraph "Utilities"
        direction TB
        EmailUtil["Email Util (SMTP)\n(src/utils/email.util.js)"]:::core
        click EmailUtil "https://github.com/moshahat1/medify4u/blob/main/src/utils/email.util.js"
        JwtUtil["JWT Helpers\n(src/utils/jwt.util.js)"]:::core
        click JwtUtil "https://github.com/moshahat1/medify4u/blob/main/src/utils/jwt.util.js"
        ErrorUtil["Error Formatting\n(src/utils/error.util.js)"]:::core
        click ErrorUtil "https://github.com/moshahat1/medify4u/blob/main/src/utils/error.util.js"
        ValidationUtil["Validation Helpers\n(src/utils/validation.util.js)"]:::core
        click ValidationUtil "https://github.com/moshahat1/medify4u/blob/main/src/utils/validation.util.js"
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        PrismaSchema["Prisma Schema\n(prisma/schema.prisma)"]:::datastore
        click PrismaSchema "https://github.com/moshahat1/medify4u/blob/main/prisma/schema.prisma"
        Models["Mongoose-style Models\n(src/models)"]:::datastore
        click Models "https://github.com/moshahat1/medify4u/tree/main/src/models"
        MongoDB["MongoDB Atlas"]:::datastore
    end

    %% External Services
    subgraph "External Services"
        direction TB
        FirebaseAPI["Firebase Push Notifications"]:::external
        AIAPI["AI Service API"]:::external
        SMTP["Email SMTP"]:::external
        BlobStorage["Vercel Blob Storage"]:::external
    end

    %% Ancillary
    subgraph "Ancillary Components"
        direction TB
        Scripts["Dev Scripts\n(src/scripts/resetDb.js)"]:::ancillary
        click Scripts "https://github.com/moshahat1/medify4u/blob/main/src/scripts/resetDb.js"
        PostmanCol["Postman Collection\n(Medify.postman_collection.json)"]:::ancillary
        click PostmanCol "https://github.com/moshahat1/medify4u/blob/main/Medify.postman_collection.json"
        PostmanEnv["Postman Environment\n(Medify.postman_environment.json)"]:::ancillary
        click PostmanEnv "https://github.com/moshahat1/medify4u/blob/main/Medify.postman_environment.json"
    end

    %% Connections
    Clients -->|"HTTPS Request"| Gateway
    Gateway --> AppCore
    AppCore --> Firebase
    AppCore --> PrismaConfig
    AppCore --> UploadConfig
    AppCore --> LoggingConfig

    AppCore --> AuthMW
    AppCore --> ErrorMW
    AppCore --> Validators

    AppCore --> Routes
    Routes --> Controllers
    Controllers --> AppointmentSvc
    Controllers --> MessageSvc
    Controllers --> AiSvc
    Controllers --> NotificationSvc
    Controllers --> UserSvc
    Controllers --> MedicalSvc

    AppointmentSvc --> PrismaSchema
    MessageSvc --> FirebaseAPI
    AiSvc --> AIAPI
    NotificationSvc --> FirebaseAPI
    NotificationSvc --> SMTP
    MedicalSvc --> BlobStorage
    MedicalSvc --> PrismaSchema

    AppointmentSvc --> MongoDB
    MessageSvc --> MongoDB
    AiSvc --> MongoDB
    NotificationSvc --> MongoDB
    UserSvc --> MongoDB
    MedicalSvc --> MongoDB

    %% Utilities usage
    Controllers --> EmailUtil
    Controllers --> JwtUtil
    Controllers --> ErrorUtil
    Controllers --> ValidationUtil

    %% Ancillary flows
    Scripts --> PrismaSchema
    PostmanCol --> Routes
    PostmanEnv --> Routes

    %% Styles
    classDef client fill:#dddddd,stroke:#888888,color:#333333;
    classDef gateway fill:#bbbbbb,stroke:#666666,color:#111111;
    classDef core fill:#e0f7ff,stroke:#60b0d0,color:#004d66;
    classDef config fill:#d0f0ff,stroke:#80c0e0,color:#004450;
    classDef datastore fill:#ffe0b2,stroke:#d0a060,color:#663300;
    classDef external fill:#d0ffd0,stroke:#60c060,color:#006600;
    classDef ancillary fill:#f0e0ff,stroke:#b080d0,color:#440066;
```
