# hamzachaali-web/test_e-commerce architecture

> Architecture diagram of the GitHub repository hamzachaali-web/test_e-commerce, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hamzachaali-web/test_e-commerce
- Repository: https://github.com/hamzachaali-web/test_e-commerce

## Overview

##### Project type and purpose
- This is a full-stack e-commerce web application built with Next.js (using the new App Router).
- The front end is React/TypeScript pages under `app/`, the back end lives in Next.js API routes under `app/api/`, and Prisma/SQLite (or another SQL DB) is used for persistence.

##### File structure
- Top-level directories:
- `app/` – Next.js pages & layout, feature-specific routes (account, product, checkout, login, search).
- `app/api/` – serverless API endpoints for products, customers (login/register/profile), orders, favorites, ratings, search, similar-products, auth operations.
- `components/` – reusable UI components (nav bar, banners, product rating, favorite button, etc.).
- `lib/` – shared libraries: JWT handling, middleware, Prisma client.
- `prisma/` – database schema, migrations, and seed data file (`data.db`).
- `public/`, config files (`next.config.ts`, Tailwind, ESLint, PostCSS, tsconfig).

##### README for additional insights
- Confirms standard Next.js setup (create-next-app), no special monorepo or microservices splitting.
- The application will be deployed on Vercel or any Node.js host; uses next/font optimization.

a. Main components
- Browser (client) – issues page requests and API calls.
- Next.js Application – performs SSR/SSG for pages, hosts API routes under `app/api`.
- Prisma ORM layer (`lib/prisma.ts`) – abstracts DB access.
- Database – SQLite for local dev (`prisma/data.db`) or PostgreSQL/MySQL in production.
- External Payment Provider – invoked in checkout/paymentProvider.
- JWT Auth Service – custom middleware in `lib/middleware.ts` validating tokens.
b. Relationships & data flow
1. Client → Next.js page request (e.g., `/product/123`) → Server-side rendering → DB via Prisma → render HTML.
2. Client → API request (`/api/auth`, `/api/products`, `/api/orders`, etc.) → Next.js Lambda → Auth Middleware → Prisma → DB → JSON response.
3. Checkout flow: Client → `/api/orders` → server creates order in DB → invokes external Payment Provider API → callback/webhook.
4. Favorites & ratings: Client uses protected API endpoints with JWT in headers → middleware validates → prisma updates favorites/ratings tables.
c. Architectural patterns & design principles
- Layered architecture: Presentation (React pages & components), Application (Next.js routing & API), Data (Prisma & DB).
- Use of Context API (`components/ContextAPI`) for front-end state.
- JWT-based stateless authentication.
- Modular pages under `app/feature` for feature separation.
- Database migrations to evolve schema in `prisma/migrations`.
d. Key technologies
- Next.js (App Router, server components, API routes)
- React & TypeScript
- Prisma ORM + SQLite/PostgreSQL
- JWT for auth
- Tailwind CSS for styling
- Vercel (or Node) for deployment

## Diagram (Mermaid)

