# cosmicpython/code architecture

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

- Interactive diagram: https://gitdiagram.com/cosmicpython/code
- Repository: https://github.com/cosmicpython/code

## Overview

##### Main Architectural Layers

a) Entrypoints (Interface Layer)
- Flask API (flask_app.py)
- Redis Event Consumer (redis_eventconsumer.py)

b) Service Layer
- Below entrypoints
- Message Bus (messagebus.py)
- Command/Event Handlers (handlers.py)
- Unit of Work (unit_of_work.py)

c) Domain Layer
- At the core
- Model (model.py)
- Commands and Events (commands.py, events.py)

d) Adapters Layer
- On the right side
- Repository
- ORM
- Redis Event Publisher
- Notifications

##### Component Relationships

- HTTP requests (e.g., blue)
- Event flows (e.g., orange)
- Database operations (e.g., green)

##### External Systems
- Redis (for event publishing/consuming)
- Database (referenced in ORM)
- Email/Notification System

##### Specific Elements

**┌─────────────────┐**
- Component Name  │
- ┤
- Key Functions   │
- ┘

##### Key Flows to Highlight

a) Primary Flow:
HTTP Request → Flask App → Message Bus → Handlers → Domain Model → Repository → Database

b) Event Flow:
Domain Events → Redis Publisher → Redis → Redis Consumer → Message Bus

##### Additional Details

- Component types (Entrypoint, Service, Domain, Adapter)

##### Testing Layer (Optional)
Adding a testing layer showing:
- E2E Tests
- Integration Tests
- Unit Tests

Key Architectural Patterns to Emphasize:
- Event-Driven Architecture (Redis events)
- Repository Pattern (data access)
- Unit of Work Pattern (transaction management)

Remember to:
##### Version/date information

This architecture represents a modern, clean architecture implementation with clear separation of concerns, following DDD principles, and incorporating both synchronous (HTTP) and asynchronous (events) processing patterns.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles and Classes
    classDef entrypoint fill:#a8e6cf,stroke:#3b6978,stroke-width:2px
    classDef service fill:#dcedc1,stroke:#3b6978,stroke-width:2px
    classDef domain fill:#ffd3b6,stroke:#3b6978,stroke-width:2px
    classDef adapter fill:#ffaaa5,stroke:#3b6978,stroke-width:2px
    classDef external fill:#e5e5e5,stroke:#3b6978,stroke-width:2px
    classDef testing fill:#d4a5ff,stroke:#3b6978,stroke-width:2px

    %% External Systems
    subgraph External["External Systems"]
        Redis[(Redis)]:::external
        DB[(Database)]:::external
        Email[Email System]:::external
    end

    %% Entrypoints Layer
    subgraph Entrypoints["Entrypoints Layer"]
        FlaskAPI[Flask API]:::entrypoint
        RedisConsumer[Redis Event Consumer]:::entrypoint
    end

    %% Service Layer
    subgraph ServiceLayer["Service Layer"]
        MessageBus[Message Bus]:::service
        Handlers[Command/Event Handlers]:::service
        UoW[Unit of Work]:::service
    end

    %% Domain Layer
    subgraph DomainLayer["Domain Layer"]
        Model[Domain Model]:::domain
        Commands[Commands]:::domain
        Events[Events]:::domain
    end

    %% Adapters Layer
    subgraph AdaptersLayer["Adapters Layer"]
        Repository[Repository]:::adapter
        ORM[ORM]:::adapter
        RedisPublisher[Redis Event Publisher]:::adapter
        Notifications[Notifications]:::adapter
    end

    %% Testing Layer
    subgraph TestingLayer["Testing Layer"]
        E2ETests[E2E Tests]:::testing
        IntegrationTests[Integration Tests]:::testing
        UnitTests[Unit Tests]:::testing
    end

    %% Relationships
    FlaskAPI --> MessageBus
    RedisConsumer --> MessageBus
    MessageBus --> Handlers
    Handlers --> Model
    Model --> Events
    Events --> RedisPublisher
    RedisPublisher --> Redis
    Redis --> RedisConsumer
    Repository --> DB
    ORM --> DB
    Handlers --> Repository
    UoW --> Repository
    Notifications --> Email
    Model --> Commands

    %% Click Events
    click FlaskAPI "https://github.com/cosmicpython/code/blob/master/src/allocation/entrypoints/flask_app.py"
    click RedisConsumer "https://github.com/cosmicpython/code/blob/master/src/allocation/entrypoints/redis_eventconsumer.py"
    click MessageBus "https://github.com/cosmicpython/code/blob/master/src/allocation/service_layer/messagebus.py"
    click Handlers "https://github.com/cosmicpython/code/blob/master/src/allocation/service_layer/handlers.py"
    click UoW "https://github.com/cosmicpython/code/blob/master/src/allocation/service_layer/unit_of_work.py"
    click Model "https://github.com/cosmicpython/code/blob/master/src/allocation/domain/model.py"
    click Commands "https://github.com/cosmicpython/code/blob/master/src/allocation/domain/commands.py"
    click Events "https://github.com/cosmicpython/code/blob/master/src/allocation/domain/events.py"
    click Repository "https://github.com/cosmicpython/code/blob/master/src/allocation/adapters/repository.py"
    click ORM "https://github.com/cosmicpython/code/blob/master/src/allocation/adapters/orm.py"
    click RedisPublisher "https://github.com/cosmicpython/code/blob/master/src/allocation/adapters/redis_eventpublisher.py"
    click Notifications "https://github.com/cosmicpython/code/blob/master/src/allocation/adapters/notifications.py"
    click E2ETests "https://github.com/cosmicpython/code/tree/master/tests/e2e/"
    click IntegrationTests "https://github.com/cosmicpython/code/tree/master/tests/integration/"
    click UnitTests "https://github.com/cosmicpython/code/tree/master/tests/unit/"

    %% Legend
    subgraph Legend
        L1[Entrypoint]:::entrypoint
        L2[Service]:::service
        L3[Domain]:::domain
        L4[Adapter]:::adapter
        L5[External]:::external
        L6[Testing]:::testing
    end
```
