diff --git a/backend/app/api/api_v1/routers/platform.py b/backend/app/api/api_v1/routers/platform.py index 1ba9a78..70288c6 100644 --- a/backend/app/api/api_v1/routers/platform.py +++ b/backend/app/api/api_v1/routers/platform.py @@ -368,21 +368,21 @@ async def userdomain_delete( @r.get( "/activedomain", - response_model=ApiReturnModel[Domain], + response_model=ApiReturnModel[ActiveDomain|None], response_model_exclude_none=True, ) -async def get_useractivedomain( +async def get_activeuserdomain( request: Request, - userId: Optional[int] = Query(None, alias="userId"), user=Depends(get_current_active_user), db=Depends(get_db), ): try: # domain = get_activedomain(db, user.id) - domain = get_activedomain(db, userId if userId is not None else user.id) + domain = get_activedomain(db, user.id) # if domain is None: # return JSONResponse(content=None,status_code=HTTPStatus.OK) - return ApiReturnModel(data =domain) + return ApiReturnModel(data = domain) + except Exception as e: raise APIException('platform:activedomain',request.url._url,f"Error occurred while get user({user.id}) activedomain:",e) diff --git a/backend/app/db/crud.py b/backend/app/db/crud.py index 33dbc04..571d0ff 100644 --- a/backend/app/db/crud.py +++ b/backend/app/db/crud.py @@ -371,14 +371,15 @@ def active_userdomain(db: Session, userid: int,domainid: int): db.commit() return db_userdomains -def get_activedomain(db: Session, userid: int)-> t.Optional[models.Domain]: - user_domains = (db.query(models.Domain,models.UserDomain.active) - .join(models.UserDomain,models.UserDomain.domainid == models.Domain.id ) - .filter(models.UserDomain.userid == userid) - .all()) - db_domain=None - if len(user_domains)==1: - db_domain = user_domains[0][0]; +def get_activedomain(db: Session, userid: int): + # user_domains = (db.query(models.Domain,models.UserDomain.active) + # .join(models.UserDomain,models.UserDomain.domainid == models.Domain.id ) + # .filter(models.UserDomain.userid == userid) + # .all()) + db_domain=(db.query(models.Domain).filter(models.Domain.is_active) + .join(models.UserDomain,models.UserDomain.domainid == models.Domain.id).filter(and_(models.UserDomain.active,models.UserDomain.userid == userid)).first()) + # if len(user_domains)==1: + # db_domain = user_domains[0][0]; # else: # db_domain = next((domain for domain,active in user_domains if active),None) # raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Domain not found") diff --git a/backend/app/db/schemas.py b/backend/app/db/schemas.py index ab07302..56ddb8b 100644 --- a/backend/app/db/schemas.py +++ b/backend/app/db/schemas.py @@ -157,6 +157,13 @@ class DomainIn(BaseModel): encrypted_pwd = chacha20Encrypt(self.kintonepwd) self.kintonepwd = encrypted_pwd +class ActiveDomain(BaseModel): + id: int + tenantid: str + name: str + url: str + is_active: bool + class Domain(Base): id: int tenantid: str