From 1f8090e1ff4426a4ee856d51a0bfc3a00edd4dd3 Mon Sep 17 00:00:00 2001 From: Alma Date: Sat, 12 Apr 2025 15:09:17 +0200 Subject: [PATCH] working leantime widget 47 --- app/api/leantime/tasks/route.ts | 44 +++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/app/api/leantime/tasks/route.ts b/app/api/leantime/tasks/route.ts index dc537fa4..391e3f6c 100644 --- a/app/api/leantime/tasks/route.ts +++ b/app/api/leantime/tasks/route.ts @@ -15,6 +15,12 @@ interface Task { async function getLeantimeUserId(email: string): Promise { try { + if (!process.env.LEANTIME_TOKEN) { + console.error('LEANTIME_TOKEN is not set in environment variables'); + return null; + } + + console.log('Fetching Leantime users for email:', email); const response = await fetch(`${process.env.LEANTIME_API_URL}/api/jsonrpc`, { method: 'POST', headers: { @@ -29,13 +35,30 @@ async function getLeantimeUserId(email: string): Promise { }); if (!response.ok) { - console.error('Failed to fetch Leantime users'); + console.error('Failed to fetch Leantime users:', { + status: response.status, + statusText: response.statusText + }); return null; } const data = await response.json(); + console.log('Leantime users response:', data); + + if (!data.result || !Array.isArray(data.result)) { + console.error('Invalid response format from Leantime users API'); + return null; + } + const users = data.result; const user = users.find((u: any) => u.email === email); + + if (user) { + console.log('Found Leantime user:', { id: user.id, email: user.email }); + } else { + console.log('No Leantime user found for email:', email); + } + return user ? user.id : null; } catch (error) { console.error('Error fetching Leantime user ID:', error); @@ -50,12 +73,15 @@ export async function GET(request: NextRequest) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } + console.log('Fetching tasks for user:', session.user.email); const userId = await getLeantimeUserId(session.user.email); + if (!userId) { + console.error('User not found in Leantime:', session.user.email); return NextResponse.json({ error: "User not found in Leantime" }, { status: 404 }); } - // Fetch tasks assigned to the user + console.log('Fetching tasks for Leantime user ID:', userId); const response = await fetch(`${process.env.LEANTIME_API_URL}/api/jsonrpc`, { method: 'POST', headers: { @@ -74,10 +100,24 @@ export async function GET(request: NextRequest) { }); if (!response.ok) { + console.error('Failed to fetch tasks from Leantime:', { + status: response.status, + statusText: response.statusText + }); throw new Error('Failed to fetch tasks from Leantime'); } const data = await response.json(); + console.log('Leantime tasks response:', { + success: true, + taskCount: data.result?.length || 0 + }); + + if (!data.result || !Array.isArray(data.result)) { + console.error('Invalid response format from Leantime tasks API'); + throw new Error('Invalid response format from Leantime'); + } + const tasks = data.result.map((task: any) => ({ id: task.id.toString(), headline: task.headline,