nomadro/apps/hackrobot/nomadmini/backend/app/services/books_live.py
Eric 1c2486a9c5 Hot-sync ebook and VIP offer changes from admin to clients within 1-2s.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-22 11:33:00 +08:00

51 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""小册付费方案变更 → 轻触 remote-config,走现有 WS/SSE/1s 轮询热推。"""
from __future__ import annotations
import json
from datetime import datetime, timezone
from typing import Any
from app.services.tt_platform_config import resolve_config_file
def bump_books_offer_live(book_id: str) -> dict[str, Any]:
"""后台切换小册道具后调用:copy.booksOfferRev++ 并 notify。"""
path = resolve_config_file()
path.parent.mkdir(parents=True, exist_ok=True)
raw: dict[str, Any] = {}
if path.is_file():
try:
parsed = json.loads(path.read_text(encoding="utf-8"))
if isinstance(parsed, dict):
raw = parsed
except Exception:
raw = {}
out = dict(raw) if raw else {"version": 1, "modules": {}, "flags": {}, "copy": {}}
copy = dict(out.get("copy") or {})
try:
rev = int(copy.get("booksOfferRev") or 0) + 1
except Exception:
rev = 1
copy["booksOfferRev"] = rev
copy["booksOfferBookId"] = str(book_id or "").strip()
out["copy"] = copy
out["updatedAt"] = datetime.now(timezone.utc).isoformat()
try:
out["version"] = int(out.get("version") or 1) + 1
except Exception:
out["version"] = 2
path.write_text(json.dumps(out, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
try:
from app.services.config_live import notify_config_written
notify_config_written(out)
except Exception:
pass
return {
"booksOfferRev": rev,
"booksOfferBookId": copy["booksOfferBookId"],
"version": out["version"],
}