missions api2

This commit is contained in:
alma 2025-05-06 18:24:32 +02:00
parent fceea8c3d9
commit d2e58da0a7

View File

@ -288,6 +288,8 @@ export class LeantimeService {
// Ensure at least the API user is assigned to prevent "Current team member is New API Access" issue
console.log('Ensuring API user is assigned to the project to prevent orphaned projects.');
// Wait a bit longer before API user request to avoid rate limiting
await new Promise(resolve => setTimeout(resolve, 5000));
const apiUserId = await this.getApiUserId();
if (apiUserId) {
try {
@ -309,38 +311,32 @@ export class LeantimeService {
let successCount = 0;
let errorCount = 0;
for (const missionUser of missionUsers) {
try {
// Get or create the user in Leantime
const leantimeUserId = await this.getUserByEmail(missionUser.user.email);
if (!leantimeUserId) {
console.warn(`⚠️ User not found in Leantime: ${missionUser.user.email}`);
errorCount++;
continue;
}
// Determine role (Gardien du Temps gets editor, others get commenter)
const role = missionUser.role === 'gardien-temps' ? 'editor' : 'commenter';
// Assign the user to the project - IMPORTANT: Only increment success if assignment actually succeeds
const assignmentSucceeded = await this.assignUserToProject(projectId, leantimeUserId, role);
if (assignmentSucceeded) {
// First try to assign just one user (the first one) to reduce API calls and avoid rate limits
const firstUser = missionUsers[0];
try {
const leantimeUserId = await this.getUserByEmail(firstUser.user.email);
if (leantimeUserId) {
const role = firstUser.role === 'gardien-temps' ? 'editor' : 'commenter';
const success = await this.assignUserToProject(projectId, leantimeUserId, role);
if (success) {
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}`);
console.log(`✅ Successfully assigned user ${firstUser.user.email} to project ${projectId}`);
}
} catch (userError) {
console.error(`Error assigning user ${missionUser.user.email}:`, userError);
errorCount++;
// Continue with next user
}
} catch (error) {
console.error(`Error assigning first user: ${error}`);
}
// If no users were successfully assigned, assign the API user as a fallback
if (successCount === 0) {
console.log('No users successfully assigned. Adding API user as a fallback.');
// Skip other users if we got at least one successful assignment
if (successCount > 0) {
console.log(`Successfully assigned ${successCount} user to project. Skipping other users to avoid rate limits.`);
console.log(`⚠️ Other users will need to be added manually at: ${projectUrl}`);
errorCount = missionUsers.length - successCount;
} else {
// If first user failed, try API user assignment
console.log('No regular users successfully assigned. Adding API user as a fallback.');
// Wait a bit longer before API user request to avoid rate limiting
await new Promise(resolve => setTimeout(resolve, 5000));
const apiUserId = await this.getApiUserId();
if (apiUserId) {
try {
@ -353,6 +349,7 @@ export class LeantimeService {
console.error('Error assigning API user to project:', apiUserError);
}
}
errorCount = missionUsers.length;
}
console.log(`User assignment complete: ${successCount} successful, ${errorCount} failed.`);
@ -366,6 +363,8 @@ export class LeantimeService {
// Always ensure the API user is assigned as a last resort
try {
// Wait a bit longer before API user request to avoid rate limiting
await new Promise(resolve => setTimeout(resolve, 5000));
const apiUserId = await this.getApiUserId();
if (apiUserId) {
const apiAssigned = await this.assignApiUserToProject(projectId, apiUserId);