Coupon Code Expiration using “JavaScript
By: Dulal Sandip
Tags: Nodejs, Coupon Codes, Coupon, Discount Coupon Codes, JavaScript

In E-commerce or any, if there is an offer for different Products valid for certain time, then users can order the offer products within time boundary.If, offer time expires then user can no longer get the offer product at minimum discount price using coupon code.

Coupon Code offer for product…

So, you are Node js back-end developer and your role is to set Coupon Code logic for product and its expiration time.So, lets assume the website is hosted(www.example.com) and if you company gives offer product and its expiration time then the backend server should always run continously so that the server always check its expiration time and if expiration time meets, the running server deletes the Coupon Code from databases(Mongodb) so that users can no longer use that coupon code.So, we follow CRUD functionality…

In models/Product.Models.js

In models/CouponCode.Models.js

In controllers/CouponCode.js

Explanation of CouponCode.js: we have two time allocation,one is currentDate and endDate.

const endDate = new Date(expirationTime) .Here, expirationTime is the time provided in body of postman which is wrapped with new Date and decalared in endDate.

let currentDate = new Date().getTime(). Here currentDate is converted in new Date() which gives current time and getTIme() is used to produce the currentDate in number(eg:1243836373).

CouponCodeDiscount.findOne({ productId }).exec((newCouponCodePrice, couponCodePriceUpdate). Here, newCouponCodeprice defines the new Coupon Code set for product and couponcCodepriceUpdate defines the updated price,name on existed coupon code

Now, we also need delete option for coupon code after its expiration time meets.So, the logic behind this is, server should always run and deleting coupon code logic should be available in app.js(server) so that,no request has to be sent for deleting coupon code instead it deletes the coupon code automatically after its expiration time finishes.

In routes/CouponCodes.Routes.js

In app.js

Explanation of app.js of checkexpirationTime logic: Server always run so that our server delete automatically if coupon code expires.

Coupon.map((getCoupon) => { if ( new Date().getTime() >= new Date(getCoupon.expirationTime).getTime() ) CouponCodeDiscount.findOneAndDelete({ _id: getCoupon._id, }) setInterval(checkExpirationTime, 1000); // converting to millisecond

The above logic defines that new Date().getTime() represents current date and converts into number(eg:13526363) and new Date(getCoupon.expirationTime).getTime() represents expiration date and converts into number(eg:13527363) and ≥ means if the current date is greater or equals to expirationTime of coupon i.e the current coupon time meets the time of expirationtime or already expired then it would autoamtically delete from database as our server is running continously.In real, expiration time > current time (eg: 13527363>13526363). CouponCodeDiscount.findOneAndDelete deletes the coupon code from Mongodb database after expiration time finish.For setInterval(checkExpirationTime, 1000) , it checks and show result after coupon code is activated and deleted from database every 1000 millisecond(1 second) in our continuously running back-end server.So, in real world, to reduce our server load, we can extend the time for 3 days or 7 days instead of 1000 millisecond because coupon code offer might exists for 3 to 7 days.

Now, in postman,developer can test the code:

Post request : http:localhost:800/api/product/couponCode-discount

Post request for coupon code…

Coupon Code deletes from Mongodb database after expiration time finish…

Conclusion: This is all about Coupon Code and its expiration logic.Feedback are appreciated. If this article helps in your project then you can clap and support the article.