Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
import requests
|
| 3 |
-
from datetime import datetime, timezone
|
| 4 |
-
from dateutil import parser
|
| 5 |
import os
|
| 6 |
|
| 7 |
app = FastAPI()
|
|
@@ -9,55 +7,52 @@ app = FastAPI()
|
|
| 9 |
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 10 |
SUPABASE_API_KEY = os.getenv("SUPABASE_API_KEY")
|
| 11 |
|
| 12 |
-
@app.get("/")
|
| 13 |
-
def root():
|
| 14 |
-
return {"message": "Plan reset API is running."}
|
| 15 |
-
|
| 16 |
@app.get("/reset-plans")
|
| 17 |
def reset_expired_plans():
|
| 18 |
-
if not SUPABASE_URL or not SUPABASE_API_KEY:
|
| 19 |
-
return {"status": "error", "detail": "Missing Supabase config."}
|
| 20 |
-
|
| 21 |
headers = {
|
| 22 |
"apikey": SUPABASE_API_KEY,
|
| 23 |
"Authorization": f"Bearer {SUPABASE_API_KEY}",
|
| 24 |
"Content-Type": "application/json"
|
| 25 |
}
|
| 26 |
|
| 27 |
-
# Fetch users
|
| 28 |
-
|
| 29 |
-
f"{SUPABASE_URL}/rest/v1/users?plan=in.(basic,premium)&select=id,
|
| 30 |
headers=headers
|
| 31 |
)
|
| 32 |
|
| 33 |
-
if
|
| 34 |
-
return {"status": "error", "detail":
|
| 35 |
|
| 36 |
-
users =
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
expired_user_ids = []
|
| 40 |
for user in users:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
return {
|
| 60 |
"status": "success",
|
| 61 |
-
"
|
| 62 |
-
"
|
| 63 |
}
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
import requests
|
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
app = FastAPI()
|
|
|
|
| 7 |
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 8 |
SUPABASE_API_KEY = os.getenv("SUPABASE_API_KEY")
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
@app.get("/reset-plans")
|
| 11 |
def reset_expired_plans():
|
|
|
|
|
|
|
|
|
|
| 12 |
headers = {
|
| 13 |
"apikey": SUPABASE_API_KEY,
|
| 14 |
"Authorization": f"Bearer {SUPABASE_API_KEY}",
|
| 15 |
"Content-Type": "application/json"
|
| 16 |
}
|
| 17 |
|
| 18 |
+
# Fetch users on paid plans
|
| 19 |
+
r = requests.get(
|
| 20 |
+
f"{SUPABASE_URL}/rest/v1/users?plan=in.(basic,premium)&select=id,days_remaining",
|
| 21 |
headers=headers
|
| 22 |
)
|
| 23 |
|
| 24 |
+
if r.status_code != 200:
|
| 25 |
+
return {"status": "error", "detail": r.text}
|
| 26 |
|
| 27 |
+
users = r.json()
|
| 28 |
+
reset_count = 0
|
| 29 |
+
updated_count = 0
|
| 30 |
|
|
|
|
| 31 |
for user in users:
|
| 32 |
+
uid = user["id"]
|
| 33 |
+
days = user.get("days_remaining", 0)
|
| 34 |
+
|
| 35 |
+
if days <= 1:
|
| 36 |
+
# Reset to free
|
| 37 |
+
res = requests.patch(
|
| 38 |
+
f"{SUPABASE_URL}/rest/v1/users?id=eq.{uid}",
|
| 39 |
+
headers=headers,
|
| 40 |
+
json={"plan": "free", "days_remaining": 0}
|
| 41 |
+
)
|
| 42 |
+
if res.status_code == 204:
|
| 43 |
+
reset_count += 1
|
| 44 |
+
else:
|
| 45 |
+
# Subtract one day
|
| 46 |
+
res = requests.patch(
|
| 47 |
+
f"{SUPABASE_URL}/rest/v1/users?id=eq.{uid}",
|
| 48 |
+
headers=headers,
|
| 49 |
+
json={"days_remaining": days - 1}
|
| 50 |
+
)
|
| 51 |
+
if res.status_code == 204:
|
| 52 |
+
updated_count += 1
|
| 53 |
|
| 54 |
return {
|
| 55 |
"status": "success",
|
| 56 |
+
"reset_to_free": reset_count,
|
| 57 |
+
"decremented_days": updated_count
|
| 58 |
}
|