# sloweyyy/trustify-backend architecture

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

- Interactive diagram: https://gitdiagram.com/sloweyyy/trustify-backend
- Repository: https://github.com/sloweyyy/trustify-backend

## Overview

##### Project Type and Purpose
- Trustify-Backend is a **monolithic backend API** service built with Node.js and Express.
- Its core purpose is to handle user requests for document notarization, integrate with blockchain/NFT storage, manage payments, send emails, and store documents in Firebase.

##### File Structure to Extract Architectural Layers
- **Entry Point & Configuration**:
- `src/index.js` / `src/app.js`
- `src/config/*.js` (database, Firebase, blockchain, authentication, logging)
- **API Layer**:
- `src/routes/v1/*.route.js` → express routers
- `src/controllers/*.controller.js` → HTTP request handlers
- Swagger docs under `src/docs`
- **Business / Service Layer**:
- `src/services/*.service.js` → core business logic (notarization, payments, emails, chatbots, cron jobs)
- **Data Access Layer**:
- `src/models/*.model.js` → Mongoose schemas and plugins under `src/models/plugins`
- **Middleware & Utilities**:
- `src/middlewares/*.js` → request parsing, auth, rate limiting, error handling
- `src/utils/*.js` → error classes, async wrappers, picker functions
- **Static Assets & Email Templates**:
- `src/public` → static HTML/CSS/JS and email templates
- **Testing & CI/CD**:
- Jest tests under `tests/*`, GitHub Actions, Travis CI, Dockerfiles, Jenkinsfile, buildspec.yml

##### External Dependencies (from README and Config)
- **Database**: MongoDB via Mongoose
- **Storage**: Firebase Cloud Storage
- **Blockchain**: Solana devnet (and NFT/IPFS via Pinata, Bundlr)
- **Authentication**: JWT, Passport.js + Google OAuth
- **Email**: SMTP (Ethereal or real providers)
- **Payments**: PayOS
- **QR Codes**: VietQR API
- **AI Documents**: Gemini API
- **Deployment & Containerization**: Docker, PM2, Railway

##### Main Components & Their Relationships
a. API Gateway (Express Server)
- Accepts client requests (REST calls or webhooks)
- Passes requests through middlewares (auth, validation, rate-limit, logging)
- Routes to controllers
b. Controllers
- Orchestrate input validation → call service methods → format responses
c. Services
- Implement business rules:
- Notarization flows (upload, request, track status)
- Payment processing (PayOS integration)
- Email notifications (SMTP templates)
- AI doc generation (Gemini)
- Chatbot interactions
- Cron jobs for cleanup or retries
d. Data Layer
- Mongoose models handle persistence in MongoDB
- Pagination, toJSON, and other plugins
e. External Systems
- **MongoDB** (primary document store)
- **Firebase** (binary document storage)
- **Blockchain/NFT** (Solana program via web3, IPFS/Pinata, Bundlr)
- **SMTP server**
- **OAuth provider** (Google)
- **Gemini API**, **VietQR API**, **PayOS API**

##### Architectural Patterns & Principles
- **Layered Architecture (MVC-style)**
- **Dependency Injection** via configuration modules
- **Plugin Pattern** for Mongoose (paginate, toJSON)
- **Separation of Concerns**: controllers vs services vs models
- **External Integration Adapters** keep 3rd-party logic isolated in services

