missions api2
This commit is contained in:
parent
32e88946ea
commit
fceea8c3d9
@ -380,27 +380,23 @@ export class LeantimeService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Special method to assign the API user to a project
|
||||
* This uses different approaches to try to ensure success
|
||||
* @returns true if assignment succeeded, false otherwise
|
||||
* Verify if a user is assigned to a project
|
||||
* @param userId The Leantime user ID
|
||||
* @param projectId The Leantime project ID
|
||||
* @returns True if the user is assigned to the project, false otherwise
|
||||
*/
|
||||
private async assignApiUserToProject(projectId: number, apiUserId: string): Promise<boolean> {
|
||||
console.log(`Assigning API user ${apiUserId} to project ${projectId} as admin`);
|
||||
|
||||
// Try various methods to assign the API user
|
||||
|
||||
// Method 1: Direct API assignment
|
||||
private async verifyUserAssignedToProject(userId: string, projectId: number): Promise<boolean> {
|
||||
try {
|
||||
console.log(`Verifying if user ${userId} is assigned to project ${projectId}...`);
|
||||
|
||||
const response = await axios.post(
|
||||
this.getApiEndpoint(),
|
||||
{
|
||||
method: 'leantime.rpc.Projects.Projects.addUser',
|
||||
method: 'leantime.rpc.Projects.Projects.getProjectIdAssignedToUser',
|
||||
jsonrpc: '2.0',
|
||||
id: 1,
|
||||
params: {
|
||||
projectId: projectId,
|
||||
userId: apiUserId,
|
||||
role: 'admin' // API user gets admin to ensure full control
|
||||
userId: userId
|
||||
}
|
||||
},
|
||||
{
|
||||
@ -411,79 +407,24 @@ export class LeantimeService {
|
||||
}
|
||||
);
|
||||
|
||||
if (response.data && response.data.result) {
|
||||
console.log(`Successfully assigned API user to project ${projectId}`);
|
||||
return true;
|
||||
// Check if the response contains project IDs
|
||||
if (response.data && response.data.result && Array.isArray(response.data.result)) {
|
||||
// Convert project IDs to strings for comparison (API might return strings or numbers)
|
||||
const assignedProjects = response.data.result.map((id: number | string) => String(id));
|
||||
const projectIdStr = String(projectId);
|
||||
|
||||
// Check if the user is assigned to our project
|
||||
const isAssigned = assignedProjects.includes(projectIdStr);
|
||||
console.log(`User ${userId} ${isAssigned ? 'is' : 'is not'} assigned to project ${projectId}`);
|
||||
return isAssigned;
|
||||
}
|
||||
|
||||
console.log(`User ${userId} is not assigned to any projects`);
|
||||
return false;
|
||||
} catch (error) {
|
||||
console.log('API user assignment method 1 failed, trying next method...');
|
||||
console.error(`Error verifying user assignment for user ${userId}:`, error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Method 2: editUserProjectRelations
|
||||
try {
|
||||
const response = await axios.post(
|
||||
this.getApiEndpoint(),
|
||||
{
|
||||
method: 'leantime.rpc.Projects.editUserProjectRelations',
|
||||
jsonrpc: '2.0',
|
||||
id: 1,
|
||||
params: {
|
||||
action: 'add',
|
||||
userId: apiUserId,
|
||||
projects: [{
|
||||
id: projectId,
|
||||
role: 'admin'
|
||||
}]
|
||||
}
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-API-Key': this.apiToken
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (response.data && response.data.result) {
|
||||
console.log(`Successfully assigned API user to project ${projectId} (method 2)`);
|
||||
return true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('API user assignment method 2 failed, trying next method...');
|
||||
}
|
||||
|
||||
// Method 3: addProjectUser
|
||||
try {
|
||||
const response = await axios.post(
|
||||
this.getApiEndpoint(),
|
||||
{
|
||||
method: 'leantime.rpc.Projects.addProjectUser',
|
||||
jsonrpc: '2.0',
|
||||
id: 1,
|
||||
params: {
|
||||
projectId: projectId,
|
||||
userId: apiUserId,
|
||||
role: 'admin'
|
||||
}
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-API-Key': this.apiToken
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (response.data && response.data.result) {
|
||||
console.log(`Successfully assigned API user to project ${projectId} (method 3)`);
|
||||
return true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('API user assignment method 3 failed.');
|
||||
}
|
||||
|
||||
console.warn(`⚠️ Could not assign API user to project ${projectId}. The project may appear with "New API Access" as the only team member.`);
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -497,6 +438,13 @@ export class LeantimeService {
|
||||
try {
|
||||
console.log(`Assigning user ${userId} to project ${projectId} with role ${role}`);
|
||||
|
||||
// First, check if the user is already assigned to the project
|
||||
const alreadyAssigned = await this.verifyUserAssignedToProject(userId, projectId);
|
||||
if (alreadyAssigned) {
|
||||
console.log(`✅ User ${userId} is already assigned to project ${projectId}`);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Try method 1: Projects.Projects.addUser
|
||||
try {
|
||||
const response = await axios.post(
|
||||
@ -521,7 +469,14 @@ export class LeantimeService {
|
||||
|
||||
if (response.data && response.data.result) {
|
||||
console.log(`Assigned user ${userId} to project ${projectId} with role ${role} (method 1)`);
|
||||
return true;
|
||||
|
||||
// Verify assignment was successful
|
||||
const verified = await this.verifyUserAssignedToProject(userId, projectId);
|
||||
if (verified) {
|
||||
return true;
|
||||
} else {
|
||||
console.log('Assignment appeared successful but verification failed. Trying next method...');
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('Method 1 failed, trying next method...');
|
||||
@ -621,6 +576,13 @@ export class LeantimeService {
|
||||
console.log('Method 4 failed.');
|
||||
}
|
||||
|
||||
// After all methods, verify one last time
|
||||
const finalVerification = await this.verifyUserAssignedToProject(userId, projectId);
|
||||
if (finalVerification) {
|
||||
console.log(`✅ Verified user ${userId} is now assigned to project ${projectId}`);
|
||||
return true;
|
||||
}
|
||||
|
||||
// If we reach here, all methods failed
|
||||
console.warn(`⚠️ Could not assign user ${userId} to project ${projectId} with any method. This is not a critical error, the project was created.`);
|
||||
return false;
|
||||
@ -633,6 +595,127 @@ export class LeantimeService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Special method to assign the API user to a project
|
||||
* This uses different approaches to try to ensure success
|
||||
* @returns true if assignment succeeded, false otherwise
|
||||
*/
|
||||
private async assignApiUserToProject(projectId: number, apiUserId: string): Promise<boolean> {
|
||||
console.log(`Assigning API user ${apiUserId} to project ${projectId} as admin`);
|
||||
|
||||
// First, check if the API user is already assigned
|
||||
const alreadyAssigned = await this.verifyUserAssignedToProject(apiUserId, projectId);
|
||||
if (alreadyAssigned) {
|
||||
console.log(`✅ API user ${apiUserId} is already assigned to project ${projectId}`);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Try various methods to assign the API user
|
||||
|
||||
// Method 1: Direct API assignment
|
||||
try {
|
||||
const response = await axios.post(
|
||||
this.getApiEndpoint(),
|
||||
{
|
||||
method: 'leantime.rpc.Projects.Projects.addUser',
|
||||
jsonrpc: '2.0',
|
||||
id: 1,
|
||||
params: {
|
||||
projectId: projectId,
|
||||
userId: apiUserId,
|
||||
role: 'admin' // API user gets admin to ensure full control
|
||||
}
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-API-Key': this.apiToken
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (response.data && response.data.result) {
|
||||
console.log(`Successfully assigned API user to project ${projectId}`);
|
||||
return true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('API user assignment method 1 failed, trying next method...');
|
||||
}
|
||||
|
||||
// Method 2: editUserProjectRelations
|
||||
try {
|
||||
const response = await axios.post(
|
||||
this.getApiEndpoint(),
|
||||
{
|
||||
method: 'leantime.rpc.Projects.editUserProjectRelations',
|
||||
jsonrpc: '2.0',
|
||||
id: 1,
|
||||
params: {
|
||||
action: 'add',
|
||||
userId: apiUserId,
|
||||
projects: [{
|
||||
id: projectId,
|
||||
role: 'admin'
|
||||
}]
|
||||
}
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-API-Key': this.apiToken
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (response.data && response.data.result) {
|
||||
console.log(`Successfully assigned API user to project ${projectId} (method 2)`);
|
||||
return true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('API user assignment method 2 failed, trying next method...');
|
||||
}
|
||||
|
||||
// Method 3: addProjectUser
|
||||
try {
|
||||
const response = await axios.post(
|
||||
this.getApiEndpoint(),
|
||||
{
|
||||
method: 'leantime.rpc.Projects.addProjectUser',
|
||||
jsonrpc: '2.0',
|
||||
id: 1,
|
||||
params: {
|
||||
projectId: projectId,
|
||||
userId: apiUserId,
|
||||
role: 'admin'
|
||||
}
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-API-Key': this.apiToken
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (response.data && response.data.result) {
|
||||
console.log(`Successfully assigned API user to project ${projectId} (method 3)`);
|
||||
return true;
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('API user assignment method 3 failed.');
|
||||
}
|
||||
|
||||
// After all methods, verify assignment one last time
|
||||
const finalVerification = await this.verifyUserAssignedToProject(apiUserId, projectId);
|
||||
if (finalVerification) {
|
||||
console.log(`✅ Verified API user ${apiUserId} is now assigned to project ${projectId}`);
|
||||
return true;
|
||||
}
|
||||
|
||||
console.warn(`⚠️ Could not assign API user to project ${projectId}. The project may appear with "New API Access" as the only team member.`);
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a client ID by name
|
||||
* @param clientName The client name to search for
|
||||
|
||||
Loading…
Reference in New Issue
Block a user