Spaces:
Running

axeronis commited on
Commit
b5e13c8
·
1 Parent(s): e59ff70

Fix: Sync SubscriptionPlan Enum with DB (Uppercase) and fix Admin API casing

Browse files
backend/app/api/admin.py CHANGED
@@ -318,11 +318,19 @@ async def update_user(
318
  if body.subscription_plan and user.tenant_id:
319
  tenant = await session.get(Tenant, user.tenant_id)
320
  if tenant:
321
- # PostgreSQL enum requires lowercase values
322
- tenant.subscription_plan = body.subscription_plan.lower()
 
 
 
 
 
 
 
 
323
  tenant.subscription_status = "active" # Activate when admin changes plan
324
  session.add(tenant)
325
- logger.info(f"Admin updated tenant {tenant.id} plan to {body.subscription_plan}")
326
 
327
  await session.commit()
328
  await session.refresh(user)
 
318
  if body.subscription_plan and user.tenant_id:
319
  tenant = await session.get(Tenant, user.tenant_id)
320
  if tenant:
321
+ # Match Postgres Enum (UPPERCASE)
322
+ try:
323
+ # Robustly handle input case by converting to upper
324
+ plan_enum = SubscriptionPlan(body.subscription_plan.upper())
325
+ tenant.subscription_plan = plan_enum
326
+ except ValueError:
327
+ # If invalid value, log error but maybe don't crash? Or let it crash to alert caller?
328
+ # Better to validate via Pydantic, but here we just ensure casing.
329
+ tenant.subscription_plan = body.subscription_plan.upper() # Fallback for direct assignment if enum validation fails locally but Pydantic allowed str
330
+
331
  tenant.subscription_status = "active" # Activate when admin changes plan
332
  session.add(tenant)
333
+ logger.info(f"Admin updated tenant {tenant.id} plan to {tenant.subscription_plan}")
334
 
335
  await session.commit()
336
  await session.refresh(user)
backend/app/models/tenant.py CHANGED
@@ -7,11 +7,15 @@ from sqlalchemy import JSON, Enum as SAEnum
7
 
8
 
9
  class SubscriptionPlan(str, Enum):
10
- """Subscription plan types."""
11
- TRIAL = "trial" # 7 days free trial, 50 dialogs
12
- START = "start" # $99/month - 500 conversations, 1 channel
13
- GROWTH = "growth" # $199/month - 1500 conversations, 3 channels
14
- ENTERPRISE = "enterprise" # $500/month - unlimited everything
 
 
 
 
15
 
16
 
17
  class SubscriptionStatus(str, Enum):
@@ -34,10 +38,7 @@ class Tenant(SQLModel, table=True):
34
  name: str = Field(index=True)
35
 
36
  # Subscription
37
- subscription_plan: SubscriptionPlan = Field(
38
- default=SubscriptionPlan.TRIAL,
39
- sa_column=Column(SAEnum(SubscriptionPlan, values_callable=lambda obj: [e.value for e in obj]))
40
- )
41
  subscription_status: str = Field(default="trial") # trial, active, expired, cancelled
42
  subscription_start: datetime = Field(default_factory=datetime.utcnow)
43
  subscription_end: Optional[datetime] = None
 
7
 
8
 
9
  class SubscriptionPlan(str, Enum):
10
+ """Subscription plan types.
11
+ Must match Postgres Enum: 'FREE', 'START', 'GROWTH', 'SCALE', 'UNLIMITED', 'ENTERPRISE'
12
+ """
13
+ FREE = "FREE"
14
+ START = "START"
15
+ GROWTH = "GROWTH"
16
+ SCALE = "SCALE"
17
+ UNLIMITED = "UNLIMITED"
18
+ ENTERPRISE = "ENTERPRISE"
19
 
20
 
21
  class SubscriptionStatus(str, Enum):
 
38
  name: str = Field(index=True)
39
 
40
  # Subscription
41
+ subscription_plan: SubscriptionPlan = Field(default=SubscriptionPlan.FREE)
 
 
 
42
  subscription_status: str = Field(default="trial") # trial, active, expired, cancelled
43
  subscription_start: datetime = Field(default_factory=datetime.utcnow)
44
  subscription_end: Optional[datetime] = None