# dv64bit/studentflow architecture

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

- Interactive diagram: https://gitdiagram.com/dv64bit/studentflow
- Repository: https://github.com/dv64bit/studentflow

## Overview

##### Project Type and Purpose
- StudentFlow is a full‐stack, server‐side-rendered Q&A web application built on Next.js.
- Purpose: allow students to ask/answer questions, manage profiles, search/filter content, and integrate with ChatGPT for AI suggestions and Svix for webhook handling.

##### File Structure Analysis
- app/
- Next.js “app router” directory, grouping pages by route:
– (auth): sign-in, sign-up via Clerk
– (root): home, ask‐question, collection, community, profile, question, tags
– api/chatgpt: ChatGPT integration endpoint
– api/webhooks: Svix webhook endpoint
- components/
- UI building blocks (cards, forms, shared widgets, navbar, search components)
- lib/
- Business logic layer (“actions”) for questions, answers, users, tags, interactions
- mongoose.ts: MongoDB connection
- utils, validations
- database/
- Mongoose models for User, Question, Answer, Tag, Interaction
- context/
- ThemeProvider for global theming (dark/light)
- public/, styles/, constants/, types/: static assets, global CSS/themes, app constants, TypeScript types
- middleware.ts, next.config.mjs, Tailwind/ESLint configs, package.json

##### Key Architecture Insights from README
- React + Next.js handles SSR/SSG and API Routes
- Clerk for auth flows
- MongoDB (Mongoose) stores documents
- TinyMCE for rich‐text editor inside question/answer forms
- Prism.js for code syntax highlighting
- Svix proxies webhooks for real-time events
- Zod validates request payloads
- Deployment on Vercel

##### Main Components & Their Relationships
A. Client (Browser)
– Renders React pages/components under app/
– Sends API calls to Next.js endpoints
– Uses Clerk SDK for authentication UIs & tokens
B. Next.js Server
– Page rendering logic (SSR/SSG): app/layout.tsx, page.tsx
– API Routes (app/api/chatgpt/route.ts, app/api/webhooks/route.ts)
– Middleware for auth checks, request logging
C. Business Logic Layer (lib/actions)
– Receives validated inputs (via Zod)
– Performs CRUD on Mongoose models
– Implements search, pagination, vote interactions
D. Data Persistence
– MongoDB cluster (hosted on Atlas)
– Mongoose ODM models under database/
E. External Services
– Clerk: authentication & user management
– ChatGPT API: AI‐powered suggestions
– Svix: webhook proxy & delivery
– Query-string: URL param parsing
F. Global Context & Theming
– React Context (ThemeProvider) for UI theming
G. UI Library
– Shadcn-UI + Tailwind CSS for design system
– TinyMCE embedded editor
– Prism.js syntax highlighting

