11from fastapi import APIRouter , Depends , HTTPException
2- from sqlalchemy .orm import Session
2+ from sqlalchemy .ext .asyncio import AsyncSession
3+ from sqlalchemy import select
34from typing import Any , Dict , List
45
56from app .api import deps
910from app .models .task .model import Task
1011from app .models .note .model import Note
1112from app .models .worklog .model import WorkLog , ActivityLog
13+ from app .models .agent_chat .model import AgentConversation
1214
1315router = APIRouter ()
1416
1517@router .get ("/" , response_model = Dict [str , List [Any ]])
16- def get_user_activity (
17- db : Session = Depends (deps .get_db ),
18+ async def get_user_activity (
19+ db : AsyncSession = Depends (deps .get_db ),
1820 current_user : User = Depends (deps .get_current_active_user ),
1921 skip : int = 0 ,
2022 limit : int = 100 ,
@@ -31,40 +33,44 @@ def format_list(items):
3133 ]
3234
3335 # 1. Agents Created
34- agents = db .query (Agent ).filter (Agent .user_id == current_user .id ).all ()
36+ result = await db .execute (select (Agent ).filter (Agent .created_by_user_id == current_user .id ))
37+ agents = result .scalars ().all ()
3538
3639 # 2. Projects Created (Owned)
37- projects = db .query (Project ).filter (Project .owner_id == current_user .id ).all ()
40+ result = await db .execute (select (Project ).filter (Project .owner_id == current_user .id ))
41+ projects = result .scalars ().all ()
3842
3943 # 3. Tasks Created
40- # Note: Using created_by_user_id based on User model relationship
4144 try :
42- tasks = db .query (Task ).filter (Task .created_by_user_id == current_user .id ).limit (limit ).all ()
45+ result = await db .execute (select (Task ).filter (Task .created_by_id == current_user .id ).limit (limit ))
46+ tasks = result .scalars ().all ()
4347 except Exception :
44- # Fallback if field name differs (e.g. might be user_id or creator_id)
45- # Using a safer generic try/except to avoid crashing the whole endpoint
4648 tasks = []
4749
4850 # 4. Notes Created
4951 try :
50- notes = db .query (Note ).filter (Note .owner_id == current_user .id ).limit (limit ).all ()
52+ result = await db .execute (select (Note ).filter (Note .owner_id == current_user .id ).limit (limit ))
53+ notes = result .scalars ().all ()
5154 except Exception :
5255 notes = []
5356
54- # 5. Agent Sessions
57+ # 5. Agent Conversations
5558 try :
56- sessions = db .query (AgentSession ).filter (AgentSession .user_id == current_user .id ).limit (limit ).all ()
59+ result = await db .execute (select (AgentConversation ).filter (AgentConversation .user_id == current_user .id ).limit (limit ))
60+ sessions = result .scalars ().all ()
5761 except Exception :
5862 sessions = []
5963
6064 # 6. Work Logs
6165 try :
62- work_logs = db .query (WorkLog ).filter (WorkLog .user_id == current_user .id ).limit (limit ).all ()
66+ result = await db .execute (select (WorkLog ).filter (WorkLog .user_id == current_user .id ).limit (limit ))
67+ work_logs = result .scalars ().all ()
6368 except Exception :
6469 work_logs = []
6570
6671 # 7. Activity Logs
67- activity_logs = db .query (ActivityLog ).filter (ActivityLog .user_id == current_user .id ).order_by (ActivityLog .timestamp .desc ()).limit (limit ).all ()
72+ result = await db .execute (select (ActivityLog ).filter (ActivityLog .user_id == current_user .id ).order_by (ActivityLog .created_at .desc ()).limit (limit ))
73+ activity_logs = result .scalars ().all ()
6874
6975 return {
7076 "agents" : format_list (agents ),
@@ -77,20 +83,23 @@ def format_list(items):
7783 }
7884
7985@router .get ("/logs" , response_model = List [Any ])
80- def get_user_activity_logs (
81- db : Session = Depends (deps .get_db ),
86+ async def get_user_activity_logs (
87+ db : AsyncSession = Depends (deps .get_db ),
8288 current_user : User = Depends (deps .get_current_active_user ),
8389 skip : int = 0 ,
8490 limit : int = 50 ,
8591) -> Any :
8692 """
8793 Get specific activity logs for the user (audit trail).
8894 """
89- logs = db .query (ActivityLog ).filter (ActivityLog .user_id == current_user .id )\
90- .order_by (ActivityLog .timestamp .desc ())\
91- .offset (skip )\
92- .limit (limit )\
93- .all ()
95+ result = await db .execute (
96+ select (ActivityLog )
97+ .filter (ActivityLog .user_id == current_user .id )
98+ .order_by (ActivityLog .created_at .desc ())
99+ .offset (skip )
100+ .limit (limit )
101+ )
102+ logs = result .scalars ().all ()
94103
95104 return [
96105 {k : v for k , v in log .__dict__ .items () if not k .startswith ('_' )}
0 commit comments