Spaces:
Paused
Paused
CBV01 commited on
Commit ·
4ede7da
1
Parent(s): de8d8fa
Fix update_user_vitals - remove lru_cache dependency, add bulletproof error handling
Browse files- backend/main.py +71 -47
backend/main.py
CHANGED
|
@@ -2020,54 +2020,78 @@ async def get_monetization_users(admin_id: str = Depends(get_current_admin)):
|
|
| 2020 |
@app.post("/api/admin/monetization/users/{uid}/vitals")
|
| 2021 |
async def update_user_vitals(uid: str, req: UserVitalsUpdateRequest, admin_id: str = Depends(get_current_admin)):
|
| 2022 |
conn = get_db_connection()
|
| 2023 |
-
|
| 2024 |
-
|
| 2025 |
-
|
| 2026 |
-
|
| 2027 |
-
|
| 2028 |
-
|
| 2029 |
-
|
| 2030 |
-
|
| 2031 |
-
|
| 2032 |
-
|
| 2033 |
-
|
| 2034 |
-
|
| 2035 |
-
|
| 2036 |
-
|
| 2037 |
-
|
| 2038 |
-
|
| 2039 |
-
|
| 2040 |
-
|
| 2041 |
-
|
| 2042 |
-
|
| 2043 |
-
|
| 2044 |
-
|
| 2045 |
-
|
| 2046 |
-
|
| 2047 |
-
|
| 2048 |
-
|
| 2049 |
-
|
| 2050 |
-
|
| 2051 |
-
|
| 2052 |
-
|
| 2053 |
-
|
| 2054 |
-
|
| 2055 |
-
|
| 2056 |
-
|
| 2057 |
-
|
| 2058 |
-
|
| 2059 |
-
|
| 2060 |
-
|
| 2061 |
-
|
| 2062 |
-
|
| 2063 |
-
|
| 2064 |
-
|
| 2065 |
-
|
| 2066 |
-
|
| 2067 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2068 |
|
| 2069 |
-
|
| 2070 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2071 |
|
| 2072 |
@app.get("/api/admin/monetization/coupons")
|
| 2073 |
async def get_coupons_admin(admin_id: str = Depends(get_current_admin)):
|
|
|
|
| 2020 |
@app.post("/api/admin/monetization/users/{uid}/vitals")
|
| 2021 |
async def update_user_vitals(uid: str, req: UserVitalsUpdateRequest, admin_id: str = Depends(get_current_admin)):
|
| 2022 |
conn = get_db_connection()
|
| 2023 |
+
try:
|
| 2024 |
+
# Always look up the plan config live from DB to avoid stale cache issues
|
| 2025 |
+
plan_row = conn.execute("SELECT * FROM plans WHERE key = ?", (req.plan,)).fetchone()
|
| 2026 |
+
|
| 2027 |
+
# Safe defaults in case plan doesn't exist in DB
|
| 2028 |
+
DEFAULTS = {
|
| 2029 |
+
"free": {"scrape_limit": 50, "max_accounts": 1, "max_daily_campaigns": 20, "max_daily_keywords": 5, "max_templates": 1, "price": 0},
|
| 2030 |
+
"basic": {"scrape_limit": 100, "max_accounts": 2, "max_daily_campaigns": 50, "max_daily_keywords": 10, "max_templates": 1, "price": 2000},
|
| 2031 |
+
"standard": {"scrape_limit": 250, "max_accounts": 5, "max_daily_campaigns": 150, "max_daily_keywords": 25, "max_templates": 2, "price": 3500},
|
| 2032 |
+
"premium": {"scrape_limit": 500, "max_accounts": 10, "max_daily_campaigns": 300, "max_daily_keywords": 50, "max_templates": 2, "price": 5000},
|
| 2033 |
+
"unlimited": {"scrape_limit": 1000, "max_accounts": 99, "max_daily_campaigns": 999999, "max_daily_keywords": 999999, "max_templates": 999, "price": 0},
|
| 2034 |
+
}
|
| 2035 |
+
|
| 2036 |
+
if plan_row:
|
| 2037 |
+
plan_cfg = dict(plan_row)
|
| 2038 |
+
else:
|
| 2039 |
+
plan_cfg = DEFAULTS.get(req.plan, DEFAULTS["free"])
|
| 2040 |
+
|
| 2041 |
+
# Use provided override values or fall back to plan defaults
|
| 2042 |
+
s_limit = req.scrape_limit if req.scrape_limit is not None else plan_cfg["scrape_limit"]
|
| 2043 |
+
m_acc = req.max_accounts if req.max_accounts is not None else plan_cfg["max_accounts"]
|
| 2044 |
+
m_camp = req.max_daily_campaigns if req.max_daily_campaigns is not None else plan_cfg["max_daily_campaigns"]
|
| 2045 |
+
m_key = req.max_daily_keywords if req.max_daily_keywords is not None else plan_cfg["max_daily_keywords"]
|
| 2046 |
+
m_tmpl = plan_cfg.get("max_templates", 1)
|
| 2047 |
+
|
| 2048 |
+
# Handle Subscription Dates
|
| 2049 |
+
activated_at = None
|
| 2050 |
+
expires_at = None
|
| 2051 |
+
|
| 2052 |
+
# Check current user status for payment logging
|
| 2053 |
+
curr_user = conn.execute("SELECT plan, is_approved, username FROM users WHERE id = ?", (uid,)).fetchone()
|
| 2054 |
+
already_paid = (curr_user and curr_user["is_approved"] == 1 and curr_user["plan"] != 'free')
|
| 2055 |
+
|
| 2056 |
+
if req.plan != 'free' and req.is_approved:
|
| 2057 |
+
now = datetime.now()
|
| 2058 |
+
activated_at = now.strftime('%Y-%m-%dT%H:%M')
|
| 2059 |
+
expires_at = (now + timedelta(days=30)).strftime('%Y-%m-%dT%H:%M')
|
| 2060 |
+
|
| 2061 |
+
# Log payment only on first-time upgrade
|
| 2062 |
+
if not already_paid:
|
| 2063 |
+
try:
|
| 2064 |
+
price = plan_cfg.get("price", 0)
|
| 2065 |
+
uname = curr_user["username"] if curr_user else "Unknown"
|
| 2066 |
+
conn.execute(
|
| 2067 |
+
"INSERT INTO payments (user_id, username, plan, amount) VALUES (?, ?, ?, ?)",
|
| 2068 |
+
(uid, uname, req.plan, price)
|
| 2069 |
+
)
|
| 2070 |
+
log_debug(f"MONEY: Logged payment of #{price} for {uname} (Plan: {req.plan})")
|
| 2071 |
+
except Exception as pay_err:
|
| 2072 |
+
log_debug(f"MONEY: Payment log skipped (table may not exist): {pay_err}")
|
| 2073 |
+
|
| 2074 |
+
# Perform the actual user update
|
| 2075 |
+
conn.execute(
|
| 2076 |
+
"""UPDATE users SET
|
| 2077 |
+
plan = ?, scrape_limit = ?, max_accounts = ?,
|
| 2078 |
+
max_daily_campaigns = ?, max_daily_keywords = ?,
|
| 2079 |
+
is_approved = ?, plan_activated_at = ?, plan_expires_at = ?,
|
| 2080 |
+
max_templates = ?
|
| 2081 |
+
WHERE id = ?""",
|
| 2082 |
+
(req.plan, s_limit, m_acc, m_camp, m_key, req.is_approved, activated_at, expires_at, m_tmpl, uid)
|
| 2083 |
+
)
|
| 2084 |
+
if hasattr(conn, "commit"): conn.commit()
|
| 2085 |
+
log_debug(f"ADMIN: Updated user {uid} to plan '{req.plan}', approved={req.is_approved}")
|
| 2086 |
+
return {"status": "success", "activated_at": activated_at, "expires_at": expires_at}
|
| 2087 |
|
| 2088 |
+
except Exception as e:
|
| 2089 |
+
log_debug(f"ERROR in update_user_vitals: {e}")
|
| 2090 |
+
import traceback
|
| 2091 |
+
traceback.print_exc()
|
| 2092 |
+
raise HTTPException(status_code=500, detail=f"Failed to update user: {str(e)}")
|
| 2093 |
+
finally:
|
| 2094 |
+
if hasattr(conn, "close"): conn.close()
|
| 2095 |
|
| 2096 |
@app.get("/api/admin/monetization/coupons")
|
| 2097 |
async def get_coupons_admin(admin_id: str = Depends(get_current_admin)):
|