@@ -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 (
0 commit comments