# nguyen1976/techshop architecture

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

- Interactive diagram: https://gitdiagram.com/nguyen1976/techshop
- Repository: https://github.com/nguyen1976/techshop

## Overview

##### Main Components

A. Client Layer
- External clients (Mobile/Web applications)
- Thunder Client/Postman for API testing

B. API Gateway Layer
- Routes (src/routes)
- Authentication Middleware (src/middleware/authMiddleWare.js)

C. Application Layer
- Controllers (src/controllers)
- Services (src/services)
- Models (src/models)

D. External Services
- MongoDB Database
- Google Authentication (indicated by verifyGoogleToken.js)
- Email Service (EmailService.js)

##### Component Relationships

**Client → Routes → Controllers → Services → Models → Database**

##### Detailed Architecture Elements

A. Authentication Flow:
- JWT token flow (access_token & refresh_token)
- Token storage (localStorage & cookies)
- Authentication middleware position

B. Core Business Domains:
- User Management
- Product Management
- Order Processing
- Address Management
- Visit Tracking

##### Component Details

A. Routes Layer:
- AddressRouter
- OrderRouter
- PaymentRouter
- ProductRouter
- UserRouter
- VisitRouter

B. Controller Layer:
- How controllers handle business logic
- Request/response processing

C. Service Layer:
- Business logic implementation
- External service integration (JWT, Email)

D. Model Layer:
- Data models
- MongoDB schema relationships

##### Security Flow

Authentication process:
**Login Request → JWT Service → Access Token Generation →**
**Refresh Token Storage → Protected Routes**

##### Additional Elements

- Error handling flows
- Data validation points
- Where middleware intercepts requests
- Public vs protected routes

##### External service integration points

Remember to:
- Notes for important security considerations
- Both synchronous and asynchronous operations
- Where data transformation occurs

