missions api2

This commit is contained in:
alma 2025-05-06 18:11:04 +02:00
parent 7dbe53cae2
commit 32e88946ea

View File

@ -291,7 +291,10 @@ export class LeantimeService {
const apiUserId = await this.getApiUserId();
if (apiUserId) {
try {
await this.assignApiUserToProject(projectId, apiUserId);
const apiAssigned = await this.assignApiUserToProject(projectId, apiUserId);
if (apiAssigned) {
console.log('API user successfully assigned to project as admin.');
}
} catch (apiUserError) {
console.error('Error assigning API user to project:', apiUserError);
}
@ -319,9 +322,15 @@ export class LeantimeService {
// Determine role (Gardien du Temps gets editor, others get commenter)
const role = missionUser.role === 'gardien-temps' ? 'editor' : 'commenter';
// Assign the user to the project
await this.assignUserToProject(projectId, leantimeUserId, role);
successCount++;
// Assign the user to the project - IMPORTANT: Only increment success if assignment actually succeeds
const assignmentSucceeded = await this.assignUserToProject(projectId, leantimeUserId, role);
if (assignmentSucceeded) {
successCount++;
console.log(`✅ Successfully assigned user ${missionUser.user.email} (ID: ${leantimeUserId}) to project ${projectId} with role ${role}`);
} else {
errorCount++;
console.log(`❌ Failed to assign user ${missionUser.user.email} (ID: ${leantimeUserId}) to project ${projectId}`);
}
} catch (userError) {
console.error(`Error assigning user ${missionUser.user.email}:`, userError);
errorCount++;
@ -335,8 +344,11 @@ export class LeantimeService {
const apiUserId = await this.getApiUserId();
if (apiUserId) {
try {
await this.assignApiUserToProject(projectId, apiUserId);
successCount++;
const apiAssigned = await this.assignApiUserToProject(projectId, apiUserId);
if (apiAssigned) {
successCount++;
console.log('✅ API user successfully assigned to project as admin.');
}
} catch (apiUserError) {
console.error('Error assigning API user to project:', apiUserError);
}
@ -356,8 +368,10 @@ export class LeantimeService {
try {
const apiUserId = await this.getApiUserId();
if (apiUserId) {
await this.assignApiUserToProject(projectId, apiUserId);
console.log('API user assigned as fallback to prevent orphaned project.');
const apiAssigned = await this.assignApiUserToProject(projectId, apiUserId);
if (apiAssigned) {
console.log('✅ API user assigned as fallback to prevent orphaned project.');
}
}
} catch (apiUserError) {
console.error('Error assigning API user as fallback:', apiUserError);
@ -368,8 +382,9 @@ 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<void> {
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
@ -398,7 +413,7 @@ export class LeantimeService {
if (response.data && response.data.result) {
console.log(`Successfully assigned API user to project ${projectId}`);
return;
return true;
}
} catch (error) {
console.log('API user assignment method 1 failed, trying next method...');
@ -431,7 +446,7 @@ export class LeantimeService {
if (response.data && response.data.result) {
console.log(`Successfully assigned API user to project ${projectId} (method 2)`);
return;
return true;
}
} catch (error) {
console.log('API user assignment method 2 failed, trying next method...');
@ -461,13 +476,14 @@ export class LeantimeService {
if (response.data && response.data.result) {
console.log(`Successfully assigned API user to project ${projectId} (method 3)`);
return;
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;
}
/**
@ -475,8 +491,9 @@ export class LeantimeService {
* @param projectId The Leantime project ID
* @param userId The Leantime user ID
* @param role The role to assign
* @returns true if assignment succeeded, false otherwise
*/
async assignUserToProject(projectId: number, userId: string, role: string): Promise<void> {
async assignUserToProject(projectId: number, userId: string, role: string): Promise<boolean> {
try {
console.log(`Assigning user ${userId} to project ${projectId} with role ${role}`);
@ -504,7 +521,7 @@ export class LeantimeService {
if (response.data && response.data.result) {
console.log(`Assigned user ${userId} to project ${projectId} with role ${role} (method 1)`);
return;
return true;
}
} catch (error) {
console.log('Method 1 failed, trying next method...');
@ -534,7 +551,7 @@ export class LeantimeService {
if (response2.data && response2.data.result) {
console.log(`Assigned user ${userId} to project ${projectId} with role ${role} (method 2)`);
return;
return true;
}
} catch (error) {
console.log('Method 2 failed, trying next method...');
@ -564,7 +581,7 @@ export class LeantimeService {
if (response3.data && response3.data.result) {
console.log(`Assigned user ${userId} to project ${projectId} with role ${role} (method 3)`);
return;
return true;
}
} catch (error) {
console.log('Method 3 failed, trying next method...');
@ -598,7 +615,7 @@ export class LeantimeService {
if (response4.data && response4.data.result) {
console.log(`Assigned user ${userId} to project ${projectId} with role ${role} (method 4)`);
return;
return true;
}
} catch (error) {
console.log('Method 4 failed.');
@ -606,11 +623,13 @@ export class LeantimeService {
// 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;
} catch (error) {
console.error(`Error assigning user ${userId} to project ${projectId}:`, error);
// We'll allow this to fail since the project was created successfully
console.warn(`⚠️ User assignment failed, but this is not critical. The project ${projectId} was created successfully.`);
return false;
}
}