# knightempire/lab-backend architecture

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

- Interactive diagram: https://gitdiagram.com/knightempire/lab-backend
- Repository: https://github.com/knightempire/lab-backend

## Overview

##### Project type and purpose
- This is a Node.js-based backend service (“lab-backend”), built with Express.js.
- It exposes RESTful endpoints for lab asset management (products, damaged items, requests, re-issuance, returns, notifications, user/admin profiles).
- It’s designed to run in Docker (Dockerfile + docker-compose), connects to a database, integrates with Google Sheets and an email service, and uses JWT/RSA for auth.

##### File structure
Top-level components:
- app.js – Express app entrypoint
- config/db.js – database connection setup
- controllers/ – business logic grouped by domain (admin, damaged, login, etc.)
- routers/ – Express route definitions that map URLs → controllers
- models/ – ORM/Mongoose schemas for each domain object
- middleware/
– auth/ (tokencreation.js, tokenvalidate.js)
– mail/ (templating, mail.js)
– googlesheet.js (integration with Google Sheets API)
– rsa/ (key management for JWT or encryption)
- docker-compose.yml & Dockerfile – defines service containers (app + DB)
- .github/workflows – CI pipeline

Architectural patterns:
- MVC-like layering: Routers → Controllers → Models
- Middleware pipeline for cross-cutting concerns (auth, mail, encryption)
- Containerized deployment

##### README
- Minimal – only project name (“lab-backend”).

##### Diagramming recommendations
– Blue for API/Middleware
– Green for Business Logic
– Orange for Data Layer
– Gray for external services
- Related components inside labeled containers (e.g., “Auth Middleware,” “Mail Middleware”).