##### Architectural Patterns & Design Principles
- Layered Architecture:
- Presentation (Next.js pages & React components)
- API layer (Next.js Route Handlers)
- Service/Action layer (lib/actions)
- Data layer (Mongoose models)
- Feature-based module organization (e.g., components/forms, components/cards)
- Separation of concerns: UI vs business logic vs data models
- Middleware for cross-cutting concerns (auth, logging)
- Schema validation (Zod) before business logic

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Layer
    subgraph "Client (Browser)" 
        direction TB
        subgraph "Next.js Pages (App Router)" 
            direction TB
            AuthSignIn["SignIn Page"]:::ui
            AuthSignUp["SignUp Page"]:::ui
            HomePage["Home Page"]:::ui
            AskPage["Ask Question Page"]:::ui
            CollectionPage["Collection Page"]:::ui
            CommunityPage["Community Page"]:::ui
            ProfilePage["Profile Page"]:::ui
            ProfileEditPage["Edit Profile Page"]:::ui
            QuestionPage["Question Details Page"]:::ui
            QuestionEditPage["Edit Question Page"]:::ui
            TagsPage["Tags List Page"]:::ui
            TagDetailPage["Tag Detail Page"]:::ui
            Layout["App Layout"]:::ui
        end
        subgraph "UI Components Library" 
            direction TB
            AnswerCard["AnswerCard"]:::ui
            QuestionCard["QuestionCard"]:::ui
            UserCard["UserCard"]:::ui
            QuestionForm["QuestionForm"]:::ui
            AnswerForm["Answer"]:::ui
            EditProfileForm["EditProfileForm"]:::ui
            Navbar["Navbar"]:::ui
            SearchComp["Search"]:::ui
            Filters["Filters"]:::ui
            Pagination["Pagination"]:::ui
            Votes["Votes"]:::ui
            ShadcnUI["Shadcn-UI Primitives"]:::ui
        end
    end

    %% Server Layer
    subgraph "Next.js Server" 
        direction TB
        Pages["SSR/SSG Pages"]:::server
        Middleware["middleware.ts"]:::server
        subgraph "API Routes" 
            direction TB
            ChatGPTAPI["/api/chatgpt"]:::api
            WebhookAPI["/api/webhooks"]:::api
        end
    end

    %% Business Logic Layer
    subgraph "Business Logic (lib/actions)" 
        direction TB
        QuestionAction["question.action.ts"]:::service
        AnswerAction["answer.action.ts"]:::service
        UserAction["user.action.ts"]:::service
        TagActions["tag.actions.ts"]:::service
        InteractionAction["interaction.action.ts"]:::service
        GeneralAction["general.action.ts"]:::service
        SharedTypes["shared.types.d.ts"]:::service
        MongooseConn["mongoose.ts"]:::service
        Utils["utils.ts"]:::service
        Validations["validations.ts"]:::service
    end

    %% Data Layer
    subgraph "Database (MongoDB Atlas)" 
        direction TB
        UserModel["user.model.ts"]:::db
        QuestionModel["question.model.ts"]:::db
        AnswerModel["answer.model.ts"]:::db
        TagModel["tag.model.ts"]:::db
        InteractionModel["interaction.model.ts"]:::db
    end

    %% External Services
    subgraph "External Services" 
        direction TB
        Clerk["Clerk Auth"]:::external
        ChatGPT["ChatGPT API"]:::external
        Svix["Svix Webhooks"]:::external
        QueryString["query-string"]:::external
    end

    %% Connections
    AuthSignIn -->|uses| Pages
    AuthSignUp -->|uses| Pages
    HomePage -->|SSR Request| Pages
    AskPage -->|SSR Request| Pages
    CollectionPage -->|SSR Request| Pages
    CommunityPage -->|SSR Request| Pages
    ProfilePage -->|SSR Request| Pages
    ProfileEditPage -->|SSR Request| Pages
    QuestionPage -->|SSR Request| Pages
    QuestionEditPage -->|SSR Request| Pages
    TagsPage -->|SSR Request| Pages
    TagDetailPage -->|SSR Request| Pages

    AnswerCard -->|UI| Pages
    QuestionCard -->|UI| Pages
    UserCard -->|UI| Pages
    QuestionForm -->|submit→| ChatGPTAPI
    QuestionForm -->|submit→| WebhookAPI
    AnswerForm -->|submit→| WebhookAPI

    Pages -->|calls API| ChatGPTAPI
    Pages -->|calls API| WebhookAPI
    ChatGPTAPI -->|validate| Validations
    WebhookAPI -->|validate| Validations

    ChatGPTAPI -->|invoke→| QuestionAction
    WebhookAPI -->|invoke→| GeneralAction

    QuestionAction -->|CRUD| QuestionModel
    AnswerAction -->|CRUD| AnswerModel
    UserAction -->|CRUD| UserModel
    TagActions -->|CRUD| TagModel
    InteractionAction -->|CRUD| InteractionModel

    QuestionAction -->|calls| ChatGPT
    GeneralAction -->|calls| Svix

    Pages -->|auth check| Middleware
    Middleware -->|auth| Clerk

    Utils -->|used by| QuestionAction
    Utils -->|used by| AnswerAction
    Validations -->|used by| UserAction

    MongooseConn -->|connects| UserModel
    MongooseConn -->|connects| QuestionModel
    MongooseConn -->|connects| AnswerModel
    MongooseConn -->|connects| TagModel
    MongooseConn -->|connects| InteractionModel

    %% Click Events
    click AuthSignIn "https://github.com/dv64bit/studentflow/blob/main/app/(auth)/sign-in/[[...sign-in]]/page.tsx"
    click AuthSignUp "https://github.com/dv64bit/studentflow/blob/main/app/(auth)/sign-up/[[...sign-up]]/page.tsx"
    click HomePage "https://github.com/dv64bit/studentflow/blob/main/app/(root)/(home)/page.tsx"
    click AskPage "https://github.com/dv64bit/studentflow/blob/main/app/(root)/ask-question/page.tsx"
    click CollectionPage "https://github.com/dv64bit/studentflow/blob/main/app/(root)/collection/page.tsx"
    click CommunityPage "https://github.com/dv64bit/studentflow/blob/main/app/(root)/community/page.tsx"
    click ProfilePage "https://github.com/dv64bit/studentflow/blob/main/app/(root)/profile/[id]/page.tsx"
    click ProfileEditPage "https://github.com/dv64bit/studentflow/blob/main/app/(root)/profile/edit/page.tsx"
    click QuestionPage "https://github.com/dv64bit/studentflow/blob/main/app/(root)/question/[id]/page.tsx"
    click QuestionEditPage "https://github.com/dv64bit/studentflow/blob/main/app/(root)/question/edit/[id]/page.tsx"
    click TagsPage "https://github.com/dv64bit/studentflow/blob/main/app/(root)/tags/page.tsx"
    click TagDetailPage "https://github.com/dv64bit/studentflow/blob/main/app/(root)/tags/[id]/page.tsx"
    click Layout "https://github.com/dv64bit/studentflow/blob/main/app/layout.tsx"
    click AnswerCard "https://github.com/dv64bit/studentflow/blob/main/components/cards/AnswerCard.tsx"
    click QuestionCard "https://github.com/dv64bit/studentflow/blob/main/components/cards/QuestionCard.tsx"
    click UserCard "https://github.com/dv64bit/studentflow/blob/main/components/cards/UserCard.tsx"
    click QuestionForm "https://github.com/dv64bit/studentflow/blob/main/components/forms/QuestionForm.tsx"
    click AnswerForm "https://github.com/dv64bit/studentflow/blob/main/components/forms/Answer.tsx"
    click EditProfileForm "https://github.com/dv64bit/studentflow/blob/main/components/forms/EditProfileForm.tsx"
    click ChatGPTAPI "https://github.com/dv64bit/studentflow/blob/main/app/api/chatgpt/route.ts"
    click WebhookAPI "https://github.com/dv64bit/studentflow/blob/main/app/api/webhooks/route.ts"
    click QuestionAction "https://github.com/dv64bit/studentflow/blob/main/lib/actions/question.action.ts"
    click AnswerAction "https://github.com/dv64bit/studentflow/blob/main/lib/actions/answer.action.ts"
    click UserAction "https://github.com/dv64bit/studentflow/blob/main/lib/actions/user.action.ts"
    click TagActions "https://github.com/dv64bit/studentflow/blob/main/lib/actions/tag.actions.ts"
    click InteractionAction "https://github.com/dv64bit/studentflow/blob/main/lib/actions/interaction.action.ts"
    click GeneralAction "https://github.com/dv64bit/studentflow/blob/main/lib/actions/general.action.ts"
    click MongooseConn "https://github.com/dv64bit/studentflow/blob/main/lib/mongoose.ts"
    click Utils "https://github.com/dv64bit/studentflow/blob/main/lib/utils.ts"
    click Validations "https://github.com/dv64bit/studentflow/blob/main/lib/validations.ts"
    click UserModel "https://github.com/dv64bit/studentflow/blob/main/database/user.model.ts"
    click QuestionModel "https://github.com/dv64bit/studentflow/blob/main/database/question.model.ts"
    click AnswerModel "https://github.com/dv64bit/studentflow/blob/main/database/answer.model.ts"
    click TagModel "https://github.com/dv64bit/studentflow/blob/main/database/tag.model.ts"
    click InteractionModel "https://github.com/dv64bit/studentflow/blob/main/database/interaction.model.ts"

    %% Styles
    classDef ui fill:#E3F2FD,stroke:#1976D2,color:#0D47A1
    classDef server fill:#E8F5E9,stroke:#2E7D32,color:#1B5E20
    classDef api fill:#C8E6C9,stroke:#2E7D32,color:#1B5E20
    classDef service fill:#FFF3E0,stroke:#EF6C00,color:#E65100
    classDef db fill:#FFE0B2,stroke:#EF6C00,color:#BF360C
    classDef external fill:#F3E5F5,stroke:#6A1B9A,color:#4A148C
```
