# thaleprechaun/agric-invest architecture

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

- Interactive diagram: https://gitdiagram.com/thaleprechaun/agric-invest
- Repository: https://github.com/thaleprechaun/agric-invest

## Overview

##### Project type and purpose
- This is a full-stack TypeScript application combining an Express.js server (with both REST and GraphQL endpoints) and a React front end.
- The server exposes `/api` REST routes and a GraphQL schema, handles authentication via JWT, and persists data in MongoDB.
- The React client consumes those APIs, uses Redux for state management, and employs Axios for HTTP/GraphQL calls.

##### File structure
- Top-level: CI/CD configs (`.circleci/`, `Jenkinsfile`), lint/format configs, `package.json`, `tsconfig.json`.
- `src/`: server code—`app.ts` bootstrap, `routes/`, `controllers/`, `models/`, `middleware/`, `helpers/`, `schema.ts` (GraphQL), `types/`.
- `scripts/`: auxiliary scripts (e.g., `mongo-setup.ts`).
- `client/`: React app—`public/`, `src/` (components, pages, redux, axios wrapper, common utilities), its own configs.
- Test suites: `__tests__/` for unit tests, `cypress/` for end-to-end tests, `jest.config.js`.

##### README
- Confirms dual start process (`yarn start` server, then client).
- Describes server’s REST and GraphQL interfaces and client’s ability to consume either.

##### Key components
a. Frontend (React/TypeScript)
– Pages & components layer (pages → components → common UI).
– Redux store and actions/reducers.
– HTTP client (Axios) and GraphQL client.
b. Backend (Express/TypeScript)
– Entry point (`app.ts`, `bin/www`).
– Middleware (CORS, logging, JWT verification: `verifyUserToken`, `verifyAdminToken`).
– Routing layer (`routes/*`).
– Controller layer (`controllers/*`).
– Data layer (Mongoose models in `models/*`).
– GraphQL schema (`schema.ts`) and resolver functions.
c. Database
– MongoDB cluster or local instance (initialized via `scripts/mongo-setup.ts`).
d. External/infrastructure
– CI/CD (CircleCI, Jenkins).
– Testing frameworks (Jest, Cypress).
– Deployment shell scripts (`scripts/build.sh`).

##### Relationships and interactions
- The React UI dispatches Redux actions that invoke Axios or GraphQL requests → Express routes.
- Express routes call controller methods → interact with Mongoose models → read/write MongoDB.
- GraphQL queries/mutations map to resolvers that likewise call models.
- Middleware intercepts requests for authentication/authorization.
- CI/CD pipelines pull code → run tests → build artifacts → deploy.

##### Architectural patterns and design principles
- Layered architecture: Presentation (UI) → API (routes/controllers) → Persistence (models/DB).
- Separation of concerns: UI components vs. state (Redux) vs. data‐access models.
- Modularization: each feature (farms, investments, users) has its own routes/controllers/models.
- Use of middleware for cross-cutting concerns (auth, validation).
- Dual-API strategy: REST + GraphQL coexisting behind the same Express server.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Side
    subgraph "Client" 
        direction TB
        UI["React UI (Pages & Components)"]:::client
        Entry["React Entry Point"]:::client
        Boundary["Error Boundary"]:::client
        Store["Redux Store & State Management"]:::client
        HTTP["HTTP Client (Axios)"]:::client
        GQLClient["GraphQL Client"]:::client
    end

    %% Server Side
    subgraph "Server"
        direction TB
        App["Express Server Entry Point"]:::server
        Middleware["Authentication Middleware"]:::server
        RESTRoutes["REST Routing Layer"]:::server
        Controllers["Controllers (Business Logic)"]:::server
        Models["Data Models (Mongoose Schemas)"]:::server
        Schema["GraphQL Schema & Resolvers"]:::server
        Types["Type Definitions"]:::server
        Helpers["Input Validation Helpers"]:::server
    end

    %% Database
    DB["MongoDB"]:::db

    %% Infrastructure
    subgraph "Infrastructure & CI/CD"
        direction TB
        CICD["CI/CD Configurations"]:::infra
        BuildScript["Build / Deployment Script"]:::infra
        MongoSetup["MongoDB Setup Script"]:::infra
        UnitTests["Unit Tests (Jest)"]:::infra
        E2E["End-to-End Tests (Cypress)"]:::infra
    end

    %% Connections Client -> Server
    UI -->|dispatches actions| Store
    Store -->|uses| HTTP
    Store -->|uses| GQLClient
    HTTP -->|calls REST API| RESTRoutes
    GQLClient -->|calls GraphQL| Schema

    %% Server Flow REST
    App -->|uses| Middleware
    Middleware -->|protects| RESTRoutes
    RESTRoutes -->|invokes| Controllers
    Controllers -->|accesses| Models
    Models -->|reads/writes| DB

    %% Server Flow GraphQL
    App -->|serves| Schema
    Schema -->|resolves| Controllers
    Controllers -->|accesses| Models

    %% Helpers & Types
    Controllers -->|validates| Helpers
    Controllers -->|uses| Types

    %% Infrastructure Connections
    CICD -.-> App
    CICD -.-> UI
    CICD -->|runs| UnitTests
    CICD -->|runs| E2E
    BuildScript -.-> App
    MongoSetup --> DB

    %% Click Events Client
    click UI "https://github.com/thaleprechaun/agric-invest/tree/master/client/src/pages"
    click UI "https://github.com/thaleprechaun/agric-invest/tree/master/client/src/components"
    click Entry "https://github.com/thaleprechaun/agric-invest/blob/master/client/src/index.tsx"
    click Entry "https://github.com/thaleprechaun/agric-invest/blob/master/client/src/App.tsx"
    click Boundary "https://github.com/thaleprechaun/agric-invest/blob/master/client/src/common/Boundary/SuspenseBoundary.tsx"
    click Store "https://github.com/thaleprechaun/agric-invest/blob/master/client/src/store.ts"
    click Store "https://github.com/thaleprechaun/agric-invest/tree/master/client/src/redux/actions"
    click Store "https://github.com/thaleprechaun/agric-invest/tree/master/client/src/redux/reducers"
    click HTTP "https://github.com/thaleprechaun/agric-invest/blob/master/client/src/axios/index.ts"

    %% Click Events Server
    click App "https://github.com/thaleprechaun/agric-invest/blob/master/src/app.ts"
    click App "https://github.com/thaleprechaun/agric-invest/tree/master/bin/www"
    click Middleware "https://github.com/thaleprechaun/agric-invest/blob/master/src/middleware/verifyUserToken.ts"
    click Middleware "https://github.com/thaleprechaun/agric-invest/blob/master/src/middleware/verifyAdminToken.ts"
    click RESTRoutes "https://github.com/thaleprechaun/agric-invest/tree/master/src/routes"
    click Controllers "https://github.com/thaleprechaun/agric-invest/tree/master/src/controllers"
    click Models "https://github.com/thaleprechaun/agric-invest/tree/master/src/models"
    click Schema "https://github.com/thaleprechaun/agric-invest/blob/master/src/schema.ts"
    click Types "https://github.com/thaleprechaun/agric-invest/blob/master/src/types/sample.ts"
    click Helpers "https://github.com/thaleprechaun/agric-invest/blob/master/src/helpers/validation.ts"

    %% Click Events Infrastructure
    click MongoSetup "https://github.com/thaleprechaun/agric-invest/blob/master/scripts/mongo-setup.ts"
    click BuildScript "https://github.com/thaleprechaun/agric-invest/blob/master/scripts/build.sh"
    click CICD "https://github.com/thaleprechaun/agric-invest/blob/master/.circleci/config.yml"
    click CICD "https://github.com/thaleprechaun/agric-invest/tree/master/Jenkinsfile"
    click UnitTests "https://github.com/thaleprechaun/agric-invest/blob/master/__tests__/users.ts"
    click UnitTests "https://github.com/thaleprechaun/agric-invest/blob/master/jest.config.js"
    click E2E "https://github.com/thaleprechaun/agric-invest/tree/master/cypress"
    click E2E "https://github.com/thaleprechaun/agric-invest/blob/master/cypress.json"

    %% Styles
    classDef client fill:#cce5ff,stroke:#3399ff,stroke-width:1px
    classDef server fill:#d4edda,stroke:#28a745,stroke-width:1px
    classDef db fill:#fff3cd,stroke:#ffcd39,stroke-width:1px
    classDef infra fill:#e2e3e5,stroke:#6c757d,stroke-width:1px
```
