Files
site/apps/api/app/modules/zootech/wesp_compat_sync.py
T
2026-07-17 12:57:18 +03:00

106 lines
3.6 KiB
Python

"""WESP /api/sync/clients compat — maps to orchestrator farm hubs."""
from __future__ import annotations
from fastapi import APIRouter, Depends, HTTPException, Query, status
from pydantic import BaseModel
from app.core.database import session_scope
from app.modules.sync import repository as sync_repo
from app.modules.sync.models import FarmHub
from app.modules.sync.tenant import TenantContext, require_enterprise_zootech
router = APIRouter()
class SyncClientDisplayNameBody(BaseModel):
display_name: str = ""
@router.get("/sync/clients")
def list_sync_clients(
enterprise_id: str = Query(...),
tenant: TenantContext = Depends(require_enterprise_zootech),
):
if tenant.enterprise_id != enterprise_id:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="ENTERPRISE_FORBIDDEN")
hubs = sync_repo.list_farm_hubs(enterprise_id)
return [
{
"id": h.id,
"node_id": h.hub_site_id,
"client_name": h.name,
"display_name": h.name,
"status": h.status,
"last_seen": h.last_seen.isoformat() if h.last_seen else None,
}
for h in hubs
]
def _hub_for_node(enterprise_id: str, node_id: str) -> FarmHub:
hub = sync_repo.get_farm_hub_by_site_id(node_id)
if hub is None or hub.enterprise_id != enterprise_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail={"error": True, "message": "Клиент не найден"},
)
return hub
@router.put("/sync/clients/{node_id}/display_name")
def sync_client_set_display_name(
node_id: str,
body: SyncClientDisplayNameBody,
enterprise_id: str = Query(...),
tenant: TenantContext = Depends(require_enterprise_zootech),
):
if tenant.enterprise_id != enterprise_id:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="ENTERPRISE_FORBIDDEN")
hub = _hub_for_node(enterprise_id, node_id)
display_name = (body.display_name or "").strip()
with session_scope() as db:
row = db.get(FarmHub, hub.id)
if row is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail={"error": True, "message": "Клиент не найден"},
)
if not display_name:
return {"success": True, "message": "Имя клиента сброшено"}
row.name = display_name
return {"success": True, "display_name": display_name}
@router.delete("/sync/clients/{node_id}")
def sync_client_delete(
node_id: str,
enterprise_id: str = Query(...),
tenant: TenantContext = Depends(require_enterprise_zootech),
):
if tenant.enterprise_id != enterprise_id:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="ENTERPRISE_FORBIDDEN")
hub = _hub_for_node(enterprise_id, node_id)
with session_scope() as db:
row = db.get(FarmHub, hub.id)
if row is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail={"error": True, "message": "Клиент не найден"},
)
row.status = "inactive"
return {"success": True}
@router.get("/sync/clients/{node_id}/deliveries")
def sync_client_deliveries(
node_id: str,
enterprise_id: str = Query(...),
hours: int = Query(24, ge=1),
tenant: TenantContext = Depends(require_enterprise_zootech),
):
if tenant.enterprise_id != enterprise_id:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="ENTERPRISE_FORBIDDEN")
_hub_for_node(enterprise_id, node_id)
return {"deliveries": []}