# davidchavess/clean-architecture-ride-api architecture

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

- Interactive diagram: https://gitdiagram.com/davidchavess/clean-architecture-ride-api
- Repository: https://github.com/davidchavess/clean-architecture-ride-api

## Overview

Based on both the file tree and the README, the project is a multi-service, full-stack API backend for a ride-hailing application—akin to services like Uber or 99. The project consists of four main APIs (account, payment, position, ride) that each follow Clean Architecture principles and Hexagonal Architecture (also known as Ports and Adapters). They are implemented using Node.js, leveraging tools like Express for HTTP, PostgreSQL and MongoDB for persistence, and RabbitMQ for message/event communication. The system also applies several design patterns (e.g., Factory, Adapter, Dependency Injection) and adheres to SOLID principles, TDD, and DDD.

##### Main components
- Account Service: Handles user signup and account management.
- Ride Service: Manages ride requests, acceptance, updates, and finishes.
- Payment Service: Processes payments for rides.
- Service: Updates and retrieves driver or ride positions.
- Shared or external components:
- Database systems (PostgreSQL for relational data in account, payment, and position services; MongoDB primarily in ride service for certain data types).
- Messaging/Event system: RabbitMQ (as seen in event adapters and connection components) to handle inter-service communication.
- External HTTP interfaces with Express-adapters serving as the entry point for API calls.
- Gateways: Components acting as external integrations (for example, Account Gateway HTTP in the ride service).

##### Relationships and interactions
- For instance, the Ride Service interacts with the Account Service (through a gateway) to obtain account details or verify user identity.
- The Payment Service can be triggered by a message/event from the Ride Service for processing payments.
- The Position Service interacts with the Ride Service via event-driven updates (and vice versa).
- That communication between services occurs both synchronously (HTTP API calls) and asynchronously (events via RabbitMQ).

##### Architectural patterns and design principles
- Separate the inner layers (Application, Domain) that contain business logic from the outer layers (Infrastructure, HTTP, Database, Event) that deal with external systems and frameworks.
- Separation of concerns (e.g., use cases vs. domain entities vs. infrastructure adapters) inside each service.
- Dependency injection and composition roots (e.g., the “Registry.ts” files in each service) that wire the components together.

