64 lines
1.4 KiB
JavaScript
64 lines
1.4 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);
|
|
}
|
|
} |