working leantime widget 6
This commit is contained in:
parent
7a66d356aa
commit
aabe557fad
@ -2,6 +2,9 @@ import { getServerSession } from "next-auth/next";
|
||||
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
// Simple in-memory cache for user IDs
|
||||
const userCache = new Map<string, number>();
|
||||
|
||||
export async function GET() {
|
||||
const session = await getServerSession(authOptions);
|
||||
|
||||
@ -13,32 +16,47 @@ export async function GET() {
|
||||
console.log('Fetching status labels for user:', session.user.id);
|
||||
console.log('Using LEANTIME_TOKEN:', process.env.LEANTIME_TOKEN ? 'Present' : 'Missing');
|
||||
|
||||
// First, get the Leantime user ID for the current user
|
||||
const userResponse = await fetch('https://agilite.slm-lab.net/api/jsonrpc', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-API-Key': process.env.LEANTIME_TOKEN || '',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
method: 'leantime.rpc.Users.Users.getUserByEmail',
|
||||
jsonrpc: '2.0',
|
||||
id: 1,
|
||||
params: {
|
||||
email: session.user.email
|
||||
}
|
||||
})
|
||||
});
|
||||
// Check cache first
|
||||
let leantimeUserId = userCache.get(session.user.email);
|
||||
|
||||
// If not in cache, fetch from API
|
||||
if (!leantimeUserId) {
|
||||
const userResponse = await fetch('https://agilite.slm-lab.net/api/jsonrpc', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-API-Key': process.env.LEANTIME_TOKEN || '',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
method: 'leantime.rpc.Users.Users.getUserByEmail',
|
||||
jsonrpc: '2.0',
|
||||
id: 1,
|
||||
params: {
|
||||
email: session.user.email
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
const userData = await userResponse.json();
|
||||
console.log('User lookup response:', userData);
|
||||
const userData = await userResponse.json();
|
||||
console.log('User lookup response:', userData);
|
||||
|
||||
if (!userData.result || !userData.result.id) {
|
||||
throw new Error('Could not find Leantime user ID');
|
||||
if (userData.error === 'Too many requests per minute.') {
|
||||
return NextResponse.json(
|
||||
{ error: "Rate limit exceeded. Please try again in a minute." },
|
||||
{ status: 429 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!userData.result || !userData.result.id) {
|
||||
throw new Error('Could not find Leantime user ID');
|
||||
}
|
||||
|
||||
leantimeUserId = userData.result.id;
|
||||
// Cache the user ID for 5 minutes
|
||||
userCache.set(session.user.email, leantimeUserId);
|
||||
setTimeout(() => userCache.delete(session.user.email), 5 * 60 * 1000);
|
||||
}
|
||||
|
||||
const leantimeUserId = userData.result.id;
|
||||
|
||||
// Now fetch the status labels
|
||||
const response = await fetch('https://agilite.slm-lab.net/api/jsonrpc', {
|
||||
method: 'POST',
|
||||
@ -68,6 +86,12 @@ export async function GET() {
|
||||
if (response.status === 403) {
|
||||
return NextResponse.json({ error: "Forbidden access to Leantime API" }, { status: 403 });
|
||||
}
|
||||
if (response.status === 429) {
|
||||
return NextResponse.json(
|
||||
{ error: "Rate limit exceeded. Please try again in a minute." },
|
||||
{ status: 429 }
|
||||
);
|
||||
}
|
||||
throw new Error(`Leantime API returned ${response.status}: ${responseText}`);
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user