Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›‘οΈ FraudShield β€” AI-Powered Fraud Detection Dashboard

A production-grade, real-time fraud detection platform with AI-powered risk scoring, analyst workflows, and live transaction monitoring.

Features Β· Tech Stack Β· Architecture Β· Setup Β· API Reference


πŸ“Œ Overview

FraudShield is a full-stack MERN application that simulates a real-world payment fraud detection system. Every incoming transaction is analyzed by an AI engine (Groq + Llama 3) that computes a risk score from 0–100, flags suspicious activity, and surfaces actionable alerts for human analysts to review.

The system mirrors workflows used at fintech companies like Razorpay, PayU, and Stripe β€” where ML-based risk scoring is combined with analyst review queues to minimize false positives while catching genuine fraud.

Built to demonstrate: real-time systems design, AI integration, REST API architecture, MongoDB aggregation pipelines, and production-grade React UI patterns.


✨ Features

πŸ€– AI Fraud Engine

  • Risk scoring (0–100) on every transaction using Groq AI (Llama 3)
  • Factors analyzed: transaction amount, time of day, merchant reputation, user history, location anomaly, device type, payment method
  • Automatic model fallback chain β€” if one Groq model hits rate limits, the engine silently retries with the next available model
  • Rule-based fallback β€” if AI is unavailable, a deterministic scoring engine kicks in so the system never goes dark

πŸ“Š Real-Time Dashboard

  • Live transaction feed via Socket.io β€” zero polling
  • 7-day fraud trend (bar chart), risk distribution (pie chart), volume by payment method
  • Stats: total transactions, flagged count, pending reviews, confirmed fraud, total volume

🚨 Alert Management

  • Prioritized queue of flagged transactions sorted by risk score
  • One-click analyst actions: Confirm Fraud, False Positive, Approve
  • AI Explain button β€” asks the AI to explain in plain English why a transaction was flagged
  • Real-time badge counter on sidebar updates without page refresh

πŸ’³ Transaction Management

  • Full CRUD with server-side filtering, search, and pagination
  • Review modal with inline risk score breakdown, flagging reasons, and analyst notes
  • Audit trail: every decision is stamped with analyst name and timestamp

πŸͺ Merchant Risk Profiling

  • Fraud rate per merchant auto-calculated from transaction history
  • Dynamic risk levels (Low / Medium / High / Critical) based on fraud rate thresholds
  • Blacklist / unblacklist merchants β€” blacklisted merchants automatically increase risk score on new transactions

πŸ€– AI Analyst Chat

  • Conversational interface backed by Groq AI with full transaction context injected into every query
  • Suggested questions for quick analysis
  • Maintains conversation history for multi-turn dialogue

πŸ—οΈ Tech Stack

Layer Technology Reason
Frontend React 18, React Router v6 Component model, client-side routing
State Management React Context API Lightweight β€” no Redux overhead needed
Charts Recharts Composable, React-native charting
Real-Time Socket.io Client Bi-directional event streaming
Backend Node.js + Express.js Non-blocking I/O, ideal for event-driven systems
Database MongoDB Atlas + Mongoose Flexible schema, powerful aggregation pipeline
Authentication JWT + bcryptjs Stateless auth, bcrypt salt rounds: 12
AI / LLM Groq API (Llama 3.1, Llama 3, Mixtral) Free tier, fastest inference available (~500 tok/s)
Real-Time Server Socket.io Push fraud alerts to all connected dashboards instantly
Validation express-validator Schema-level request validation

