Spaces:
Paused
Paused
CBV01 commited on
Commit ·
e5ee4a5
1
Parent(s): 2616a08
Fix Admin Search Auditing for Streams and Sync Plan Template Limits
Browse files- backend/main.py +36 -9
backend/main.py
CHANGED
|
@@ -1478,10 +1478,25 @@ async def scrape_keyword_stream(
|
|
| 1478 |
if hasattr(conn, "close"): conn.close()
|
| 1479 |
raise HTTPException(status_code=403, detail="Daily usage limit reached for scraping. Reset at Midnight UTC.")
|
| 1480 |
|
| 1481 |
-
# Increment keyword use
|
| 1482 |
if plan != "unlimited":
|
| 1483 |
conn.execute("UPDATE users SET daily_keyword_count = daily_keyword_count + 1 WHERE id = ?", (user_id,))
|
| 1484 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1485 |
|
| 1486 |
if plan == "unlimited" or user_id == "admin_virtual_id":
|
| 1487 |
final_limit: int = 999999999
|
|
@@ -1966,24 +1981,36 @@ class UserVitalsUpdateRequest(BaseModel):
|
|
| 1966 |
@app.get("/api/admin/monetization/users")
|
| 1967 |
async def get_monetization_users(admin_id: str = Depends(get_current_admin)):
|
| 1968 |
conn = get_db_connection()
|
| 1969 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1970 |
if hasattr(conn, "close"): conn.close()
|
|
|
|
| 1971 |
return {"users": [dict(
|
| 1972 |
id=r["id"],
|
| 1973 |
username=r["username"],
|
| 1974 |
plan=r["plan"],
|
| 1975 |
-
|
| 1976 |
-
|
| 1977 |
-
max_daily_campaigns=r["max_daily_campaigns"],
|
|
|
|
|
|
|
|
|
|
| 1978 |
daily_campaign_count=r["daily_campaign_count"],
|
| 1979 |
-
max_daily_keywords=r["max_daily_keywords"],
|
| 1980 |
daily_keyword_count=r["daily_keyword_count"],
|
| 1981 |
is_approved=r["is_approved"],
|
| 1982 |
total_scraped=r["total_scraped"],
|
| 1983 |
payment_proof=r["payment_proof"],
|
| 1984 |
plan_activated_at=r["plan_activated_at"],
|
| 1985 |
-
plan_expires_at=r["plan_expires_at"]
|
| 1986 |
-
max_templates=r["max_templates"]
|
| 1987 |
) for r in rows]}
|
| 1988 |
|
| 1989 |
@app.post("/api/admin/monetization/users/{uid}/vitals")
|
|
|
|
| 1478 |
if hasattr(conn, "close"): conn.close()
|
| 1479 |
raise HTTPException(status_code=403, detail="Daily usage limit reached for scraping. Reset at Midnight UTC.")
|
| 1480 |
|
| 1481 |
+
# Increment keyword use and Log Search Intelligence
|
| 1482 |
if plan != "unlimited":
|
| 1483 |
conn.execute("UPDATE users SET daily_keyword_count = daily_keyword_count + 1 WHERE id = ?", (user_id,))
|
| 1484 |
+
|
| 1485 |
+
# Unified global logging for Admin
|
| 1486 |
+
try:
|
| 1487 |
+
uname = "Admin"
|
| 1488 |
+
if user_id != "admin_virtual_id":
|
| 1489 |
+
user_row = conn.execute("SELECT username FROM users WHERE id = ?", (user_id,)).fetchone()
|
| 1490 |
+
if user_row: uname = user_row["username"]
|
| 1491 |
+
|
| 1492 |
+
conn.execute(
|
| 1493 |
+
"INSERT INTO keyword_logs (user_id, username, keyword) VALUES (?, ?, ?)",
|
| 1494 |
+
(user_id, uname, query.strip())
|
| 1495 |
+
)
|
| 1496 |
+
except Exception as e:
|
| 1497 |
+
print(f"FAILED TO LOG KEYWORD: {e}")
|
| 1498 |
+
|
| 1499 |
+
if hasattr(conn, "commit"): conn.commit()
|
| 1500 |
|
| 1501 |
if plan == "unlimited" or user_id == "admin_virtual_id":
|
| 1502 |
final_limit: int = 999999999
|
|
|
|
| 1981 |
@app.get("/api/admin/monetization/users")
|
| 1982 |
async def get_monetization_users(admin_id: str = Depends(get_current_admin)):
|
| 1983 |
conn = get_db_connection()
|
| 1984 |
+
# Join with plans to get default limits if user record is stale or has defaults
|
| 1985 |
+
query = """
|
| 1986 |
+
SELECT u.*, p.max_templates as plan_max_templates,
|
| 1987 |
+
p.max_daily_campaigns as plan_max_campaigns,
|
| 1988 |
+
p.max_daily_keywords as plan_max_keywords,
|
| 1989 |
+
p.scrape_limit as plan_scrape_limit,
|
| 1990 |
+
p.max_accounts as plan_max_accounts
|
| 1991 |
+
FROM users u
|
| 1992 |
+
LEFT JOIN plans p ON u.plan = p.key
|
| 1993 |
+
"""
|
| 1994 |
+
rows = conn.execute(query).fetchall()
|
| 1995 |
if hasattr(conn, "close"): conn.close()
|
| 1996 |
+
|
| 1997 |
return {"users": [dict(
|
| 1998 |
id=r["id"],
|
| 1999 |
username=r["username"],
|
| 2000 |
plan=r["plan"],
|
| 2001 |
+
# Favor plan defaults (synchronized) unless a specific override exists in users table
|
| 2002 |
+
max_templates=r["plan_max_templates"] if r["max_templates"] == 1 or r["max_templates"] is None else r["max_templates"],
|
| 2003 |
+
max_daily_campaigns=r["plan_max_campaigns"] if r["max_daily_campaigns"] is None else r["max_daily_campaigns"],
|
| 2004 |
+
max_daily_keywords=r["plan_max_keywords"] if r["max_daily_keywords"] is None else r["max_daily_keywords"],
|
| 2005 |
+
scrape_limit=r["plan_scrape_limit"] if r["scrape_limit"] is None else r["scrape_limit"],
|
| 2006 |
+
max_accounts=r["plan_max_accounts"] if r["max_accounts"] is None else r["max_accounts"],
|
| 2007 |
daily_campaign_count=r["daily_campaign_count"],
|
|
|
|
| 2008 |
daily_keyword_count=r["daily_keyword_count"],
|
| 2009 |
is_approved=r["is_approved"],
|
| 2010 |
total_scraped=r["total_scraped"],
|
| 2011 |
payment_proof=r["payment_proof"],
|
| 2012 |
plan_activated_at=r["plan_activated_at"],
|
| 2013 |
+
plan_expires_at=r["plan_expires_at"]
|
|
|
|
| 2014 |
) for r in rows]}
|
| 2015 |
|
| 2016 |
@app.post("/api/admin/monetization/users/{uid}/vitals")
|