Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions src/lib/gamification/streaks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,33 +162,3 @@ export async function updateStreak(apiKeyId: string): Promise<number> {

return newStreak;
}

/**
* Check if a streak is still active (last activity within the last 2 days).
* Does not modify any data.
*
* @param apiKeyId - The API key identifier
* @returns true if the streak is still alive
*/
export async function isStreakActive(apiKeyId: string): Promise<boolean> {
const streak = await getStreak(apiKeyId);
if (!streak.lastActiveDate) return false;

const last = new Date(streak.lastActiveDate + "T00:00:00Z").getTime();
const now = Date.now();
const daysSince = Math.floor((now - last) / MS_PER_DAY);

return daysSince <= 1; // Today or yesterday
}

/**
* Reset streak data for an API key (admin/testing use).
*
* @param apiKeyId - The API key identifier
*/
export async function resetStreak(apiKeyId: string): Promise<void> {
if (isBuildPhase || isCloud) return;

const db = getDbInstance() as unknown as DbLike;
db.prepare("DELETE FROM key_value WHERE namespace = ? AND key = ?").run(NAMESPACE, apiKeyId);
}
10 changes: 10 additions & 0 deletions tests/unit/gamification/streaks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,15 @@ describe("Streak Tracker", () => {
const second = await updateStreak("test-user-2");
assert.equal(first, second);
});

it("stores date metadata when starting a streak", async () => {
await updateStreak("test-user-3");

const streak = await getStreak("test-user-3");
assert.equal(streak.currentStreak, 1);
assert.equal(streak.longestStreak, 1);
assert.match(streak.lastActiveDate, /^\d{4}-\d{2}-\d{2}$/);
assert.equal(streak.streakStartDate, streak.lastActiveDate);
});
});
});
Loading