# mandeepthenoncoder/themainsarkarcrm architecture

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

- Interactive diagram: https://gitdiagram.com/mandeepthenoncoder/themainsarkarcrm
- Repository: https://github.com/mandeepthenoncoder/themainsarkarcrm

## Overview

##### Project type and purpose
- This is a full-stack CRM web application for jewelry businesses, built on Next.js (React) with a TypeScript codebase, styled by Tailwind CSS and shadcn/ui, and backed by Supabase (PostgreSQL, Auth, Realtime).
- It supports three user roles (Admin, Manager, Salesperson) each with distinct dashboards and workflows for customer management, appointments, sales tracking, tasks, and analytics.

##### File structure
- Top-level config/deployment files (eslint.config.mjs, next.config.ts, tailwind.config.ts, tsconfig.json, DEPLOYMENT.md, SETUP.md, etc.).
- src/app/: Next.js App Router containing role-scoped subfolders (admin/, manager/, salesperson/) plus global routes (login, sales-login, api/, auth/).
- src/components/: shared UI and domain-specific components (AuthButton, ProtectedRoute, customer dialogs, cards, etc.).
- src/contexts/: React AuthContext for managing session state.
- src/lib/: database clients (Postgres, Supabase client/server/middleware), type definitions, utilities.
- supabase/: database schema migrations & seed files.
- public/: static assets.

##### README insights
- Tech stack: Next.js 15, React, TypeScript at frontend; Supabase for backend, Auth, RLS and Realtime features.
- Multi-role architecture with role‐based routing and RLS ensures data isolation.
- Deployment on Vercel with automatic builds and environment variables.

##### Detail and essential elements
- Middleware.ts enforcing auth & RBAC before page access.
- Both direct client-side Supabase interactions (realtime, CRUD) and server-side routes.
- Migration pipeline as part of CI/CD step feeding into the database.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontend Layer"
        direction TB
        Browser["Browser (Client)"]:::frontend
        AppRouter["Next.js App Router (src/app)"]:::frontend
        subgraph "Public Pages"
            direction TB
            Home["Home / Landing"]:::frontend
            Login["Login Page"]:::frontend
            SalesLogin["Sales Login Page"]:::frontend
        end
        subgraph "Role-Scoped Routes"
            direction TB
            AdminLayout["Admin Layout"]:::frontend
            AdminDash["Admin Dashboard"]:::frontend
            ManagerLayout["Manager Layout"]:::frontend
            ManagerDash["Manager Dashboard"]:::frontend
            SalesLayout["Salesperson Layout"]:::frontend
            SalesDash["Salesperson Dashboard"]:::frontend
        end
        subgraph "UI & Auth Components"
            direction TB
            AuthButton["AuthButton"]:::frontend
            ProtectedRoute["ProtectedRoute"]:::frontend
            AuthContext["AuthContext"]:::frontend
        end
        SupClient["Supabase Client (client.ts)"]:::service
    end

    %% Backend Layer
    subgraph "Backend Layer"
        direction TB
        subgraph "Next.js Middleware & Server Helpers"
            direction TB
            Middleware["middleware.ts"]:::backend
            SupServer["Supabase Server Helpers"]:::service
        end
        subgraph "API Routes"
            direction TB
            DBStats["DB Statistics Route"]:::backend
            GrossSales["Gross Sales Route"]:::backend
            DebugSales["Debug Salespeople Route"]:::backend
        end
    end

    %% Database & Auth
    subgraph "Database & Auth"
        direction TB
        AuthLayer["Supabase Auth"]:::auth
        Realtime["Realtime Engine"]:::auth
        Database["PostgreSQL Database"]:::auth
    end

    %% Migrations & Seed
    subgraph "Migrations & Seed"
        direction TB
        Migrations["Schema Migrations"]:::db
        Seed["Seed Data"]:::db
    end

    %% Deployment
    Vercel["Vercel (CI/CD & Hosting)"]:::deployment

    %% Connections
    Browser -->|Requests/Navigation| AppRouter
    Browser -->|Direct CRUD & Realtime| SupClient
    AppRouter -->|Loads Pages| Home
    AppRouter -->|Loads Pages| Login
    AppRouter -->|Loads Pages| SalesLogin
    AppRouter -->|Loads Layout| AdminLayout
    AppRouter -->|Loads Page| AdminDash
    AppRouter -->|Loads Layout| ManagerLayout
    AppRouter -->|Loads Page| ManagerDash
    AppRouter -->|Loads Layout| SalesLayout
    AppRouter -->|Loads Page| SalesDash
    AppRouter -->|Wraps with| ProtectedRoute
    AppRouter -->|Provides| AuthContext
    AppRouter -->|Imports| AuthButton
    AppRouter -->|Uses| Middleware
    AppRouter -->|Server Calls| DBStats
    AppRouter -->|Server Calls| GrossSales
    AppRouter -->|Server Calls| DebugSales
    SupClient -->|Auth Calls| AuthLayer
    SupClient -->|Realtime Subscriptions| Realtime
    SupClient -->|Queries/Mutations| Database
    SupServer -->|Queries/Mutations| Database
    SupServer -->|Auth Validation| AuthLayer
    Middleware -->|Enforces RBAC| AppRouter
    Migrations -->|Deploys Schema| Database
    Seed -->|Populates Data| Database
    Vercel -->|Deploys| AppRouter
    Vercel -->|Runs CI/CD| Migrations

    %% Click Events
    click AppRouter "https://github.com/mandeepthenoncoder/themainsarkarcrm/tree/master/src/app"
    click Home "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/src/app/page.tsx"
    click Login "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/src/app/login/page.tsx"
    click SalesLogin "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/src/app/sales-login/page.tsx"
    click AdminLayout "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/src/app/admin/layout.tsx"
    click AdminDash "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/src/app/admin/dashboard/page.tsx"
    click ManagerLayout "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/src/app/manager/layout.tsx"
    click ManagerDash "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/src/app/manager/dashboard/page.tsx"
    click SalesLayout "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/src/app/salesperson/layout.tsx"
    click SalesDash "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/src/app/salesperson/dashboard/page.tsx"
    click ProtectedRoute "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/src/components/auth/ProtectedRoute.tsx"
    click Middleware "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/middleware.ts"
    click DBStats "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/src/app/api/db-stats/route.ts"
    click GrossSales "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/src/app/api/gross-sales/route.ts"
    click DebugSales "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/src/app/api/debug-salespeople/route.ts"
    click SupClient "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/src/lib/supabase/client.ts"
    click SupServer "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/src/lib/supabase/server.ts"
    click AuthButton "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/src/components/auth/AuthButton.tsx"
    click AuthContext "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/src/contexts/AuthContext.tsx"
    click Migrations "https://github.com/mandeepthenoncoder/themainsarkarcrm/tree/master/supabase/migrations"
    click Seed "https://github.com/mandeepthenoncoder/themainsarkarcrm/blob/master/supabase/seed.sql"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#0366d6,color:#0366d6
    classDef backend fill:#D2F8D2,stroke:#28a745,color:#28a745
    classDef service fill:#E8F0D0,stroke:#6f42c1,color:#6f42c1
    classDef auth fill:#FFE8C0,stroke:#d73a49,color:#d73a49
    classDef db fill:#F0D0D0,stroke:#dbab09,color:#dbab09
    classDef deployment fill:#E0E0E0,stroke:#6a737d,color:#6a737d
```
