-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcronjobs.js
177 lines (150 loc) · 6.56 KB
/
cronjobs.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Clean up expired notifications
const cron = require('node-cron');
const path = require('path');
const { purgeExpiredNotifications } = require('../controllers/notificationcontroller');
// Run daily at midnight
cron.schedule('0 0 * * *', () => {
console.log('Running daily notification purge...');
purgeExpiredNotifications();
});
// Now the daily email job (at midnight too)
const User = require('../models/user');
const nodemailer = require('nodemailer'); // Or another email service
// Set up Nodemailer (example configuration)
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS
}
});
// Function to send the daily digest email
async function sendDailyDigestEmail(user) {
const mailOptions = {
from: process.env.EMAIL_USER,
to: user.email,
subject: 'Your Daily Activity Summary',
text: `
Hi ${user.username},
Here is your activity summary for today:
- Total visits to your links: ${user.dailyVisits}
- Total tolls earned: ${user.dailyTollsEarned} credits
Keep up the great work and check in tomorrow for more opportunities!
Best,
The WWWOpoly Team
`
};
try {
await transporter.sendMail(mailOptions);
console.log(`Daily digest sent to ${user.email}`);
} catch (error) {
console.error(`Failed to send daily digest to ${user.email}:`, error);
}
}
// Cron job to run daily at midnight UTC
cron.schedule('0 0 * * *', async () => {
console.log('Running daily digest job...');
const users = await User.find({ 'emailPreferences.dailyDigest': true });
for (const user of users) {
await sendDailyDigestEmail(user);
}
console.log('Daily digest job completed');
});
// Run the tournament controller every week
const tournamentController = require('../controllers/tournamentcontroller');
// Start a new tournament every Monday at midnight
cron.schedule('0 0 * * MON', async () => {
await tournamentController.startNewTournament();
});
// End the tournament every Sunday at midnight
cron.schedule('59 23 * * SUN', async () => {
await tournamentController.endTournament();
});
// utils/cronjobs.js (add this to the same cronjobs file)
cron.schedule('59 23 * * SUN', async () => { // Run every Sunday at 11:59 PM
try {
const topSitesVisited = await User.find().sort({ 'weeklyMetrics.sitesVisited': -1 }).limit(3);
const topTollsCollected = await User.find().sort({ 'weeklyMetrics.tollsCollected': -1 }).limit(3);
const topTradesMade = await User.find().sort({ 'weeklyMetrics.tradesMade': -1 }).limit(3);
const rewards = { credits: 1000, points: 500 }; // Example rewards
const categories = [
{ topUsers: topSitesVisited, category: 'Most Sites Visited' },
{ topUsers: topTollsCollected, category: 'Most Tolls Collected' },
{ topUsers: topTradesMade, category: 'Most Trades Made' },
];
for (const { topUsers, category } of categories) {
for (const user of topUsers) {
user.credits += rewards.credits;
user.points += rewards.points;
await user.save();
sendNotification(user._id, 'Tournament Reward', `Congratulations! You placed in the top 3 for "${category}" and earned ${rewards.credits} credits and ${rewards.points} points.`);
}
}
console.log('Weekly tournament rewards distributed.');
} catch (error) {
console.error('Error distributing tournament rewards:', error);
}
});
const IndustryEvent = require('../models/industryevent');
// Scheduler to deactivate expired events every hour
cron.schedule('0 * * * *', async () => { // Runs at the start of every hour
const now = new Date();
try {
const expiredEvents = await IndustryEvent.updateMany(
{ endDate: { $lt: now }, isActive: true },
{ isActive: false }
);
if (expiredEvents.nModified > 0) {
console.log(`Deactivated ${expiredEvents.nModified} expired events.`);
}
} catch (error) {
console.error('Error deactivating expired events:', error);
}
});
const { adjustInflation, applyMaintenanceFees } = require('../controllers/globaleconomycontroller');
// Schedule inflation adjustment every day at midnight
cron.schedule('0 0 * * *', async () => {
console.log('Running daily inflation adjustment...');
await adjustInflation();
});
// Schedule maintenance fee application every week (Sunday at midnight)
cron.schedule('0 0 * * 0', async () => {
console.log('Applying weekly maintenance fees...');
await applyMaintenanceFees();
});
// Fuel checker
const Generator = require('../models/generator');
const FuelInventory = require('../models/fuelinventory');
const { sendNotification } = require('../utils/notifications');
// Run fuel consumption check every hour
cron.schedule('0 * * * *', async () => {
try {
const generators = await Generator.find({});
for (const generator of generators) {
const fuelInventory = await FuelInventory.findOne({ user: generator.user });
// Check if 12 hours have elapsed since the last refill
const hoursElapsed = (Date.now() - generator.lastRefill) / (1000 * 60 * 60);
if (hoursElapsed >= 12) {
if (fuelInventory.fuelAmount >= generator.fuelCapacity) {
// Deduct fuel and update last refill time
fuelInventory.fuelAmount -= generator.fuelCapacity;
generator.lastRefill = new Date();
await fuelInventory.save();
await generator.save();
} else {
// Not enough fuel - send notification and shut down generator
generator.isOperational = false;
await generator.save();
await sendNotification(generator.user, 'Generator Shut Down', 'Your generator has shut down due to lack of fuel.');
}
}
// Send a low fuel warning if fuel is below a threshold (e.g., less than 10 hours of fuel)
if (fuelInventory.fuelAmount < 10) {
await sendNotification(generator.user, 'Low Fuel Warning', 'Your fuel level is low. Refill soon to keep your generator running.');
}
}
} catch (error) {
console.error('Error running fuel consumption scheduler:', error);
}
});
module.exports = cron;