2026-08-03 09:34:08 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-08-21 09:49:48 +08:00
|
|
|
from fastapi import APIRouter, Body, Request, Depends
|
2026-08-03 09:34:08 +08:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from app.api.v1.endpoints.platform import ok, fail
|
|
|
|
|
from app.db.platform_store import get_platform_store
|
2026-08-21 09:49:48 +08:00
|
|
|
from app.core.auth import require_admin
|
2026-08-03 09:34:08 +08:00
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/retention-policies", tags=["retention"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _actor(request: Request) -> str | None:
|
|
|
|
|
auth = request.headers.get("Authorization", "")
|
|
|
|
|
token = auth.replace("Bearer ", "").strip()
|
|
|
|
|
return token or None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("")
|
2026-08-21 09:49:48 +08:00
|
|
|
def list_policies(current_user: dict = Depends(require_admin)) -> dict[str, Any]:
|
2026-08-03 09:34:08 +08:00
|
|
|
return ok(get_platform_store().retention_policies())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("")
|
2026-08-21 09:49:48 +08:00
|
|
|
def create_policy(payload: dict[str, Any] = Body(...), request: Request = None, current_user: dict = Depends(require_admin)) -> dict[str, Any]:
|
2026-08-03 09:34:08 +08:00
|
|
|
if not payload.get("name"):
|
|
|
|
|
raise fail(400, "name 必填")
|
|
|
|
|
policy = get_platform_store().create_retention_policy(payload)
|
|
|
|
|
get_platform_store().record_audit(
|
|
|
|
|
action="retention.create",
|
2026-08-21 09:49:48 +08:00
|
|
|
actor_id=current_user.get("id"),
|
2026-08-03 09:34:08 +08:00
|
|
|
target_type="retention_policy",
|
|
|
|
|
target_id=policy["id"],
|
|
|
|
|
detail=f"name={policy.get('name')}",
|
|
|
|
|
)
|
|
|
|
|
return ok(policy)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/{policy_id}")
|
2026-08-21 09:49:48 +08:00
|
|
|
def get_policy(policy_id: str, current_user: dict = Depends(require_admin)) -> dict[str, Any]:
|
2026-08-03 09:34:08 +08:00
|
|
|
try:
|
|
|
|
|
return ok(get_platform_store().retention_policy(policy_id))
|
|
|
|
|
except KeyError:
|
|
|
|
|
raise fail(404, "retention policy not found")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.put("/{policy_id}")
|
|
|
|
|
def update_policy(
|
2026-08-21 09:49:48 +08:00
|
|
|
policy_id: str, payload: dict[str, Any] = Body(...), request: Request = None,
|
|
|
|
|
current_user: dict = Depends(require_admin),
|
2026-08-03 09:34:08 +08:00
|
|
|
) -> dict[str, Any]:
|
|
|
|
|
store = get_platform_store()
|
|
|
|
|
try:
|
|
|
|
|
policy = store.update_retention_policy(policy_id, payload)
|
|
|
|
|
except KeyError:
|
|
|
|
|
raise fail(404, "retention policy not found")
|
|
|
|
|
store.record_audit(
|
|
|
|
|
action="retention.update",
|
2026-08-21 09:49:48 +08:00
|
|
|
actor_id=current_user.get("id"),
|
2026-08-03 09:34:08 +08:00
|
|
|
target_type="retention_policy",
|
|
|
|
|
target_id=policy_id,
|
|
|
|
|
detail=f"fields={','.join(payload.keys())}",
|
|
|
|
|
)
|
|
|
|
|
return ok(policy)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.delete("/{policy_id}")
|
2026-08-21 09:49:48 +08:00
|
|
|
def delete_policy(policy_id: str, request: Request = None, current_user: dict = Depends(require_admin)) -> dict[str, Any]:
|
2026-08-03 09:34:08 +08:00
|
|
|
store = get_platform_store()
|
|
|
|
|
store.delete_retention_policy(policy_id)
|
|
|
|
|
store.record_audit(
|
|
|
|
|
action="retention.delete",
|
2026-08-21 09:49:48 +08:00
|
|
|
actor_id=current_user.get("id"),
|
2026-08-03 09:34:08 +08:00
|
|
|
target_type="retention_policy",
|
|
|
|
|
target_id=policy_id,
|
|
|
|
|
)
|
|
|
|
|
return ok({"deleted": policy_id})
|