##### Essential architectural elements to capture
- Docker Compose network boundary showing the two main containers (app + DB).
- Express.js entrypoint with middleware sequence: RSA auth → business controllers → mail/Google Sheets calls.
- Models mapping to persistent storage inside the DB container.
- External service calls separate from core HTTP request flow.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Docker Compose Network"
        subgraph "API Container"
            direction TB
            App["Express API (app.js)"]:::api
            DBConn["DB Connection (config/db.js)"]:::data

            subgraph "Middleware Layer"
                direction TB
                AuthCreate["Token Creation Middleware"]:::middleware
                AuthValidate["Token Validation Middleware"]:::middleware
                MailService["Mail Service"]:::middleware
                GoogleSheet["Google Sheets Integration"]:::middleware
                RSAConfig["RSA Key Management"]:::middleware
            end

            subgraph "Router Layer"
                direction TB
                DmgRoute["Damaged Items Routes"]:::router
                DashRoute["Admin Dashboard Routes"]:::router
                LoginRoute["Login Routes"]:::router
                NotifRoute["Notification Routes"]:::router
                ProductRoute["Product Routes"]:::router
                ReIssuedRoute["Re-issued Items Routes"]:::router
                RefRoute["Reference Data Routes"]:::router
                ReqRoute["Requests Routes"]:::router
                ReturnRoute["Return Routes"]:::router
                UserRoute["User Routes"]:::router
            end

            subgraph "Controller Layer"
                direction TB
                AdminDashCtrl["Admin Dashboard Controller"]:::controller
                AdminProfileCtrl["Admin Profile Controller"]:::controller
                DamagedCtrl["Damaged Items Controller"]:::controller
                LoginCtrl["Login Controller"]:::controller
                NotifCtrl["Notification Controller"]:::controller
                ProductCtrl["Product Controller"]:::controller
                UserProfileCtrl["User Profile Controller"]:::controller
                ReIssuedCtrl["Re-issued Items Controller"]:::controller
                RefCtrl["Reference Data Controller"]:::controller
                ReqCtrl["Requests Controller"]:::controller
                ReturnCtrl["Return Controller"]:::controller
            end

            subgraph "Model Layer"
                direction TB
                DamagedModel["Damaged Model"]:::model
                NotifModel["Notifications Model"]:::model
                ProductModel["Product Model"]:::model
                ReIssuedModel["Re-issued Model"]:::model
                RefModel["Reference Model"]:::model
                ReqModel["Requests Model"]:::model
                TokenModel["Token Model"]:::model
                UserModel["User Model"]:::model
            end

            App --> DBConn
            App --> AuthCreate
            AuthCreate --> AuthValidate
            AuthValidate --> DmgRoute
            AuthValidate --> DashRoute
            AuthValidate --> LoginRoute
            AuthValidate --> NotifRoute
            AuthValidate --> ProductRoute
            AuthValidate --> ReIssuedRoute
            AuthValidate --> RefRoute
            AuthValidate --> ReqRoute
            AuthValidate --> ReturnRoute
            AuthValidate --> UserRoute

            DmgRoute --> DamagedCtrl
            DashRoute --> AdminDashCtrl
            LoginRoute --> LoginCtrl
            NotifRoute --> NotifCtrl
            ProductRoute --> ProductCtrl
            ReIssuedRoute --> ReIssuedCtrl
            RefRoute --> RefCtrl
            ReqRoute --> ReqCtrl
            ReturnRoute --> ReturnCtrl
            UserRoute --> UserProfileCtrl
            AdminDashCtrl --> AdminProfileCtrl

            DamagedCtrl --> DamagedModel
            NotifCtrl --> NotifModel
            ProductCtrl --> ProductModel
            ReIssuedCtrl --> ReIssuedModel
            RefCtrl --> RefModel
            ReqCtrl --> ReqModel
            ReturnCtrl --> UserModel
            LoginCtrl --> TokenModel
            UserProfileCtrl --> UserModel

            DamagedCtrl --> MailService
            NotifCtrl --> MailService
            ProductCtrl --> MailService
            UserProfileCtrl --> MailService
            LoginCtrl --> MailService

            DamagedCtrl --> GoogleSheet
            NotifCtrl --> GoogleSheet

            MailService --> RSAConfig
            GoogleSheet --> RSAConfig
        end

        subgraph "DB Container"
            Mongo["Database"]:::db
        end
    end

    SMTP["SMTP Service"]:::external
    GoogleAPI["Google Sheets API"]:::external
    GitHubCI["GitHub Actions"]:::external

    MailService --> SMTP
    GoogleSheet --> GoogleAPI
    GitHubCI --> App

    click App "https://github.com/knightempire/lab-backend/blob/main/app.js"
    click DBConn "https://github.com/knightempire/lab-backend/blob/main/config/db.js"
    click AdminDashCtrl "https://github.com/knightempire/lab-backend/blob/main/controllers/admin/dashboard.controllers.js"
    click AdminProfileCtrl "https://github.com/knightempire/lab-backend/blob/main/controllers/admin/profile.controllers.js"
    click DamagedCtrl "https://github.com/knightempire/lab-backend/blob/main/controllers/damaged.controllers.js"
    click LoginCtrl "https://github.com/knightempire/lab-backend/blob/main/controllers/login.contollers.js"
    click NotifCtrl "https://github.com/knightempire/lab-backend/blob/main/controllers/notification.controllers.js"
    click ProductCtrl "https://github.com/knightempire/lab-backend/blob/main/controllers/product.controllers.js"
    click UserProfileCtrl "https://github.com/knightempire/lab-backend/blob/main/controllers/profile.controllers.js"
    click ReIssuedCtrl "https://github.com/knightempire/lab-backend/blob/main/controllers/reIssued.controllers.js"
    click RefCtrl "https://github.com/knightempire/lab-backend/blob/main/controllers/reference.controllers.js"
    click ReqCtrl "https://github.com/knightempire/lab-backend/blob/main/controllers/requests.controllers.js"
    click ReturnCtrl "https://github.com/knightempire/lab-backend/blob/main/controllers/return.controllers.js"
    click DmgRoute "https://github.com/knightempire/lab-backend/blob/main/routers/damaged.js"
    click DashRoute "https://github.com/knightempire/lab-backend/blob/main/routers/dashboard.js"
    click LoginRoute "https://github.com/knightempire/lab-backend/blob/main/routers/login.js"
    click NotifRoute "https://github.com/knightempire/lab-backend/blob/main/routers/notification.js"
    click ProductRoute "https://github.com/knightempire/lab-backend/blob/main/routers/product.js"
    click ReIssuedRoute "https://github.com/knightempire/lab-backend/blob/main/routers/reIssued.js"
    click RefRoute "https://github.com/knightempire/lab-backend/blob/main/routers/reference.js"
    click ReqRoute "https://github.com/knightempire/lab-backend/blob/main/routers/request.js"
    click ReturnRoute "https://github.com/knightempire/lab-backend/blob/main/routers/return.js"
    click UserRoute "https://github.com/knightempire/lab-backend/blob/main/routers/user.js"
    click DamagedModel "https://github.com/knightempire/lab-backend/blob/main/models/damaged.model.js"
    click NotifModel "https://github.com/knightempire/lab-backend/blob/main/models/notifications.model.js"
    click ProductModel "https://github.com/knightempire/lab-backend/blob/main/models/product.model.js"
    click ReIssuedModel "https://github.com/knightempire/lab-backend/blob/main/models/reIssued.model.js"
    click RefModel "https://github.com/knightempire/lab-backend/blob/main/models/reference.model.js"
    click ReqModel "https://github.com/knightempire/lab-backend/blob/main/models/requests.model.js"
    click TokenModel "https://github.com/knightempire/lab-backend/blob/main/models/token.model.js"
    click UserModel "https://github.com/knightempire/lab-backend/blob/main/models/user.model.js"
    click AuthCreate "https://github.com/knightempire/lab-backend/blob/main/middleware/auth/tokencreation.js"
    click AuthValidate "https://github.com/knightempire/lab-backend/blob/main/middleware/auth/tokenvalidate.js"
    click MailService "https://github.com/knightempire/lab-backend/blob/main/middleware/mail/mail.js"
    click GoogleSheet "https://github.com/knightempire/lab-backend/blob/main/middleware/googlesheet.js"
    click RSAConfig "https://github.com/knightempire/lab-backend/blob/main/middleware/rsa/key.js"
    click SMTP "https://github.com/knightempire/lab-backend/tree/main/middleware/mail/html/"
    click GoogleAPI "https://github.com/knightempire/lab-backend/blob/main/middleware/rsa/public_key.pem"
    click GitHubCI "https://github.com/knightempire/lab-backend/blob/main/.github/workflows/main_lab-backend.yml"

    classDef api fill:#cce5ff,stroke:#3399ff,color:#000;
    classDef middleware fill:#e6f7ff,stroke:#66c2ff,color:#000;
    classDef router fill:#c3f0ca,stroke:#28a745,color:#000;
    classDef controller fill:#d4edda,stroke:#218838,color:#000;
    classDef model fill:#fff3cd,stroke:#ffc107,color:#000;
    classDef data fill:#ffe5b4,stroke:#ff8c00,color:#000;
    classDef db fill:#f2f2f2,stroke:#666,color:#000;
    classDef external fill:#f8f9fa,stroke:#adb5bd,color:#000;
```