πŸ›οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     React Frontend                       β”‚
β”‚   Dashboard β”‚ Transactions β”‚ Alerts β”‚ Merchants β”‚ AI     β”‚
β”‚                  Socket.io Client                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚ HTTP + WebSocket
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  Express.js Backend                      β”‚
β”‚                                                          β”‚
β”‚  /api/auth         /api/transactions   /api/merchants   β”‚
β”‚  /api/alerts       /api/ai                              β”‚
β”‚                                                          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                       β”‚
β”‚  β”‚       Fraud Engine           β”‚                       β”‚
β”‚  β”‚  1. Build transaction prompt β”‚                       β”‚
β”‚  β”‚  2. Call Groq AI (w/fallback)β”‚                       β”‚
β”‚  β”‚  3. Parse risk score 0-100   β”‚                       β”‚
β”‚  β”‚  4. Rule-based fallback      β”‚                       β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                       β”‚
β”‚                 β”‚                                        β”‚
β”‚  Socket.io ────── Emit: new_transaction, fraud_alert    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                  β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚               MongoDB Atlas                               β”‚
β”‚   users β”‚ transactions β”‚ merchants                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                  β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  Groq AI API                              β”‚
β”‚   llama-3.1-8b-instant β†’ llama3-8b-8192 β†’ mixtral       β”‚
β”‚                  (fallback chain)                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Design Decisions

Why Socket.io over polling? Fraud alerts are time-sensitive. A polling interval of even 5 seconds means an analyst might miss a critical transaction window. Socket.io pushes events the instant a transaction is processed, keeping every connected dashboard in sync.

Why a model fallback chain? Free-tier AI APIs have per-minute and per-day quota limits. Rather than failing with a 429 error, the fraud engine silently retries with the next model in the chain (llama-3.1-8b-instant β†’ llama3-8b-8192 β†’ mixtral-8x7b-32768). If all AI models are exhausted, a deterministic rule-based engine takes over β€” the system never returns an error to the user.

Why MongoDB aggregation pipelines? Dashboard stats (fraud rates, 7-day trends, category breakdowns) are computed server-side using MongoDB's $group, $match, and $sort stages rather than fetching all documents and calculating in JavaScript. This keeps response times fast even with large datasets.


πŸ“ Project Structure

fraud-detection/
β”‚
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   └── auth.js                 # JWT verification middleware
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ User.js                 # Analyst accounts (bcrypt hashed passwords)
β”‚   β”‚   β”œβ”€β”€ Transaction.js          # Core transaction schema with risk fields
β”‚   β”‚   └── Merchant.js             # Merchant profiles with fraud rate tracking
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ auth.js                 # Register, login, /me
β”‚   β”‚   β”œβ”€β”€ transactions.js         # CRUD, stats aggregation, analyst review
β”‚   β”‚   β”œβ”€β”€ merchants.js            # List, blacklist toggle
β”‚   β”‚   β”œβ”€β”€ alerts.js               # Flagged transaction queue
β”‚   β”‚   └── ai.js                   # Explain transaction, analyst chat
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   └── fraudEngine.js          # AI scoring logic + model fallback chain
β”‚   β”œβ”€β”€ seed/
β”‚   β”‚   └── seedData.js             # 200 realistic transactions + demo users
β”‚   β”œβ”€β”€ server.js                   # Express app + Socket.io setup
β”‚   β”œβ”€β”€ package.json
β”‚   └── .env.example
β”‚
└── frontend/
    β”œβ”€β”€ public/
    β”‚   └── index.html
    └── src/
        β”œβ”€β”€ context/
        β”‚   └── AuthContext.jsx     # Global auth state (login/logout/token)
        β”œβ”€β”€ utils/
        β”‚   └── api.js              # Axios instance + all API call functions
        β”œβ”€β”€ components/
        β”‚   └── Layout.jsx          # Sidebar nav + Socket.io live badge updates
        β”œβ”€β”€ pages/
        β”‚   β”œβ”€β”€ Login.jsx           # Auth with demo credential shortcut
        β”‚   β”œβ”€β”€ Register.jsx        # New analyst registration
        β”‚   β”œβ”€β”€ Dashboard.jsx       # Stats + charts + live feed counter
        β”‚   β”œβ”€β”€ Transactions.jsx    # Full table with filters, review modal
        β”‚   β”œβ”€β”€ Alerts.jsx          # Fraud alert queue with AI explain
        β”‚   β”œβ”€β”€ Merchants.jsx       # Merchant risk grid + blacklist controls
        β”‚   └── AIAnalyst.jsx       # Chat interface with context injection
        β”œβ”€β”€ App.jsx                 # Router + protected/public route guards
        β”œβ”€β”€ index.js
        └── index.css               # Full dark-mode design system (no UI lib)

