Files
life-echo/api/app/features/plan/service.py
Sully 53e0065e3e refactor(api): TOML 配置 SSOT、统一错误契约、Auth/事务加固与可观测性 (#33)
配置 SSOT(TOML + .env)
统一错误契约
Auth 与事务边界
Redis / Celery 可靠性:业务 Redis(DB/0)与 Celery broker/backend(DB/1)显式拆分;连接池、sync client
可观测性(OpenTelemetry + LGTM)
2026-05-22 13:44:50 +08:00

56 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Plan service — 套餐定义与查询。"""
from app.features.plan.catalog import (
AVAILABLE_PLANS,
ENABLE_TEST_PLAN,
TEST_PLAN,
get_plan_by_type,
get_plans_for_api,
)
from app.features.plan.schemas import (
CurrentPlanResponse,
PlanResponse,
PlanUsageResponse,
)
from app.features.quota.service import QuotaService
from app.features.user.models import User
__all__ = [
"AVAILABLE_PLANS",
"ENABLE_TEST_PLAN",
"TEST_PLAN",
"PlanService",
"get_plan_by_type",
"get_plans_for_api",
]
class PlanService:
def __init__(self, quota_service: QuotaService):
self._quota = quota_service
def get_plans_for_api(self) -> list[PlanResponse]:
"""对外套餐列表(供 payment 等 feature 通过注入使用,不直接 import plan.service"""
return get_plans_for_api()
async def get_current_plan_response(self, user: User) -> CurrentPlanResponse:
plan = get_plan_by_type(user.subscription_type)
segment_count, chapter_count = await self._quota.get_usage(user.id)
usage = PlanUsageResponse(
conversations=segment_count,
chapters=chapter_count,
max_conversations=plan.max_conversations,
max_chapters=plan.max_chapters,
)
expires_at = None
if user.subscription_expires_at:
expires_at = user.subscription_expires_at.isoformat()
return CurrentPlanResponse(
plan_id=plan.id,
plan_name=plan.display_name,
subscription_type=user.subscription_type,
expires_at=expires_at,
features=plan.features,
usage=usage,
)