working leantime widget 22
This commit is contained in:
parent
56a37c68e3
commit
3f618d121a
@ -17,6 +17,9 @@ interface Project {
|
||||
labels: StatusLabel[];
|
||||
}
|
||||
|
||||
// Cache for user IDs to avoid repeated lookups
|
||||
const userCache = new Map<string, number>();
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const session = await getServerSession(authOptions);
|
||||
@ -25,29 +28,41 @@ export async function GET() {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Get user ID from Leantime
|
||||
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({
|
||||
jsonrpc: '2.0',
|
||||
method: 'leantime.rpc.users.getAll',
|
||||
id: 1,
|
||||
}),
|
||||
});
|
||||
// Check cache first
|
||||
let leantimeUserId = userCache.get(session.user.email);
|
||||
|
||||
if (!userResponse.ok) {
|
||||
throw new Error('Failed to fetch user from Leantime');
|
||||
}
|
||||
// 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({
|
||||
jsonrpc: '2.0',
|
||||
method: 'leantime.rpc.Users.Users.getUserByEmail',
|
||||
id: 1,
|
||||
params: {
|
||||
email: session.user.email
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
const userData = await userResponse.json();
|
||||
const user = userData.result.find((u: any) => u.email === session.user.email);
|
||||
if (!userResponse.ok) {
|
||||
throw new Error('Failed to fetch user from Leantime');
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "User not found in Leantime" }, { status: 404 });
|
||||
const userData = await userResponse.json();
|
||||
|
||||
if (!userData.result || !userData.result.id) {
|
||||
return NextResponse.json({ error: "User not found in Leantime" }, { status: 404 });
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// Get projects
|
||||
@ -82,7 +97,7 @@ export async function GET() {
|
||||
method: 'leantime.rpc.Tickets.Tickets.getAllStatusLabelsByUserId',
|
||||
id: 1,
|
||||
params: {
|
||||
userId: user.id
|
||||
userId: leantimeUserId
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user