This system design should reflect the RESTful nature of the API while highlighting the security measures implemented through JWT authentication and the separation of access and refresh tokens.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles and Classes
    classDef client fill:#2196F3,color:white
    classDef api fill:#64B5F6,color:white
    classDef controller fill:#4CAF50,color:white
    classDef service fill:#81C784,color:white
    classDef model fill:#FFD54F,color:black
    classDef security fill:#EF5350,color:white
    classDef external fill:#9C27B0,color:white
    
    %% Client Layer
    Client["External Clients\n(Mobile/Web)"]:::client
    Testing["Thunder Client/\nPostman"]:::client
    
    %% API Gateway Layer
    MainRouter["Main Router"]:::api
    AuthMiddleware["Auth Middleware"]:::security
    
    %% Domain Components
    subgraph Domains
        direction TB
        %% User Domain
        UserRouter["User Router"]:::api
        UserController["User Controller"]:::controller
        UserService["User Service"]:::service
        UserModel["User Model"]:::model
        
        %% Product Domain
        ProductRouter["Product Router"]:::api
        ProductController["Product Controller"]:::controller
        ProductService["Product Service"]:::service
        ProductModel["Product Model"]:::model
        
        %% Order Domain
        OrderRouter["Order Router"]:::api
        OrderController["Order Controller"]:::controller
        OrderService["Order Service"]:::service
        OrderModel["Order Model"]:::model
        
        %% Address Domain
        AddressRouter["Address Router"]:::api
        AddressController["Address Controller"]:::controller
        AddressService["Address Service"]:::service
        AddressModel["Address Model"]:::model
        
        %% Visit Domain
        VisitRouter["Visit Router"]:::api
        VisitController["Visit Controller"]:::controller
        VisitModel["Visit Model"]:::model
    end
    
    %% Security Components
    JWTService["JWT Service"]:::security
    GoogleAuth["Google Auth"]:::security
    
    %% External Services
    MongoDB[(MongoDB)]:::external
    EmailService["Email Service"]:::external
    PaymentGateway["Payment Gateway"]:::external
    
    %% Relationships
    Client --> MainRouter
    Testing --> MainRouter
    MainRouter --> AuthMiddleware
    
    AuthMiddleware --> JWTService
    AuthMiddleware --> GoogleAuth
    
    MainRouter --> UserRouter & ProductRouter & OrderRouter & AddressRouter & VisitRouter
    
    UserRouter --> UserController
    UserController --> UserService
    UserService --> UserModel
    UserModel --> MongoDB
    
    ProductRouter --> ProductController
    ProductController --> ProductService
    ProductService --> ProductModel
    ProductModel --> MongoDB
    
    OrderRouter --> OrderController
    OrderController --> OrderService
    OrderService --> OrderModel
    OrderModel --> MongoDB
    
    AddressRouter --> AddressController
    AddressController --> AddressService
    AddressService --> AddressModel
    AddressModel --> MongoDB
    
    VisitRouter --> VisitController
    VisitController --> VisitModel
    VisitModel --> MongoDB
    
    UserService --> EmailService
    OrderService --> PaymentGateway
    
    %% Click Events
    click MainRouter "https://github.com/Nguyen1976/TechShop/blob/main/src/routes/index.js"
    click AuthMiddleware "https://github.com/Nguyen1976/TechShop/blob/main/src/middleware/authMiddleWare.js"
    click JWTService "https://github.com/Nguyen1976/TechShop/blob/main/src/services/JwtService.js"
    click GoogleAuth "https://github.com/Nguyen1976/TechShop/blob/main/src/utils/verifyGoogleToken.js"
    click UserController "https://github.com/Nguyen1976/TechShop/blob/main/src/controllers/UserController.js"
    click UserService "https://github.com/Nguyen1976/TechShop/blob/main/src/services/UserService.js"
    click UserModel "https://github.com/Nguyen1976/TechShop/blob/main/src/models/UserModel.js"
    click UserRouter "https://github.com/Nguyen1976/TechShop/blob/main/src/routes/UserRouter.js"
    click ProductController "https://github.com/Nguyen1976/TechShop/blob/main/src/controllers/ProductController.js"
    click ProductService "https://github.com/Nguyen1976/TechShop/blob/main/src/services/ProductService.js"
    click ProductModel "https://github.com/Nguyen1976/TechShop/blob/main/src/models/ProductModel.js"
    click ProductRouter "https://github.com/Nguyen1976/TechShop/blob/main/src/routes/ProductRouter.js"
    click OrderController "https://github.com/Nguyen1976/TechShop/blob/main/src/controllers/OrderController.js"
    click OrderService "https://github.com/Nguyen1976/TechShop/blob/main/src/services/OrderService.js"
    click OrderModel "https://github.com/Nguyen1976/TechShop/blob/main/src/models/OrderProduct.js"
    click OrderRouter "https://github.com/Nguyen1976/TechShop/blob/main/src/routes/OrderRouter.js"
    click AddressController "https://github.com/Nguyen1976/TechShop/blob/main/src/controllers/AddressControler.js"
    click AddressService "https://github.com/Nguyen1976/TechShop/blob/main/src/services/AddressService.js"
    click AddressModel "https://github.com/Nguyen1976/TechShop/blob/main/src/models/AddressModel.js"
    click AddressRouter "https://github.com/Nguyen1976/TechShop/blob/main/src/routes/AddressRouter.js"
    click VisitController "https://github.com/Nguyen1976/TechShop/blob/main/src/controllers/VisitController.js"
    click VisitModel "https://github.com/Nguyen1976/TechShop/blob/main/src/models/VisitModel.js"
    click VisitRouter "https://github.com/Nguyen1976/TechShop/blob/main/src/routes/VisitRouter.js"
    click EmailService "https://github.com/Nguyen1976/TechShop/blob/main/src/services/EmailService.js"
    click PaymentGateway "https://github.com/Nguyen1976/TechShop/blob/main/src/routes/PaymentRouter.js"
```
