Skip to content
Closed
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
28 changes: 25 additions & 3 deletions src/app/api/badge/streak-shield/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ import { checkBadgeRateLimit, getBadgeClientIp } from "@/lib/badge-rate-limit";
import { calculateStreakFromDates } from "@/lib/streak";
import { logError } from "@/lib/error-handler";
import { normalizeGitHubUsername } from "@/lib/validate-github-username";
import { supabaseAdmin } from "@/lib/supabase";

/**
* Resolves the stored IANA timezone for a GitHub user so the badge's streak
* calculation agrees with the dashboard's (see /api/metrics/streak). Falls
* back to "UTC" if the user has no DevTrack account or no timezone set.
*/
async function resolveUserTimeZone(username: string): Promise<string> {
try {
const { data } = await supabaseAdmin
.from("users")
.select("timezone")
.ilike("github_login", username)
.maybeSingle();

return data?.timezone || "UTC";
} catch {
return "UTC";
}
}

export const dynamic = "force-dynamic";

Expand Down Expand Up @@ -34,7 +54,8 @@ async function fetchGitHubWithToken(

async function fetchStreak(
username: string,
token?: string
token?: string,
timeZone = "UTC"
): Promise<StreakData> {
const since = new Date();
since.setDate(since.getDate() - 90);
Expand Down Expand Up @@ -75,7 +96,7 @@ async function fetchStreak(
activeDates.add(item.commit.author.date.slice(0, 10));
}

const result = calculateStreakFromDates(activeDates);
const result = calculateStreakFromDates(activeDates, new Set(), timeZone);
return {
current: result.current,
longest: result.longest,
Expand Down Expand Up @@ -115,7 +136,8 @@ export async function GET(req: NextRequest) {
}

const githubToken = process.env.GITHUB_TOKEN;
const streak = await fetchStreak(username, githubToken);
const timeZone = await resolveUserTimeZone(username);
const streak = await fetchStreak(username, githubToken, timeZone);

const svg = generateBadgeSVG({
label: "DevTrack",
Expand Down
Loading
Loading