-
Notifications
You must be signed in to change notification settings - Fork 138
feat: Add Interview Streak Tracking Feature (#625) #1533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,22 @@ const { VALID_ACHIEVEMENTS } = require('../constants/achievements'); | |
|
|
||
| exports.getAchievements = async (req, res) => { | ||
| try { | ||
| const user = await User.findById(req.user._id).select('unlockedAchievements'); | ||
| const user = await User.findById(req.user._id).select('unlockedAchievements lastPracticeDate currentStreak'); | ||
| if (!user) return res.status(404).json({ success: false, error: 'User not found' }); | ||
|
|
||
| // Reset streak to 0 if one or more calendar days were missed | ||
| if (user.lastPracticeDate && user.currentStreak > 0) { | ||
| const now = new Date(); | ||
| const d1 = new Date(user.lastPracticeDate); | ||
| const utc1 = Date.UTC(d1.getUTCFullYear(), d1.getUTCMonth(), d1.getUTCDate()); | ||
| const utc2 = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()); | ||
| const diffDays = Math.floor((utc2 - utc1) / (1000 * 60 * 60 * 24)); | ||
| if (diffDays > 1) { | ||
| user.currentStreak = 0; | ||
| await user.save(); | ||
|
Comment on lines
+16
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift 🧩 Analysis chain🏁 Script executed: #!/usr/bin/env bash
set -euo pipefail
# Inspect the affected handlers and Mongoose concurrency configuration.
ast-grep outline backend/controllers/achievementController.js --items all
ast-grep outline backend/controllers/authController.js --items all
ast-grep outline backend/models/User.js --items all
# Expect no custom protection to be required after the reset becomes conditional.
rg -n -C 3 'optimisticConcurrency|versionKey|pre\(|post\(|currentStreak|lastPracticeDate' \
backend/controllers/achievementController.js \
backend/controllers/authController.js \
backend/controllers/sessionController.js \
backend/models/User.jsRepository: Canopus-Labs/PrepPilot Length of output: 8050 🏁 Script executed: #!/usr/bin/env bash
set -euo pipefail
printf '%s\n' '--- achievement handler ---'
sed -n '1,80p' backend/controllers/achievementController.js
printf '%s\n' '--- profile handler ---'
sed -n '390,455p' backend/controllers/authController.js
printf '%s\n' '--- session write path ---'
sed -n '70,155p' backend/controllers/sessionController.js
printf '%s\n' '--- User schema ---'
sed -n '1,110p' backend/models/User.js
printf '%s\n' '--- dependency and schema configuration ---'
rg -n -C 2 '"mongoose"|optimisticConcurrency|versionKey|findOneAndUpdate|findByIdAndUpdate|save\(' \
package.json backend package-lock.json yarn.lock 2>/dev/null || trueRepository: Canopus-Labs/PrepPilot Length of output: 35642 🏁 Script executed: #!/usr/bin/env bash
set -euo pipefail
printf '%s\n' '--- authentication middleware ---'
sed -n '1,180p' backend/middlewares/authMiddleware.js
printf '%s\n' '--- routes for the affected handlers ---'
rg -n -C 4 'getUserProfile|getAchievements|createSession|profile|achievements' \
backend/routes backend/controllers backend/middlewares
printf '%s\n' '--- relevant controller tests ---'
sed -n '1,240p' backend/tests/achievementController.unit.test.js
rg -n -C 5 'getUserProfile|currentStreak|lastPracticeDate|createSession' backend/testsRepository: Canopus-Labs/PrepPilot Length of output: 27972 🏁 Script executed: #!/usr/bin/env bash
set -euo pipefail
python3 - <<'PY'
from pathlib import Path
import re
achievement = Path("backend/controllers/achievementController.js").read_text()
auth = Path("backend/controllers/authController.js").read_text()
session = Path("backend/controllers/sessionController.js").read_text()
user = Path("backend/models/User.js").read_text()
checks = {
"achievement reads then saves reset": re.search(
r"findById\(req\.user\._id\).*?currentStreak.*?user\.currentStreak\s*=\s*0;\s*await user\.save\(\)",
achievement, re.S
),
"profile uses middleware-loaded document": re.search(
r"const user = req\.user;.*?user\.currentStreak\s*=\s*0;\s*await user\.save\(\)",
auth, re.S
),
"session updates streak and practice date": re.search(
r"user\.currentStreak.*?user\.lastPracticeDate\s*=\s*now;.*?await user\.save",
session, re.S
),
"User schema has no optimisticConcurrency": not re.search(
r"optimisticConcurrency\s*:\s*true", user
),
}
for name, result in checks.items():
print(f"{'PASS' if result else 'FAIL'}: {name}")
if not all(checks.values()):
raise SystemExit(1)
print("Both reset handlers can issue an unconditional save after a stale read.")
print("The session path can write a new currentStreak and lastPracticeDate between that read and save.")
PYRepository: Canopus-Labs/PrepPilot Length of output: 509 Prevent stale streak resets from overwriting new practice data. Use an atomic conditional update in both handlers. Match the user only when 📍 Affects 2 files
🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
|
|
||
| res.json({ success: true, unlockedAchievements: user.unlockedAchievements }); | ||
| } catch (err) { | ||
| res.status(500).json({ success: false, error: "A server error occurred" }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Keep streak badges out of the client-writable allowlist.
saveAchievementsaccepts every value inVALID_ACHIEVEMENTS. A client can submit"3-Day Streak","7-Day Streak", or"30-Day Streak"without completing the required streak.Reject server-managed streak badges in
saveAchievements, or remove these identifiers fromVALID_ACHIEVEMENTS. KeepsessionControlleras the only writer for these badges.🤖 Prompt for AI Agents