53 lines
1.2 KiB
JavaScript
53 lines
1.2 KiB
JavaScript
const Coupon = require("../models/Coupon");
|
|
|
|
module.exports.getAllCoupons = async (req, res, next) => {
|
|
try {
|
|
const coupons = await Coupon.find();
|
|
console.log(coupons);
|
|
res.json({
|
|
coupons: coupons,
|
|
});
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
};
|
|
|
|
module.exports.addCoupon = async (req, res, next) => {
|
|
try {
|
|
const couponCode = req.body.couponCode;
|
|
const percentage = req.body.percentage;
|
|
const numAllowed = req.body.numAllowed;
|
|
|
|
let coupon = await Coupon.findOne({ couponCode: couponCode });
|
|
if (coupon) {
|
|
res.json({
|
|
error: "Coupon already Exist",
|
|
});
|
|
} else {
|
|
let coupon = new Coupon({
|
|
couponCode: couponCode,
|
|
percentage: percentage,
|
|
numAllowed: numAllowed,
|
|
});
|
|
coupon = await coupon.save();
|
|
res.json({
|
|
message: "Created Successfully",
|
|
});
|
|
}
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
};
|
|
|
|
module.exports.deleteCoupon = async (req, res, next) => {
|
|
try {
|
|
const couponCode = req.body.couponCode;
|
|
await Coupon.deleteOne({ couponCode: couponCode });
|
|
res.json({
|
|
message: "Deleted Successfully",
|
|
});
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
};
|