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