From cbc14d14779764fb08519a647f741234adbe2cb2 Mon Sep 17 00:00:00 2001 From: Alma Date: Sat, 12 Apr 2025 13:16:15 +0200 Subject: [PATCH] working leantime widget 13 --- app/api/leantime/status-labels/route.ts | 44 +++++++++++++++---------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/app/api/leantime/status-labels/route.ts b/app/api/leantime/status-labels/route.ts index 4f2f9b23..ebd83290 100644 --- a/app/api/leantime/status-labels/route.ts +++ b/app/api/leantime/status-labels/route.ts @@ -52,22 +52,27 @@ export async function GET() { }) }); + if (!userResponse.ok) { + const errorData = await userResponse.json(); + console.error('User lookup failed:', errorData); + if (userResponse.status === 429) { + const retryAfter = userResponse.headers.get('retry-after') || '60'; + return NextResponse.json( + { error: "Rate limit exceeded. Please try again later." }, + { + status: 429, + headers: { + 'Retry-After': retryAfter + } + } + ); + } + throw new Error('Failed to fetch user data from Leantime'); + } + const userData = await userResponse.json(); console.log('User lookup response:', userData); - if (userData.error === 'Too many requests per minute.') { - const retryAfter = userResponse.headers.get('retry-after') || '60'; - return NextResponse.json( - { error: "Rate limit exceeded. Please try again later." }, - { - status: 429, - headers: { - 'Retry-After': retryAfter - } - } - ); - } - if (!userData.result || !userData.result.id) { throw new Error('Could not find Leantime user ID'); } @@ -86,7 +91,7 @@ export async function GET() { return NextResponse.json({ projects: cachedLabels.data }); } - // Now fetch the status labels + // Now fetch the status labels using leantimeUserId const response = await fetch('https://agilite.slm-lab.net/api/jsonrpc', { method: 'POST', headers: { @@ -98,16 +103,19 @@ export async function GET() { method: 'leantime.rpc.Tickets.Tickets.getAllStatusLabelsByUserId', id: 1, params: { - userId: session.user.id + userId: leantimeUserId // Use the Leantime user ID here } }) }); if (!response.ok) { - throw new Error('Failed to fetch status labels from Leantime'); + const errorData = await response.json(); + console.error('Status labels fetch failed:', errorData); + throw new Error(`Failed to fetch status labels: ${errorData.error || 'Unknown error'}`); } const data = await response.json(); + console.log('Status labels response:', data); if (!data.result) { return NextResponse.json({ projects: [] }); @@ -120,7 +128,7 @@ export async function GET() { class: label.class, statusType: label.statusType, kanbanCol: label.kanbanCol, - sortKey: parseInt(label.sortKey) + sortKey: Number(label.sortKey) || 0 })); // Sort labels by sortKey @@ -142,7 +150,7 @@ export async function GET() { } catch (error) { console.error('Error fetching status labels:', error); return NextResponse.json( - { error: "Failed to fetch status labels" }, + { error: error instanceof Error ? error.message : "Failed to fetch status labels" }, { status: 500 } ); }