Files
ai-contract-platform/backend/app/api/tenant_settings.py
T
2026-04-24 13:44:17 +08:00

69 lines
2.2 KiB
Python

from typing import Optional
from fastapi import APIRouter, Depends, Form, Header
from sqlalchemy.orm import Session
from app.core.response import success_response
from app.db import get_db_session
from app.services.audit_log_service import AuditLogService
from app.services.auth_service import AuthService
from app.services.tenant_service import TenantService
router = APIRouter(prefix="/tenant-settings", tags=["tenant-settings"])
ROUTE_PERMISSION_SCOPE = "tenant_settings"
ROUTE_PERMISSION_ACTIONS = {
"get": "tenant_settings:get",
"update": "tenant_settings:update",
}
def get_tenant_service(db: Session = Depends(get_db_session)) -> TenantService:
return TenantService(db)
def get_audit_log_service(db: Session = Depends(get_db_session)) -> AuditLogService:
return AuditLogService(db)
def require_current_user(
authorization: Optional[str] = Header(default=None),
db: Session = Depends(get_db_session),
):
return AuthService(db).require_current_user(authorization)
@router.get("/me")
def get_current_tenant_settings(
tenant_service: TenantService = Depends(get_tenant_service),
current_user: dict = Depends(require_current_user),
):
return success_response(tenant_service.get_current_settings(current_user))
@router.put("/me")
def update_current_tenant_settings(
name: str = Form(...),
code: str = Form(...),
status_value: str = Form(alias="status", default="active"),
tenant_service: TenantService = Depends(get_tenant_service),
audit_log_service: AuditLogService = Depends(get_audit_log_service),
current_user: dict = Depends(require_current_user),
):
result = tenant_service.update_current_settings(current_user, name, code, status_value)
audit_log_service.record_event(
tenant_id=current_user.get("tenant_id"),
user_id=current_user.get("id"),
action="update_tenant_settings",
resource_type="tenant",
resource_id=result["id"],
detail_json={
"name": result["name"],
"code": result["code"],
"status": result["status"],
"plan_name": result["plan_name"],
"max_users": result["max_users"],
"max_storage_mb": result["max_storage_mb"],
},
)
return success_response(result)