βš™οΈ Setup

Prerequisites

Step 1 β€” Clone

git clone https://github.com/yourusername/fraud-detection.git
cd fraud-detection

Step 2 β€” Backend

cd backend
npm install
cp .env.example .env

Edit .env:

MONGO_URI=mongodb+srv://<user>:<password>@cluster0.xxxxx.mongodb.net/fraud-detection
JWT_SECRET=your_long_random_secret_here_minimum_32_chars
GROQ_API_KEY=gsk_your_groq_key_here
PORT=5000
FRONTEND_URL=http://localhost:3000

Step 3 β€” Seed the Database

Populates MongoDB with 200 realistic transactions across 10 merchants and 5 users, with a realistic mix of low/medium/high/critical risk scores.

npm run seed

Expected output:

βœ… Connected to MongoDB
πŸ—‘οΈ  Cleared existing data
βœ… Seeded 10 merchants
βœ… Seeded 200 transactions
βœ… Updated merchant fraud stats
βœ… Created demo user: analyst@demo.com / demo1234
πŸŽ‰ Seed complete!

Step 4 β€” Start Backend

npm run dev
# βœ… MongoDB connected
# πŸš€ Server running on port 5000

Step 5 β€” Frontend

cd ../frontend
npm install
npm start
# Opens http://localhost:3000

Step 6 β€” Login

Email:    analyst@demo.com
Password: demo1234

πŸ“‘ API Reference

Authentication

Method Endpoint Auth Description
POST /api/auth/register β€” Create analyst account
POST /api/auth/login β€” Login, returns JWT
GET /api/auth/me βœ… Get current user profile

Transactions

Method Endpoint Auth Description
GET /api/transactions βœ… List with filters: riskLevel, isFlagged, status, search, page, limit
GET /api/transactions/stats βœ… Dashboard stats + 7-day trend + risk distribution
GET /api/transactions/:id βœ… Single transaction detail
POST /api/transactions βœ… Create transaction β€” triggers AI fraud scoring
PUT /api/transactions/:id/review βœ… Submit analyst decision (Approved, Confirmed Fraud, False Positive)

Merchants

Method Endpoint Auth Description
GET /api/merchants βœ… List with filters: riskLevel, search
GET /api/merchants/:merchantId βœ… Single merchant profile
PUT /api/merchants/:merchantId/blacklist βœ… Toggle blacklist status

Alerts

Method Endpoint Auth Description
GET /api/alerts βœ… Pending flagged transactions, sorted by risk score
GET /api/alerts/summary βœ… Count by risk level (for sidebar badge)

AI

Method Endpoint Auth Description
POST /api/ai/explain βœ… Plain-English explanation of why a transaction was flagged
POST /api/ai/chat βœ… Conversational fraud analyst with transaction context

Socket.io Events

Event Direction Payload Description
new_transaction Server β†’ Client Transaction object Fires on every new transaction
fraud_alert Server β†’ Client Transaction object Fires when isFlagged: true
transaction_reviewed Server β†’ Client Updated transaction Fires after analyst review

πŸ”’ Security

  • All private routes protected with JWT middleware β€” unauthenticated requests return 401
  • Passwords hashed with bcryptjs at 12 salt rounds
  • JWT tokens expire after 30 days
  • CORS restricted to FRONTEND_URL environment variable only
  • Request body validation on all POST/PUT routes via express-validator
  • .env excluded from version control via .gitignore β€” secrets never committed

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages