Fix subscription limits; seed Free/Good/Better/Best default plans
Agent-Logs-Url: https://github.com/christianlouis/pop_puller_to_gmail/sessions/98c1f90b-0287-4908-a0be-ad066c453cc5 Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
@@ -8,7 +8,12 @@ from sqlalchemy import select, desc
|
||||
from app.core.database import get_db
|
||||
from app.core.deps import get_current_active_user
|
||||
from app.core.security import encrypt_credential
|
||||
from app.models.database_models import User, MailAccount, AccountStatus
|
||||
from app.models.database_models import (
|
||||
User,
|
||||
MailAccount,
|
||||
AccountStatus,
|
||||
SubscriptionPlan,
|
||||
)
|
||||
from app.models.schemas import (
|
||||
MailAccountCreate,
|
||||
MailAccountResponse,
|
||||
@@ -34,26 +39,41 @@ async def create_mail_account(
|
||||
):
|
||||
"""Create a new mail account"""
|
||||
|
||||
# Check subscription limits
|
||||
result = await db.execute(
|
||||
select(MailAccount).where(MailAccount.user_id == current_user.id)
|
||||
)
|
||||
existing_accounts = result.scalars().all()
|
||||
|
||||
tier_limits = {
|
||||
"free": settings.TIER_FREE_MAX_ACCOUNTS,
|
||||
"basic": settings.TIER_BASIC_MAX_ACCOUNTS,
|
||||
"pro": settings.TIER_PRO_MAX_ACCOUNTS,
|
||||
"enterprise": settings.TIER_ENTERPRISE_MAX_ACCOUNTS,
|
||||
}
|
||||
|
||||
max_accounts = tier_limits.get(current_user.subscription_tier.value, 1)
|
||||
|
||||
if len(existing_accounts) >= max_accounts:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_402_PAYMENT_REQUIRED,
|
||||
detail="Account limit reached. Upgrade your subscription to add more accounts.",
|
||||
# Superusers are not subject to subscription limits
|
||||
if not current_user.is_superuser:
|
||||
# Count existing accounts for this user
|
||||
result = await db.execute(
|
||||
select(MailAccount).where(MailAccount.user_id == current_user.id)
|
||||
)
|
||||
existing_accounts = result.scalars().all()
|
||||
|
||||
# Try to look up the limit from the active SubscriptionPlan in the DB first
|
||||
# so that admin-managed plan limits take effect immediately.
|
||||
plan_result = await db.execute(
|
||||
select(SubscriptionPlan).where(
|
||||
SubscriptionPlan.tier == current_user.subscription_tier,
|
||||
SubscriptionPlan.is_active.is_(True),
|
||||
)
|
||||
)
|
||||
plan = plan_result.scalar_one_or_none()
|
||||
|
||||
if plan is not None:
|
||||
max_accounts = plan.max_mail_accounts
|
||||
else:
|
||||
# Fall back to env-var / config values when no plan row exists
|
||||
tier_limits = {
|
||||
"free": settings.TIER_FREE_MAX_ACCOUNTS,
|
||||
"basic": settings.TIER_BASIC_MAX_ACCOUNTS,
|
||||
"pro": settings.TIER_PRO_MAX_ACCOUNTS,
|
||||
"enterprise": settings.TIER_ENTERPRISE_MAX_ACCOUNTS,
|
||||
}
|
||||
max_accounts = tier_limits.get(current_user.subscription_tier.value, 1)
|
||||
|
||||
if len(existing_accounts) >= max_accounts:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_402_PAYMENT_REQUIRED,
|
||||
detail="Account limit reached. Upgrade your subscription to add more accounts.",
|
||||
)
|
||||
|
||||
# Encrypt password
|
||||
encrypted_password = encrypt_credential(account_in.password)
|
||||
|
||||
@@ -25,10 +25,22 @@ async def list_subscription_plans(db: AsyncSession = Depends(get_db)):
|
||||
@router.get("/current")
|
||||
async def get_current_subscription(
|
||||
current_user: User = Depends(get_current_active_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
"""Get current user's subscription details"""
|
||||
"""Get current user's subscription details including plan limits"""
|
||||
plan_result = await db.execute(
|
||||
select(SubscriptionPlan).where(
|
||||
SubscriptionPlan.tier == current_user.subscription_tier,
|
||||
SubscriptionPlan.is_active.is_(True),
|
||||
)
|
||||
)
|
||||
plan = plan_result.scalar_one_or_none()
|
||||
|
||||
return {
|
||||
"tier": current_user.subscription_tier,
|
||||
"status": current_user.subscription_status,
|
||||
"expires_at": current_user.subscription_expires_at,
|
||||
"max_mail_accounts": plan.max_mail_accounts if plan else None,
|
||||
"max_emails_per_day": plan.max_emails_per_day if plan else None,
|
||||
"check_interval_minutes": plan.check_interval_minutes if plan else None,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user