# mervin-njy/malamola architecture

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

- Interactive diagram: https://gitdiagram.com/mervin-njy/malamola
- Repository: https://github.com/mervin-njy/malamola

## Overview

##### Project Type & Purpose
- Malamola is a full-stack e-commerce web application built on Next.js (App Router), React + TypeScript.
- It serves two personas: end-users (browse, search, add to cart, checkout) and admin (inventory, orders, enquiries, user management).
- Features include authentication (NextAuth with Google), product catalogue, cart & checkout, admin dashboard, and educational/social integrations.

##### Top-Level Components
a. Client Browser
- Static assets & React pages (app/(public)/..., app/admin/...)
- Next.js SSR/SSG and client-side React components (Navbar, CartDisplay, ProductCard, etc.)
b. Next.js Server (Node.js runtime)
- App Router endpoints map to React pages and server-side functions
- API routes under app/api/auth/[...nextauth]/route.ts for authentication
c. Business-Logic Layer
- Controller-like modules in app/components/actions and lib/db/* that orchestrate requests
- State management with Redux Toolkit (app/state/store.ts, languageSlice)
d. Data-Access Layer
- Prisma client (lib/db/prisma.ts) interacting with MongoDB via DATABASE_URL
- Data helpers in lib/db/cart.ts
e. External Services
- Google OAuth (via NextAuth adapter)
- MongoDB Atlas

##### Interactions & Data Flow
- User requests page → Next.js Router → SSR/SSG or CSR
- Page makes client-side calls to internal API endpoints (fetch /app/api/…) or uses Redux + React-Hook-Form + Zod for local form validation
- API routes invoke Prisma client → MongoDB
- NextAuth middleware syncs session with Google and stores user data in MongoDB
- Admin interface (protected routes) reads/writes via API or direct server component calls

##### Architectural Patterns & Principles
- Monolithic Next.js “app router” structure with separation of UI, API, and data.
- Layered architecture: Presentation (React components), Business (action helpers), Data (Prisma + MongoDB).
- Component-driven development: atomic UI (buttons, badges, inputs) under app/components.
- State slice per domain (language, cart, products).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        direction TB
        RL["Root Layout"]:::frontend
        GS["Global Styles"]:::frontend
        NB["Navbar"]:::frontend
        FT["Footer"]:::frontend
        SP["SessionProvider"]:::frontend
        subgraph "Public Storefront"
            direction TB
            PP["Client Browser – Public Storefront Pages"]:::frontend
            PL["Product List (page.tsx)"]:::frontend
            PD["Product Detail ( [id]/page.tsx )"]:::frontend
            CP["Cart Page (cart/page.tsx)"]:::frontend
            CD["CartDisplay.tsx"]:::frontend
            CO["CheckoutDisplay.tsx"]:::frontend
        end
        subgraph "Admin Dashboard"
            direction TB
            AP["Client Browser – Admin Dashboard Pages"]:::frontend
            IP["Inventory Page"]:::frontend
            APG["Add Product Page"]:::frontend
            OPC["OptionField.tsx"]:::frontend
            APC["AdminProductCard.tsx"]:::frontend
            ORP["Orders Management Page"]:::frontend
            ENP["Enquiries Management Page"]:::frontend
            USP["User Management Page"]:::frontend
        end
        subgraph "Atomic Components"
            direction TB
            PC["ProductCard.tsx"]:::frontend
            SO["SelectOption.tsx"]:::frontend
            SQ["SelectQuantity.tsx"]:::frontend
            BTN["Buttons"]:::frontend
            BDG["Badges"]:::frontend
            FLT["Filters"]:::frontend
            INP["Inputs"]:::frontend
        end
        subgraph "State Management"
            direction TB
            RS["Redux Store"]:::frontend
            LS["Language Slice"]:::frontend
        end
    end

    %% Backend Layer
    subgraph "Backend Layer"
        direction TB
        NA["NextAuth API Route"]:::api
        AC["NextAuth Configuration"]:::api
        AH["Actions Helpers"]:::api
        CH["Cart Helpers"]:::api
        PCli["Prisma Client"]:::db
    end

    %% Database & External
    subgraph "Database & External Services"
        direction TB
        DB["MongoDB Atlas"]:::db
        GO["Google OAuth"]:::ext
    end

    %% Connections
    RL -->|provides layout to| PP
    RL -->|provides layout to| AP
    PP -->|renders| PL
    PP -->|renders| PD
    PP -->|renders| CP
    CP -->|uses| CD
    CP -->|uses| CO
    AP -->|renders| IP
    AP -->|renders| APG
    APG -->|uses| OPC
    IP -->|renders| APC
    AP -->|renders| ORP
    AP -->|renders| ENP
    AP -->|renders| USP
    PL -->|uses| PC
    PD -->|uses| PC
    PL -->|uses| SO
    PL -->|uses| SQ
    PD -->|uses| SO
    PD -->|uses| SQ
    PP -->|wrapped by| SP
    AP -->|wrapped by| SP
    PP -->|state via| RS
    AP -->|state via| RS
    RS -->|slice| LS

    %% Client-Server
    PP -->|page request| NA
    AP -->|page request| NA
    CD -->|fetch| AH
    CO -->|fetch| AH
    PL -->|fetch| AH
    PD -->|fetch| AH
    IP -->|fetch| AH
    ORP -->|fetch| AH
    ENP -->|fetch| AH
    USP -->|fetch| AH

    %% Auth Flow
    NA -->|config| AC
    NA -->|calls| GO
    GO -->|callback| NA
    NA -->|uses| PCli

    %% Data Access
    AH -->|uses| CH
    AH -->|uses| PCli
    CH -->|uses| PCli
    PCli -->|connects| DB

    %% Click Events
    click PP "https://github.com/mervin-njy/malamola/tree/main/malamola/app/(public)"
    click AP "https://github.com/mervin-njy/malamola/tree/main/malamola/app/admin"
    click NA "https://github.com/mervin-njy/malamola/blob/main/malamola/app/api/auth/[...nextauth]/route.ts"
    click AC "https://github.com/mervin-njy/malamola/blob/main/malamola/lib/configs/auth.ts"
    click SP "https://github.com/mervin-njy/malamola/blob/main/malamola/app/providers/SessionProvider.tsx"
    click PL "https://github.com/mervin-njy/malamola/blob/main/malamola/app/(public)/products/page.tsx"
    click PD "https://github.com/mervin-njy/malamola/blob/main/malamola/app/(public)/products/[id]/page.tsx"
    click PC "https://github.com/mervin-njy/malamola/blob/main/malamola/app/components/products/ProductCard.tsx"
    click SO "https://github.com/mervin-njy/malamola/blob/main/malamola/app/components/products/SelectOption.tsx"
    click SQ "https://github.com/mervin-njy/malamola/blob/main/malamola/app/components/products/SelectQuantity.tsx"
    click CP "https://github.com/mervin-njy/malamola/blob/main/malamola/app/(public)/cart/page.tsx"
    click CD "https://github.com/mervin-njy/malamola/blob/main/malamola/app/(public)/cart/components/CartDisplay.tsx"
    click CO "https://github.com/mervin-njy/malamola/blob/main/malamola/app/(public)/cart/components/CheckoutDisplay.tsx"
    click AH "https://github.com/mervin-njy/malamola/tree/main/malamola/app/components/actions"
    click PCli "https://github.com/mervin-njy/malamola/blob/main/malamola/lib/db/prisma.ts"
    click CH "https://github.com/mervin-njy/malamola/blob/main/malamola/lib/db/cart.ts"
    click RS "https://github.com/mervin-njy/malamola/blob/main/malamola/app/state/store.ts"
    click LS "https://github.com/mervin-njy/malamola/blob/main/malamola/app/state/language/languageSlice.ts"
    click IP "https://github.com/mervin-njy/malamola/blob/main/malamola/app/admin/inventory/page.tsx"
    click APG "https://github.com/mervin-njy/malamola/blob/main/malamola/app/admin/inventory/addProduct/page.tsx"
    click APC "https://github.com/mervin-njy/malamola/blob/main/malamola/app/admin/inventory/AdminProductCard.tsx"
    click OPC "https://github.com/mervin-njy/malamola/blob/main/malamola/app/admin/inventory/addProduct/OptionField.tsx"
    click ORP "https://github.com/mervin-njy/malamola/blob/main/malamola/app/admin/orders/page.tsx"
    click ENP "https://github.com/mervin-njy/malamola/blob/main/malamola/app/admin/enquiries/page.tsx"
    click USP "https://github.com/mervin-njy/malamola/blob/main/malamola/app/admin/users/page.tsx"
    click BTN "https://github.com/mervin-njy/malamola/tree/main/malamola/app/components/buttons"
    click BDG "https://github.com/mervin-njy/malamola/tree/main/malamola/app/components/badges"
    click FLT "https://github.com/mervin-njy/malamola/tree/main/malamola/app/components/filters"
    click INP "https://github.com/mervin-njy/malamola/tree/main/malamola/app/components/inputs"
    click RL "https://github.com/mervin-njy/malamola/blob/main/malamola/app/layout.tsx"
    click GS "https://github.com/mervin-njy/malamola/blob/main/malamola/app/globals.css"
    click NB "https://github.com/mervin-njy/malamola/blob/main/malamola/app/Navbar/NavBar.tsx"
    click FT "https://github.com/mervin-njy/malamola/blob/main/malamola/app/Footer.tsx"

    %% Styles
    classDef frontend fill:#bbdefb,stroke:#1e88e5,color:#0d47a1
    classDef api fill:#ffe0b2,stroke:#fb8c00,color:#e65100
    classDef db fill:#c8e6c9,stroke:#43a047,color:#1b5e20
    classDef ext fill:#e0e0e0,stroke:#9e9e9e,color:#424242
```
