Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions BACKEND/config/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ export const connectDB = async () => {
const memoryUri = mongoServer.getUri();
await mongoose.connect(memoryUri);
console.log(`MongoDB In-Memory Connected: Development Mode`);

try {
const Product = (await import("../models/product.model.js")).default;
const count = await Product.countDocuments();
if (count === 0) {
await Product.create([
{ name: "Gaming Laptop Pro", price: 1299, image: "https://images.unsplash.com/photo-1593642632823-8f785ba67e45?w=500", stock: 15, category: "Electronics", brand: "TechBrand" },
{ name: "Wireless Mouse", price: 49, image: "https://images.unsplash.com/photo-1527864550417-7fd91fc51a46?w=500", stock: 50, category: "Electronics", brand: "TechBrand" }
]);
console.log(`MongoDB In-Memory Seeded with mock data.`);
}
} catch (seedErr) {
console.error("Failed to seed in-memory DB:", seedErr.message);
}
} else {
// Connect to the provided MongoDB URI
const conn = await mongoose.connect(mongoURI);
Expand Down
7 changes: 7 additions & 0 deletions BACKEND/controllers/checkout.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Coupon from '../models/coupon.model.js';
import mongoose from 'mongoose';
import Stripe from 'stripe';
import { processReferralOnPurchase } from '../services/referral.service.js';
import { io } from '../server.js';

let stripe;
if (process.env.NODE_ENV === 'test') {
Expand Down Expand Up @@ -206,6 +207,12 @@ export const stripeWebhook = async (req, res) => {
return res.json({ received: true });
}

// Emit real-time stock update
io.emit("stockUpdate", {
productId: item._id,
newStock: updated.stock - item.quantity
});

deductions.push({ productId: item._id, quantity: item.quantity });
}

Expand Down
Loading
Loading