Spaces:
Sleeping
Sleeping
Commit ·
034eb8f
1
Parent(s): fac8c03
fix: badges save/check, year selector for contributions
Browse files
src/app/api/spark/gamification/calendar/[username]/route.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { getUserCalendar } from "@/lib/db/queries/gamification";
|
|
| 3 |
import { fetchGitHubContributions, contributionLevelToIntensity } from "@/lib/github-contributions";
|
| 4 |
import { db } from "@/db";
|
| 5 |
import { users } from "@/db/schema";
|
| 6 |
-
import { eq } from "drizzle-orm";
|
| 7 |
|
| 8 |
export async function GET(
|
| 9 |
request: NextRequest,
|
|
@@ -16,18 +16,24 @@ export async function GET(
|
|
| 16 |
const yearParam = searchParams.get('year');
|
| 17 |
const year = yearParam ? parseInt(yearParam) : undefined;
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
const user = await db.select()
|
| 21 |
.from(users)
|
| 22 |
-
.where(
|
| 23 |
.limit(1);
|
| 24 |
|
| 25 |
const githubToken = user[0]?.githubAccessToken;
|
| 26 |
|
|
|
|
|
|
|
| 27 |
// Try fetching from GitHub first (with optional year parameter)
|
| 28 |
const githubData = await fetchGitHubContributions(username, githubToken, year);
|
| 29 |
|
| 30 |
if (githubData) {
|
|
|
|
|
|
|
| 31 |
// Convert GitHub data to calendar format
|
| 32 |
const calendar = githubData.weeks.flatMap(week =>
|
| 33 |
week.contributionDays.map(day => ({
|
|
@@ -41,11 +47,13 @@ export async function GET(
|
|
| 41 |
calendar,
|
| 42 |
totalContributions: githubData.totalContributions,
|
| 43 |
year: year || new Date().getFullYear(),
|
| 44 |
-
source: 'github'
|
|
|
|
| 45 |
});
|
| 46 |
}
|
| 47 |
|
| 48 |
// Fallback to local database
|
|
|
|
| 49 |
const calendar = await getUserCalendar(username, days);
|
| 50 |
return NextResponse.json({
|
| 51 |
calendar,
|
|
|
|
| 3 |
import { fetchGitHubContributions, contributionLevelToIntensity } from "@/lib/github-contributions";
|
| 4 |
import { db } from "@/db";
|
| 5 |
import { users } from "@/db/schema";
|
| 6 |
+
import { eq, sql } from "drizzle-orm";
|
| 7 |
|
| 8 |
export async function GET(
|
| 9 |
request: NextRequest,
|
|
|
|
| 16 |
const yearParam = searchParams.get('year');
|
| 17 |
const year = yearParam ? parseInt(yearParam) : undefined;
|
| 18 |
|
| 19 |
+
console.log(`[Calendar API] Fetching for username: ${username}, year: ${year || 'current'}`);
|
| 20 |
+
|
| 21 |
+
// Try to get user's GitHub token for API access (case-insensitive)
|
| 22 |
const user = await db.select()
|
| 23 |
.from(users)
|
| 24 |
+
.where(sql`LOWER(${users.username}) = LOWER(${username})`)
|
| 25 |
.limit(1);
|
| 26 |
|
| 27 |
const githubToken = user[0]?.githubAccessToken;
|
| 28 |
|
| 29 |
+
console.log(`[Calendar API] Found user: ${user[0]?.username || 'none'}, has token: ${!!githubToken}`);
|
| 30 |
+
|
| 31 |
// Try fetching from GitHub first (with optional year parameter)
|
| 32 |
const githubData = await fetchGitHubContributions(username, githubToken, year);
|
| 33 |
|
| 34 |
if (githubData) {
|
| 35 |
+
console.log(`[Calendar API] GitHub returned ${githubData.totalContributions} contributions for year ${year || 'current'}`);
|
| 36 |
+
|
| 37 |
// Convert GitHub data to calendar format
|
| 38 |
const calendar = githubData.weeks.flatMap(week =>
|
| 39 |
week.contributionDays.map(day => ({
|
|
|
|
| 47 |
calendar,
|
| 48 |
totalContributions: githubData.totalContributions,
|
| 49 |
year: year || new Date().getFullYear(),
|
| 50 |
+
source: 'github',
|
| 51 |
+
hasUserToken: !!githubToken
|
| 52 |
});
|
| 53 |
}
|
| 54 |
|
| 55 |
// Fallback to local database
|
| 56 |
+
console.log(`[Calendar API] Falling back to local database for ${username}`);
|
| 57 |
const calendar = await getUserCalendar(username, days);
|
| 58 |
return NextResponse.json({
|
| 59 |
calendar,
|
src/lib/github-contributions.ts
CHANGED
|
@@ -78,12 +78,15 @@ export async function fetchGitHubContributions(
|
|
| 78 |
|
| 79 |
// Use provided token or fall back to app token for public data
|
| 80 |
const token = githubToken || process.env.GITHUB_TOKEN;
|
|
|
|
| 81 |
|
| 82 |
if (!token) {
|
| 83 |
console.warn('[GitHub] No token available for contributions fetch');
|
| 84 |
return null;
|
| 85 |
}
|
| 86 |
|
|
|
|
|
|
|
| 87 |
try {
|
| 88 |
// Build variables object, including from/to dates if year is specified
|
| 89 |
const variables: { username: string; from?: string; to?: string } = { username };
|
|
|
|
| 78 |
|
| 79 |
// Use provided token or fall back to app token for public data
|
| 80 |
const token = githubToken || process.env.GITHUB_TOKEN;
|
| 81 |
+
const tokenSource = githubToken ? 'user token (includes private contributions)' : 'app token (public only)';
|
| 82 |
|
| 83 |
if (!token) {
|
| 84 |
console.warn('[GitHub] No token available for contributions fetch');
|
| 85 |
return null;
|
| 86 |
}
|
| 87 |
|
| 88 |
+
console.log(`[GitHub] Using ${tokenSource} for ${username}, year=${year || 'default'}`);
|
| 89 |
+
|
| 90 |
try {
|
| 91 |
// Build variables object, including from/to dates if year is specified
|
| 92 |
const variables: { username: string; from?: string; to?: string } = { username };
|