Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"mongoose-autopopulate": "^1.1.0",
"multer": "^1.4.4",
"node-cache": "^5.1.2",
"node-cron": "^4.2.1",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rahul-rajak-nsut why we are not using agenda or node-schedule in place of node-cron.

"openai": "^4.27.0",
"pug": "^3.0.2",
"resend": "^2.0.0",
Expand Down
36 changes: 36 additions & 0 deletions backend/src/cron/invoiceCron.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const cron = require('node-cron');
const Invoice = require('../models/appModels/Invoice');

// 📚 Cron format: 'minute hour day month weekday'
// '0 0 * * *' means → at 00:00 (midnight) every day
cron.schedule('0 0 * * *', async () => {
console.log('⏰ Running daily invoice expiry check...');

const today = new Date();

try {
// 📚 updateMany = update ALL documents that match the filter
const expiredInvoices = await Invoice.updateMany(
{
// 📚 $lt = "less than" → find invoices where expiredDate < today
expiredDate: { $lt: today },
isOverdue: false, // only update ones not already marked
removed: false, // skip deleted invoices
},
{
// 📚 $set = update these fields
$set: {
isOverdue: true,
status: 'overdue',
},
}
);

console.log(`✅ ${expiredInvoices.modifiedCount} invoices marked as overdue`);

} catch (error) {
console.error('❌ Cron job failed:', error.message);
}
});

console.log('✅ Invoice expiry cron job scheduled — runs daily at midnight!');
3 changes: 3 additions & 0 deletions backend/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ for (const filePath of modelsFiles) {
require(path.resolve(filePath));
}

// Start cron jobs
require('./src/cron/invoiceCron');

// Start our app!
const app = require('./app');
app.set('port', process.env.PORT || 8888);
Expand Down
Loading