Skip to content

Commit 58672c1

Browse files
committed
Refactor get_team_by_id and improve plan retrieval error handling
Renamed the get_team_by_id method parameter from 'id' to 'team_id' for clarity and updated its usage in CosmosDBClient and DatabaseBase. Refactored the /plan endpoint in router.py to use Optional plan_id and added improved error handling with logging for plan retrieval.
1 parent 3132e5b commit 58672c1

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

src/backend/common/database/cosmosdb.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ async def get_team(self, team_id: str) -> Optional[TeamConfiguration]:
293293
teams = await self.query_items(query, parameters, TeamConfiguration)
294294
return teams[0] if teams else None
295295

296-
async def get_team_by_id(self, id: str) -> Optional[TeamConfiguration]:
296+
async def get_team_by_id(self, team_id: str) -> Optional[TeamConfiguration]:
297297
"""Retrieve a specific team configuration by its document id.
298298
299299
Args:
@@ -302,9 +302,9 @@ async def get_team_by_id(self, id: str) -> Optional[TeamConfiguration]:
302302
Returns:
303303
TeamConfiguration object or None if not found
304304
"""
305-
query = "SELECT * FROM c WHERE c.id=@id AND c.data_type=@data_type"
305+
query = "SELECT * FROM c WHERE c.team_id=@team_id AND c.data_type=@data_type"
306306
parameters = [
307-
{"name": "@id", "value": id},
307+
{"name": "@team_id", "value": team_id},
308308
{"name": "@data_type", "value": DataType.team_config},
309309
]
310310
teams = await self.query_items(query, parameters, TeamConfiguration)

src/backend/common/database/database_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ async def get_team(self, team_id: str) -> Optional[TeamConfiguration]:
138138
pass
139139

140140
@abstractmethod
141-
async def get_team_by_id(self, id: str) -> Optional[TeamConfiguration]:
141+
async def get_team_by_id(self, team_id: str) -> Optional[TeamConfiguration]:
142142
"""Retrieve a team configuration by internal id."""
143143
pass
144144

src/backend/v3/api/router.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ async def get_plans(request: Request):
12551255

12561256
# Get plans is called in the initial side rendering of the frontend
12571257
@app_v3.get("/plan")
1258-
async def get_plan_by_id(request: Request, plan_id: str):
1258+
async def get_plan_by_id(request: Request, plan_id: Optional[str] = Query(None),):
12591259
"""
12601260
Retrieve plans for the current user.
12611261
@@ -1326,29 +1326,32 @@ async def get_plan_by_id(request: Request, plan_id: str):
13261326

13271327
# # Initialize memory context
13281328
memory_store = await DatabaseFactory.get_database(user_id=user_id)
1329+
try:
1330+
if plan_id:
1331+
plan = await memory_store.get_plan_by_plan_id(plan_id=plan_id)
1332+
if not plan:
1333+
track_event_if_configured(
1334+
"GetPlanBySessionNotFound",
1335+
{"status_code": 400, "detail": "Plan not found"},
1336+
)
1337+
raise HTTPException(status_code=404, detail="Plan not found")
1338+
1339+
# Use get_steps_by_plan to match the original implementation
13291340

1330-
if plan_id:
1331-
plan = await memory_store.get_plan_by_plan_id(plan_id=plan_id)
1332-
if not plan:
1341+
team = await memory_store.get_team_by_id(team_id=plan.team_id)
1342+
agent_messages = await memory_store.get_agent_messages(plan_id=plan.plan_id)
1343+
m_plan = await memory_store.get_mplan(plan_id=plan.plan_id)
1344+
return {
1345+
"plan": plan,
1346+
"team": team if team else None,
1347+
"messages": agent_messages,
1348+
"m_plan": m_plan if m_plan else None,
1349+
}
1350+
else:
13331351
track_event_if_configured(
1334-
"GetPlanBySessionNotFound",
1335-
{"status_code": 400, "detail": "Plan not found"},
1352+
"GetPlanId", {"status_code": 400, "detail": "no plan id"}
13361353
)
1337-
raise HTTPException(status_code=404, detail="Plan not found")
1338-
1339-
# Use get_steps_by_plan to match the original implementation
1340-
1341-
team = await memory_store.get_team_by_id(team_id=plan.team_id)
1342-
agent_messages = await memory_store.get_agent_messages(plan_id=plan.plan_id)
1343-
m_plan = await memory_store.get_mplan(plan_id=plan.plan_id)
1344-
return {
1345-
"plan": plan,
1346-
"team": team if team else None,
1347-
"messages": agent_messages,
1348-
"m_plan": m_plan if m_plan else None,
1349-
}
1350-
else:
1351-
track_event_if_configured(
1352-
"GetPlanId", {"status_code": 400, "detail": "no plan id"}
1353-
)
1354-
raise HTTPException(status_code=400, detail="no plan id")
1354+
raise HTTPException(status_code=400, detail="no plan id")
1355+
except Exception as e:
1356+
logging.error(f"Error retrieving plan: {str(e)}")
1357+
raise HTTPException(status_code=500, detail="Internal server error occurred")

0 commit comments

Comments
 (0)