CBV01 commited on
Commit
017347c
·
1 Parent(s): 2a848f1

Final Fixes move plan configs to dynamic cache and restore prices

Browse files
Files changed (2) hide show
  1. backend/database.py +4 -3
  2. backend/main.py +39 -30
backend/database.py CHANGED
@@ -427,11 +427,12 @@ def init_db():
427
  pass
428
 
429
  # Update seeding to match user's explicit request: Free(1), Basic(1), Standard(2), Premium(2), Unlimited(999)
 
430
  default_plans = [
431
  ("free", "Free Starter", 0, 20, 1, 5, 50, 1, 0, json.dumps(["20 Daily Campaigns", "1 Template", "5 Scrape Keywords", "Basic Support"])),
432
- ("basic", "Growth Basic", 3000, 50, 2, 10, 100, 1, 1, json.dumps(["50 Daily Campaigns", "1 Template", "10 Scrape Keywords", "Priority Support"])),
433
- ("standard", "Pro Standard", 7500, 150, 5, 25, 250, 2, 1, json.dumps(["150 Daily Campaigns", "2 Templates", "25 Scrape Keywords", "24/7 Support", "Ad-Free Experience"])),
434
- ("premium", "Enterprise Premium", 15000, 300, 10, 50, 500, 2, 1, json.dumps(["300 Daily Campaigns", "2 Templates", "Unlimited Keywords", "Dedicated Manager"])),
435
  ("unlimited", "Owner Unlimited", 0, 999999, 99, 999999, 1000, 999, 1, json.dumps(["Absolute Control", "999 Templates", "Admin Privilege"]))
436
  ]
437
  for p in default_plans:
 
427
  pass
428
 
429
  # Update seeding to match user's explicit request: Free(1), Basic(1), Standard(2), Premium(2), Unlimited(999)
430
+ # Prices: Basic: 2000, Standard: 3500, Premium: 5000
431
  default_plans = [
432
  ("free", "Free Starter", 0, 20, 1, 5, 50, 1, 0, json.dumps(["20 Daily Campaigns", "1 Template", "5 Scrape Keywords", "Basic Support"])),
433
+ ("basic", "Growth Basic", 2000, 50, 2, 10, 100, 1, 1, json.dumps(["50 Daily Campaigns", "1 Template", "10 Scrape Keywords", "Priority Support"])),
434
+ ("standard", "Pro Standard", 3500, 150, 5, 25, 250, 2, 1, json.dumps(["150 Daily Campaigns", "2 Templates", "25 Scrape Keywords", "24/7 Support", "Ad-Free Experience"])),
435
+ ("premium", "Enterprise Premium", 5000, 300, 10, 50, 500, 2, 1, json.dumps(["300 Daily Campaigns", "2 Templates", "Unlimited Keywords", "Dedicated Manager"])),
436
  ("unlimited", "Owner Unlimited", 0, 999999, 99, 999999, 1000, 999, 1, json.dumps(["Absolute Control", "999 Templates", "Admin Privilege"]))
437
  ]
438
  for p in default_plans:
backend/main.py CHANGED
@@ -20,6 +20,7 @@ from typing import Optional, List, Dict, Any, Union, cast
20
  import re
21
  import ast
22
  import traceback
 
23
  from contextlib import asynccontextmanager
24
  import httpx # pyre-ignore[21]
25
  try:
@@ -53,6 +54,7 @@ def log_debug(msg: str):
53
  LOG_BUFFER.append(formatted)
54
 
55
  # --- Tiered Plan Configurations (Dynamic from DB) ---
 
56
  def get_plan_configs() -> Dict[str, Dict[str, Any]]:
