Skip to content

Commit 14cc118

Browse files
Merge pull request #738 from microsoft/psl-bug-28621
fix: updated the initialize team logic
2 parents 93dbe21 + 3cf7102 commit 14cc118

4 files changed

Lines changed: 72 additions & 29 deletions

File tree

src/backend/common/utils/utils_af.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,40 @@
1717

1818
async def find_first_available_team(team_service: TeamService, user_id: str) -> str:
1919
"""
20-
Check teams in priority order (4 to 1) and return the first available team ID.
21-
Priority: RFP (4) -> Retail (3) -> Marketing (2) -> HR (1)
20+
Check teams in priority order and return the first available team ID.
21+
First tries default teams in priority order, then falls back to any available team.
22+
Priority: RFP (4) -> Retail (3) -> Marketing (2) -> HR (1) -> Any available team
2223
"""
24+
# Standard team priority order
2325
team_priority_order = [
2426
"00000000-0000-0000-0000-000000000004", # RFP
2527
"00000000-0000-0000-0000-000000000003", # Retail
2628
"00000000-0000-0000-0000-000000000002", # Marketing
2729
"00000000-0000-0000-0000-000000000001", # HR
2830
]
2931

32+
# First, check standard teams in priority order
3033
for team_id in team_priority_order:
3134
try:
3235
team_config = await team_service.get_team_configuration(team_id, user_id)
3336
if team_config is not None:
34-
print(f"Found available team: {team_id}")
37+
print(f"Found available standard team: {team_id}")
3538
return team_id
3639
except Exception as e:
3740
print(f"Error checking team {team_id}: {str(e)}")
3841
continue
3942

40-
print("No teams found in priority order")
43+
# If no standard teams found, check for any available teams
44+
try:
45+
all_teams = await team_service.get_all_team_configurations()
46+
if all_teams:
47+
first_team = all_teams[0]
48+
print(f"Found available custom team: {first_team.team_id}")
49+
return first_team.team_id
50+
except Exception as e:
51+
print(f"Error checking for any available teams: {str(e)}")
52+
53+
print("No teams found in database")
4154
return None
4255

4356

src/backend/v4/api/router.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,33 +119,47 @@ async def init_team(
119119
memory_store = await DatabaseFactory.get_database(user_id=user_id)
120120
team_service = TeamService(memory_store)
121121

122-
# Find the first available team from 4 to 1, or use HR as fallback
123122
init_team_id = await find_first_available_team(team_service, user_id)
124-
if not init_team_id:
125-
init_team_id = "00000000-0000-0000-0000-000000000001" # HR fallback
126-
print("No available teams found, using HR fallback")
127-
else:
128-
print(f"Using first available team: {init_team_id}")
129123

124+
# Get current team if user has one
130125
user_current_team = await memory_store.get_current_team(user_id=user_id)
131-
if not user_current_team:
132-
print("User has no current team, setting to default:", init_team_id)
126+
127+
# If no teams available and no current team, return empty state to allow custom team upload
128+
if not init_team_id and not user_current_team:
129+
print("No teams found in database. System ready for custom team upload.")
130+
return {
131+
"status": "No teams configured. Please upload a team configuration to get started.",
132+
"team_id": None,
133+
"team": None,
134+
"requires_team_upload": True,
135+
}
136+
137+
# Use current team if available, otherwise use found team
138+
if user_current_team:
139+
init_team_id = user_current_team.team_id
140+
print(f"Using user's current team: {init_team_id}")
141+
elif init_team_id:
142+
print(f"Using first available team: {init_team_id}")
133143
user_current_team = await team_service.handle_team_selection(
134144
user_id=user_id, team_id=init_team_id
135145
)
136146
if user_current_team:
137147
init_team_id = user_current_team.team_id
138-
else:
139-
init_team_id = user_current_team.team_id
148+
140149
# Verify the team exists and user has access to it
141150
team_configuration = await team_service.get_team_configuration(
142151
init_team_id, user_id
143152
)
144153
if team_configuration is None:
145-
raise HTTPException(
146-
status_code=404,
147-
detail=f"Team configuration '{init_team_id}' not found or access denied",
148-
)
154+
# If team doesn't exist, clear current team and return empty state
155+
await memory_store.delete_current_team(user_id)
156+
print(f"Team configuration '{init_team_id}' not found. Cleared current team.")
157+
return {
158+
"status": "Current team configuration not found. Please select or upload a team configuration.",
159+
"team_id": None,
160+
"team": None,
161+
"requires_team_upload": True,
162+
}
149163

150164
# Set as current team in memory
151165
team_config.set_current_team(

src/frontend/src/pages/HomePage.tsx

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const HomePage: React.FC = () => {
3535
// Call the backend init_team endpoint (takes ~20 seconds)
3636
const initResponse = await TeamService.initializeTeam();
3737

38+
// Handle successful team initialization
3839
if (initResponse.data?.status === 'Request started successfully' && initResponse.data?.team_id) {
3940
console.log('Team initialization completed:', initResponse.data?.team_id);
4041

@@ -54,12 +55,10 @@ const HomePage: React.FC = () => {
5455
"success"
5556
);
5657
} else {
57-
// Fallback: if we can't find the specific team, use HR team or first available
58+
// Fallback: if we can't find the specific team, use first available
5859
console.log('Specific team not found, using default selection logic');
59-
const hrTeam = teams.find(team => team.name === "Human Resources Team");
60-
const defaultTeam = hrTeam || teams[0];
61-
62-
if (defaultTeam) {
60+
if (teams.length > 0) {
61+
const defaultTeam = teams[0];
6362
setSelectedTeam(defaultTeam);
6463
TeamService.storageTeam(defaultTeam);
6564
showToast(
@@ -68,15 +67,23 @@ const HomePage: React.FC = () => {
6867
);
6968
}
7069
}
71-
70+
}
71+
// Handle case when no teams are configured
72+
else if (initResponse.data?.requires_team_upload) {
73+
console.log('No teams configured. User needs to upload a team configuration.');
74+
setSelectedTeam(null);
75+
showToast(
76+
"Welcome! Please upload a team configuration file to get started.",
77+
"info"
78+
);
7279
}
7380

7481
} catch (error) {
7582
console.error('Error initializing team from backend:', error);
76-
showToast("Team initialization failed", "warning");
77-
78-
// Fallback to the old client-side method
83+
showToast("Team initialization failed. You can still upload a custom team configuration.", "info");
7984

85+
// Don't block the user - allow them to upload custom teams
86+
setSelectedTeam(null);
8087
} finally {
8188
setIsLoadingTeam(false);
8289
}
@@ -125,12 +132,19 @@ const HomePage: React.FC = () => {
125132
"success"
126133
);
127134
}
128-
135+
} else if (initResponse.data?.requires_team_upload) {
136+
// Handle case when no teams are available
137+
setSelectedTeam(null);
138+
showToast(
139+
"No teams are configured. Please upload a team configuration to continue.",
140+
"info"
141+
);
129142
} else {
130143
throw new Error('Invalid response from init_team endpoint');
131144
}
132145
} catch (error) {
133146
console.error('Error setting current team:', error);
147+
showToast("Error switching team. Please try again.", "warning");
134148
} finally {
135149
setIsLoadingTeam(false);
136150
}

src/frontend/src/services/TeamService.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ export class TeamService {
2727
success: boolean;
2828
data?: {
2929
status: string;
30-
team_id: string;
30+
team_id?: string;
31+
team?: any;
32+
requires_team_upload?: boolean;
3133
};
3234
error?: string;
3335
}> {

0 commit comments

Comments
 (0)