diff --git a/backend/models/Product.js b/backend/models/Product.js index fffac364..136b2dde 100644 --- a/backend/models/Product.js +++ b/backend/models/Product.js @@ -19,6 +19,12 @@ const productSchema = new Schema({ type: Number, required: true, }, + couponId: { + type: String, + required: true, + unique: true, + match: /^[A-Z0-9]{6,10}$/ // Adjust regex to your requirements + }, }); const Product = mongoose.model("Product", productSchema); diff --git a/backend/routes/verify-coupon.js b/backend/routes/verify-coupon.js new file mode 100644 index 00000000..e3497fa9 --- /dev/null +++ b/backend/routes/verify-coupon.js @@ -0,0 +1,24 @@ +const express = require("express"); +const router = express.Router(); +const User = require("../models/Product"); + + +router.post("/verify-coupon", async (req, res) => { + try { + const { code } = req.body; + + // Check if coupon exists in the database + const coupon = await User.findOne({ code }); + if (coupon) { + return res.status(200).send("Valid Code"); + } + else { + return res.status(404).send("Invalid Code"); + } + } catch (error) { + res.status(500).send("Server error: " + error.message); + } + }); + + module.exports = router; + \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 9e5c1c44..cc0bd380 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,11 +27,11 @@ "node": "^22.7.0", "nodemailer": "^6.9.15", "razorpay": "^2.9.4", - "react": "^18.2.0", + "react": "^18.3.1", "react-confetti": "^6.1.0", "react-cookie-consent": "^9.0.0", "react-ctrl-f": "^0.0.4", - "react-dom": "^18.2.0", + "react-dom": "^18.3.1", "react-helmet": "^6.1.0", "react-hot-toast": "^2.4.1", "react-icons": "^5.3.0", diff --git a/package.json b/package.json index fab8c3da..0e8310e0 100644 --- a/package.json +++ b/package.json @@ -30,11 +30,11 @@ "node": "^22.7.0", "nodemailer": "^6.9.15", "razorpay": "^2.9.4", - "react": "^18.2.0", + "react": "^18.3.1", "react-confetti": "^6.1.0", "react-cookie-consent": "^9.0.0", "react-ctrl-f": "^0.0.4", - "react-dom": "^18.2.0", + "react-dom": "^18.3.1", "react-helmet": "^6.1.0", "react-hot-toast": "^2.4.1", "react-icons": "^5.3.0", diff --git a/src/User/pages/Dashboard/dashboard-cart.jsx b/src/User/pages/Dashboard/dashboard-cart.jsx index b7174774..e737edd8 100644 --- a/src/User/pages/Dashboard/dashboard-cart.jsx +++ b/src/User/pages/Dashboard/dashboard-cart.jsx @@ -114,6 +114,31 @@ const Subtotal = ({ items }) => { }; const ProceedToCheckout = () => { + const [couponCode, setCouponCode] = useState(''); + const buttonBgClass = 'bg-blue-500 text-white'; // Adjust this as per your styling + + const validateCoupon = async () => { + try { + const response = await fetch('/api/verify-coupon', { // Replace with your actual API endpoint + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ couponId: couponCode }), + }); + + const result = await response.json(); + if (!response.ok) { + throw new Error(result.message || 'Invalid coupon code'); // Handle response error + } + + // Proceed with the valid coupon code logic + alert('Coupon applied successfully!'); // You can replace this with your success handling + + } catch (error) { + alert(error.message); // Alert the user with the error message + } + }; return (