working leantime widget 67

This commit is contained in:
Alma 2025-04-12 19:19:13 +02:00
parent 5a59f124e2
commit fbeb73ec44
2 changed files with 29 additions and 30 deletions

View File

@ -142,23 +142,27 @@ export async function GET(request: NextRequest) {
throw new Error('Invalid response format from Leantime'); throw new Error('Invalid response format from Leantime');
} }
console.log('Leantime tasks response:', {
success: true,
taskCount: data.result?.length || 0
});
if (!data.result || !Array.isArray(data.result)) { if (!data.result || !Array.isArray(data.result)) {
console.error('Invalid response format from Leantime tasks API'); console.error('Invalid response format from Leantime tasks API');
throw new Error('Invalid response format from Leantime'); throw new Error('Invalid response format from Leantime');
} }
// Log the first task to see its complete structure
console.log('Sample task structure:', JSON.stringify(data.result[0], null, 2));
const tasks = data.result const tasks = data.result
.filter((task: any) => .filter((task: any) => {
// Include tasks where the user is the editor // Log task assignment details
task.editorId === userId.toString() || console.log(`Task ${task.id} - ${task.headline}:`, {
// Or tasks explicitly assigned to the user (if assignedTo exists) editorId: task.editorId,
(Array.isArray(task.assignedTo) && task.assignedTo.includes(userId)) userId: userId,
) assignedTo: task.assignedTo,
status: task.status
});
// Only include tasks where the current user is assigned
return task.editorId === userId.toString();
})
.map((task: any) => ({ .map((task: any) => ({
id: task.id.toString(), id: task.id.toString(),
headline: task.headline, headline: task.headline,
@ -175,13 +179,7 @@ export async function GET(request: NextRequest) {
editorLastname: task.editorLastname editorLastname: task.editorLastname
})); }));
// Sort tasks by creation date (oldest first) console.log(`Found ${tasks.length} tasks assigned to user ${userId}`);
tasks.sort((a: Task, b: Task) => {
const dateA = new Date(a.createdOn).getTime();
const dateB = new Date(b.createdOn).getTime();
return dateA - dateB;
});
return NextResponse.json({ tasks }); return NextResponse.json({ tasks });
} catch (error) { } catch (error) {
console.error('Error in tasks route:', error); console.error('Error in tasks route:', error);

View File

@ -9,19 +9,12 @@ interface Task {
id: number; id: number;
headline: string; headline: string;
description: string; description: string;
date: string;
dateToFinish: string;
projectId: number;
projectName: string; projectName: string;
type: string;
status: number; status: number;
dateToFinish: string;
editorId: string; editorId: string;
editorFirstname: string | null; editorFirstname: string | null;
editorLastname: string | null; editorLastname: string | null;
authorFirstname: string;
authorLastname: string;
milestoneHeadline: string | null;
tags: string;
} }
interface ProjectSummary { interface ProjectSummary {
@ -91,13 +84,15 @@ export function Flow() {
if (!response.ok) throw new Error('Failed to fetch tasks'); if (!response.ok) throw new Error('Failed to fetch tasks');
const data = await response.json(); const data = await response.json();
console.log('Tasks API response:', data);
if (!data.tasks || !Array.isArray(data.tasks)) { if (!data.tasks || !Array.isArray(data.tasks)) {
console.warn('No tasks found in response:', data); console.warn('No tasks found in response:', data);
setTasks([]); setTasks([]);
return; return;
} }
// Sort tasks by date (oldest first) // Sort tasks by status and date
const sortedTasks = data.tasks const sortedTasks = data.tasks
.filter((task: Task) => .filter((task: Task) =>
task.headline && task.headline &&
@ -109,11 +104,17 @@ export function Flow() {
if (a.status !== b.status) { if (a.status !== b.status) {
return a.status - b.status; return a.status - b.status;
} }
// Then sort by due date // Then sort by due date if available
return getDateToSort(a) - getDateToSort(b); const dateA = a.dateToFinish && a.dateToFinish !== '0000-00-00 00:00:00'
? new Date(a.dateToFinish).getTime()
: Date.now();
const dateB = b.dateToFinish && b.dateToFinish !== '0000-00-00 00:00:00'
? new Date(b.dateToFinish).getTime()
: Date.now();
return dateA - dateB;
}); });
// Limit to 6 tasks console.log('Filtered and sorted tasks:', sortedTasks);
setTasks(sortedTasks.slice(0, 6)); setTasks(sortedTasks.slice(0, 6));
setError(null); setError(null);
} catch (err) { } catch (err) {