```mermaid
flowchart TD
    Browser["Browser"] 
    pages["Next.js Frontend\napp/ (SSR/CSR Pages)"]

    subgraph "API Layer"
        Auth["Auth API\napp/api/auth/route.ts"]
        Products["Products API\napp/api/products/route.ts"]
        Orders["Orders API\napp/api/orders/route.ts"]
        CustomersLogin["Login API\napp/api/customers/login/route.ts"]
        CustomersRegister["Register API\napp/api/customers/register/route.ts"]
        CustomersProfile["Profile API\napp/api/customers/profile/[id]/route.ts"]
        Favorites["Favorites API\napp/api/favorites/route.ts"]
        FavoritesByCustomer["Favorites By Customer API\napp/api/favorites/[customerId]/route.ts"]
        Rating["Rating API\napp/api/rating/route.ts"]
        Search["Search API\napp/api/search/route.ts"]
        Similar["Similar Products API\napp/api/similar-products/route.ts"]
        Logout["Logout API\napp/api/logout/route.ts"]
        Me["Me API\napp/api/me/route.ts"]
    end

    JWT["JWT Middleware\nlib/middleware.ts"]
    Prisma["Prisma ORM\nlib/prisma.ts"]
    DB["Database\nprisma/data.db"]
    External["External Payment Provider\n(app/checkout/paymentProvider/page.tsx)"]

    Browser -->|"HTTP GET (SSR/CSR)"| pages
    Browser -->|"XHR/Fetch"| Auth
    Browser -->|"XHR/Fetch"| Products
    Browser -->|"XHR/Fetch"| Orders
    Browser -->|"XHR/Fetch"| CustomersLogin
    Browser -->|"XHR/Fetch"| CustomersRegister
    Browser -->|"XHR/Fetch"| CustomersProfile
    Browser -->|"XHR/Fetch"| Favorites
    Browser -->|"XHR/Fetch"| FavoritesByCustomer
    Browser -->|"XHR/Fetch"| Rating
    Browser -->|"XHR/Fetch"| Search
    Browser -->|"XHR/Fetch"| Similar
    Browser -->|"XHR/Fetch"| Logout
    Browser -->|"XHR/Fetch"| Me

    Auth -->|protected| JWT
    Products -->|protected| JWT
    Orders -->|protected| JWT
    CustomersLogin -->|public| JWT
    CustomersRegister -->|public| JWT
    CustomersProfile -->|protected| JWT
    Favorites -->|protected| JWT
    FavoritesByCustomer -->|protected| JWT
    Rating -->|protected| JWT
    Search -->|public| JWT
    Similar -->|public| JWT
    Logout -->|protected| JWT
    Me -->|protected| JWT

    JWT -->|"DB Query"| Prisma
    Prisma -->|"SQL Query"| DB

    Orders -->|"Invoke payment API"| External

    click pages "https://github.com/hamzachaali-web/test_e-commerce/tree/main/app/"
    click Auth "https://github.com/hamzachaali-web/test_e-commerce/blob/main/app/api/auth/route.ts"
    click Products "https://github.com/hamzachaali-web/test_e-commerce/blob/main/app/api/products/route.ts"
    click Orders "https://github.com/hamzachaali-web/test_e-commerce/blob/main/app/api/orders/route.ts"
    click CustomersLogin "https://github.com/hamzachaali-web/test_e-commerce/blob/main/app/api/customers/login/route.ts"
    click CustomersRegister "https://github.com/hamzachaali-web/test_e-commerce/blob/main/app/api/customers/register/route.ts"
    click CustomersProfile "https://github.com/hamzachaali-web/test_e-commerce/blob/main/app/api/customers/profile/[id]/route.ts"
    click Favorites "https://github.com/hamzachaali-web/test_e-commerce/blob/main/app/api/favorites/route.ts"
    click FavoritesByCustomer "https://github.com/hamzachaali-web/test_e-commerce/blob/main/app/api/favorites/[customerId]/route.ts"
    click Rating "https://github.com/hamzachaali-web/test_e-commerce/blob/main/app/api/rating/route.ts"
    click Search "https://github.com/hamzachaali-web/test_e-commerce/blob/main/app/api/search/route.ts"
    click Similar "https://github.com/hamzachaali-web/test_e-commerce/blob/main/app/api/similar-products/route.ts"
    click Logout "https://github.com/hamzachaali-web/test_e-commerce/blob/main/app/api/logout/route.ts"
    click Me "https://github.com/hamzachaali-web/test_e-commerce/blob/main/app/api/me/route.ts"
    click JWT "https://github.com/hamzachaali-web/test_e-commerce/blob/main/lib/middleware.ts"
    click Prisma "https://github.com/hamzachaali-web/test_e-commerce/blob/main/lib/prisma.ts"
    click DB "https://github.com/hamzachaali-web/test_e-commerce/blob/main/prisma/data.db"
    click External "https://github.com/hamzachaali-web/test_e-commerce/blob/main/app/checkout/paymentProvider/page.tsx"

    classDef client fill:#ADD8E6,stroke:#000
    classDef server fill:#90EE90,stroke:#000
    classDef data fill:#FFD580,stroke:#000
    classDef external fill:#D291BC,stroke:#000

    class Browser client
    class pages,Auth,Products,Orders,CustomersLogin,CustomersRegister,CustomersProfile,Favorites,FavoritesByCustomer,Rating,Search,Similar,Logout,Me,JWT server
    class Prisma,DB data
    class External external
```
