# rahmounidev/v2 architecture

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

- Interactive diagram: https://gitdiagram.com/rahmounidev/v2
- Repository: https://github.com/rahmounidev/v2

## Overview

##### Project type and purpose
- This is a full-stack web application built with Next.js (App Router + Pages Router) serving both UI pages and API endpoints.
- Its purpose is to power the customer-facing side (“espace client”) of Droovo, a food-delivery service: handling authentication, browsing restaurants/dishes, placing orders, loyalty points, reviews, and notifications.

##### File structure
- **app/**: Next.js App Router entrypoints for pages (cart, checkout, login, loyalty, orders, restaurant details, etc.) and global layout/styles.
- **pages/api/**: Next.js API routes (auth, restaurants, orders, profile, reviews, stats, setup).
- **components/**: React components for UI, theming, header/footer, loyalty badge, map, notifications, review system, and a UI primitives library.
- **lib/**: Business logic utilities (auth, DB-access via Prisma, session handling, input validations with Zod).
- **prisma/** + **database/**: DB schema, migrations, seed scripts, and raw SQL for MySQL.
- **middleware.ts**: For session protection or route-level guards.
- **scripts/**: Shell scripts for DB setup.
- **dist/**: Compiled output (for deployment or SSR).
- Configuration files: next.config.mjs, tsconfig.json, tailwind.config.ts, .env.example, package.json.

##### README for additional insights
- Uses MySQL via Prisma (DATABASE_URL).
- Iron-session for server-side session management.
- Zod for request validation.
- Lists all API endpoints and security considerations.
- Mentions notifications (WhatsApp).
- Database entities: users, customers, dishes, orders, categories, reviews, promotions.

a. Main components:
- **Client Browser**: React-based pages served by Next.js; calls API endpoints via fetch.
- **Next.js Server**:
- **App Router**: Server and client components rendering UI (app/).
- **Pages Router (API)**: Serverless or Node API routes (pages/api/).
- **Middleware**: Session validation, route guarding.
- **Authentication & Session**: iron-session + session cookies; auth library in lib/auth.ts
- **Business Logic Layer**: lib/db.ts (Prisma Client), lib/utils.ts, lib/validations/*.
- **Database**: MySQL (resto_db) managed with Prisma; migrations/seed.
- **External Services**:
- WhatsApp notifications (components/notifications/whatsapp-notification.tsx)
- Potential email/WebSocket future.
b. Relationships & data flow:
1. User → Browser (UI React pages)
2. Browser → Next.js API (POST/GET /api/*)
3. API → Middleware (session check) → Validation (Zod) → Business logic (lib/)
4. Business logic → Prisma → MySQL ←→ migrations/seed
5. Business logic → External Service (WhatsApp)
6. Next.js SSR/SSG uses DB to pre-render pages in app/.
c. Architectural patterns/design principles:
- **Separation of concerns**: UI components, API routes, business logic, data access layer.
- **Layered architecture**: Presentation (app/, components), Application (pages/api), Domain logic (lib/validations, lib/utils), Data access (Prisma).
- **Server-side rendering + client hydration** via Next.js App Router.
- **Serverless functions** (API routes) or Node endpoints.
- **Middleware pipeline** for cross-cutting concerns (session, logging).
d. Key technologies/frameworks/libraries:
- Next.js 13 (App + Pages Router)
- React, TypeScript
- Prisma ORM + MySQL
- iron-session for session management
- Zod for input validation
- Tailwind CSS
- External notifications (e.g., Twilio/WhatsApp)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        direction TB
        Browser["Client Browser"]:::external
        AppRouter["Next.js App Router\n(UI Pages)"]:::frontend
        ComponentsUI["UI Components & Theming"]:::frontend
        DomainComponents["Domain-Specific Components"]:::frontend
        Hooks["Custom Hooks"]:::frontend
    end

    %% API Layer
    subgraph "API Layer"
        direction TB
        APIRoutes["Next.js API Routes"]:::api
        Middleware["Middleware\n(Session Enforcement)"]:::middleware
    end

    %% Business Logic Layer
    subgraph "Business Logic Layer"
        direction TB
        AuthSession["Authentication & Session Management"]:::logic
        Validation["Validation Layer (Zod Schemas)"]:::logic
        Utils["Utility Functions"]:::logic
        DBClient["Prisma Client\n(lib/db.ts)"]:::db
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        PrismaSchema["Prisma Schema & Migrations"]:::db
        Database["MySQL Database\n(resto_db)"]:::database
    end

    %% External Services
    subgraph "External Services"
        direction TB
        WhatsApp["WhatsApp Notification Service"]:::external
    end

    %% Flows
    Browser -->|"renders UI"| AppRouter
    AppRouter -->|"uses components"| ComponentsUI
    AppRouter -->|"uses components"| DomainComponents
    AppRouter -->|"uses hooks"| Hooks

    Browser -->|"fetch API"| APIRoutes
    APIRoutes -->|"passes through"| Middleware
    Middleware -->|"calls"| AuthSession
    Middleware -->|"calls"| Validation
    Middleware -->|"calls"| Utils
    APIRoutes -->|"queries"| DBClient
    AuthSession --> DBClient
    Validation --> DBClient
    Utils --> DBClient
    DBClient -->|"executes SQL"| PrismaSchema
    PrismaSchema -->|"stores data in"| Database

    AuthSession -->|"sends notifications via"| WhatsApp
    Utils --> WhatsApp

    %% Click Events
    click AppRouter "https://github.com/rahmounidev/v2/tree/main/app/"
    click ComponentsUI "https://github.com/rahmounidev/v2/tree/main/components/ui/"
    click DomainComponents "https://github.com/rahmounidev/v2/blob/main/components/loyalty/loyalty-badge.tsx"
    click Hooks "https://github.com/rahmounidev/v2/blob/main/hooks/use-mobile.tsx"
    click APIRoutes "https://github.com/rahmounidev/v2/tree/main/pages/api/"
    click Middleware "https://github.com/rahmounidev/v2/blob/main/middleware.ts"
    click AuthSession "https://github.com/rahmounidev/v2/blob/main/lib/auth.ts"
    click Validation "https://github.com/rahmounidev/v2/blob/main/lib/validations/auth.ts"
    click Utils "https://github.com/rahmounidev/v2/blob/main/lib/utils.ts"
    click DBClient "https://github.com/rahmounidev/v2/blob/main/lib/db.ts"
    click PrismaSchema "https://github.com/rahmounidev/v2/blob/main/prisma/schema.prisma"
    click Database "https://github.com/rahmounidev/v2/blob/main/database/resto_db.sql"
    click WhatsApp "https://github.com/rahmounidev/v2/blob/main/components/notifications/whatsapp-notification.tsx"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#0066CC,color:#000
    classDef api fill:#E0FFD8,stroke:#339900,color:#000
    classDef middleware fill:#FFF4CC,stroke:#D4A417,color:#000
    classDef logic fill:#FFF0F5,stroke:#C71585,color:#000
    classDef db fill:#FFE4B5,stroke:#D2691E,color:#000
    classDef database fill:#FFDAB9,stroke:#CD5C5C,color:#000
    classDef external fill:#FFE4E1,stroke:#FF1493,color:#000
```