##### Elements
- Provide clear labels for each microservice and its internal layers (e.g., Domain, Application, Infrastructure) if space allows.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Account Service Subgraph
    subgraph "Account Service"
        A_acc_reg["Registry"]:::registry
        A_acc_http["HTTP Adapter"]:::http
        A_acc_domain["Domain & Use Cases"]:::domain
        A_acc_db["PostgreSQL Adapter"]:::database
        A_acc_reg --> A_acc_http
        A_acc_http --> A_acc_domain
        A_acc_domain --> A_acc_db
    end

    %% Ride Service Subgraph
    subgraph "Ride Service"
        R_reg["Registry"]:::registry
        R_http["HTTP Adapter"]:::http
        R_domain["Domain & Use Cases"]:::domain
        R_gateway["Account Gateway HTTP"]:::gateway
        R_mediator["Event Mediator"]:::mediator
        R_event["RabbitMQ Event Adapter"]:::event
        R_mongo["MongoDB Adapter"]:::database
        R_pg["PostgreSQL Adapter"]:::database
        R_reg --> R_http
        R_http --> R_domain
        R_domain --> R_gateway
        R_domain --> R_mediator
        R_domain --> R_event
        R_domain --> R_mongo
        R_domain --> R_pg
    end

    %% Payment Service Subgraph
    subgraph "Payment Service"
        P_reg["Registry"]:::registry
        P_http["HTTP Adapter"]:::http
        P_domain["Domain & Use Cases"]:::domain
        P_db["PostgreSQL Adapter"]:::database
        P_event["RabbitMQ Event Adapter"]:::event
        P_reg --> P_http
        P_http --> P_domain
        P_domain --> P_db
        P_domain --> P_event
    end

    %% Position Service Subgraph
    subgraph "Position Service"
        Pos_reg["Registry"]:::registry
        Pos_http["HTTP Adapter"]:::http
        Pos_domain["Domain & Use Cases"]:::domain
        Pos_db["PostgreSQL Adapter"]:::database
        Pos_event["RabbitMQ Event Adapter"]:::event
        Pos_reg --> Pos_http
        Pos_http --> Pos_domain
        Pos_domain --> Pos_db
        Pos_domain --> Pos_event
    end

    %% External Infrastructure Nodes
    PG_db["PostgreSQL Database"]:::extdb
    Mongo_db["MongoDB Database"]:::extdb
    RabbitMQ["RabbitMQ"]:::extmsg

    %% Connect Service Database Adapters to External Databases
    A_acc_db --> PG_db
    R_pg --> PG_db
    P_db --> PG_db
    Pos_db --> PG_db
    R_mongo --> Mongo_db

    %% Connect Event Adapters to RabbitMQ
    R_event --> RabbitMQ
    P_event --> RabbitMQ
    Pos_event --> RabbitMQ

    %% Inter-service Communications
    %% Ride Service communicates with Account Service via HTTP gateway
    R_gateway -->|"HTTP"| A_acc_http

    %% Ride Service triggers Payment Service via asynchronous events
    R_mediator -->|"async"| P_event

    %% Ride and Position communicate via asynchronous events
    R_mediator -->|"async"| Pos_event
    R_event <-->|"async"| Pos_event

    %% Click Events for Account Service
    click A_acc_reg "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/account/src/Registry.ts"
    click A_acc_http "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/account/src/infra/http/ExpressAdapter.ts"
    click A_acc_db "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/account/src/infra/database/PostgresDataBase.ts"

    %% Click Events for Ride Service
    click R_reg "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/ride/src/Registry.ts"
    click R_http "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/ride/src/infra/http/ExpressAdapter.ts"
    click R_mongo "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/ride/src/infra/database/MongoDataBase.ts"
    click R_pg "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/ride/src/infra/database/PostgresDataBase.ts"
    click R_gateway "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/ride/src/infra/gateway/AccountGatewayHttp.ts"
    click R_mediator "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/ride/src/infra/mediator/Mediator.ts"
    click R_event "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/ride/src/infra/event/RabbitMqEventAdapter.ts"

    %% Click Events for Payment Service
    click P_reg "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/payment/src/Registry.ts"
    click P_http "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/payment/src/infra/http/ExpressAdapter.ts"
    click P_db "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/payment/src/infra/database/PostgresDataBase.ts"
    click P_event "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/payment/src/infra/event/connection/RabbitMqEventAdapter.ts"

    %% Click Events for Position Service
    click Pos_reg "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/position/src/Registry.ts"
    click Pos_http "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/position/src/infra/http/ExpressAdapter.ts"
    click Pos_db "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/position/src/infra/database/PostgresDataBase.ts"
    click Pos_event "https://github.com/davidchavess/clean-architecture-ride-api/blob/main/backend/position/src/infra/event/RabbitMqEventAdapter.ts"

    %% Style Definitions
    classDef registry fill:#FFCCFF,stroke:#333,stroke-width:2px;
    classDef http fill:#BBD1F6,stroke:#333,stroke-width:2px;
    classDef domain fill:#C3F6B2,stroke:#333,stroke-width:2px;
    classDef database fill:#FFE699,stroke:#333,stroke-width:2px;
    classDef event fill:#F6B2C3,stroke:#333,stroke-width:2px;
    classDef mediator fill:#F6D6B2,stroke:#333,stroke-width:2px;
    classDef gateway fill:#D6B2F6,stroke:#333,stroke-width:2px;
    classDef extdb fill:#E7E7E7,stroke:#333,stroke-dasharray: 5,5,stroke-width:2px;
    classDef extmsg fill:#E7E7E7,stroke:#333,stroke-dasharray: 5,5,stroke-width:2px;
```
