-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
111 lines (91 loc) · 3.69 KB
/
server.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
const express = require('express');
const app = express();
const { connectToDatabase } = require('./database.js');
const PORT = 7071;
const User = require('./User.js');
app.use(express.json());
connectToDatabase();
app.get('/', (req, res) => {
res.send('Hello from the server!');
res.send('Server is running!');
console.log('Server is running!');
});
app.post('/coding-session', async (req, res) => {
console.log('Received coding session request:', req.body);
const { userId, duration, sessionDate, languages } = req.body;
if (!userId || !duration || !sessionDate) {
console.log('Missing required fields:', { userId, duration, sessionDate });
return res.status(400).json({ message: "Missing required fields" });
}
try {
let user = await User.findOne({ userId });
console.log('User found:', user);
const today = new Date(sessionDate);
// If user doesn't exist, create a new document
if (!user) {
console.log('Creating new user:', userId);
user = new User({ userId });
}
const lastSessionDate = user.lastSessionDate ? new Date(user.lastSessionDate) : null;
console.log('Last session date:', lastSessionDate);
// Update total coding time
user.totalCodingTime += duration;
console.log('Updated total coding time:', user.totalCodingTime);
// Streak logic
if (lastSessionDate) {
const daysBetween = Math.floor((today - lastSessionDate) / (1000 * 60 * 60 * 24));
console.log('Days between sessions:', daysBetween);
if (daysBetween === 1) {
user.currentStreak += 1;
console.log('Increased streak:', user.currentStreak);
} else if (daysBetween > 1) {
user.currentStreak = 1;
console.log('Reset streak to 1');
}
} else {
user.currentStreak = 1;
console.log('First session, streak set to 1');
}
// Update longest streak if the current streak exceeds it
if (user.currentStreak > user.longestStreak) {
user.longestStreak = user.currentStreak;
console.log('Updated longest streak:', user.longestStreak);
}
// Update last session date
user.lastSessionDate = today;
console.log('Updated last session date:', user.lastSessionDate);
// Update language-specific coding time
for (const lang in languages) {
if (languages.hasOwnProperty(lang) && user.languages.hasOwnProperty(lang)) {
user.languages[lang] += languages[lang];
console.log(`Updated ${lang} time:`, user.languages[lang]);
}
}
// Save the updated user document
await user.save();
console.log('User saved successfully');
res.status(200).json({ message: "Session recorded successfully!" });
} catch (error) {
console.error("Error recording session:", error);
res.status(500).json({ message: "Error recording session" });
}
});
app.post('/link', async (req, res) => {
console.log("POST /link endpoint hit");
const { userId } = req.body;
try {
let user = await User.findOne({ userId });
if (!user) {
user = new User({ userId });
await user.save();
}
res.status(200).json({ message: "User linked successfully" });
console.log(`User ${userId} linked successfully.`);
} catch (error) {
console.error("Error linking user:", error);
res.status(500).json({ message: "Error linking user" });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});