diff --git a/app/api/users/[userId]/route.ts b/app/api/users/[userId]/route.ts index 9aca4e20..a22933fc 100644 --- a/app/api/users/[userId]/route.ts +++ b/app/api/users/[userId]/route.ts @@ -3,37 +3,40 @@ import { authOptions } from "@/app/api/auth/[...nextauth]/route"; import { NextResponse } from "next/server"; // Helper function to delete user from Leantime -async function deleteLeantimeUser(username: string): Promise<{ success: boolean; error?: string }> { +async function deleteLeantimeUser(email: string): Promise<{ success: boolean; error?: string }> { try { - // First, get the Leantime user by username - const getUserResponse = await fetch('https://agilite.slm-lab.net/api/jsonrpc', { + // First, get all users from Leantime + const getUsersResponse = await fetch('https://agilite.slm-lab.net/api/jsonrpc', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-API-Key': process.env.LEANTIME_TOKEN || '', }, body: JSON.stringify({ - method: 'leantime.rpc.Users.Users.getUser', + method: 'leantime.rpc.Users.Users.getAll', jsonrpc: '2.0', id: 1, - params: { - username: username - } + params: {} }) }); - const getUserData = await getUserResponse.json(); - console.log('Leantime get user response:', getUserData); + const getUsersData = await getUsersResponse.json(); + console.log('Leantime get users response:', getUsersData); - if (!getUserResponse.ok || !getUserData.result) { - console.error('Failed to get Leantime user:', getUserData); + if (!getUsersResponse.ok || !getUsersData.result) { + console.error('Failed to get Leantime users:', getUsersData); return { success: false, - error: getUserData.error?.message || 'Failed to get user in Leantime' + error: getUsersData.error?.message || 'Failed to get users in Leantime' }; } - const leantimeUserId = getUserData.result.id; + // Find the user with matching email + const user = getUsersData.result.find((u: any) => u.email === email); + if (!user) { + console.log('User not found in Leantime, might have been already deleted'); + return { success: true }; // Consider it a success if user doesn't exist + } // Now delete the user using their Leantime ID const deleteResponse = await fetch('https://agilite.slm-lab.net/api/jsonrpc', { @@ -47,7 +50,7 @@ async function deleteLeantimeUser(username: string): Promise<{ success: boolean; jsonrpc: '2.0', id: 1, params: { - id: leantimeUserId + id: user.id } }) }); @@ -111,7 +114,7 @@ export async function DELETE( ); } - // Get user details before deletion to get their username + // Get user details before deletion to get their email const userResponse = await fetch( `${process.env.KEYCLOAK_BASE_URL}/admin/realms/${process.env.KEYCLOAK_REALM}/users/${params.userId}`, { @@ -157,7 +160,7 @@ export async function DELETE( } // Delete user from Leantime - const leantimeResult = await deleteLeantimeUser(userDetails.username); + const leantimeResult = await deleteLeantimeUser(userDetails.email); if (!leantimeResult.success) { console.error("Leantime user deletion failed:", leantimeResult.error); diff --git a/app/api/users/route.ts b/app/api/users/route.ts index 517398a1..9a281a35 100644 --- a/app/api/users/route.ts +++ b/app/api/users/route.ts @@ -233,7 +233,6 @@ async function createLeantimeUser(userData: { values: { firstname: userData.firstName, lastname: userData.lastName, - username: userData.username, email: userData.email, password: userData.password, status: 'active',