feat(админка): обратимые действия и undo
This commit is contained in:
@@ -6,6 +6,7 @@ import time
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
|
||||
import admin_actions
|
||||
import attention
|
||||
import customers
|
||||
import db
|
||||
@@ -153,6 +154,36 @@ async def customer_history(email: str, request: Request):
|
||||
return {"items": data}
|
||||
|
||||
|
||||
@router.post("/customers/{email}/action")
|
||||
async def customer_action(email: str, request: Request):
|
||||
user = require_admin(request)
|
||||
try:
|
||||
body = await request.json()
|
||||
except Exception:
|
||||
raise HTTPException(400, "тело — JSON-объект") from None
|
||||
action = body.get("action") if isinstance(body, dict) else None
|
||||
value = body.get("value") if isinstance(body, dict) else None
|
||||
if not isinstance(value, dict):
|
||||
raise HTTPException(400, "value должен быть объектом")
|
||||
try:
|
||||
return await asyncio.to_thread(admin_actions.perform, user, email, action, value)
|
||||
except ValueError as e:
|
||||
raise HTTPException(400, str(e)) from e
|
||||
except LookupError as e:
|
||||
raise HTTPException(404, str(e)) from e
|
||||
|
||||
|
||||
@router.post("/actions/{action_id}/undo")
|
||||
async def undo_action(action_id: int, request: Request):
|
||||
user = require_admin(request)
|
||||
try:
|
||||
return await asyncio.to_thread(admin_actions.undo, user, action_id)
|
||||
except LookupError as e:
|
||||
raise HTTPException(404, str(e)) from e
|
||||
except ValueError as e:
|
||||
raise HTTPException(400, str(e)) from e
|
||||
|
||||
|
||||
@router.get("/dashboard")
|
||||
async def dashboard(request: Request):
|
||||
user = require_admin(request)
|
||||
|
||||
Reference in New Issue
Block a user