Spaces:
Running
Running
Update subscriptions.py
Browse files- subscriptions.py +38 -0
subscriptions.py
CHANGED
|
@@ -3,6 +3,16 @@ import psycopg
|
|
| 3 |
from psycopg.rows import dict_row
|
| 4 |
from supabase import create_client, Client
|
| 5 |
import asyncio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
conn = None
|
| 8 |
conn_lock = asyncio.Lock()
|
|
@@ -47,6 +57,34 @@ async def fetch_subscription(jwt: str):
|
|
| 47 |
user = auth_res.user
|
| 48 |
email = user.email
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
async with conn_lock:
|
| 51 |
connection = await get_conn()
|
| 52 |
|
|
|
|
| 3 |
from psycopg.rows import dict_row
|
| 4 |
from supabase import create_client, Client
|
| 5 |
import asyncio
|
| 6 |
+
import json
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
ADMINS_PATH = Path("assets/admins.json")
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
ADMIN_EMAIL_MAP = json.loads(ADMINS_PATH.read_text())
|
| 13 |
+
except Exception as e:
|
| 14 |
+
print(f"Failed to load admins.json: {e}")
|
| 15 |
+
ADMIN_EMAIL_MAP = {}
|
| 16 |
|
| 17 |
conn = None
|
| 18 |
conn_lock = asyncio.Lock()
|
|
|
|
| 57 |
user = auth_res.user
|
| 58 |
email = user.email
|
| 59 |
|
| 60 |
+
auth_res = supabase.auth.get_user(jwt)
|
| 61 |
+
if auth_res.user is None:
|
| 62 |
+
return {"error": "Invalid or expired session"}
|
| 63 |
+
|
| 64 |
+
user = auth_res.user
|
| 65 |
+
email = user.email.lower()
|
| 66 |
+
|
| 67 |
+
if email in ADMIN_EMAIL_MAP:
|
| 68 |
+
admin = ADMIN_EMAIL_MAP[email]
|
| 69 |
+
print(f"[ADMIN OVERRIDE] {email} → forcing plan '{admin['plan_key']}'")
|
| 70 |
+
|
| 71 |
+
subscription_obj = {
|
| 72 |
+
"subscription_id": admin.get("subscription_id", "admin-override"),
|
| 73 |
+
"status": admin.get("status", "active"),
|
| 74 |
+
"current_period_end": None,
|
| 75 |
+
"price_id": None,
|
| 76 |
+
"product_name": admin.get("product_name"),
|
| 77 |
+
"nickname": admin.get("nickname"),
|
| 78 |
+
"plan_key": admin.get("plan_key"),
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
return {
|
| 82 |
+
"email": email,
|
| 83 |
+
"signed_up": user.created_at.isoformat(),
|
| 84 |
+
"subscription": [subscription_obj],
|
| 85 |
+
"plan_key": admin["plan_key"],
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
async with conn_lock:
|
| 89 |
connection = await get_conn()
|
| 90 |
|