# rifatkasikci/mercury-backend architecture

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

- Interactive diagram: https://gitdiagram.com/rifatkasikci/mercury-backend
- Repository: https://github.com/rifatkasikci/mercury-backend

## Overview

##### Main Components

a) Client Layer:
- External Client Applications (Mobile/Web)
- reCAPTCHA Integration Point

b) API Gateway Layer:
- Express.js Server
- Middleware Components:
- Authentication Middleware
- Rate Limiter
- Validation Middleware
- Error Handling

c) Core Service Layer:
- Authentication Service
- User Management
- Barter System (Adverts & Trade Products)
- Category Management
- Verification System
- Waitlist Management

d) Infrastructure Layer:
- PostgreSQL Database
- Redis Cache
- Email Services (Mailgun/Nodemailer)

**[Clients] → [API Gateway/Load Balancer] → [Application Server] → [Services] → [Data Layer]**

##### Key Elements to Emphasize

a) Authentication Flow:
- JWT token generation and verification
- Refresh token mechanism
- Role-based access control

b) Data Flow:
- Repository pattern implementation
- Redis caching strategy
- Database relationships

c) External Services:
- Email service integration (both Mailgun and Nodemailer)
- reCAPTCHA verification flow

##### Specific Implementation Details

- Rectangles: Services and Controllers
- Hexagons: External Services
- Diamonds: Decision Points

##### Critical Paths

a) Authentication Flow:
**Client → AuthMiddleware → AuthController → UserToken/RefreshToken → Redis/PostgreSQL**

b) Barter Transaction Flow:
**Client → AdvertController/TradeProductController → Repository → PostgreSQL**

c) Email Verification Flow:
**VerificationController → MailService → Mailgun/Nodemailer → User Email**

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        CL[Client Applications]
        RC["reCAPTCHA"]:::external
    end

    subgraph Gateway
        ES["Express Server"]:::server
        subgraph Middleware
            AM["Auth Middleware"]:::middleware
            RL["Rate Limiter"]:::middleware
            VM["Validation Middleware"]:::middleware
            EH["Error Handler"]:::middleware
        end
    end

    subgraph Services
        AS["Authentication Service"]:::service
        US["User Management"]:::service
        BS["Barter System"]:::service
        CM["Category Management"]:::service
        VS["Verification System"]:::service
        WM["Waitlist Management"]:::service
    end

    subgraph Infrastructure
        PG[(PostgreSQL)]:::database
        RD[(Redis Cache)]:::cache
        subgraph "Email Services"
            MG["Mailgun"]:::external
            NM["Nodemailer"]:::external
        end
    end

    subgraph Core
        TM["Token Management"]:::util
        DR["Data Repository"]:::util
        VR["Validation Rules"]:::util
    end

    CL --> RC
    CL --> ES
    
    ES --> AM
    ES --> RL
    ES --> VM
    ES --> EH
    
    AM --> AS
    AM --> TM
    
    AS --> DR
    US --> DR
    BS --> DR
    CM --> DR
    VS --> DR
    WM --> DR
    
    VS --> MG
    VS --> NM
    
    DR --> PG
    DR --> RD

    classDef server fill:#1E88E5
    classDef middleware fill:#FDD835
    classDef service fill:#43A047
    classDef database fill:#6D4C41
    classDef cache fill:#8E24AA
    classDef external fill:#F4511E
    classDef util fill:#00ACC1

    click ES "https://github.com/rifatKasikci/mercury-backend/blob/main/server.js"
    click AM "https://github.com/rifatKasikci/mercury-backend/blob/main/middlewares/authMiddleware.js"
    click RL "https://github.com/rifatKasikci/mercury-backend/blob/main/middlewares/rateLimiterMiddleware.js"
    click VM "https://github.com/rifatKasikci/mercury-backend/blob/main/middlewares/validationMiddleware.js"
    click EH "https://github.com/rifatKasikci/mercury-backend/blob/main/middlewares/errorMiddleware.js"
    click AS "https://github.com/rifatKasikci/mercury-backend/blob/main/controllers/authController.js"
    click US "https://github.com/rifatKasikci/mercury-backend/blob/main/controllers/usersController.js"
    click BS "https://github.com/rifatKasikci/mercury-backend/blob/main/controllers/advertsController.js"
    click CM "https://github.com/rifatKasikci/mercury-backend/blob/main/controllers/categoriesController.js"
    click VS "https://github.com/rifatKasikci/mercury-backend/blob/main/controllers/verificationCodesController.js"
    click WM "https://github.com/rifatKasikci/mercury-backend/blob/main/controllers/waitlistUsersController.js"
    click RD "https://github.com/rifatKasikci/mercury-backend/blob/main/core/utils/redisClient.js"
    click MG "https://github.com/rifatKasikci/mercury-backend/blob/main/core/mailing/mailgunService/mailgunService.js"
    click NM "https://github.com/rifatKasikci/mercury-backend/blob/main/core/mailing/nodemailerService/nodemailerService.js"
    click RC "https://github.com/rifatKasikci/mercury-backend/blob/main/core/utils/reCaptchaHelper.js"
    click DR "https://github.com/rifatKasikci/mercury-backend/blob/main/repositories/repositoryBase.js"
    click TM "https://github.com/rifatKasikci/mercury-backend/blob/main/core/utils/tokenHelper.js"
    click VR "https://github.com/rifatKasikci/mercury-backend/tree/main/validationRules/"
```
