# iot-lab-kiit/kritique-backend architecture

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

- Interactive diagram: https://gitdiagram.com/iot-lab-kiit/kritique-backend
- Repository: https://github.com/iot-lab-kiit/kritique-backend

## Overview

##### Project Type & Purpose
- This is a backend service (written in TypeScript) that appears central to a larger application dealing with faculty (or teacher) reviews. The naming in directories such as “faculty”, “review”, and “user” suggests its role in managing data and logic for handling user reviews for faculty.
- The project acts as an API server and HTML view renderer using Express (as inferred from the presence of type definitions in src/@types/express.ts and EJS view templates). It is also containerized (Dockerfile, compose.yaml, vercel.json) and uses external modules like Firebase Admin.

##### Analyzing the File Structure
- Entry Point & Core Logic:
- index.ts is likely the main starting point.
- Types & Constants:
- The src/@types folder contains custom type definitions for Express as well as for domain concepts like faculty, review, and user.
- The src/constants directory holds status codes and profanity dictionary JSON files (for English and Hindi), indicating some moderation or filtering features.
- Controllers & Routes:
- The src/controllers directory manages business logic for faculty, review, and user.
- Corresponding route files (under src/routes) are separated by file extension, with JSON and HTML formats indicating that the backend serves both web pages and API responses.
- Views:
- EJS templates in src/views provide the HTML rendering for various UI components related to faculty and reviews.
- Middleware & Helpers:
- The presence of files under src/middleware (like auth.ts) implies pre-request processing such as authentication.
- The helper functions in src/helper (for anonymizing names, validating teacher data, simulating profanity) add utility functions central to the business logic.
- External Libraries:
- Under src/lib, integration with Firebase Admin and custom library functions (such as profanity filtering and random name generation) are implemented.

##### Main Components
a. Client / Request Entry
- Incoming HTTP requests (via REST endpoints) that hit the backend.
b. API & Routing Layer:
- Node/Express server entry point (index.ts) that routes requests based on URL paths.
c. Middleware:
- Authentication middleware and possibly other pre-processing steps that manage sessions/tokens.
d. Controllers:
- Faculty, Review, and User controllers handling request processing.
e. Service & Helper Layer:
- Helper modules (like anonName, dataFill, profanityMock) and libraries (firebase-admin integration, profanity, random-names) that encapsulate core logic.
f. Data Models:
- Under the src/model folder, structure for faculty, review, and user data is defined.
g. Views:
- EJS templates used for generating HTML responses.
h. External Services & Configurations:
- Firebase service (via src/lib/firebase-admin.ts) and environment configuration (Docker, Vercel, .env variables).

##### Relationships and Flow
- Controllers then use helpers, libraries, and models to process data, communicate with external services (Firebase) and then generate a response either as JSON or rendered HTML (using views).
- Dependency flow from controllers to helper functions and libraries is essential.
- Also, include the configuration and deployment aspects (Docker, compose.yaml, vercel.json) to show how the backend is packaged and deployed.

