delete user leantime api 7
This commit is contained in:
parent
7abcc16cdb
commit
55652a92d6
@ -3,37 +3,40 @@ import { authOptions } from "@/app/api/auth/[...nextauth]/route";
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
// Helper function to delete user from Leantime
|
// 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 {
|
try {
|
||||||
// First, get the Leantime user by username
|
// First, get all users from Leantime
|
||||||
const getUserResponse = await fetch('https://agilite.slm-lab.net/api/jsonrpc', {
|
const getUsersResponse = await fetch('https://agilite.slm-lab.net/api/jsonrpc', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'X-API-Key': process.env.LEANTIME_TOKEN || '',
|
'X-API-Key': process.env.LEANTIME_TOKEN || '',
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
method: 'leantime.rpc.Users.Users.getUser',
|
method: 'leantime.rpc.Users.Users.getAll',
|
||||||
jsonrpc: '2.0',
|
jsonrpc: '2.0',
|
||||||
id: 1,
|
id: 1,
|
||||||
params: {
|
params: {}
|
||||||
username: username
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
const getUserData = await getUserResponse.json();
|
const getUsersData = await getUsersResponse.json();
|
||||||
console.log('Leantime get user response:', getUserData);
|
console.log('Leantime get users response:', getUsersData);
|
||||||
|
|
||||||
if (!getUserResponse.ok || !getUserData.result) {
|
if (!getUsersResponse.ok || !getUsersData.result) {
|
||||||
console.error('Failed to get Leantime user:', getUserData);
|
console.error('Failed to get Leantime users:', getUsersData);
|
||||||
return {
|
return {
|
||||||
success: false,
|
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
|
// Now delete the user using their Leantime ID
|
||||||
const deleteResponse = await fetch('https://agilite.slm-lab.net/api/jsonrpc', {
|
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',
|
jsonrpc: '2.0',
|
||||||
id: 1,
|
id: 1,
|
||||||
params: {
|
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(
|
const userResponse = await fetch(
|
||||||
`${process.env.KEYCLOAK_BASE_URL}/admin/realms/${process.env.KEYCLOAK_REALM}/users/${params.userId}`,
|
`${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
|
// Delete user from Leantime
|
||||||
const leantimeResult = await deleteLeantimeUser(userDetails.username);
|
const leantimeResult = await deleteLeantimeUser(userDetails.email);
|
||||||
|
|
||||||
if (!leantimeResult.success) {
|
if (!leantimeResult.success) {
|
||||||
console.error("Leantime user deletion failed:", leantimeResult.error);
|
console.error("Leantime user deletion failed:", leantimeResult.error);
|
||||||
|
|||||||
@ -233,7 +233,6 @@ async function createLeantimeUser(userData: {
|
|||||||
values: {
|
values: {
|
||||||
firstname: userData.firstName,
|
firstname: userData.firstName,
|
||||||
lastname: userData.lastName,
|
lastname: userData.lastName,
|
||||||
username: userData.username,
|
|
||||||
email: userData.email,
|
email: userData.email,
|
||||||
password: userData.password,
|
password: userData.password,
|
||||||
status: 'active',
|
status: 'active',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user