From 8d1b7c1efbc7e6d8329dd5c4f32ad6a9e356aae9 Mon Sep 17 00:00:00 2001 From: Urtzi Alfaro Date: Sat, 2 Aug 2025 18:38:14 +0200 Subject: [PATCH] Fix user delete flow 4 --- gateway/app/routes/tenant.py | 5 ++++ .../auth/app/services/auth_service_clients.py | 2 +- services/tenant/app/api/tenants.py | 27 +------------------ .../tenant/app/services/tenant_service.py | 26 ------------------ 4 files changed, 7 insertions(+), 53 deletions(-) diff --git a/gateway/app/routes/tenant.py b/gateway/app/routes/tenant.py index 815624b9..06996b38 100644 --- a/gateway/app/routes/tenant.py +++ b/gateway/app/routes/tenant.py @@ -38,6 +38,11 @@ async def get_tenant_members(request: Request, tenant_id: str = Path(...)): """Get tenant members""" return await _proxy_to_tenant_service(request, f"/api/v1/tenants/{tenant_id}/members") +@router.get("/user/{user_id}") +async def get_user_tenants(request: Request, user_id: str = Path(...)): + """Get all tenant memberships for a user (admin only)""" + return await _proxy_to_tenant_service(request, f"/api/v1/tenants/user/{user_id}") + # ================================================================ # TENANT-SCOPED DATA SERVICE ENDPOINTS # ================================================================ diff --git a/services/auth/app/services/auth_service_clients.py b/services/auth/app/services/auth_service_clients.py index c7ae01ed..159811bf 100644 --- a/services/auth/app/services/auth_service_clients.py +++ b/services/auth/app/services/auth_service_clients.py @@ -40,7 +40,7 @@ class AuthTenantServiceClient(BaseServiceClient): async def get_user_tenants(self, user_id: str) -> Optional[List[Dict[str, Any]]]: """Get all tenant memberships for a user""" try: - result = await self.get(f"tenants/memberships/{user_id}") + result = await self.get(f"tenants/user/{user_id}") return result.get("memberships", []) if result else [] except Exception as e: logger.error("Failed to get user tenants", user_id=user_id, error=str(e)) diff --git a/services/tenant/app/api/tenants.py b/services/tenant/app/api/tenants.py index 3500a58e..bbe3fbf5 100644 --- a/services/tenant/app/api/tenants.py +++ b/services/tenant/app/api/tenants.py @@ -73,31 +73,6 @@ async def verify_tenant_access( detail="Access verification failed" ) -@router.get("/tenants/users/{user_id}", response_model=List[TenantResponse]) -async def get_user_tenants( - user_id: str, - current_user: Dict[str, Any] = Depends(get_current_user_dep), - db: AsyncSession = Depends(get_db) -): - - # Users can only see their own tenants - if current_user["user_id"] != user_id: - raise HTTPException( - status_code=status.HTTP_403_FORBIDDEN, - detail="Access denied" - ) - - try: - tenants = await TenantService.get_user_tenants(user_id, db) - return tenants - - except Exception as e: - logger.error(f"Failed to get user tenants: {e}") - raise HTTPException( - status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, - detail="Failed to retrieve tenants" - ) - @router.get("/tenants/{tenant_id}", response_model=TenantResponse) async def get_tenant( tenant_id: UUID = Path(..., description="Tenant ID"), @@ -313,7 +288,7 @@ async def delete_tenant_complete( detail=f"Failed to delete tenant: {str(e)}" ) -@router.get("/tenants/memberships/{user_id}") +@router.get("/user/{user_id}") async def get_user_tenants( user_id: str, current_user = Depends(get_current_user_dep), diff --git a/services/tenant/app/services/tenant_service.py b/services/tenant/app/services/tenant_service.py index 7b08db39..d51d57b6 100644 --- a/services/tenant/app/services/tenant_service.py +++ b/services/tenant/app/services/tenant_service.py @@ -129,32 +129,6 @@ class TenantService: permissions=[] ) - @staticmethod - async def get_user_tenants(user_id: str, db: AsyncSession) -> List[TenantResponse]: - """Get all tenants accessible by user""" - - try: - # Get user's tenant memberships - result = await db.execute( - select(Tenant) - .join(TenantMember, Tenant.id == TenantMember.tenant_id) - .where( - and_( - TenantMember.user_id == user_id, - TenantMember.is_active == True, - Tenant.is_active == True - ) - ) - .order_by(Tenant.name) - ) - - tenants = result.scalars().all() - return [TenantResponse.from_orm(tenant) for tenant in tenants] - - except Exception as e: - logger.error(f"Error getting user tenants: {e}") - return [] - @staticmethod async def get_tenant_by_id(tenant_id: str, db: AsyncSession) -> Optional[TenantResponse]: """Get tenant by ID"""