# druvkotwani/leetcode-profiles architecture

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

- Interactive diagram: https://gitdiagram.com/druvkotwani/leetcode-profiles
- Repository: https://github.com/druvkotwani/leetcode-profiles

## Overview

##### Project Type and Purpose
- This is a full-stack web application built on Next.js (React + Node) that lets users fetch and display their LeetCode statistics.
- The “client” folder houses both the frontend (UI) and backend (API routes and cron job) via Next.js’s hybrid routing. Data is stored in MongoDB and the entire app is deployed on Vercel.

##### Main Components
a. Frontend (Next.js “app” & “pages” folders)
– React components (Card, Heatmap, Navbar, etc.)
– Pages and Routes:
- Public UI pages: Home, Worth, Admin sync page
- DataContext wrapper for app-wide state
b. Backend (Next.js API routes + Cron)
– API endpoints under client/src/pages/api and client/src/app/api/cron
- fetchdata, add, syncProfile, syncAllProfiles
- Cron scheduler route (/api/cron) for periodic profile syncs
– Database layer:
- lib/mongodb.js (connection)
- models/userdata.ts (data schema)
c. Database
– MongoDB Atlas (hosted externally)
d. External Service
– LeetCode platform (data source)
e. Hosting/Deployment
– Vercel platform (Serverless functions + static hosting)

##### Relationships & Data Flows
- User → Frontend UI → Internal API endpoints → MongoDB
- Cron service (serverless function invoked on schedule) → sync endpoints → fetch from LeetCode → update MongoDB
- Frontend pages fetch stored data via client-side calls to API or via Next.js Server Side Rendering/Static Generation
- All serverless API functions run on Vercel Lambdas

##### Architectural Patterns & Principles
- Component-based UI (React + Tailwind CSS)
- Hybrid routing (Next.js App Router + Pages Router for legacy APIs)
- Serverless functions for backend logic (Next.js API routes)
- Scheduled jobs modeled as HTTP-triggered cron functions
- Single repository monorepo for both frontend and backend

##### Essential Elements to Capture
– React UI components (Card, Heatmap, etc.) served by Next.js
– Next.js App Router vs Pages Router for API vs UI
– MongoDB connection (lib/mongodb.js) and schema (models/userdata.ts)
– Cron endpoint under /app/api/cron/route.ts
– External data source (LeetCode) with scrape/fetch logic
– Deployment on Vercel (serverless hosting)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% UI Layer
    subgraph "UI Layer (Browser)" 
        direction TB
        subgraph "Pages" 
            direction TB
            Home["Home Page"]:::frontend
            Worth["Worth Page"]:::frontend
            AdminSync["Admin Sync Page"]:::frontend
        end
        subgraph "Components" 
            direction TB
            Card["Card"]:::frontend
            Heatmap["Heatmap"]:::frontend
            Navbar["Navbar"]:::frontend
            Circle["Circle"]:::frontend
            Footer["Footer"]:::frontend
            GenerateStats["GenerateStats"]:::frontend
            Line["Line"]:::frontend
            Line2["Line2"]:::frontend
            ProfileComp["Profile"]:::frontend
            PromotionCard["PromotionCard"]:::frontend
            Questions["Questions"]:::frontend
            Skeleton["Skeleton"]:::frontend
            SocialLinks["SocialLinks"]:::frontend
        end
        DataContext["DataContext"]:::frontend
        Layout["Global Layout"]:::frontend
        GlobalsCSS["Global CSS"]:::frontend
        TailwindConfig["Tailwind Config"]:::frontend
    end

    %% API Layer
    subgraph "API Layer (Vercel Lambdas)" 
        direction TB
        Cron["Cron Scheduler\n(/api/cron)"]:::api
        FetchData["POST /api/fetchdata"]:::api
        AddProfile["POST /api/add"]:::api
        SyncProfile["POST /api/syncProfile"]:::api
        SyncAll["POST /api/syncAllProfiles"]:::api
        DynamicUser["GET /api/[username]"]:::api
        MongoUtil["MongoDB Connection"]:::api
        UserSchema["UserData Schema"]:::api
    end

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

    %% External Service
    LeetCode["LeetCode Platform"]:::external

    %% Deployment & Config
    subgraph "Config & Deployment"
        direction TB
        NextConfig["Next.js Config"]:::infra
        VercelSettings["Vercel Settings"]:::infra
        CICron["CI Cron Workflow"]:::infra
    end

    %% Data Flows
    Home -->|"renders via\nSSR/CSR"| FetchData
    Worth -->|"renders via\nSSR/CSR"| FetchData
    AdminSync -->|"user triggers"| SyncProfile
    FetchData -->|"reads/writes"| MongoUtil
    AddProfile -->|"writes"| MongoUtil
    SyncProfile -->|"writes"| MongoUtil
    SyncAll -->|"writes"| MongoUtil
    DynamicUser -->|"reads"| MongoUtil
    MongoUtil -->|"uses schema"| UserSchema
    Cron -->|"scheduled trigger"| SyncAll
    SyncAll -->|"fetch profile"| LeetCode
    SyncProfile -->|"fetch profile"| LeetCode
    LeetCode -->|"response"| SyncAll
    LeetCode -->|"response"| SyncProfile
    MongoUtil -->|"connects to"| MongoDB

    %% Click Events - Frontend Components
    click Card "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/components/Card.tsx"
    click Heatmap "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/components/Heatmap.tsx"
    click Navbar "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/components/Navbar.tsx"
    click Circle "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/components/Circle.tsx"
    click Footer "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/components/Footer.tsx"
    click GenerateStats "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/components/GenerateStats.tsx"
    click Line "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/components/Line.tsx"
    click Line2 "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/components/Line2.tsx"
    click ProfileComp "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/components/Profile.tsx"
    click PromotionCard "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/components/PromotionCard.tsx"
    click Questions "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/components/Questions.tsx"
    click Skeleton "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/components/Skeleton.tsx"
    click SocialLinks "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/components/SocialLinks.tsx"
    click Home "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/page.tsx"
    click Worth "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/worth/page.tsx"
    click AdminSync "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/admin/sync/page.tsx"
    click DataContext "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/context/DataContext.tsx"
    click Layout "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/layout.tsx"
    click GlobalsCSS "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/globals.css"
    click TailwindConfig "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/tailwind.config.ts"
    click Cron "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/app/api/cron/route.ts"
    click FetchData "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/pages/api/fetchdata.ts"
    click AddProfile "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/pages/api/add.ts"
    click SyncProfile "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/pages/api/syncProfile.ts"
    click SyncAll "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/pages/api/syncAllProfiles.ts"
    click DynamicUser "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/pages/api/[username].ts"
    click MongoUtil "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/lib/mongodb.js"
    click UserSchema "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/src/models/userdata.ts"
    click NextConfig "https://github.com/druvkotwani/leetcode-profiles/blob/main/client/next.config.mjs"
    click VercelSettings "https://github.com/druvkotwani/leetcode-profiles/blob/main/vercel.json"
    click CICron "https://github.com/druvkotwani/leetcode-profiles/blob/main/.github/workflows/weekly-sync.yml"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#0366D6,color:#0366D6
    classDef api fill:#E6FFFA,stroke:#01867D,color:#01867D
    classDef db fill:#FFF4D6,stroke:#DE9E36,color:#DE9E36
    classDef external fill:#F0F0F0,stroke:#888888,color:#888888
    classDef infra fill:#F5E6FF,stroke:#9B59B6,color:#9B59B6
```
