Skip to content

Instantly share code, notes, and snippets.

@DulalSandip
Created April 13, 2021 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DulalSandip/553612a20566eff1b0f2fb9f22acf3c0 to your computer and use it in GitHub Desktop.
Save DulalSandip/553612a20566eff1b0f2fb9f22acf3c0 to your computer and use it in GitHub Desktop.
server running continously (app.js)
const couponCodeDiscount = require("./routes/CouponCode.Routes");
app.use("/api", couponCodeDiscount);
// checking coupon code expiration time valid or not to reduce server load
const checkExpirationTime = () => {
CouponCodeDiscount.find({})
.exec()
.then((Coupon) => {
if (Coupon) {
Coupon.map((getCoupon) => {
if (
new Date().getTime() >= new Date(getCoupon.expirationTime).getTime() // expirationTime data access from database
) {
CouponCodeDiscount.findOneAndDelete({
_id: getCoupon._id,
})
.exec()
.then((deleteCoupon) => {
console.log(`Coupon doesnt exists or expired`);
})
.catch((error) => {
console.log(error, "Error occured on coupon section");
});
}
});
}
if (!Coupon) {
console.log("No Coupon found...");
}
});
};
setInterval(checkExpirationTime, 1000); // converting to millisecond
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment