Overview
The Crypto Shop Backend uses MongoDB with Mongoose for data persistence. This page documents all database models, their schemas, relationships, and business logic.User Model
Represents user accounts with authentication, wallet, and security features. Location:src/models/User.js
Schema
User’s email address (unique, lowercase)
Username (unique, minimum 3 characters)
Hashed password (minimum 8 characters, not selected by default)
User role:
user or adminTRON wallet information
User’s phone number (optional)
User’s country (optional)
Recovery email address (optional)
Whether 2FA is enabled
2FA secret key (encrypted)
Whether the user account is active
Timestamp of last successful login
Account creation timestamp
Last account update timestamp
Methods
matchPassword(enteredPassword)
Compares a plain-text password with the hashed password.
enteredPassword(string) - Plain-text password to verify
Promise<boolean> - True if password matches
Hooks
Pre-save Hook
Automatically hashes the password before saving if it has been modified.Example Document
Order Model
Represents customer orders with products, pricing, and payment status. Location:src/models/Order.js
Schema
Auto-generated order ID (format:
#TRX-{number})Reference to User model
Array of products in the order
Order subtotal before fees and discounts
TRON network transaction fee
Discount amount applied
Final total amount (subtotal + networkFee - discount)
Order status:
pending, completed, refunded, failed, cancelledPayment method used
TRON blockchain transaction hash
Customer’s wallet address
Merchant’s wallet address (payment destination)
Order creation timestamp
Last order update timestamp
Hooks
Pre-save Hook
Auto-generates sequential order IDs.Example Document
Product Model
Represents products available for purchase. Location:src/models/Product.js
Schema
Product name (trimmed)
Product description
Product price in TRX (minimum 0)
Available stock quantity (minimum 0)
Product category:
digital, physical, or serviceProduct image URL
Reference to User (admin) who created the product
Whether the product is active and available
Product creation timestamp
Last product update timestamp
Example Document
Transaction Model
Represents blockchain transactions for payments and refunds. Location:src/models/Transaction.js
Schema
Reference to User model
Reference to Order model (null for non-purchase transactions)
Transaction type:
purchase, refund, deposit, or withdrawalTransaction amount in TRX
Currency code
Blockchain network
Blockchain transaction hash
Sender wallet address
Recipient wallet address
Transaction status:
pending, confirmed, or failedNumber of blockchain confirmations
Transaction creation timestamp
Last transaction update timestamp
Example Document
Session Model
Tracks user sessions for security and auditing. Location:src/models/Session.js
Schema
Reference to User model
Device identifier
Client IP address
Browser/client user agent string
Last activity timestamp
Whether the session is currently active
Session creation timestamp