Spaces:
Sleeping
Sleeping
File size: 2,467 Bytes
299f191 09b7a43 973b009 299f191 09b7a43 a019c27 09b7a43 299f191 09b7a43 a019c27 299f191 a019c27 299f191 94e4145 a019c27 4b660e0 94e4145 a019c27 e592664 a019c27 94e4145 a019c27 09b7a43 a019c27 4b660e0 a019c27 94e4145 a019c27 299f191 a019c27 94e4145 a019c27 299f191 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | from fastapi import FastAPI
import requests
import os
app = FastAPI()
SUPABASE_URL = os.getenv("SUPABASE_URL", "").strip()
SUPABASE_API_KEY = os.getenv("SUPABASE_API_KEY", "").strip()
@app.get("/reset-plans")
def reset_expired_plans():
print("📣 Starting plan reset task...")
print(f"✅ Using Supabase URL: {SUPABASE_URL}")
headers = {
"apikey": SUPABASE_API_KEY,
"Authorization": f"Bearer {SUPABASE_API_KEY}",
"Content-Type": "application/json"
}
url = f"{SUPABASE_URL}/rest/v1/users?select=id,plan,days_remaining"
print("📡 Fetching users with basic or premium plans...")
try:
r = requests.get(url, headers=headers)
if r.status_code != 200:
print(f"❌ Failed to fetch users: {r.text}")
return {"status": "error", "detail": r.text}
except Exception as e:
print(f"🚨 Exception while fetching users: {e}")
return {"status": "error", "exception": str(e)}
users = r.json()
print(f"🔍 Found {len(users)} users")
reset_count = 0
updated_count = 0
for user in users:
uid = user["id"]
plan = user.get("plan", "free")
days = user.get("days_remaining", 0)
print(f"👤 User {uid} - Plan: {plan}, Days Remaining: {days}")
if days <= 0 and plan in ("basic", "premium"):
print(f"➡️ Resetting user {uid} to free")
res = requests.patch(
f"{SUPABASE_URL}/rest/v1/users?id=eq.{uid}",
headers=headers,
json={"plan": "free", "days_remaining": 0}
)
if res.status_code == 204:
reset_count += 1
else:
print(f"❗ Failed to reset user {uid}: {res.text}")
elif days > 0 and plan in ("basic", "premium"):
print(f"📉 Decrementing days for user {uid} to {days - 1}")
res = requests.patch(
f"{SUPABASE_URL}/rest/v1/users?id=eq.{uid}",
headers=headers,
json={"days_remaining": days - 1}
)
if res.status_code == 204:
updated_count += 1
else:
print(f"❗ Failed to decrement days for user {uid}: {res.text}")
else:
print(f"✅ No action needed for user {uid}")
return {
"status": "success",
"reset_to_free": reset_count,
"decremented_days": updated_count
}
|