-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
35 lines (29 loc) · 1.04 KB
/
server.js
File metadata and controls
35 lines (29 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import express from 'express';
import next from 'next';
import cron from 'node-cron';
import { processEmails } from './src/app/tasks/emailProcessor.js';
import { connectToDatabase } from './src/lib/mongodb.js';
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
server.all('*', (req, res) => handle(req, res));
server.listen(3000, (err) => {
if (err) throw err;
console.log('> Ready on https://inboxiq-seven.vercel.app');
// Schedule the cron job
cron.schedule('*/5 * * * *', async () => {
const { db } = await connectToDatabase();
const users = await db.collection('Users').find({}).toArray();
for (const user of users) {
try {
await processEmails(user.userId);
console.log(`Processed emails for user ${user.userId}`);
} catch (error) {
console.error(`Failed to process emails for user ${user.userId}`, error);
}
}
});
});
});