57
  configs: Dict[str, Dict[str, Any]] = {
58
  "free": {"max_daily_campaigns": 20, "max_accounts": 1, "max_daily_keywords": 5, "scrape_limit": 50, "max_templates": 1, "has_premium_access": False},
@@ -82,7 +84,9 @@ def get_plan_configs() -> Dict[str, Dict[str, Any]]:
82
 
83
  return configs
84
 
85
- PLAN_CONFIGS: Dict[str, Dict[str, Any]] = get_plan_configs() # Initial load
 
 
86
 
87
  def check_and_reset_daily_limits(conn, user_id, user_row):
88
  """Resets daily counts if the day has changed."""
@@ -742,7 +746,7 @@ async def send_code(req: PhoneLoginRequest, user_id: str = Depends(get_current_u
742
  if hasattr(conn, "close"): conn.close()
743
 
744
  plan = (user["plan"] if user else "free") or "free"
745
- plan_cfg = PLAN_CONFIGS.get(plan, PLAN_CONFIGS["free"])
746
 
747
  # Use user-specific override or fallback to plan default
748
  max_acc = user["max_accounts"] if user and user["max_accounts"] is not None else plan_cfg["max_accounts"]
@@ -1051,9 +1055,9 @@ async def dump_session_string(phone_number: str, user_id: str = Depends(get_curr
1051
  return {"phone_number": phone_number, "session_string": row[0]}
1052
 
1053
 
1054
- # ─────────────────────────────────────────────────────────────────────────────
1055
  # HYBRID SCRAPER HELPERS (Layer 2 & 3: web directories)
1056
- # ─────────────────────────────────────────────────────────────────────────────
1057
 
1058
  SCRAPER_HEADERS = {
1059
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
@@ -1213,7 +1217,7 @@ async def _scrape_telegram_search(base_query: str, rows: list, queue: Optional[a
1213
  for n in ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]:
1214
  search_queries.append(f"{base_query}{n}")
1215
 
1216
- print(f"SCRAPER[telegram] {len(search_queries)} variations × {len(rows)} accounts = "
1217
  f"up to {len(search_queries) * len(rows) * 100} raw results")
1218
 
1219
  for row in rows:
@@ -1463,7 +1467,7 @@ async def scrape_keyword_stream(
1463
  check_and_reset_daily_limits(conn, user_id, user_row)
1464
 
1465
  plan = user_row["plan"] or "free"
1466
- plan_cfg = PLAN_CONFIGS.get(plan, PLAN_CONFIGS["free"])
1467
 
1468
  # Check Premium Access
1469
  if not plan_cfg["has_premium_access"]:
@@ -1587,7 +1591,7 @@ async def scrape_keyword(
1587
  ):
1588
  conn = get_db_connection()
1589
  if user_id == "admin_virtual_id":
1590
- plan_cfg = PLAN_CONFIGS["unlimited"]
1591
  max_keywords = 9999
1592
  current_keywords = 0
1593
  scrape_limit = 9999
@@ -1604,7 +1608,7 @@ async def scrape_keyword(
1604
 
1605
  check_and_reset_daily_limits(conn, user_id, user_row)
1606
  plan = (user_row["plan"] or "free")
1607
- plan_cfg = PLAN_CONFIGS.get(plan, PLAN_CONFIGS["free"])
1608
 
1609
  max_keywords = int(user_row["max_daily_keywords"] or plan_cfg["max_daily_keywords"])
1610
  current_keywords = int(user_row["daily_keyword_count"] or 0)
@@ -1646,7 +1650,7 @@ async def scrape_keyword(
1646
  telegram_task, lyzem_task, return_exceptions=True
1647
  )
1648
 
1649
- # Merge — Telegram results keyed by numeric ID, directory results keyed by @username
1650
  unique_groups: Dict[str, Dict[str, Any]] = {}
1651
 
1652
  if isinstance(telegram_results, dict):
@@ -1683,7 +1687,7 @@ async def scrape_keyword(
1683
  if hasattr(conn2, "commit"): conn2.commit()
1684
  if hasattr(conn2, "close"): conn2.close()
1685
 
1686
- print(f"SCRAPER: Done — {len(final_list)} unique results for '{base_query}' "
1687
  f"(TG:{len(telegram_results) if isinstance(telegram_results, dict) else 0} "
1688
  f"Lyzem:{len(lyzem_results) if isinstance(lyzem_results, list) else 0} "
1689
  f"Spider:{len(spider_results)})")
@@ -1731,7 +1735,7 @@ async def bulk_join_stream(
1731
  token: str = ""
1732
  ):
1733
  """
1734
- SSE Streaming Bulk Join – emits real-time progress events.
1735
  Uses token query param instead of Authorization header (EventSource limitation).
1736
  group_ids: JSON-encoded list of group IDs.
1737
  """
@@ -1817,7 +1821,7 @@ async def bulk_join_stream(
1817
 
1818
  if isinstance(e, FloodWaitError):
1819
  wait_seconds: int = cast(int, getattr(e, 'seconds', 0))
1820
- yield f"data: {_json.dumps({'type': 'progress', 'idx': idx, 'total': total, 'msg': f'🚨 RATE LIMIT: Telegram says wait {wait_seconds}s. Use the manual Copy button if you are in a hurry.'})}\n\n"
1821
  # We stop the automated process for FloodWait to protect the account
1822
  break
1823
 
@@ -1923,7 +1927,7 @@ async def get_saved_members(user_id: str = Depends(get_current_user_id)):
1923
  conn = get_db_connection()
1924
  user_row = conn.execute("SELECT plan FROM users WHERE id = ?", (user_id,)).fetchone()
1925
  plan = user_row["plan"] if user_row else "free"
1926
- plan_cfg = PLAN_CONFIGS.get(plan, PLAN_CONFIGS["free"])
1927
 
1928
  if not plan_cfg["has_premium_access"] and user_id != "admin_virtual_id":
1929
  if hasattr(conn, "close"): conn.close()
@@ -1981,7 +1985,7 @@ class UserVitalsUpdateRequest(BaseModel):
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,
@@ -1998,7 +2002,7 @@ async def get_monetization_users(admin_id: str = Depends(get_current_admin)):
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"],
@@ -2016,13 +2020,15 @@ async def get_monetization_users(admin_id: str = Depends(get_current_admin)):
2016
  @app.post("/api/admin/monetization/users/{uid}/vitals")
2017
  async def update_user_vitals(uid: str, req: UserVitalsUpdateRequest, admin_id: str = Depends(get_current_admin)):
2018
  conn = get_db_connection()
2019
- plan_cfg = PLAN_CONFIGS.get(req.plan, PLAN_CONFIGS["free"])
 
2020
 
2021
  # Use provided values or defaults from plan
2022
  s_limit = req.scrape_limit if req.scrape_limit is not None else plan_cfg["scrape_limit"]
2023
  m_acc = req.max_accounts if req.max_accounts is not None else plan_cfg["max_accounts"]
2024
  m_camp = req.max_daily_campaigns if req.max_daily_campaigns is not None else plan_cfg["max_daily_campaigns"]
2025
  m_key = req.max_daily_keywords if req.max_daily_keywords is not None else plan_cfg["max_daily_keywords"]
 
2026
 
2027
  # Handle Subscription Dates and Payment Logging
2028
  activated_at = None
@@ -2051,11 +2057,15 @@ async def update_user_vitals(uid: str, req: UserVitalsUpdateRequest, admin_id: s
2051
  """UPDATE users SET
2052
  plan = ?, scrape_limit = ?, max_accounts = ?,
2053
  max_daily_campaigns = ?, max_daily_keywords = ?,
2054
- is_approved = ?, plan_activated_at = ?, plan_expires_at = ?
 
2055
  WHERE id = ?""",
2056
- (req.plan, s_limit, m_acc, m_camp, m_key, req.is_approved, activated_at, expires_at, uid)
2057
  )
2058
  if hasattr(conn, "commit"): conn.commit()
 
 
 
2059
  if hasattr(conn, "close"): conn.close()
2060
  return {"status": "success", "activated_at": activated_at, "expires_at": expires_at}
2061
 
@@ -2105,9 +2115,9 @@ async def delete_coupon_admin(code: str, admin_id: str = Depends(get_current_adm
2105
  @app.get("/api/monetization/status")
2106
  async def get_user_status(user_id: str = Depends(get_current_user_id)):
2107
  if user_id == "admin_virtual_id":
2108
- cfg = PLAN_CONFIGS["unlimited"]
2109
  # Fetch 'unlimited' config
2110
- cfg = PLAN_CONFIGS.get("unlimited", PLAN_CONFIGS["free"])
2111
  return {
2112
  "status": "success",
2113
  "plan": "unlimited",
@@ -2141,7 +2151,7 @@ async def get_user_status(user_id: str = Depends(get_current_user_id)):
2141
  if hasattr(conn, "close"): conn.close()
2142
 
2143
  # Tier mapping
2144
- p_cfg = PLAN_CONFIGS.get(row["plan"] or "free", PLAN_CONFIGS["free"])
2145
 
2146
  return {
2147
  "status": "success",
@@ -2234,7 +2244,7 @@ async def verify_paystack(req: PaystackVerifyRequest, user_id: str = Depends(get
2234
  if response.status_code == 200 and res_data.get("status") and res_data["data"]["status"] == "success":
2235
  # Payment confirmed! Upgrade user.
2236
  plan_key = req.plan_key
2237
- plan_cfg = PLAN_CONFIGS.get(plan_key, PLAN_CONFIGS["basic"])
2238
 
2239
  # Subscription timing
2240
  now = datetime.now()
@@ -2290,7 +2300,7 @@ async def paystack_webhook(request: Request):
2290
  user = conn.execute("SELECT id FROM users WHERE username = ? OR id = ?", (cust_email, data.get("metadata", {}).get("user_id"))).fetchone()
2291
  if user:
2292
  u_id = user["id"]
2293
- plan_cfg = PLAN_CONFIGS.get(plan_key, PLAN_CONFIGS["basic"])
2294
  now = datetime.now()
2295
  act_date = now.strftime('%Y-%m-%dT%H:%M')
2296
  exp_date = (now + timedelta(days=30)).strftime('%Y-%m-%dT%H:%M')
@@ -2347,7 +2357,7 @@ async def get_saved_groups(user_id: str = Depends(get_current_user_id)):
2347
  # Premium check
2348
  user_row = conn.execute("SELECT plan FROM users WHERE id = ?", (user_id,)).fetchone()
2349
  plan = user_row["plan"] if user_row else "free"
2350
- plan_cfg = PLAN_CONFIGS.get(plan, PLAN_CONFIGS["free"])
2351
 
2352
  if not plan_cfg["has_premium_access"] and user_id != "admin_virtual_id":
2353
  if hasattr(conn, "close"): conn.close()
@@ -2479,7 +2489,7 @@ async def extract_members(req: ExtractRequest, user_id: str = Depends(get_curren
2479
  raise HTTPException(status_code=404, detail="User not found")
2480
 
2481
  plan = user_row["plan"] or "free"
2482
- plan_cfg = PLAN_CONFIGS.get(plan, PLAN_CONFIGS["free"])
2483
  if not plan_cfg.get("has_premium_access"):
2484
  raise HTTPException(status_code=403, detail="Member Extraction is a Premium feature. Upgrade to Standard or Premium to unlock.")
2485
 
@@ -2566,7 +2576,7 @@ async def create_template(req: TemplateRequest, user_id: str = Depends(get_curre
2566
  # Check limits
2567
  u_row = conn.execute("SELECT plan FROM users WHERE id = ?", (user_id,)).fetchone()
2568
  plan = u_row["plan"] if u_row else "free"
2569
- plan_cfg = PLAN_CONFIGS.get(plan, PLAN_CONFIGS["free"])
2570
  max_templates = plan_cfg.get("max_templates", 1)
2571
 
2572
  current_count = conn.execute("SELECT COUNT(*) FROM templates WHERE user_id = ?", (user_id,)).fetchone()[0]
@@ -2675,7 +2685,7 @@ async def create_campaign(req: CampaignRequest, user_id: str = Depends(get_curre
2675
  check_and_reset_daily_limits(conn, user_id, user_row)
2676
 
2677
  plan = user_row["plan"] or "free"
2678
- plan_cfg = PLAN_CONFIGS.get(plan, PLAN_CONFIGS["free"])
2679
 
2680
  max_daily = int(user_row["max_daily_campaigns"] or plan_cfg["max_daily_campaigns"])
2681
  current_daily = int(user_row["daily_campaign_count"] or 0)
@@ -3132,8 +3142,7 @@ async def admin_update_plan(req: PlanUpdateRequest, admin_id: str = Depends(get_
3132
  if hasattr(conn, "close"): conn.close()
3133
 
3134
  # Reload global configs
3135
- global PLAN_CONFIGS
3136
- PLAN_CONFIGS = get_plan_configs()
3137
  return {"status": "success"}
3138
 
3139
  @app.get("/")
@@ -3233,7 +3242,7 @@ async def task_poller():
3233
  if u_row:
3234
  u_plan = u_row["plan"] or "free"
3235
  u_count = u_row["daily_campaign_count"] or 0
3236
- u_cfg = PLAN_CONFIGS.get(u_plan, PLAN_CONFIGS["free"])
3237
  u_max = u_row["max_daily_campaigns"] or u_cfg["max_daily_campaigns"]
3238
 
3239
  if u_plan != "unlimited" and u_count >= u_max:
 
20
  import re
21
  import ast
22
  import traceback
23
+ import functools
24
  from contextlib import asynccontextmanager
25
  import httpx # pyre-ignore[21]
26
  try:
 
54
  LOG_BUFFER.append(formatted)
55
 
56
  # --- Tiered Plan Configurations (Dynamic from DB) ---
57
+ @functools.lru_cache(maxsize=1)
58
  def get_plan_configs() -> Dict[str, Dict[str, Any]]:
59
  configs: Dict[str, Dict[str, Any]] = {
60
  "free": {"max_daily_campaigns": 20, "max_accounts": 1, "max_daily_keywords": 5, "scrape_limit": 50, "max_templates": 1, "has_premium_access": False},
 
84
 
85
  return configs
86
 
87
+ def get_current_plan_configs() -> Dict[str, Dict[str, Any]]:
88
+ # Bridge for cached lookup
89
+ return get_plan_configs()
90
 
91
  def check_and_reset_daily_limits(conn, user_id, user_row):
92
  """Resets daily counts if the day has changed."""
 
746
  if hasattr(conn, "close"): conn.close()
747
 
748
  plan = (user["plan"] if user else "free") or "free"
749
+ plan_cfg = get_plan_configs().get(plan, get_plan_configs()["free"])
750
 
751
  # Use user-specific override or fallback to plan default
752
  max_acc = user["max_accounts"] if user and user["max_accounts"] is not None else plan_cfg["max_accounts"]
 
1055
  return {"phone_number": phone_number, "session_string": row[0]}
1056
 
1057
 
1058
+ # ─────────────────────────────────────────────────────────────────────────────
1059
  # HYBRID SCRAPER HELPERS (Layer 2 & 3: web directories)
1060
+ # ─────────────────────────────────────────────────────────────────────────────
1061
 
1062
  SCRAPER_HEADERS = {
1063
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
 
1217
  for n in ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]:
1218
  search_queries.append(f"{base_query}{n}")
1219
 
1220
+ print(f"SCRAPER[telegram] {len(search_queries)} variations × {len(rows)} accounts = "
1221
  f"up to {len(search_queries) * len(rows) * 100} raw results")
1222
 
1223
  for row in rows:
 
1467
  check_and_reset_daily_limits(conn, user_id, user_row)
1468
 
1469
  plan = user_row["plan"] or "free"
1470
+ plan_cfg = get_plan_configs().get(plan, get_plan_configs()["free"])
1471
 
1472
  # Check Premium Access
1473
  if not plan_cfg["has_premium_access"]:
 
1591
  ):
1592
  conn = get_db_connection()
1593
  if user_id == "admin_virtual_id":
1594
+ plan_cfg = get_plan_configs()["unlimited"]
1595
  max_keywords = 9999
1596
  current_keywords = 0
1597
  scrape_limit = 9999
 
1608
 
1609
  check_and_reset_daily_limits(conn, user_id, user_row)
1610
  plan = (user_row["plan"] or "free")
1611
+ plan_cfg = get_plan_configs().get(plan, get_plan_configs()["free"])
1612
 
1613
  max_keywords = int(user_row["max_daily_keywords"] or plan_cfg["max_daily_keywords"])
1614
  current_keywords = int(user_row["daily_keyword_count"] or 0)
 
1650
  telegram_task, lyzem_task, return_exceptions=True
1651
  )
1652
 
1653
+ # Merge — Telegram results keyed by numeric ID, directory results keyed by @username
1654
  unique_groups: Dict[str, Dict[str, Any]] = {}
1655
 
1656
  if isinstance(telegram_results, dict):
 
1687
  if hasattr(conn2, "commit"): conn2.commit()
1688
  if hasattr(conn2, "close"): conn2.close()
1689
 
1690
+ print(f"SCRAPER: Done — {len(final_list)} unique results for '{base_query}' "
1691
  f"(TG:{len(telegram_results) if isinstance(telegram_results, dict) else 0} "
1692
  f"Lyzem:{len(lyzem_results) if isinstance(lyzem_results, list) else 0} "
1693
  f"Spider:{len(spider_results)})")
 
1735
  token: str = ""
1736
  ):
1737
  """
1738
+ SSE Streaming Bulk Join – emits real-time progress events.
1739
  Uses token query param instead of Authorization header (EventSource limitation).
1740
  group_ids: JSON-encoded list of group IDs.
1741
  """
 
1821
 
1822
  if isinstance(e, FloodWaitError):
1823
  wait_seconds: int = cast(int, getattr(e, 'seconds', 0))
1824
+ yield f"data: {_json.dumps({'type': 'progress', 'idx': idx, 'total': total, 'msg': f'🚨 RATE LIMIT: Telegram says wait {wait_seconds}s. Use the manual Copy button if you are in a hurry.'})}\n\n"
1825
  # We stop the automated process for FloodWait to protect the account
1826
  break
1827
 
 
1927
  conn = get_db_connection()
1928
  user_row = conn.execute("SELECT plan FROM users WHERE id = ?", (user_id,)).fetchone()
1929
  plan = user_row["plan"] if user_row else "free"
1930
+ plan_cfg = get_plan_configs().get(plan, get_plan_configs()["free"])
1931
 
1932
  if not plan_cfg["has_premium_access"] and user_id != "admin_virtual_id":
1933
  if hasattr(conn, "close"): conn.close()
 
1985
  @app.get("/api/admin/monetization/users")
1986
  async def get_monetization_users(admin_id: str = Depends(get_current_admin)):
1987
  conn = get_db_connection()
1988
+ # Join with plans to get default limits dynamically
1989
  query = """
1990
  SELECT u.*, p.max_templates as plan_max_templates,
1991
  p.max_daily_campaigns as plan_max_campaigns,
 
2002
  id=r["id"],
2003
  username=r["username"],
2004
  plan=r["plan"],
2005
+ # Dynamically pulling from plan if user fields are the system default (1 or NULL)
2006
  max_templates=r["plan_max_templates"] if r["max_templates"] == 1 or r["max_templates"] is None else r["max_templates"],
2007
  max_daily_campaigns=r["plan_max_campaigns"] if r["max_daily_campaigns"] is None else r["max_daily_campaigns"],
2008
  max_daily_keywords=r["plan_max_keywords"] if r["max_daily_keywords"] is None else r["max_daily_keywords"],
 
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
+ plan_configs = get_current_plan_configs()
2024
+ plan_cfg = get_plan_configs().get(req.plan, get_plan_configs()["free"])
2025
 
2026
  # Use provided values or defaults from plan
2027
  s_limit = req.scrape_limit if req.scrape_limit is not None else plan_cfg["scrape_limit"]
2028
  m_acc = req.max_accounts if req.max_accounts is not None else plan_cfg["max_accounts"]
2029
  m_camp = req.max_daily_campaigns if req.max_daily_campaigns is not None else plan_cfg["max_daily_campaigns"]
2030
  m_key = req.max_daily_keywords if req.max_daily_keywords is not None else plan_cfg["max_daily_keywords"]
2031
+ m_tmpl = plan_cfg["max_templates"]
2032
 
2033
  # Handle Subscription Dates and Payment Logging
2034
  activated_at = None
 
2057
  """UPDATE users SET
2058
  plan = ?, scrape_limit = ?, max_accounts = ?,
2059
  max_daily_campaigns = ?, max_daily_keywords = ?,
2060
+ is_approved = ?, plan_activated_at = ?, plan_expires_at = ?,
2061
+ max_templates = ?
2062
  WHERE id = ?""",
2063
+ (req.plan, s_limit, m_acc, m_camp, m_key, req.is_approved, activated_at, expires_at, m_tmpl, uid)
2064
  )
2065
  if hasattr(conn, "commit"): conn.commit()
2066
+ # Force refresh plan configs global if needed
2067
+ get_plan_configs.cache_clear()
2068
+
2069
  if hasattr(conn, "close"): conn.close()
2070
  return {"status": "success", "activated_at": activated_at, "expires_at": expires_at}
2071
 
 
2115
  @app.get("/api/monetization/status")
2116
  async def get_user_status(user_id: str = Depends(get_current_user_id)):
2117
  if user_id == "admin_virtual_id":
2118
+ cfg = get_plan_configs()["unlimited"]
2119
  # Fetch 'unlimited' config
2120
+ cfg = get_plan_configs().get("unlimited", get_plan_configs()["free"])
2121
  return {
2122
  "status": "success",
2123
  "plan": "unlimited",
 
2151
  if hasattr(conn, "close"): conn.close()
2152
 
2153
  # Tier mapping
2154
+ p_cfg = get_plan_configs().get(row["plan"] or "free", get_plan_configs()["free"])
2155
 
2156
  return {
2157
  "status": "success",
 
2244
  if response.status_code == 200 and res_data.get("status") and res_data["data"]["status"] == "success":
2245
  # Payment confirmed! Upgrade user.
2246
  plan_key = req.plan_key
2247
+ plan_cfg = get_plan_configs().get(plan_key, get_plan_configs()["basic"])
2248
 
2249
  # Subscription timing
2250
  now = datetime.now()
 
2300
  user = conn.execute("SELECT id FROM users WHERE username = ? OR id = ?", (cust_email, data.get("metadata", {}).get("user_id"))).fetchone()
2301
  if user:
2302
  u_id = user["id"]
2303
+ plan_cfg = get_plan_configs().get(plan_key, get_plan_configs()["basic"])
2304
  now = datetime.now()
2305
  act_date = now.strftime('%Y-%m-%dT%H:%M')
2306
  exp_date = (now + timedelta(days=30)).strftime('%Y-%m-%dT%H:%M')
 
2357
  # Premium check
2358
  user_row = conn.execute("SELECT plan FROM users WHERE id = ?", (user_id,)).fetchone()
2359
  plan = user_row["plan"] if user_row else "free"
2360
+ plan_cfg = get_plan_configs().get(plan, get_plan_configs()["free"])
2361
 
2362
  if not plan_cfg["has_premium_access"] and user_id != "admin_virtual_id":
2363
  if hasattr(conn, "close"): conn.close()
 
2489
  raise HTTPException(status_code=404, detail="User not found")
2490
 
2491
  plan = user_row["plan"] or "free"
2492
+ plan_cfg = get_plan_configs().get(plan, get_plan_configs()["free"])
2493
  if not plan_cfg.get("has_premium_access"):
2494
  raise HTTPException(status_code=403, detail="Member Extraction is a Premium feature. Upgrade to Standard or Premium to unlock.")
2495
 
 
2576
  # Check limits
2577
  u_row = conn.execute("SELECT plan FROM users WHERE id = ?", (user_id,)).fetchone()
2578
  plan = u_row["plan"] if u_row else "free"
2579
+ plan_cfg = get_plan_configs().get(plan, get_plan_configs()["free"])
2580
  max_templates = plan_cfg.get("max_templates", 1)
2581
 
2582
  current_count = conn.execute("SELECT COUNT(*) FROM templates WHERE user_id = ?", (user_id,)).fetchone()[0]
 
2685
  check_and_reset_daily_limits(conn, user_id, user_row)
2686
 
2687
  plan = user_row["plan"] or "free"
2688
+ plan_cfg = get_plan_configs().get(plan, get_plan_configs()["free"])
2689
 
2690
  max_daily = int(user_row["max_daily_campaigns"] or plan_cfg["max_daily_campaigns"])
2691
  current_daily = int(user_row["daily_campaign_count"] or 0)
 
3142
  if hasattr(conn, "close"): conn.close()
3143
 
3144
  # Reload global configs
3145
+ get_plan_configs.cache_clear()
 
3146
  return {"status": "success"}
3147
 
3148
  @app.get("/")
 
3242
  if u_row:
3243
  u_plan = u_row["plan"] or "free"
3244
  u_count = u_row["daily_campaign_count"] or 0
3245
+ u_cfg = get_plan_configs().get(u_plan, get_plan_configs()["free"])
3246
  u_max = u_row["max_daily_campaigns"] or u_cfg["max_daily_campaigns"]
3247
 
3248
  if u_plan != "unlimited" and u_count >= u_max: