test: add service tests and fix architecture violations
- Add comprehensive unit tests for FeatureService (24 tests) - Add comprehensive unit tests for UsageService (11 tests) - Fix API-002/API-003 architecture violations in feature/usage APIs - Move database queries from API layer to service layer - Create UsageService for usage and limits management - Create custom exceptions (FeatureNotFoundError, TierNotFoundError) - Fix ValidationException usage in content_pages.py - Refactor vendor features API to use proper response models - All 35 new tests passing 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
293
app/api/v1/vendor/usage.py
vendored
293
app/api/v1/vendor/usage.py
vendored
@@ -12,16 +12,12 @@ import logging
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_vendor_api
|
||||
from app.core.database import get_db
|
||||
from app.services.subscription_service import subscription_service
|
||||
from models.database.product import Product
|
||||
from models.database.subscription import SubscriptionTier
|
||||
from app.services.usage_service import usage_service
|
||||
from models.database.user import User
|
||||
from models.database.vendor import VendorUser
|
||||
|
||||
router = APIRouter(prefix="/usage")
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -106,181 +102,44 @@ def get_usage(
|
||||
"""
|
||||
vendor_id = current_user.token_vendor_id
|
||||
|
||||
# Get subscription
|
||||
subscription = subscription_service.get_or_create_subscription(db, vendor_id)
|
||||
|
||||
# Get current tier
|
||||
tier = subscription.tier_obj
|
||||
if not tier:
|
||||
tier = (
|
||||
db.query(SubscriptionTier)
|
||||
.filter(SubscriptionTier.code == subscription.tier)
|
||||
.first()
|
||||
)
|
||||
|
||||
# Calculate usage metrics
|
||||
usage_metrics = []
|
||||
|
||||
# Orders this period
|
||||
orders_current = subscription.orders_this_period or 0
|
||||
orders_limit = subscription.orders_limit
|
||||
orders_unlimited = orders_limit is None or orders_limit < 0
|
||||
orders_percentage = 0 if orders_unlimited else (orders_current / orders_limit * 100 if orders_limit > 0 else 100)
|
||||
|
||||
usage_metrics.append(
|
||||
UsageMetric(
|
||||
name="orders",
|
||||
current=orders_current,
|
||||
limit=None if orders_unlimited else orders_limit,
|
||||
percentage=orders_percentage,
|
||||
is_unlimited=orders_unlimited,
|
||||
is_at_limit=not orders_unlimited and orders_current >= orders_limit,
|
||||
is_approaching_limit=not orders_unlimited and orders_percentage >= 80,
|
||||
)
|
||||
)
|
||||
|
||||
# Products
|
||||
products_count = (
|
||||
db.query(func.count(Product.id))
|
||||
.filter(Product.vendor_id == vendor_id)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
products_limit = subscription.products_limit
|
||||
products_unlimited = products_limit is None or products_limit < 0
|
||||
products_percentage = 0 if products_unlimited else (products_count / products_limit * 100 if products_limit > 0 else 100)
|
||||
|
||||
usage_metrics.append(
|
||||
UsageMetric(
|
||||
name="products",
|
||||
current=products_count,
|
||||
limit=None if products_unlimited else products_limit,
|
||||
percentage=products_percentage,
|
||||
is_unlimited=products_unlimited,
|
||||
is_at_limit=not products_unlimited and products_count >= products_limit,
|
||||
is_approaching_limit=not products_unlimited and products_percentage >= 80,
|
||||
)
|
||||
)
|
||||
|
||||
# Team members
|
||||
team_count = (
|
||||
db.query(func.count(VendorUser.id))
|
||||
.filter(VendorUser.vendor_id == vendor_id, VendorUser.is_active == True) # noqa: E712
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
team_limit = subscription.team_members_limit
|
||||
team_unlimited = team_limit is None or team_limit < 0
|
||||
team_percentage = 0 if team_unlimited else (team_count / team_limit * 100 if team_limit > 0 else 100)
|
||||
|
||||
usage_metrics.append(
|
||||
UsageMetric(
|
||||
name="team_members",
|
||||
current=team_count,
|
||||
limit=None if team_unlimited else team_limit,
|
||||
percentage=team_percentage,
|
||||
is_unlimited=team_unlimited,
|
||||
is_at_limit=not team_unlimited and team_count >= team_limit,
|
||||
is_approaching_limit=not team_unlimited and team_percentage >= 80,
|
||||
)
|
||||
)
|
||||
|
||||
# Check for approaching/reached limits
|
||||
has_limits_approaching = any(m.is_approaching_limit for m in usage_metrics)
|
||||
has_limits_reached = any(m.is_at_limit for m in usage_metrics)
|
||||
|
||||
# Get next tier for upgrade
|
||||
all_tiers = (
|
||||
db.query(SubscriptionTier)
|
||||
.filter(SubscriptionTier.is_active == True) # noqa: E712
|
||||
.order_by(SubscriptionTier.display_order)
|
||||
.all()
|
||||
)
|
||||
|
||||
current_tier_order = tier.display_order if tier else 0
|
||||
next_tier = None
|
||||
for t in all_tiers:
|
||||
if t.display_order > current_tier_order:
|
||||
next_tier = t
|
||||
break
|
||||
|
||||
is_highest_tier = next_tier is None
|
||||
|
||||
# Build upgrade info
|
||||
upgrade_tier_info = None
|
||||
upgrade_reasons = []
|
||||
|
||||
if next_tier:
|
||||
# Calculate benefits
|
||||
benefits = []
|
||||
if next_tier.orders_per_month and (not tier or (tier.orders_per_month and next_tier.orders_per_month > tier.orders_per_month)):
|
||||
if next_tier.orders_per_month < 0:
|
||||
benefits.append("Unlimited orders per month")
|
||||
else:
|
||||
benefits.append(f"{next_tier.orders_per_month:,} orders/month")
|
||||
|
||||
if next_tier.products_limit and (not tier or (tier.products_limit and next_tier.products_limit > tier.products_limit)):
|
||||
if next_tier.products_limit < 0:
|
||||
benefits.append("Unlimited products")
|
||||
else:
|
||||
benefits.append(f"{next_tier.products_limit:,} products")
|
||||
|
||||
if next_tier.team_members and (not tier or (tier.team_members and next_tier.team_members > tier.team_members)):
|
||||
if next_tier.team_members < 0:
|
||||
benefits.append("Unlimited team members")
|
||||
else:
|
||||
benefits.append(f"{next_tier.team_members} team members")
|
||||
|
||||
# Add feature benefits
|
||||
current_features = set(tier.features) if tier and tier.features else set()
|
||||
next_features = set(next_tier.features) if next_tier.features else set()
|
||||
new_features = next_features - current_features
|
||||
|
||||
feature_names = {
|
||||
"analytics_dashboard": "Advanced Analytics",
|
||||
"api_access": "API Access",
|
||||
"automation_rules": "Automation Rules",
|
||||
"team_roles": "Team Roles & Permissions",
|
||||
"custom_domain": "Custom Domain",
|
||||
"webhooks": "Webhooks",
|
||||
"accounting_export": "Accounting Export",
|
||||
}
|
||||
for feature in list(new_features)[:3]: # Show top 3
|
||||
if feature in feature_names:
|
||||
benefits.append(feature_names[feature])
|
||||
|
||||
current_price = tier.price_monthly_cents if tier else 0
|
||||
upgrade_tier_info = UpgradeTierInfo(
|
||||
code=next_tier.code,
|
||||
name=next_tier.name,
|
||||
price_monthly_cents=next_tier.price_monthly_cents,
|
||||
price_increase_cents=next_tier.price_monthly_cents - current_price,
|
||||
benefits=benefits,
|
||||
)
|
||||
|
||||
# Build upgrade reasons
|
||||
if has_limits_reached:
|
||||
for m in usage_metrics:
|
||||
if m.is_at_limit:
|
||||
upgrade_reasons.append(f"You've reached your {m.name.replace('_', ' ')} limit")
|
||||
elif has_limits_approaching:
|
||||
for m in usage_metrics:
|
||||
if m.is_approaching_limit:
|
||||
upgrade_reasons.append(f"You're approaching your {m.name.replace('_', ' ')} limit ({int(m.percentage)}%)")
|
||||
# Get usage data from service
|
||||
usage_data = usage_service.get_vendor_usage(db, vendor_id)
|
||||
|
||||
# Convert to response
|
||||
return UsageResponse(
|
||||
tier=TierInfo(
|
||||
code=tier.code if tier else subscription.tier,
|
||||
name=tier.name if tier else subscription.tier.title(),
|
||||
price_monthly_cents=tier.price_monthly_cents if tier else 0,
|
||||
is_highest_tier=is_highest_tier,
|
||||
code=usage_data.tier.code,
|
||||
name=usage_data.tier.name,
|
||||
price_monthly_cents=usage_data.tier.price_monthly_cents,
|
||||
is_highest_tier=usage_data.tier.is_highest_tier,
|
||||
),
|
||||
usage=usage_metrics,
|
||||
has_limits_approaching=has_limits_approaching,
|
||||
has_limits_reached=has_limits_reached,
|
||||
upgrade_available=not is_highest_tier,
|
||||
upgrade_tier=upgrade_tier_info,
|
||||
upgrade_reasons=upgrade_reasons,
|
||||
usage=[
|
||||
UsageMetric(
|
||||
name=m.name,
|
||||
current=m.current,
|
||||
limit=m.limit,
|
||||
percentage=m.percentage,
|
||||
is_unlimited=m.is_unlimited,
|
||||
is_at_limit=m.is_at_limit,
|
||||
is_approaching_limit=m.is_approaching_limit,
|
||||
)
|
||||
for m in usage_data.usage
|
||||
],
|
||||
has_limits_approaching=usage_data.has_limits_approaching,
|
||||
has_limits_reached=usage_data.has_limits_reached,
|
||||
upgrade_available=usage_data.upgrade_available,
|
||||
upgrade_tier=(
|
||||
UpgradeTierInfo(
|
||||
code=usage_data.upgrade_tier.code,
|
||||
name=usage_data.upgrade_tier.name,
|
||||
price_monthly_cents=usage_data.upgrade_tier.price_monthly_cents,
|
||||
price_increase_cents=usage_data.upgrade_tier.price_increase_cents,
|
||||
benefits=usage_data.upgrade_tier.benefits,
|
||||
)
|
||||
if usage_data.upgrade_tier
|
||||
else None
|
||||
),
|
||||
upgrade_reasons=usage_data.upgrade_reasons,
|
||||
)
|
||||
|
||||
|
||||
@@ -303,78 +162,16 @@ def check_limit(
|
||||
"""
|
||||
vendor_id = current_user.token_vendor_id
|
||||
|
||||
if limit_type == "orders":
|
||||
can_proceed, message = subscription_service.can_create_order(db, vendor_id)
|
||||
subscription = subscription_service.get_subscription(db, vendor_id)
|
||||
current = subscription.orders_this_period if subscription else 0
|
||||
limit = subscription.orders_limit if subscription else 0
|
||||
|
||||
elif limit_type == "products":
|
||||
can_proceed, message = subscription_service.can_add_product(db, vendor_id)
|
||||
subscription = subscription_service.get_subscription(db, vendor_id)
|
||||
current = (
|
||||
db.query(func.count(Product.id))
|
||||
.filter(Product.vendor_id == vendor_id)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
limit = subscription.products_limit if subscription else 0
|
||||
|
||||
elif limit_type == "team_members":
|
||||
can_proceed, message = subscription_service.can_add_team_member(db, vendor_id)
|
||||
subscription = subscription_service.get_subscription(db, vendor_id)
|
||||
current = (
|
||||
db.query(func.count(VendorUser.id))
|
||||
.filter(VendorUser.vendor_id == vendor_id, VendorUser.is_active == True) # noqa: E712
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
limit = subscription.team_members_limit if subscription else 0
|
||||
|
||||
else:
|
||||
return LimitCheckResponse(
|
||||
limit_type=limit_type,
|
||||
can_proceed=True,
|
||||
current=0,
|
||||
limit=None,
|
||||
percentage=0,
|
||||
message=f"Unknown limit type: {limit_type}",
|
||||
)
|
||||
|
||||
# Calculate percentage
|
||||
is_unlimited = limit is None or limit < 0
|
||||
percentage = 0 if is_unlimited else (current / limit * 100 if limit > 0 else 100)
|
||||
|
||||
# Get upgrade info if at limit
|
||||
upgrade_tier_code = None
|
||||
upgrade_tier_name = None
|
||||
|
||||
if not can_proceed:
|
||||
subscription = subscription_service.get_subscription(db, vendor_id)
|
||||
current_tier = subscription.tier_obj if subscription else None
|
||||
|
||||
if current_tier:
|
||||
next_tier = (
|
||||
db.query(SubscriptionTier)
|
||||
.filter(
|
||||
SubscriptionTier.is_active == True, # noqa: E712
|
||||
SubscriptionTier.display_order > current_tier.display_order,
|
||||
)
|
||||
.order_by(SubscriptionTier.display_order)
|
||||
.first()
|
||||
)
|
||||
|
||||
if next_tier:
|
||||
upgrade_tier_code = next_tier.code
|
||||
upgrade_tier_name = next_tier.name
|
||||
# Check limit using service
|
||||
check_data = usage_service.check_limit(db, vendor_id, limit_type)
|
||||
|
||||
return LimitCheckResponse(
|
||||
limit_type=limit_type,
|
||||
can_proceed=can_proceed,
|
||||
current=current,
|
||||
limit=None if is_unlimited else limit,
|
||||
percentage=percentage,
|
||||
message=message,
|
||||
upgrade_tier_code=upgrade_tier_code,
|
||||
upgrade_tier_name=upgrade_tier_name,
|
||||
limit_type=check_data.limit_type,
|
||||
can_proceed=check_data.can_proceed,
|
||||
current=check_data.current,
|
||||
limit=check_data.limit,
|
||||
percentage=check_data.percentage,
|
||||
message=check_data.message,
|
||||
upgrade_tier_code=check_data.upgrade_tier_code,
|
||||
upgrade_tier_name=check_data.upgrade_tier_name,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user