feat: get config with orgs

This commit is contained in:
swve 2024-08-20 18:27:57 +02:00
parent 66610a1493
commit 1423e4c432
3 changed files with 34 additions and 6 deletions

View file

@ -20,6 +20,10 @@ class Organization(OrganizationBase, table=True):
creation_date: str = ""
update_date: str = ""
class OrganizationWithConfig(BaseModel):
org: Organization
config: OrganizationConfig
class OrganizationUpdate(OrganizationBase):
pass

View file

@ -20,7 +20,6 @@ from src.services.orgs.users import (
from src.db.organization_config import OrganizationConfigBase
from src.db.users import PublicUser
from src.db.organizations import (
Organization,
OrganizationCreate,
OrganizationRead,
OrganizationUpdate,
@ -321,7 +320,7 @@ async def api_user_orgs(
limit: int,
current_user: PublicUser = Depends(get_current_user),
db_session: Session = Depends(get_db_session),
) -> List[Organization]:
) -> List[OrganizationRead]:
"""
Get orgs by page and limit by current user
"""

View file

@ -209,6 +209,7 @@ async def create_org(
return org
async def create_org_with_config(
request: Request,
org_object: OrganizationCreate,
@ -338,6 +339,7 @@ async def update_org(
return org
async def update_org_with_config_no_auth(
request: Request,
orgconfig: OrganizationConfigBase,
@ -462,20 +464,43 @@ async def get_orgs_by_user(
user_id: str,
page: int = 1,
limit: int = 10,
) -> list[Organization]:
) -> list[OrganizationRead]:
statement = (
select(Organization)
.join(UserOrganization)
.where(
Organization.id == UserOrganization.org_id,
UserOrganization.user_id == user_id,
UserOrganization.role_id == 1, # Only where the user is admin
)
.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)
orgs = result.all()
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
return orgs #type:ignore
# Config related
async def update_org_signup_mechanism(