From d2e58da0a7b732040566150ec70763ec775eba5c Mon Sep 17 00:00:00 2001 From: alma Date: Tue, 6 May 2025 18:24:32 +0200 Subject: [PATCH] missions api2 --- lib/services/leantime-service.ts | 53 ++++++++++++++++---------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/lib/services/leantime-service.ts b/lib/services/leantime-service.ts index ab7e4c5f..f65bb48c 100644 --- a/lib/services/leantime-service.ts +++ b/lib/services/leantime-service.ts @@ -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);