forked from Bee-Balanced/Server-Repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreak.js
More file actions
43 lines (36 loc) · 1.03 KB
/
streak.js
File metadata and controls
43 lines (36 loc) · 1.03 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
// streak.js
import db from "./db.js";
export async function markDayComplete(userId, dateStr) {
// dateStr must be "YYYY-MM-DD"
await db.query(
`INSERT IGNORE INTO daily_checkins (user_id, checkin_date)
VALUES (?, ?)`,
[userId, dateStr]
);
}
export async function getCurrentStreak(userId) {
const [rows] = await db.query(
`SELECT checkin_date
FROM daily_checkins
WHERE user_id = ?
ORDER BY checkin_date DESC`,
[userId]
);
const set = new Set(rows.map(r => toYMD(r.checkin_date)));
// If today isn't complete, start from yesterday
let cursor = new Date();
if (!set.has(toYMD(cursor))) cursor.setDate(cursor.getDate() - 1);
let streak = 0;
while (set.has(toYMD(cursor))) {
streak++;
cursor.setDate(cursor.getDate() - 1);
}
return streak;
}
function toYMD(d) {
const dt = d instanceof Date ? d : new Date(d);
const y = dt.getFullYear();
const m = String(dt.getMonth() + 1).padStart(2, "0");
const day = String(dt.getDate()).padStart(2, "0");
return `${y}-${m}-${day}`;
}