working leantime widget 47
This commit is contained in:
parent
05d33daaed
commit
1f8090e1ff
@ -15,6 +15,12 @@ interface Task {
|
|||||||
|
|
||||||
async function getLeantimeUserId(email: string): Promise<number | null> {
|
async function getLeantimeUserId(email: string): Promise<number | null> {
|
||||||
try {
|
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`, {
|
const response = await fetch(`${process.env.LEANTIME_API_URL}/api/jsonrpc`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@ -29,13 +35,30 @@ async function getLeantimeUserId(email: string): Promise<number | null> {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
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 users = data.result;
|
||||||
const user = users.find((u: any) => u.email === email);
|
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;
|
return user ? user.id : null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching Leantime user ID:', 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 });
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('Fetching tasks for user:', session.user.email);
|
||||||
const userId = await getLeantimeUserId(session.user.email);
|
const userId = await getLeantimeUserId(session.user.email);
|
||||||
|
|
||||||
if (!userId) {
|
if (!userId) {
|
||||||
|
console.error('User not found in Leantime:', session.user.email);
|
||||||
return NextResponse.json({ error: "User not found in Leantime" }, { status: 404 });
|
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`, {
|
const response = await fetch(`${process.env.LEANTIME_API_URL}/api/jsonrpc`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@ -74,10 +100,24 @@ export async function GET(request: NextRequest) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
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');
|
throw new Error('Failed to fetch tasks from Leantime');
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
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) => ({
|
const tasks = data.result.map((task: any) => ({
|
||||||
id: task.id.toString(),
|
id: task.id.toString(),
|
||||||
headline: task.headline,
|
headline: task.headline,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user