working leantime widget 40

This commit is contained in:
Alma 2025-04-12 14:38:28 +02:00
parent b4cfe735ed
commit 30dc143ffd

View File

@ -26,6 +26,8 @@ async function getLeantimeUserId(email: string): Promise<number | null> {
}
try {
console.log('Fetching Leantime users with token:', process.env.LEANTIME_TOKEN ? 'Token present' : 'Token missing');
const response = await fetch('https://agilite.slm-lab.net/api/jsonrpc', {
method: 'POST',
headers: {
@ -40,11 +42,18 @@ async function getLeantimeUserId(email: string): Promise<number | null> {
});
if (!response.ok) {
throw new Error('Failed to fetch users from Leantime');
console.error('Failed to fetch users from Leantime:', {
status: response.status,
statusText: response.statusText
});
throw new Error(`Failed to fetch users from Leantime: ${response.status} ${response.statusText}`);
}
const data = await response.json();
console.log('Leantime users response:', data);
const user = data.result.find((u: any) => u.email === email);
console.log('Found user:', user ? 'Yes' : 'No');
if (user) {
// Cache the user ID
@ -64,6 +73,7 @@ async function getLeantimeUserId(email: string): Promise<number | null> {
export async function GET(request: NextRequest) {
try {
const session = await getServerSession(authOptions);
console.log('Session:', session ? 'Present' : 'Missing');
if (!session) {
return NextResponse.json(
@ -79,8 +89,11 @@ export async function GET(request: NextRequest) {
);
}
console.log('User email:', session.user.email);
// Get Leantime user ID
const leantimeUserId = await getLeantimeUserId(session.user.email);
console.log('Leantime user ID:', leantimeUserId);
if (!leantimeUserId) {
return NextResponse.json(
@ -90,6 +103,7 @@ export async function GET(request: NextRequest) {
}
// Get all tasks assigned to the user
console.log('Fetching tasks for user:', leantimeUserId);
const response = await fetch('https://agilite.slm-lab.net/api/jsonrpc', {
method: 'POST',
headers: {
@ -109,16 +123,22 @@ export async function GET(request: NextRequest) {
});
if (!response.ok) {
throw new Error('Failed to fetch tasks from Leantime');
console.error('Failed to fetch tasks:', {
status: response.status,
statusText: response.statusText
});
throw new Error(`Failed to fetch tasks: ${response.status} ${response.statusText}`);
}
const data = await response.json();
console.log('Tasks response:', data);
if (!data.result) {
return NextResponse.json({ projects: [] });
}
// Get project details to include project names
console.log('Fetching projects');
const projectsResponse = await fetch('https://agilite.slm-lab.net/api/jsonrpc', {
method: 'POST',
headers: {
@ -133,10 +153,15 @@ export async function GET(request: NextRequest) {
});
if (!projectsResponse.ok) {
throw new Error('Failed to fetch projects from Leantime');
console.error('Failed to fetch projects:', {
status: projectsResponse.status,
statusText: projectsResponse.statusText
});
throw new Error(`Failed to fetch projects: ${projectsResponse.status} ${projectsResponse.statusText}`);
}
const projectsData = await projectsResponse.json();
console.log('Projects response:', projectsData);
// Create a map of projects with their tasks grouped by status
const projectMap = new Map<string, Project>();
@ -192,6 +217,7 @@ export async function GET(request: NextRequest) {
// Convert the map to an array and sort projects by name
const projects = Array.from(projectMap.values()).sort((a, b) => a.name.localeCompare(b.name));
console.log('Final projects:', projects);
return NextResponse.json({ projects });
} catch (error) {