Spaces:
Running
Running
axeronis commited on
Commit ·
b36e51b
1
Parent(s): 5b058f8
feat: auto-set subscription_end when plan changes (trial=7d, paid=30d)
Browse files- backend/app/api/admin.py +11 -4
backend/app/api/admin.py
CHANGED
|
@@ -197,14 +197,21 @@ async def admin_update_subscription(
|
|
| 197 |
if request.plan:
|
| 198 |
# Validate plan exists in SubscriptionPlan enum or just strict string check
|
| 199 |
tenant.subscription_plan = request.plan
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
if request.status:
|
| 202 |
tenant.subscription_status = request.status
|
| 203 |
|
| 204 |
-
# If activating manually,
|
| 205 |
-
if request.status == 'active' and tenant.
|
| 206 |
-
|
| 207 |
-
|
| 208 |
|
| 209 |
if request.set_expiration:
|
| 210 |
tenant.subscription_end = request.set_expiration
|
|
|
|
| 197 |
if request.plan:
|
| 198 |
# Validate plan exists in SubscriptionPlan enum or just strict string check
|
| 199 |
tenant.subscription_plan = request.plan
|
| 200 |
+
# Auto-set subscription_end based on plan
|
| 201 |
+
from datetime import timedelta
|
| 202 |
+
if request.plan.lower() == 'trial':
|
| 203 |
+
tenant.subscription_end = datetime.utcnow() + timedelta(days=7)
|
| 204 |
+
else:
|
| 205 |
+
tenant.subscription_end = datetime.utcnow() + timedelta(days=30)
|
| 206 |
+
tenant.subscription_status = 'active' # Activate when plan changes
|
| 207 |
|
| 208 |
if request.status:
|
| 209 |
tenant.subscription_status = request.status
|
| 210 |
|
| 211 |
+
# If activating manually, extend by 30 days if expired
|
| 212 |
+
if request.status == 'active' and not tenant.subscription_end or (tenant.subscription_end and tenant.subscription_end < datetime.utcnow()):
|
| 213 |
+
from datetime import timedelta
|
| 214 |
+
tenant.subscription_end = datetime.utcnow() + timedelta(days=30)
|
| 215 |
|
| 216 |
if request.set_expiration:
|
| 217 |
tenant.subscription_end = request.set_expiration
|