-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclear-attendance.js
More file actions
77 lines (65 loc) · 2.7 KB
/
clear-attendance.js
File metadata and controls
77 lines (65 loc) · 2.7 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import 'dotenv/config';
import { db } from './server/db.js';
import { attendanceRecords, audioRecordings, users } from './shared/schema.js';
import { sql, eq, and } from 'drizzle-orm';
import readline from 'readline';
const today = '2025-08-25';
async function clearAttendance() {
try {
const usersWithMultipleEntries = await db
.select({
userId: attendanceRecords.userId,
username: users.username,
count: sql`count(${attendanceRecords.id})`,
})
.from(attendanceRecords)
.leftJoin(users, eq(users.id, attendanceRecords.userId))
.where(eq(attendanceRecords.date, today))
.groupBy(attendanceRecords.userId, users.username)
.having(sql`count(${attendanceRecords.id}) > 1`);
if (usersWithMultipleEntries.length === 0) {
console.log("No users with multiple attendance entries for today.");
process.exit(0);
}
console.log("Users with multiple attendance entries for today:");
usersWithMultipleEntries.forEach((user, index) => {
console.log(`${index + 1}. ${user.username} (User ID: ${user.userId}) - ${user.count} entries`);
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Enter the number of the user whose attendance you want to clear (or type 'all' to clear for all listed users, or 'exit' to cancel): ", async (answer) => {
if (answer.toLowerCase() === 'exit') {
console.log("Operation cancelled.");
rl.close();
process.exit(0);
}
let usersToClear = [];
if (answer.toLowerCase() === 'all') {
usersToClear = usersWithMultipleEntries.map(u => u.userId);
} else {
const userIndex = parseInt(answer) - 1;
if (userIndex >= 0 && userIndex < usersWithMultipleEntries.length) {
usersToClear.push(usersWithMultipleEntries[userIndex].userId);
} else {
console.log("Invalid selection.");
rl.close();
process.exit(1);
}
}
for (const userId of usersToClear) {
await db.delete(audioRecordings).where(and(eq(audioRecordings.userId, userId), eq(audioRecordings.recordingDate, today)));
await db.delete(attendanceRecords).where(and(eq(attendanceRecords.userId, userId), eq(attendanceRecords.date, today)));
const user = usersWithMultipleEntries.find(u => u.userId === userId);
console.log(`Cleared attendance for ${user.username} (User ID: ${userId})`);
}
rl.close();
process.exit(0);
});
} catch (error) {
console.error("Error:", error.message);
process.exit(1);
}
}
clearAttendance();