# mariopw/medical-appointment-management-api architecture

> Architecture diagram of the GitHub repository mariopw/medical-appointment-management-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mariopw/medical-appointment-management-api
- Repository: https://github.com/mariopw/medical-appointment-management-api

## Overview

- API Server & Routing Layer: Include the Express server (noted in src/api/server.js and the app.ts entry point) and its routing mechanism (src/api/routes.ts). This module accepts incoming HTTP requests.
- Controllers: Represent the controllers for each module (appointments, doctors, patients). Each controller processes incoming requests after routing.
- Repositories & Data Access: Show repositories that encapsulate database interactions. They are responsible for CRUD operations against the database.
- Database: Illustrate the PostgreSQL database, noting that migrations are managed via Knex (files in src/db/migrations and configuration in knexfile.ts).
- Middleware: Include the error handling middleware (src/middleware/errorHandler.ts) which is used throughout the routing layer.
- Utilities & Logging: Indicate utility functions such as custom error handling and logging (src/utils/logger.ts, src/utils/customErrors.ts) which support the core functionality.
- Unit Testing: Optionally, denote the test suite (src/test) that ensures the behavior of controllers and services.

##### Relationships and Data Flow
- That utilities such as custom error messages and logging are cross-cutting concerns that support each layer.

##### Design Patterns and Architectural Principles
- Layered (or n-tier) architecture is evident in separating HTTP handling (routing/controllers) from business logic (services) and data access (repositories).
- Modularity by showing that components (appointments, doctors, patients) have their own dedicated sub-systems, which can be independently maintained.
- Mention the use of dependency injection or client libraries (for Redis and RabbitMQ), which suggests extensibility.
- Optionally, highlight testing as a core aspect, showing that unit tests validate individual components.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client("External Client"):::external

    subgraph "API Layer"
        ES("Express Server"):::api
        RT("Routing Layer"):::api
        MW("Error Handling Middleware"):::middleware
    end

    subgraph "Appointments Module"
        A_R("Appointments Routes"):::route
        A_C("Appointments Controller"):::controller
        A_S("Appointments Service"):::service
        A_Repo("Appointments Repository"):::repository
    end

    subgraph "Doctors Module"
        D_R("Doctors Routes"):::route
        D_C("Doctors Controller"):::controller
        D_S("Doctors Service"):::service
        D_Repo("Doctors Repository"):::repository
        D_Redis("Doctors Redis Client"):::external
        D_Rabbit("Doctors RabbitMQ Client"):::external
        D_Val("Doctors Validations"):::utils
    end

    subgraph "Patients Module"
        P_R("Patients Routes"):::route
        P_C("Patients Controller"):::controller
        P_S("Patients Service"):::service
        P_Repo("Patients Repository"):::repository
        P_Redis("Patients Redis Client"):::external
        P_Val("Patients Validations"):::utils
    end

    subgraph "Database Layer"
        DB("PostgreSQL Database"):::database
        Knex("Knex Configuration & Migrations"):::database
    end

    subgraph "Utilities & Testing"
        Util("Utilities & Logger"):::utils
        Tests("Unit Test Suite"):::testing
    end

    %% Data Flow
    Client -->|"HTTPRequest"| ES
    ES -->|"SetupRoutes"| RT
    RT -->|"Appointments"| A_R
    RT -->|"Doctors"| D_R
    RT -->|"Patients"| P_R
    RT --> MW

    %% Appointments Module Flow
    A_R --> A_C
    A_C --> A_S
    A_S --> A_Repo
    A_Repo --> DB
    A_Repo --> Knex

    %% Doctors Module Flow
    D_R --> D_C
    D_C --> D_S
    D_S --> D_Repo
    D_Repo --> DB
    D_Repo --> Knex
    D_S -->|"UsesRedis"| D_Redis
    D_S -->|"UsesRabbitMQ"| D_Rabbit
    D_C --> D_Val

    %% Patients Module Flow
    P_R --> P_C
    P_C --> P_S
    P_S --> P_Repo
    P_Repo --> DB
    P_Repo --> Knex
    P_S -->|"UsesRedis"| P_Redis
    P_C --> P_Val

    %% Middleware to Utilities
    MW --> Util

    %% Testing (dotted relationships indicate tests validate components)
    Tests -.-> A_C
    Tests -.-> D_S
    Tests -.-> P_C

    %% Styles
    classDef api fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef controller fill:#d4edda,stroke:#333,stroke-width:2px;
    classDef service fill:#fff3cd,stroke:#333,stroke-width:2px;
    classDef repository fill:#f8d7da,stroke:#333,stroke-width:2px;
    classDef route fill:#e2e3e5,stroke:#333,stroke-width:2px;
    classDef database fill:#d1ecf1,stroke:#333,stroke-width:2px,stroke-dasharray: 5 5;
    classDef external fill:#f5c6cb,stroke:#333,stroke-width:2px;
    classDef middleware fill:#eeeeee,stroke:#333,stroke-width:2px;
    classDef utils fill:#c3e6cb,stroke:#333,stroke-width:2px;
    classDef testing fill:#bee5eb,stroke:#333,stroke-width:2px;

    %% Click Events
    click ES "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/server.js"
    click ES "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/app.ts"
    click RT "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/routes.ts"

    click A_R "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/components/appointments/routes.ts"
    click A_C "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/components/appointments/controller.ts"
    click A_S "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/components/appointments/service.ts"
    click A_Repo "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/components/appointments/repository.ts"

    click D_R "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/components/doctors/routes.ts"
    click D_C "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/components/doctors/controller.ts"
    click D_S "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/components/doctors/service.ts"
    click D_Repo "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/components/doctors/repository.ts"
    click D_Redis "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/components/doctors/clients/redis.js"
    click D_Rabbit "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/components/doctors/clients/rabbit.ts"
    click D_Val "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/components/doctors/validations/doctor.validations.ts"

    click P_R "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/components/patients/routes.ts"
    click P_C "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/components/patients/controller.ts"
    click P_S "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/components/patients/service.ts"
    click P_Repo "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/components/patients/repository.ts"
    click P_Redis "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/components/patients/clients/redis.js"
    click P_Val "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/api/components/patients/validations/patient.validation.ts"

    click DB "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/db/database.ts"
    click DB "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/db/db.ts"
    click Knex "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/db/knexfile.ts"
    click Knex "https://github.com/mariopw/medical-appointment-management-api/tree/main/src/db/migrations"

    click MW "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/middleware/errorHandler.ts"

    click Util "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/utils/logger.ts"
    click Util "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/utils/customErrors.ts"
    click Util "https://github.com/mariopw/medical-appointment-management-api/blob/main/src/utils/model.ts"

    click Tests "https://github.com/mariopw/medical-appointment-management-api/tree/main/src/test"
```
