missions api2
This commit is contained in:
parent
fceea8c3d9
commit
d2e58da0a7
@ -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
|
// 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.');
|
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();
|
const apiUserId = await this.getApiUserId();
|
||||||
if (apiUserId) {
|
if (apiUserId) {
|
||||||
try {
|
try {
|
||||||
@ -309,38 +311,32 @@ export class LeantimeService {
|
|||||||
let successCount = 0;
|
let successCount = 0;
|
||||||
let errorCount = 0;
|
let errorCount = 0;
|
||||||
|
|
||||||
for (const missionUser of missionUsers) {
|
// First try to assign just one user (the first one) to reduce API calls and avoid rate limits
|
||||||
try {
|
const firstUser = missionUsers[0];
|
||||||
// Get or create the user in Leantime
|
try {
|
||||||
const leantimeUserId = await this.getUserByEmail(missionUser.user.email);
|
const leantimeUserId = await this.getUserByEmail(firstUser.user.email);
|
||||||
if (!leantimeUserId) {
|
if (leantimeUserId) {
|
||||||
console.warn(`⚠️ User not found in Leantime: ${missionUser.user.email}`);
|
const role = firstUser.role === 'gardien-temps' ? 'editor' : 'commenter';
|
||||||
errorCount++;
|
const success = await this.assignUserToProject(projectId, leantimeUserId, role);
|
||||||
continue;
|
if (success) {
|
||||||
}
|
|
||||||
|
|
||||||
// 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) {
|
|
||||||
successCount++;
|
successCount++;
|
||||||
console.log(`✅ Successfully assigned user ${missionUser.user.email} (ID: ${leantimeUserId}) to project ${projectId} with role ${role}`);
|
console.log(`✅ Successfully assigned user ${firstUser.user.email} to project ${projectId}`);
|
||||||
} 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++;
|
|
||||||
// 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
|
// Skip other users if we got at least one successful assignment
|
||||||
if (successCount === 0) {
|
if (successCount > 0) {
|
||||||
console.log('No users successfully assigned. Adding API user as a fallback.');
|
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();
|
const apiUserId = await this.getApiUserId();
|
||||||
if (apiUserId) {
|
if (apiUserId) {
|
||||||
try {
|
try {
|
||||||
@ -353,6 +349,7 @@ export class LeantimeService {
|
|||||||
console.error('Error assigning API user to project:', apiUserError);
|
console.error('Error assigning API user to project:', apiUserError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
errorCount = missionUsers.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`User assignment complete: ${successCount} successful, ${errorCount} failed.`);
|
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
|
// Always ensure the API user is assigned as a last resort
|
||||||
try {
|
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();
|
const apiUserId = await this.getApiUserId();
|
||||||
if (apiUserId) {
|
if (apiUserId) {
|
||||||
const apiAssigned = await this.assignApiUserToProject(projectId, apiUserId);
|
const apiAssigned = await this.assignApiUserToProject(projectId, apiUserId);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user