fix: add specific org admin endpoint

This commit is contained in:
swve 2024-08-28 18:20:55 +02:00
parent 283e5bc189
commit 4367a8a0fb
2 changed files with 60 additions and 5 deletions

View file

@ -34,6 +34,7 @@ from src.services.orgs.orgs import (
get_organization, get_organization,
get_organization_by_slug, get_organization_by_slug,
get_orgs_by_user, get_orgs_by_user,
get_orgs_by_user_admin,
update_org, update_org,
update_org_logo, update_org_logo,
update_org_signup_mechanism, update_org_signup_mechanism,
@ -329,6 +330,22 @@ async def api_user_orgs(
) )
@router.get("/user_admin/page/{page}/limit/{limit}")
async def api_user_orgs_admin(
request: Request,
page: int,
limit: int,
current_user: PublicUser = Depends(get_current_user),
db_session: Session = Depends(get_db_session),
) -> List[OrganizationRead]:
"""
Get orgs by page and limit by current user
"""
return await get_orgs_by_user_admin(
request, db_session, str(current_user.id), page, limit
)
@router.put("/{org_id}") @router.put("/{org_id}")
async def api_update_org( async def api_update_org(
request: Request, request: Request,

View file

@ -179,10 +179,7 @@ async def create_org(
collaboration=CollaborationOrgConfig(enabled=True, limit=0), collaboration=CollaborationOrgConfig(enabled=True, limit=0),
api=APIOrgConfig(enabled=True, limit=0), api=APIOrgConfig(enabled=True, limit=0),
), ),
cloud=OrgCloudConfig( cloud=OrgCloudConfig(plan="free", custom_domain=False),
plan='free',
custom_domain=False
)
) )
org_config = json.loads(org_config.json()) org_config = json.loads(org_config.json())
@ -463,7 +460,7 @@ async def delete_org(
return {"detail": "Organization deleted"} return {"detail": "Organization deleted"}
async def get_orgs_by_user( async def get_orgs_by_user_admin(
request: Request, request: Request,
db_session: Session, db_session: Session,
user_id: str, user_id: str,
@ -507,6 +504,47 @@ async def get_orgs_by_user(
return orgsWithConfig return orgsWithConfig
async def get_orgs_by_user(
request: Request,
db_session: Session,
user_id: str,
page: int = 1,
limit: int = 10,
) -> list[OrganizationRead]:
statement = (
select(Organization)
.join(UserOrganization)
.where(UserOrganization.user_id == user_id)
.offset((page - 1) * limit)
.limit(limit)
)
# Get organizations where the user is an admin
result = db_session.exec(statement)
orgs = result.all()
orgsWithConfig = []
for org in orgs:
# Get org config
statement = select(OrganizationConfig).where(
OrganizationConfig.org_id == org.id
)
result = db_session.exec(statement)
org_config = result.first()
config = OrganizationConfig.model_validate(org_config) if org_config else {}
org = OrganizationRead(**org.model_dump(), config=config)
orgsWithConfig.append(org)
return orgsWithConfig
# Config related # Config related
async def update_org_signup_mechanism( async def update_org_signup_mechanism(
request: Request, request: Request,