diff --git a/app/api/leantime/tasks/route.ts b/app/api/leantime/tasks/route.ts index 9fab365..da94353 100644 --- a/app/api/leantime/tasks/route.ts +++ b/app/api/leantime/tasks/route.ts @@ -113,28 +113,69 @@ async function getDoneStatusValues(userId: number): Promise> { }); if (!data.result || !Array.isArray(data.result)) { + console.log('[LEANTIME_TASKS] ⚠️ Invalid response format from getAllStatusLabelsByUserId, using fallback'); + console.log('[LEANTIME_TASKS] Response structure:', JSON.stringify(data, null, 2)); logger.warn('[LEANTIME_TASKS] Invalid response format from getAllStatusLabelsByUserId, using fallback'); // Fallback to default values if API fails return new Set(['0', '3', '5']); } + console.log('[LEANTIME_TASKS] 📋 getAllStatusLabelsByUserId response structure:', { + resultType: typeof data.result, + isArray: Array.isArray(data.result), + length: data.result.length, + firstItem: data.result[0] ? Object.keys(data.result[0]) : null, + }); + // data.result is an array of projects, each with labels // Each label has: id, name, statusType, class, etc. - data.result.forEach((project: any) => { + data.result.forEach((project: any, projectIndex: number) => { + console.log(`[LEANTIME_TASKS] 📁 Project ${projectIndex}:`, { + projectId: project.id, + projectName: project.name, + hasLabels: !!project.labels, + labelsType: typeof project.labels, + labelsIsArray: Array.isArray(project.labels), + labelsCount: project.labels ? (Array.isArray(project.labels) ? project.labels.length : 'not array') : 'no labels', + }); + if (project.labels && Array.isArray(project.labels)) { - project.labels.forEach((label: any) => { + project.labels.forEach((label: any, labelIndex: number) => { + console.log(`[LEANTIME_TASKS] 🏷️ Label ${labelIndex} in project ${projectIndex}:`, { + labelId: label.id, + labelName: label.name, + labelStatusType: label.statusType, + labelClass: label.class, + allKeys: Object.keys(label), + }); + // Check if the label name (case-insensitive) contains "done" const labelName = String(label.name || '').toLowerCase().trim(); - if (labelName === 'done' || labelName.includes('done')) { - // The status value is typically in label.id or label.name - // We need to extract the numeric status value - const statusValue = String(label.id || label.name || ''); - doneStatusValues.add(statusValue); + const statusType = String(label.statusType || '').toLowerCase().trim(); + + if (labelName === 'done' || labelName.includes('done') || statusType === 'done') { + // The status value might be in different places depending on Leantime version + // Try label.id, label.name, or extract from label structure + let statusValue = ''; - // Also try to extract numeric value if it's in a format like "projectId-status" - const parts = statusValue.split('-'); - if (parts.length > 1) { - doneStatusValues.add(parts[parts.length - 1]); // Last part is usually the status + // If label.id is a number, use it directly + if (typeof label.id === 'number') { + statusValue = String(label.id); + } else if (label.id) { + // If it's a string like "projectId-status", extract the status part + const idStr = String(label.id); + const parts = idStr.split('-'); + statusValue = parts.length > 1 ? parts[parts.length - 1] : idStr; + } else if (label.name) { + // Try to extract numeric value from name + const nameStr = String(label.name); + const numMatch = nameStr.match(/\d+/); + statusValue = numMatch ? numMatch[0] : nameStr; + } + + if (statusValue) { + doneStatusValues.add(statusValue); + console.log(`[LEANTIME_TASKS] ✅ Found done status: ${statusValue} from label:`, label); } } });