# noharm-ai/backend architecture

> Architecture diagram of the GitHub repository noharm-ai/backend, 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/noharm-ai/backend
- Repository: https://github.com/noharm-ai/backend

## Overview

##### Main Components
- Mobile Client: Although not directly in the code base, the README references “Api for Mobile App”. Represent that as an external mobile client that calls the API.
- API Layer (Routes): The “routes” directory contains many subdirectories — including admin endpoints, authentication, and other public endpoints. This layer is the entry point for HTTP requests.
- Controller/Decorator/Security Layer: The “decorators” and “security” directories indicate that incoming requests have additional processing such as permission checks and logging/timing.
- Service Layer: The “services” folder is extensive and includes various business logic modules (e.g., drug services, alert services, intervention, reports, etc.). This layer processes application logic and orchestrates operations.
- Data Access/Repository Layer: The “repository” directory abstracts the data persistence. Although actual database connections/files aren’t present, repositories indicate integration with a database.
- Models: The “models” directory holds business object definitions, enums, and request data structures.
- Configuration & Exception Handling: ‘config’ and ‘exception’ folders provide configuration settings and error management across the system.
- Testing & Utilities: The “tests” folder and “utils” folder contain support for automated testing and common/shared functionality.
- Deployment & CI/CD: GitHub workflows and zappa_settings.json indicate automated deployment and serverless deployment configurations.

##### Depicting Relationships and Data Flow
- That the Routes layer hands off processing to the appropriate Service Layer modules. Indicate separate services for admin functions, alerts, drug handling, and more.
- Complement this flow with supportive functions (e.g., caching, email, third-party integrations) and note the usage of configuration and error handling components.
- Optionally, include the CI/CD pipelines (from the .github/workflows files) as separate blocks that highlight the deployment, build, and test processes.

##### Architectural Patterns and Design Principles
- Layered Architecture / Separation of Concerns: Separate the API (routes), business logic (services), and data access (repositories).
- Decorator Pattern: Emphasize how decorators are used to handle cross-cutting concerns like authentication and timing.
- Modular Organization: Note that functionalities are grouped by domain (admin, reports, regulation, etc.), supporting maintainability and extensibility.
- Testing: The presence of comprehensive test suites is an important element to include as part of the CI/CD pipeline and overall system quality assurance.

##### Relevant Technologies, Frameworks, or Libraries
- Python as the core language.
- Flask (or a similar lightweight web framework) is implied by the typical “routes” and “mobile.py” entry-point usage.
- Integration with external libraries for database connectivity (e.g., MySQL, PostgreSQL as inferred from troubleshooting tips), caching (cache_service), and email utilities.
- The CI workflows through GitHub Actions indicate automated builds and deployments.
- Zappa deployment settings point to possible serverless or AWS Lambda integration.

- Each major component should be clearly labeled (e.g., Mobile Client, API/Routes, Decorators, Services, Repositories, Configuration/Exception, External Database, and CI/CD Pipeline).
– From Mobile Client to API Layer.
– From API Layer to Service Layer.
– From Service Layer to Repository Layer.
– From Repository Layer to External Database.
– API endpoints (perhaps as rectangles),
- Annotations where necessary to explain the purpose of key modules (for example, “Decorators: Managing permissions and timing”).
- Deployment workflows (GitHub Actions and Zappa) perhaps using a sidebar or separate section, showing how code moves from commit to deployment.
- Don’t be afraid to break out components even further if that adds clarity—each service or major domain (admin, reports, regulation) can be represented as subcomponents within the business logic layer.

## Diagram (Mermaid)

```mermaid
flowchart TD
    MC["Mobile Client"]:::external
    EP["Entry Point (mobile.py)"]:::entry
    AR["API Routes (routes/)"]:::routes
    subgraph CDS["Controller/Decorator/Security"]
        DEC["Decorators (decorators/)"]:::controller
        SEC["Security (security/)"]:::controller
    end
    SL["Service Layer (services/)"]:::service
    RL["Repository Layer (repository/)"]:::repository
    DB["External Database"]:::db
    MD["Models (models/)"]:::models
    CF["Configuration (config.py & config/)"]:::config
    EX["Exception Handling (exception/)"]:::config
    TS["Tests (tests/)"]:::testing
    UT["Utilities (utils/)"]:::testing
    CI["CI/CD Pipeline (.github/workflows/ & zappa_settings.json)"]:::cidcd

    MC -->|"calls"| EP
    EP -->|"routesRequest"| AR
    AR -->|"passesThrough"| DEC
    AR -->|"passesThrough"| SEC
    DEC -->|"delegates"| SL
    SEC -->|"delegates"| SL
    SL -->|"invokes"| RL
    RL -->|"queries"| DB
    SL -->|"uses"| MD

    AR -.->|"configures"| CF
    AR -.->|"handlesException"| EX
    SL -.->|"configures"| CF
    RL -.->|"configures"| CF

    CI -.->|"deploys"| EP
    CI -.->|"triggersTests"| TS
    CI -.->|"supports"| UT

    TS -.->|"tests"| SL
    UT -.->|"supports"| SL

    click EP "https://github.com/noharm-ai/backend/blob/master/mobile.py"
    click AR "https://github.com/noharm-ai/backend/tree/master/routes/"
    click DEC "https://github.com/noharm-ai/backend/tree/master/decorators/"
    click SEC "https://github.com/noharm-ai/backend/tree/master/security/"
    click SL "https://github.com/noharm-ai/backend/tree/master/services/"
    click RL "https://github.com/noharm-ai/backend/tree/master/repository/"
    click MD "https://github.com/noharm-ai/backend/tree/master/models/"
    click CF "https://github.com/noharm-ai/backend/tree/master/config/"
    click EX "https://github.com/noharm-ai/backend/tree/master/exception/"
    click TS "https://github.com/noharm-ai/backend/tree/master/tests/"
    click UT "https://github.com/noharm-ai/backend/tree/master/utils/"
    click CI "https://github.com/noharm-ai/backend/tree/master/.github/workflows/"

    classDef external fill:#ffebcd,stroke:#333,stroke-width:2px;
    classDef entry fill:#ffa07a,stroke:#333,stroke-width:2px;
    classDef routes fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef controller fill:#90ee90,stroke:#333,stroke-width:2px;
    classDef service fill:#ffcc00,stroke:#333,stroke-width:2px;
    classDef repository fill:#ff9999,stroke:#333,stroke-width:2px;
    classDef db fill:#ffe4c4,stroke:#333,stroke-width:2px;
    classDef models fill:#d3d3d3,stroke:#333,stroke-width:2px;
    classDef config fill:#f4a460,stroke:#333,stroke-width:2px;
    classDef testing fill:#dda0dd,stroke:#333,stroke-width:2px;
    classDef cidcd fill:#87ceeb,stroke:#333,stroke-width:2px;
```
