-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheckUserStatus.js
More file actions
62 lines (50 loc) · 2.45 KB
/
checkUserStatus.js
File metadata and controls
62 lines (50 loc) · 2.45 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
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
// checkUserStatus.js
async function checkUserStatus(db) {
const now = new Date();
console.log(`[DEBUG] Current Time: ${now.toISOString()}`); // Debug log
try {
// Fetch all users
const users = await db.collection('Users').find().toArray();
console.log(`[DEBUG] Retrieved ${users.length} users from the database.`); // Debug log
for (const user of users) {
const { lastLogin, CheckInFreq, status, UserId, Username, Email } = user;
// Debugging: Log user details
console.log(`[DEBUG] Processing UserID: ${UserId}, Username: ${Username}, Status: ${status}`);
// Validate necessary fields
if (!lastLogin || !CheckInFreq || !status) {
console.warn(`[WARN] UserID: ${UserId} has incomplete data. Skipping.`);
continue; // Skip users with incomplete data
}
// Ensure CheckInFreq is a positive number
if (typeof CheckInFreq !== 'number' || CheckInFreq <= 0) {
console.warn(`[WARN] UserID: ${UserId} has invalid CheckInFreq (${CheckInFreq}). Skipping.`);
continue;
}
// Calculate next expected login time
const nextCheckIn = new Date(lastLogin);
// Corrected: Use setSeconds() and getSeconds()
nextCheckIn.setSeconds(nextCheckIn.getSeconds() + CheckInFreq);
console.log(`[DEBUG] UserID: ${UserId} next expected login time: ${nextCheckIn.toUTCString()}`);
// Check if the user has not logged in within the CheckInFreq
if (nextCheckIn < now && status.toLowerCase() === "active") {
console.log(`[INFO] UserID: ${UserId} (${Username}) has not logged in since ${lastLogin}. Marking as Inactive.`);
// Update user status to "Inactive"
const updateResult = await db.collection('Users').updateOne(
{ UserId: UserId },
{ $set: { status: "Inactive" } }
);
if (updateResult.modifiedCount === 1) {
console.log(`[SUCCESS] UserID: ${UserId} status updated to "Inactive".`);
} else {
console.warn(`[WARN] Failed to update status for UserID: ${UserId}.`);
}
} else {
console.log(`[INFO] UserID: ${UserId} (${Username}) is still active.`);
}
}
} catch (error) {
console.error(`[ERROR] Error in checkUserStatus: ${error.message}`);
console.error(error); // Full stack trace for debugging
}
}
module.exports = checkUserStatus;