2026.03.30 front és garázs logika
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
#/opt/docker/dev/service_finder/backend/app/api/v1/endpoints/users.py
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from typing import Dict, Any
|
||||
|
||||
from app.api.deps import get_db, get_current_user
|
||||
from app.schemas.user import UserResponse, UserUpdate
|
||||
from app.schemas.user import UserResponse, UserUpdate, ActiveOrganizationUpdate
|
||||
from app.models.identity import User
|
||||
from app.services.trust_engine import TrustEngine
|
||||
|
||||
@@ -70,8 +71,12 @@ async def read_users_me(
|
||||
# Create a response dictionary with the active_organization_id
|
||||
# Get first_name and last_name from person relation if available
|
||||
person = current_user.person
|
||||
first_name = person.first_name if person else ""
|
||||
last_name = person.last_name if person else ""
|
||||
# Safe extraction with fallback to empty string
|
||||
first_name = ""
|
||||
last_name = ""
|
||||
if person:
|
||||
first_name = getattr(person, 'first_name', '')
|
||||
last_name = getattr(person, 'last_name', '')
|
||||
|
||||
response_data = {
|
||||
"id": current_user.id,
|
||||
@@ -141,6 +146,59 @@ async def update_user_preferences(
|
||||
else:
|
||||
raise HTTPException(status_code=400, detail=f"Invalid field: {field}")
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(current_user)
|
||||
return current_user
|
||||
try:
|
||||
await db.commit()
|
||||
await db.refresh(current_user)
|
||||
except SQLAlchemyError as e:
|
||||
await db.rollback()
|
||||
raise HTTPException(status_code=500, detail=f"Database error: {str(e)}")
|
||||
|
||||
# Return the Pydantic model instead of raw SQLAlchemy object
|
||||
return UserResponse.model_validate(current_user)
|
||||
|
||||
|
||||
@router.patch("/me/active-organization", response_model=UserResponse)
|
||||
async def update_active_organization(
|
||||
update_data: ActiveOrganizationUpdate,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""
|
||||
Update the user's active organization (scope_id).
|
||||
|
||||
Accepts an organization_id (UUID/string) or None to revert to personal mode.
|
||||
"""
|
||||
# Extract organization_id from request
|
||||
org_id = update_data.organization_id
|
||||
|
||||
# Validate that the user has access to this organization if org_id is provided
|
||||
if org_id is not None:
|
||||
from sqlalchemy import select
|
||||
from app.models.marketplace.organization import OrganizationMember
|
||||
|
||||
# Check if user is a member of the organization
|
||||
stmt = select(OrganizationMember).where(
|
||||
OrganizationMember.organization_id == org_id,
|
||||
OrganizationMember.user_id == current_user.id
|
||||
)
|
||||
result = await db.execute(stmt)
|
||||
member = result.scalar_one_or_none()
|
||||
|
||||
if not member:
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail="You are not a member of this organization"
|
||||
)
|
||||
|
||||
# Update user's scope_id
|
||||
current_user.scope_id = org_id
|
||||
|
||||
try:
|
||||
await db.commit()
|
||||
await db.refresh(current_user)
|
||||
except SQLAlchemyError as e:
|
||||
await db.rollback()
|
||||
raise HTTPException(status_code=500, detail=f"Database error: {str(e)}")
|
||||
|
||||
# Return updated user data
|
||||
return UserResponse.model_validate(current_user)
|
||||
|
||||
Reference in New Issue
Block a user