# githubstuf/gestion-comptable-api architecture

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

- Interactive diagram: https://gitdiagram.com/githubstuf/gestion-comptable-api
- Repository: https://github.com/githubstuf/gestion-comptable-api

## Overview

##### Main Components
- Server/Entry Point – Represented by the file (src/server.ts) that starts the Express (or similar) server.
- Middleware – Components handling cross-cutting concerns such as authentication (AuthMiddleware.ts) and error handling (ErrorHandlerMiddleware.ts).
- Routes – HTTP route definitions found in the src/routes directory (e.g., client.routes.ts, devis.routes.ts, marche.routes.ts). They map endpoints to controllers.
- Controllers (Use Case Coordinators) – Located within each usecase (for instance, CreateClientController.ts, CreateDevisController.ts, etc.). These act as the entry point for each business operation.
- Use Cases – Core business logic located in the src/usecases directories for different modules (Client, Devis, Marche). Each task (Create, Delete, Find, Update) embodies a discrete use case.
- Data Transfer Objects (DTOs) – Defined in the src/dtos directory. They encapsulate input/output data structures exchanged between layers.
- Repositories – Defined by interfaces in src/repositories and implemented in the two subfolders:
- In-Memory Implementations – Useful for testing environments.
- Prisma Implementations – Production-ready repository classes using the PrismaClient for database interactions.
- Data Access Layer – Represented by the Prisma client defined in src/libs/PrismaClient.ts and the prisma folder (containing schema and migration details).
- Helper Functions and Utilities – Various helper files (pagination helpers, error utilities, etc.) that support consistent business logic.

##### Relationships and Component Interactions
- Incoming HTTP Requests hit the Server and pass through Middlewares (for authentication, error handling, etc.).
- Routed requests then are forwarded from the Routes layer to the corresponding Controllers in the use cases.
- Controllers accept inputs (using DTOs) and invoke the Use Cases that implement the business logic.
- Use Cases, in turn, depend on Repository Interfaces. Depending on configuration, they use either the in-memory or Prisma repository implementations.
- The Repository delegates data operations to the Prisma client, which interacts with the underlying database.
- Responses and any errors flow back through the inverse chain: use case → controller → route → middleware → response to the client.

##### Architectural Patterns and Principles
- Clean Architecture/Onion Architecture – Separation is evident, as each layer (controllers, use cases, repositories) is independent and communicates via defined interfaces.
- Dependency Inversion – The use cases depend on repository interfaces rather than concrete implementations allowing selection between in-memory and Prisma implementations.
- Modularity – Each domain (e.g., Client, Devis, Marche) is separately organized with its own set of use cases and related DTOs.
- Error Handling and Middleware – Global error handling ensures consistency in reporting and debugging.

