working leantime widget 47

This commit is contained in:
Alma 2025-04-12 15:09:17 +02:00
parent 05d33daaed
commit 1f8090e1ff

View File

@ -15,6 +15,12 @@ interface Task {
async function getLeantimeUserId(email: string): Promise<number | null> {
try {
if (!process.env.LEANTIME_TOKEN) {
console.error('LEANTIME_TOKEN is not set in environment variables');
return null;
}
console.log('Fetching Leantime users for email:', email);
const response = await fetch(`${process.env.LEANTIME_API_URL}/api/jsonrpc`, {
method: 'POST',
headers: {
@ -29,13 +35,30 @@ async function getLeantimeUserId(email: string): Promise<number | null> {
});
if (!response.ok) {
console.error('Failed to fetch Leantime users');
console.error('Failed to fetch Leantime users:', {
status: response.status,
statusText: response.statusText
});
return null;
}
const data = await response.json();
console.log('Leantime users response:', data);
if (!data.result || !Array.isArray(data.result)) {
console.error('Invalid response format from Leantime users API');
return null;
}
const users = data.result;
const user = users.find((u: any) => u.email === email);
if (user) {
console.log('Found Leantime user:', { id: user.id, email: user.email });
} else {
console.log('No Leantime user found for email:', email);
}
return user ? user.id : null;
} catch (error) {
console.error('Error fetching Leantime user ID:', error);
@ -50,12 +73,15 @@ export async function GET(request: NextRequest) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
console.log('Fetching tasks for user:', session.user.email);
const userId = await getLeantimeUserId(session.user.email);
if (!userId) {
console.error('User not found in Leantime:', session.user.email);
return NextResponse.json({ error: "User not found in Leantime" }, { status: 404 });
}
// Fetch tasks assigned to the user
console.log('Fetching tasks for Leantime user ID:', userId);
const response = await fetch(`${process.env.LEANTIME_API_URL}/api/jsonrpc`, {
method: 'POST',
headers: {
@ -74,10 +100,24 @@ export async function GET(request: NextRequest) {
});
if (!response.ok) {
console.error('Failed to fetch tasks from Leantime:', {
status: response.status,
statusText: response.statusText
});
throw new Error('Failed to fetch tasks from Leantime');
}
const data = await response.json();
console.log('Leantime tasks response:', {
success: true,
taskCount: data.result?.length || 0
});
if (!data.result || !Array.isArray(data.result)) {
console.error('Invalid response format from Leantime tasks API');
throw new Error('Invalid response format from Leantime');
}
const tasks = data.result.map((task: any) => ({
id: task.id.toString(),
headline: task.headline,