Skip to content

Commit

Permalink
add service/expiry.js in Server
Browse files Browse the repository at this point in the history
  • Loading branch information
MrVSiK committed Mar 8, 2022
1 parent d420e9e commit 5fbf42a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions Server/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const express = require("express");
const cors = require("cors");
// const scheduleExpiryJobs = require("./services/expiry");
require("dotenv").config();


Expand Down
21 changes: 21 additions & 0 deletions Server/services/expiry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const cron = require("node-cron");
const Doc = require("../models/doc");

const scheduleExpiryJobs = () => {
cron.schedule("0 0 * * * *", () => {
Doc.find({}, (err, docs) => {
if (err) return console.log(err);

docs.forEach((doc) => {
if (doc.expiryDate < Date.now()) {
Doc.deleteOne({ _id: doc._id }, (err) => {
if (err) return console.log(err);
console.log("Document expired");
});
}
});
});
});
};

module.exports = scheduleExpiryJobs;

0 comments on commit 5fbf42a

Please sign in to comment.