System Documentation

Technical Reference & Schemas

Database models, schemas, background crons, and API listings.

1. Database Mongoose Schemas

Product Schema
{
  name: String, slug: String, description: String, price: Number,
  salePrice: Number, category: ObjectId (ref Category), brand: ObjectId (ref Brand),
  gender: enum["Men","Women","Children","Unisex"], occasion: [String],
  images: [{ url: String, public_id: String }],
  variants: [{ size: Number, color: String, colorHex: String, stock: Number, sku: String, images: [...] }],
  rating: { average: Number, count: Number }, isFeatured: Boolean, isNewArrival: Boolean,
  isActive: Boolean, returnDays: Number
}
Order Schema
{
  orderId: String, userId: ObjectId (ref User),
  items: [{ productId: ObjectId, name: String, size: Number, color: String, price: Number, qty: Number }],
  shippingAddress: { fullName: String, phone: String, line1: String, city: String, pin: String },
  pricing: { subtotal: Number, shipping: Number, couponDiscount: Number, total: Number },
  payment: { method: String, status: enum[PENDING, PAID, FAILED, REFUNDED], razorpayOrderId: String, razorpayPaymentId: String },
  status: enum[PLACED, CONFIRMED, PACKED, SHIPPED, OUT_FOR_DELIVERY, DELIVERED, CANCELLED, RETURNED],
  statusHistory: [{ status: String, timestamp: Date, note: String }],
  shipping: { courier: String, trackingNumber: String, deliveryMethod: String },
  refundDetails: { preference: String, upiId: String, method: String, transactionId: String }
}
LoyaltyPoints Schema
{
  userId: ObjectId (ref User),
  points: Number (positive for earned, negative for redeemed),
  type: enum["EARNED", "REDEEMED"],
  orderId: ObjectId (ref Order),
  description: String
}
FlashSale Schema
{
  name: String,
  discountType: enum["FLAT", "PERCENTAGE"],
  discountValue: Number,
  startDate: Date,
  endDate: Date,
  productIds: [ObjectId (ref Product)],
  isActive: Boolean
}
Cart Schema (Abandoned Recovery)
{
  userId: ObjectId (ref User),
  items: [{
    productId: ObjectId, name: String, size: Number, color: String, price: Number, quantity: Number, image: String, slug: String
  }],
  emailSent: Boolean,
  updatedAt: Date
}

2. Background Cleanup Tasks & Calculations

  • Order Expire Task (`cleanupExpiredPendingOrders`): Scans orders marked `PENDING` payment. If an online order has been pending for over 30 minutes, it automatically cancels the order, sets payment to `FAILED`, and restores the reserved stocks atomically using a Mongoose `$inc` query.
  • User Bias Deduplication (`updateProductRating`): Recalculates product reviews. To prevent user rating manipulation, it groups reviews by `userId` and only counts the review with the highest rating for each user.
  • Abandoned Cart Recovery Cron (`/api/cron/abandoned-cart`): Scans database for customer shopping carts that have been inactive for more than 45 minutes but less than 24 hours. If an email has not yet been sent (emailSent === false), it automatically dispatches a premium styled email displaying the items and a direct checkout link, then marks emailSent = true.

3. API Router Map

MethodRouteModule Purpose
GET/api/productsRetrieve active catalog with filter parameters.
POST/api/coupons/validateValidates coupon eligibility against cart value.
POST/api/ordersCreate pending customer order and hold product variant stock.
PUT/api/ordersAdmin updates order lifecycle status (Riders, AWB, COD validations).
POST/api/webhooks/razorpayRazorpay background payment verification and confirmation.
GET/api/admin/dashboardDashboard metrics, WoW deltas, low stock warnings.
GET/api/user/loyaltyRetrieve customer's loyalty points balance and ledger.
POST/api/user/cartSync local storage cart items with the database.
GET/api/flash-salesRetrieve active storefront flash sales and countdown values.
POST/api/admin/flash-salesCreate a scheduled flash sale event (Admin only).
GET/api/cron/abandoned-cartTrigger abandoned cart recovery check and Nodemailer email dispatches.
GET/api/admin/activity-logRetrieve admin activity and CMS operations history log.
POST/api/admin/products/importImport bulk products and variant inventories from CSV file uploads.
GET/api/admin/orders/exportExport order history ledger to downloadable CSV file format.