##### Detailed Elements
- Clearly label every component with its name (e.g., “Client Use Case – CreateClient”, “PrismaDevisRepository”, etc.).
- Optional layers such as mocks for testing purposes (realizing that tests and mocks are part of the overall development feedback loop).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Entry Layer
    A["Server (src/server.ts)"]:::entry

    %% Middleware Layer
    subgraph "Middleware Layer"
        B["AuthMiddleware (src/middlewares/AuthMiddleware.ts)"]:::middleware
        C["ErrorHandlerMiddleware (src/middlewares/ErrorHandlerMiddleware.ts)"]:::middleware
    end

    %% Routes Layer
    subgraph "Routes Layer"
        D["Client Routes (src/routes/client.routes.ts)"]:::routes
        E["Devis Routes (src/routes/devis.routes.ts)"]:::routes
        F["Marche Routes (src/routes/marche.routes.ts)"]:::routes
    end

    %% Controllers Layer
    subgraph "Controllers Layer"
        G["CreateClientController (src/usecases/Client/CreateClient/CreateClientController.ts)"]:::controller
        H["DeleteClientController (src/usecases/Client/DeleteClient/DeleteClientController.ts)"]:::controller
        I["CreateDevisController (src/usecases/Devis/CreateDevis/CreateDevisController.ts)"]:::controller
        J["DeleteDevisController (src/usecases/Devis/DeleteDevis/DeleteDevisController.ts)"]:::controller
        K["CreateMarcheController (src/usecases/Marche/CreateMarche/CreateMarcheController.ts)"]:::controller
        L["DeleteMarcheController (src/usecases/Marche/DeleteMarche/DeleteMarcheController.ts)"]:::controller
    end

    %% Use Cases Layer
    subgraph "Use Cases Layer"
        M["Client Use Cases (src/usecases/Client)"]:::usecase
        N["Devis Use Cases (src/usecases/Devis)"]:::usecase
        O["Marche Use Cases (src/usecases/Marche)"]:::usecase
    end

    %% DTOs
    P["DTOs (src/dtos)"]:::dto

    %% Repository Layer
    subgraph "Repository Layer"
        Q["Repository Interfaces (src/repositories)"]:::repository
        R["In-Memory Repositories (src/repositories/implementations/in-memory)"]:::repository
        S["Prisma Repositories (src/repositories/implementations/prisma)"]:::repository
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        T["Prisma Client (src/libs/PrismaClient.ts)"]:::dataAccess
        U["Prisma Schema & Migrations (prisma)"]:::dataAccess
    end

    %% Helper Functions
    V["Helpers (src/helpers)"]:::helper

    %% Connections
    A -->|"handles"| B
    A -->|"handles"| C
    B -->|"passes request"| D
    C -->|"passes error info"| D
    D -->|"routes to"| G
    D -->|"routes to"| I
    D -->|"routes to"| K
    G -->|"invokes"| M
    H -->|"invokes"| M
    I -->|"invokes"| N
    J -->|"invokes"| N
    K -->|"invokes"| O
    L -->|"invokes"| O
    %% DTO usage (dashed connection)
    G -- "uses" --- P
    I -- "uses" --- P
    K -- "uses" --- P
    %% Use Cases to Repositories
    M -->|"depends on"| Q
    N -->|"depends on"| Q
    O -->|"depends on"| Q
    %% Repository Interfaces implemented by concrete repos
    Q --> R
    Q --> S
    %% Prisma Repositories to Data Access
    S --> T
    T --> U

    %% Helpers support various layers (optional indication)
    V --- M
    V --- G

    %% Styles
    classDef entry fill:#FFEEAD,stroke:#333,stroke-width:2px;
    classDef middleware fill:#AED6F1,stroke:#333,stroke-width:2px;
    classDef routes fill:#F9E79F,stroke:#333,stroke-width:2px;
    classDef controller fill:#A9DFBF,stroke:#333,stroke-width:2px;
    classDef usecase fill:#F5CBA7,stroke:#333,stroke-width:2px;
    classDef dto fill:#D2B4DE,stroke:#333,stroke-width:2px;
    classDef repository fill:#F5B7B1,stroke:#333,stroke-width:2px;
    classDef dataAccess fill:#ABB2B9,stroke:#333,stroke-width:2px;
    classDef helper fill:#C39BD3,stroke:#333,stroke-width:2px;

    %% Click Events
    click A "https://github.com/githubstuf/gestion-comptable-api/blob/main/src/server.ts"
    click B "https://github.com/githubstuf/gestion-comptable-api/blob/main/src/middlewares/AuthMiddleware.ts"
    click C "https://github.com/githubstuf/gestion-comptable-api/blob/main/src/middlewares/ErrorHandlerMiddleware.ts"
    click D "https://github.com/githubstuf/gestion-comptable-api/blob/main/src/routes/client.routes.ts"
    click E "https://github.com/githubstuf/gestion-comptable-api/blob/main/src/routes/devis.routes.ts"
    click F "https://github.com/githubstuf/gestion-comptable-api/blob/main/src/routes/marche.routes.ts"
    click G "https://github.com/githubstuf/gestion-comptable-api/blob/main/src/usecases/Client/CreateClient/CreateClientController.ts"
    click H "https://github.com/githubstuf/gestion-comptable-api/blob/main/src/usecases/Client/DeleteClient/DeleteClientController.ts"
    click I "https://github.com/githubstuf/gestion-comptable-api/blob/main/src/usecases/Devis/CreateDevis/CreateDevisController.ts"
    click J "https://github.com/githubstuf/gestion-comptable-api/blob/main/src/usecases/Devis/DeleteDevis/DeleteDevisController.ts"
    click K "https://github.com/githubstuf/gestion-comptable-api/blob/main/src/usecases/Marche/CreateMarche/CreateMarcheController.ts"
    click L "https://github.com/githubstuf/gestion-comptable-api/blob/main/src/usecases/Marche/DeleteMarche/DeleteMarcheController.ts"
    click M "https://github.com/githubstuf/gestion-comptable-api/tree/main/src/usecases/Client"
    click N "https://github.com/githubstuf/gestion-comptable-api/tree/main/src/usecases/Devis"
    click O "https://github.com/githubstuf/gestion-comptable-api/tree/main/src/usecases/Marche"
    click P "https://github.com/githubstuf/gestion-comptable-api/tree/main/src/dtos"
    click Q "https://github.com/githubstuf/gestion-comptable-api/tree/main/src/repositories"
    click R "https://github.com/githubstuf/gestion-comptable-api/tree/main/src/repositories/implementations/in-memory"
    click S "https://github.com/githubstuf/gestion-comptable-api/tree/main/src/repositories/implementations/prisma"
    click T "https://github.com/githubstuf/gestion-comptable-api/blob/main/src/libs/PrismaClient.ts"
    click U "https://github.com/githubstuf/gestion-comptable-api/tree/main/prisma"
    click V "https://github.com/githubstuf/gestion-comptable-api/tree/main/src/helpers"
```