##### Architectural Patterns & Design Principles
- Separation of Concerns: Clear separation between routing, controllers, business logic, views, and helper utilities.
- Modular Design: Each feature (faculty, review, user) has its own controller, model, and type definitions.
- Middleware Pattern: Authentication and possibly other pre-request validations are handled through middleware.
- API/Server-Side Rendering: Dual support for JSON responses and HTML views.
- Containerization: Docker and related configuration files indicate containerized deployment.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% API Layer
    CR["Client Request"]:::external
    API["API Entry (index.ts)"]:::api
    RT["Routing Layer (src/routes)"]:::routing
    MW["Middleware (auth.ts)"]:::middleware

    %% Business Logic - Controllers
    subgraph "Controllers"
        FC["Faculty Controller"]:::controller
        RC["Review Controller"]:::controller
        UC["User Controller"]:::controller
    end

    %% Data Models
    subgraph "Data Models"
        FM["Faculty Model"]:::model
        RM["Review Model"]:::model
        UM["User Model"]:::model
    end

    %% Supporting Modules
    HM["Helper Modules (src/helper)"]:::helper
    EL["External Libraries (src/lib)"]:::external
    VT["View Templates (src/views)"]:::view

    %% Types & Constants
    subgraph "Types & Constants"
        TDDef["Type Definitions (src/@types)"]:::types
        Const["Constants (src/constants)"]:::types
    end

    %% Deployment & Configuration
    subgraph "Deployment & Config"
        DF["Dockerfile"]:::config
        CY["compose.yaml"]:::config
        ENV[".env.example"]:::config
        VC["vercel.json"]:::config
    end

    %% Connections
    CR --> API
    API --> RT
    RT --> MW
    MW --> FC
    MW --> RC
    MW --> UC

    FC --> FM
    RC --> RM
    UC --> UM

    FC --> HM
    RC --> HM
    UC --> HM

    FC --> EL
    RC --> EL
    UC --> EL

    FC --> VT
    RC --> VT
    UC --> VT

    API --> TDDef
    API --> Const

    %% Deployment connects to the API entry point
    DF --> API
    CY --> API
    ENV --> API
    VC --> API

    %% Click Events
    click API "https://github.com/iot-lab-kiit/kritique-backend/blob/main/index.ts"
    click RT "https://github.com/iot-lab-kiit/kritique-backend/tree/main/src/routes"
    click MW "https://github.com/iot-lab-kiit/kritique-backend/blob/main/src/middleware/auth.ts"
    click FC "https://github.com/iot-lab-kiit/kritique-backend/blob/main/src/controllers/faculty.ts"
    click RC "https://github.com/iot-lab-kiit/kritique-backend/blob/main/src/controllers/review.ts"
    click UC "https://github.com/iot-lab-kiit/kritique-backend/blob/main/src/controllers/user.ts"
    click FM "https://github.com/iot-lab-kiit/kritique-backend/blob/main/src/model/faculty.ts"
    click RM "https://github.com/iot-lab-kiit/kritique-backend/blob/main/src/model/review.ts"
    click UM "https://github.com/iot-lab-kiit/kritique-backend/blob/main/src/model/user.ts"
    click TDDef "https://github.com/iot-lab-kiit/kritique-backend/tree/main/src/@types"
    click Const "https://github.com/iot-lab-kiit/kritique-backend/tree/main/src/constants"
    click HM "https://github.com/iot-lab-kiit/kritique-backend/tree/main/src/helper"
    click EL "https://github.com/iot-lab-kiit/kritique-backend/tree/main/src/lib"
    click VT "https://github.com/iot-lab-kiit/kritique-backend/tree/main/src/views"
    click DF "https://github.com/iot-lab-kiit/kritique-backend/tree/main/Dockerfile"
    click CY "https://github.com/iot-lab-kiit/kritique-backend/blob/main/compose.yaml"
    click ENV "https://github.com/iot-lab-kiit/kritique-backend/blob/main/.env.example"
    click VC "https://github.com/iot-lab-kiit/kritique-backend/blob/main/vercel.json"

    %% Styles
    classDef api fill:#d9edf7,stroke:#31708f,stroke-width:2px;
    classDef routing fill:#fcf8e3,stroke:#8a6d3b,stroke-width:2px;
    classDef middleware fill:#f2dede,stroke:#a94442,stroke-width:2px;
    classDef controller fill:#dff0d8,stroke:#3c763d,stroke-width:2px;
    classDef model fill:#f5f5f5,stroke:#666,stroke-width:2px;
    classDef helper fill:#e6e6fa,stroke:#663399,stroke-width:2px;
    classDef external fill:#eeeeee,stroke:#777,stroke-width:2px;
    classDef view fill:#d1edf2,stroke:#31708f,stroke-width:2px;
    classDef config fill:#fcf8e3,stroke:#8a6d3b,stroke-width:2px;
    classDef types fill:#f9f2f4,stroke:#c9302c,stroke-width:2px;
```
