working leantime widget 41

This commit is contained in:
Alma 2025-04-12 14:40:35 +02:00
parent 30dc143ffd
commit 88211acab6

View File

@ -26,7 +26,7 @@ async function getLeantimeUserId(email: string): Promise<number | null> {
} }
try { try {
console.log('Fetching Leantime users with token:', process.env.LEANTIME_TOKEN ? 'Token present' : 'Token missing'); console.log('Fetching Leantime user with token:', process.env.LEANTIME_TOKEN ? 'Token present' : 'Token missing');
const response = await fetch('https://agilite.slm-lab.net/api/jsonrpc', { const response = await fetch('https://agilite.slm-lab.net/api/jsonrpc', {
method: 'POST', method: 'POST',
@ -36,34 +36,35 @@ async function getLeantimeUserId(email: string): Promise<number | null> {
}, },
body: JSON.stringify({ body: JSON.stringify({
jsonrpc: '2.0', jsonrpc: '2.0',
method: 'leantime.rpc.users.getAll', method: 'leantime.rpc.Users.Users.getUserByEmail',
id: 1, id: 1,
params: {
email: email
}
}), }),
}); });
if (!response.ok) { if (!response.ok) {
console.error('Failed to fetch users from Leantime:', { console.error('Failed to fetch user from Leantime:', {
status: response.status, status: response.status,
statusText: response.statusText statusText: response.statusText
}); });
throw new Error(`Failed to fetch users from Leantime: ${response.status} ${response.statusText}`); throw new Error(`Failed to fetch user from Leantime: ${response.status} ${response.statusText}`);
} }
const data = await response.json(); const data = await response.json();
console.log('Leantime users response:', data); console.log('Leantime user response:', data);
const user = data.result.find((u: any) => u.email === email); if (!data.result || data.result === false) {
console.log('Found user:', user ? 'Yes' : 'No'); console.log('User not found in Leantime');
return null;
if (user) {
// Cache the user ID
userCache.set(email, user.id);
// Clear cache after 5 minutes
setTimeout(() => userCache.delete(email), 5 * 60 * 1000);
return user.id;
} }
return null; // Cache the user ID
userCache.set(email, data.result.id);
// Clear cache after 5 minutes
setTimeout(() => userCache.delete(email), 5 * 60 * 1000);
return data.result.id;
} catch (error) { } catch (error) {
console.error('Error getting Leantime user ID:', error); console.error('Error getting Leantime user ID:', error);
return null; return null;