Skip to content

Commit

Permalink
Add node-cron for daily email cleanup older than 30 days
Browse files Browse the repository at this point in the history
  • Loading branch information
hppanpaliya committed Jan 22, 2024
1 parent 9281d20 commit 6bbe5d2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions mailserver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"express": "^4.18.2",
"mailparser": "^3.6.4",
"mongodb": "^5.6.0",
"node-cron": "^3.0.3",
"nodemailer": "^6.9.3",
"nodemon": "^2.0.22",
"querystring": "^0.2.1",
Expand Down
15 changes: 15 additions & 0 deletions mailserver/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const emailRoutes = require("./emailRoutes");
const attachmentRoutes = require("./attachmentRoutes");
const cors = require("cors");
const path = require("path");
const cron = require('node-cron');
const { getOldEmails } = require("./emailHandler");
const { deleteEmailAndAttachments } = require("./emailHandler");


async function startSMTPServer() {
const server = new SMTPServer({
Expand Down Expand Up @@ -56,6 +60,16 @@ async function startWebServer() {
});
}


cron.schedule('0 2 * * *', async () => {
console.log('Running a daily check for old emails and attachments...');
const oldEmails = await getOldEmails(30); // Get emails older than 30 days
console.log('oldEmails', oldEmails);
for (const email of oldEmails) {
await deleteEmailAndAttachments(email.emailID, email.emailId.toHexString()); // Delete each old email
}
});

async function startServer() {
try {
await startSMTPServer();
Expand All @@ -68,3 +82,4 @@ async function startServer() {
}

startServer();

12 changes: 12 additions & 0 deletions mailserver/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,13 @@ negotiator@0.6.3:
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.3.tgz#58e323a72fedc0d6f9cd4d31fe49f51479590ccd"
integrity sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==

node-cron@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/node-cron/-/node-cron-3.0.3.tgz#c4bc7173dd96d96c50bdb51122c64415458caff2"
integrity sha512-dOal67//nohNgYWb+nWmg5dkFdIwDm8EpeGYMekPMrngV3637lqnX0lbUcCtgibHTz6SEz7DAIjKvKDFYCnO1A==
dependencies:
uuid "8.3.2"

nodemailer@6.9.2:
version "6.9.2"
resolved "https://registry.yarnpkg.com/nodemailer/-/nodemailer-6.9.2.tgz#b79051811edd52c2436ad1c6aed2dc45b9c9cf1f"
Expand Down Expand Up @@ -1076,6 +1083,11 @@ uue@~1.0.0:
resolved "https://registry.yarnpkg.com/uue/-/uue-1.0.0.tgz#213b9448b98b98b9d03caf601a223589bd886430"
integrity sha512-gEwvMeDsv0Q4SFsjhp/PmsXizTfEn3B8SXqtRiz5jE2nsRgEBCY5W5gmxOFrPVzbnDCELwOKyMnOEoL/W/TG7w==

uuid@8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==

vary@^1, vary@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
Expand Down

0 comments on commit 6bbe5d2

Please sign in to comment.