cohorta/backend/app/models/app_settings.py
Vadym Samoilenko 4c307bf00d feat: increase trial credits from 10 to 50 on signup
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-23 21:05:13 +01:00

55 lines
1.3 KiB
Python

"""
App-wide configuration stored in MongoDB.
Single document with _id='config'. Cached in-memory for 60 seconds.
"""
import time
import logging
from typing import Any
from app.db import get_db
logger = logging.getLogger(__name__)
_cache: dict[str, Any] = {}
_cache_ts: float = 0
_CACHE_TTL = 60 # seconds
DEFAULTS = {
"_id": "config",
"persona_cost": 2,
"run_cost": 40,
"trial_grant": 50,
"credit_packs": [
{"id": "starter", "name": "Starter", "price_usd": 49, "credits": 50},
{"id": "pro", "name": "Pro", "price_usd": 199, "credits": 220},
{"id": "scale", "name": "Scale", "price_usd": 499, "credits": 600},
],
}
async def get_settings() -> dict:
global _cache, _cache_ts
if _cache and (time.monotonic() - _cache_ts) < _CACHE_TTL:
return _cache
db = await get_db()
doc = await db.app_settings.find_one({"_id": "config"})
if not doc:
await db.app_settings.insert_one(DEFAULTS.copy())
doc = DEFAULTS.copy()
_cache = doc
_cache_ts = time.monotonic()
return doc
async def update_settings(fields: dict) -> dict:
global _cache, _cache_ts
db = await get_db()
await db.app_settings.update_one(
{"_id": "config"},
{"$set": fields},
upsert=True,
)
_cache = {} # invalidate cache
return await get_settings()