##### Putting It All Together
- Start your whiteboard or diagramming tool with the **Client** (browser, mobile) calling REST endpoints.
- Inside, split horizontally into Controllers → Services → Models.
- **Mongoose** connecting to **MongoDB**.
- Optionally, show CI/CD workflow on a side panel.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "CI/CD Pipeline"
        GH["GitHub Actions"]:::ci
        Docker["Docker Build"]:::ci
        Deploy["Railway / PM2"]:::ci
        GH --> Docker --> Deploy
    end

    Client["Client (Browser/Mobile)"]:::client

    subgraph "API / Web Layer"
        Index["src/index.js"]:::api
        AppJs["src/app.js"]:::api
        Config["Config Modules\n(src/config)"]:::config
        MWAuth["Auth Middleware"]:::middleware
        MWVal["Validation Middleware"]:::middleware
        MRate["RateLimiter Middleware"]:::middleware
        MWLog["Logging Middleware"]:::middleware
        Router["Express Router\n(src/routes/v1)"]:::api
        Docs["Swagger Docs\n(src/docs)"]:::api
        Public["Static Assets\n(src/public)"]:::public
    end

    subgraph "Controllers"
        Controllers["Controllers\n(src/controllers)"]:::controller
    end

    subgraph "Business / Service Layer"
        AuthService["auth.service.js"]:::service
        PayService["payment.service.js"]:::service
        NotaService["notarization.service.js"]:::service
        EmailService["email.service.js"]:::service
        ChatService["chatbot.service.js"]:::service
        CronService["cron.service.js"]:::service
    end

    subgraph "Data / Persistence Layer"
        Models["Mongoose Models\n(src/models)"]:::db
        Plugins["Plugins\n(src/models/plugins)"]:::db
    end

    subgraph "External Systems"
        Mongo["MongoDB Atlas"]:::external
        Firebase["Firebase Storage"]:::external
        Blockchain["Solana / IPFS"]:::external
        SMTP["SMTP Server"]:::external
        OAuth["Google OAuth"]:::external
        Gemini["Gemini API"]:::external
        VietQR["VietQR API"]:::external
        PayOS["PayOS API"]:::external
    end

    Deploy --> Client
    Client --> Index
    Index --> AppJs
    AppJs --> Config
    AppJs --> MWAuth --> Router
    AppJs --> MWVal --> Router
    AppJs --> MRate --> Router
    AppJs --> MWLog --> Router
    AppJs --> Public
    AppJs --> Docs
    Router --> Controllers
    Controllers --> AuthService
    Controllers --> PayService
    Controllers --> NotaService
    Controllers --> EmailService
    Controllers --> ChatService
    Controllers --> CronService

    AuthService --> Models
    AuthService --> OAuth
    PayService --> Models
    PayService --> PayOS
    NotaService --> Models
    NotaService --> Firebase
    NotaService --> Blockchain
    EmailService --> SMTP
    ChatService --> Gemini
    CronService --> Firebase
    CronService --> SMTP

    Models --> Plugins
    Models --> Mongo

    classDef ci fill:#f9f,stroke:#333;
    classDef client fill:#bbf,stroke:#333;
    classDef api fill:#88f,stroke:#333;
    classDef middleware fill:#8fb,stroke:#333;
    classDef controller fill:#8bf,stroke:#333;
    classDef service fill:#48f,stroke:#333;
    classDef db fill:#4f8,stroke:#333;
    classDef external fill:#f84,stroke:#333;
    classDef public fill:#fa8,stroke:#333;
    classDef config fill:#fc8,stroke:#333;

    click Index "https://github.com/sloweyyy/trustify-backend/blob/main/src/index.js"
    click AppJs "https://github.com/sloweyyy/trustify-backend/blob/main/src/app.js"
    click Config "https://github.com/sloweyyy/trustify-backend/blob/main/src/config/config.js"
    click Router "https://github.com/sloweyyy/trustify-backend/tree/main/src/routes/v1/"
    click Controllers "https://github.com/sloweyyy/trustify-backend/tree/main/src/controllers/"
    click AuthService "https://github.com/sloweyyy/trustify-backend/blob/main/src/services/auth.service.js"
    click PayService "https://github.com/sloweyyy/trustify-backend/blob/main/src/services/payment.service.js"
    click NotaService "https://github.com/sloweyyy/trustify-backend/blob/main/src/services/notarization.service.js"
    click EmailService "https://github.com/sloweyyy/trustify-backend/blob/main/src/services/email.service.js"
    click ChatService "https://github.com/sloweyyy/trustify-backend/blob/main/src/services/chatbot.service.js"
    click CronService "https://github.com/sloweyyy/trustify-backend/blob/main/src/services/cron.service.js"
    click Models "https://github.com/sloweyyy/trustify-backend/tree/main/src/models/"
    click Plugins "https://github.com/sloweyyy/trustify-backend/tree/main/src/models/plugins/"
    click Public "https://github.com/sloweyyy/trustify-backend/tree/main/src/public/"
    click Docs "https://github.com/sloweyyy/trustify-backend/blob/main/src/docs/swaggerDef.js"
    click GH "https://github.com/sloweyyy/trustify-backend/blob/main/.github/workflows/nodejs-ci.yml"
    click Docker "https://github.com/sloweyyy/trustify-backend/tree/main/Dockerfile"
```
