fix: Resolve issue with adding user to tenants

This commit is contained in:
Igor Ilic 2025-10-19 21:11:14 +02:00
parent 9a2a84905c
commit 12785e31ea

View file

@ -48,22 +48,18 @@ async def add_user_to_tenant(
message="Only tenant owner can add other users to organization." message="Only tenant owner can add other users to organization."
) )
try: if set_active_tenant:
user.tenant_id = tenant_id
await session.merge(user)
await session.commit()
try: try:
# Add association directly to the association table # Add association directly to the association table
create_user_tenant_statement = insert(UserTenant).values( create_user_tenant_statement = insert(UserTenant).values(
user_id=user_id, tenant_id=tenant_id user_id=user_id, tenant_id=tenant_id
) )
await session.execute(create_user_tenant_statement) await session.execute(create_user_tenant_statement)
await session.commit()
except IntegrityError: except IntegrityError:
raise EntityAlreadyExistsError(message="User is already part of group.") raise EntityAlreadyExistsError(message="User is already part of group.")
if set_active_tenant:
user.tenant_id = tenant_id
await session.merge(user)
await session.commit()
except IntegrityError:
raise EntityAlreadyExistsError(
message="User is already part of a tenant. Only one tenant can be assigned to user."
)