Spaces:
Sleeping
Sleeping
axeronis commited on
Commit ·
95db790
1
Parent(s): 96e30b6
feat: trial system - 7 day trial, new limits (50/500/1500/unlimited), sidebar countdown banner
Browse files
backend/app/api/auth.py
CHANGED
|
@@ -163,13 +163,14 @@ async def register(
|
|
| 163 |
referral_code=body.referral_code
|
| 164 |
)
|
| 165 |
|
| 166 |
-
# Create new Tenant (Company)
|
| 167 |
new_tenant = Tenant(
|
| 168 |
name=body.company_name,
|
| 169 |
business_name=body.company_name,
|
| 170 |
tokens_balance=10000, # Welcome bonus
|
| 171 |
partner_ref_id=referring_partner_id, # Link to referring partner (or None)
|
| 172 |
-
is_active=True
|
|
|
|
| 173 |
)
|
| 174 |
session.add(new_tenant)
|
| 175 |
await session.flush() # Get tenant ID
|
|
@@ -430,12 +431,13 @@ async def google_auth(
|
|
| 430 |
# User doesn't exist - register
|
| 431 |
company_name = request.company_name or f"{full_name}'s Company"
|
| 432 |
|
| 433 |
-
# Create new Tenant (Company)
|
| 434 |
new_tenant = Tenant(
|
| 435 |
name=company_name,
|
| 436 |
business_name=company_name,
|
| 437 |
tokens_balance=10000, # Welcome bonus
|
| 438 |
-
is_active=True
|
|
|
|
| 439 |
)
|
| 440 |
session.add(new_tenant)
|
| 441 |
await session.flush() # Get tenant ID
|
|
|
|
| 163 |
referral_code=body.referral_code
|
| 164 |
)
|
| 165 |
|
| 166 |
+
# Create new Tenant (Company) with 7-day trial
|
| 167 |
new_tenant = Tenant(
|
| 168 |
name=body.company_name,
|
| 169 |
business_name=body.company_name,
|
| 170 |
tokens_balance=10000, # Welcome bonus
|
| 171 |
partner_ref_id=referring_partner_id, # Link to referring partner (or None)
|
| 172 |
+
is_active=True,
|
| 173 |
+
subscription_end=datetime.utcnow() + timedelta(days=7) # Trial expires in 7 days
|
| 174 |
)
|
| 175 |
session.add(new_tenant)
|
| 176 |
await session.flush() # Get tenant ID
|
|
|
|
| 431 |
# User doesn't exist - register
|
| 432 |
company_name = request.company_name or f"{full_name}'s Company"
|
| 433 |
|
| 434 |
+
# Create new Tenant (Company) with 7-day trial
|
| 435 |
new_tenant = Tenant(
|
| 436 |
name=company_name,
|
| 437 |
business_name=company_name,
|
| 438 |
tokens_balance=10000, # Welcome bonus
|
| 439 |
+
is_active=True,
|
| 440 |
+
subscription_end=datetime.utcnow() + timedelta(days=7) # Trial expires in 7 days
|
| 441 |
)
|
| 442 |
session.add(new_tenant)
|
| 443 |
await session.flush() # Get tenant ID
|
backend/app/models/tenant.py
CHANGED
|
@@ -8,10 +8,10 @@ from sqlalchemy import JSON
|
|
| 8 |
|
| 9 |
class SubscriptionPlan(str, Enum):
|
| 10 |
"""Subscription plan types."""
|
| 11 |
-
TRIAL = "trial" # 7 days free trial
|
| 12 |
-
START = "start" # $99/month -
|
| 13 |
-
GROWTH = "growth" # $199/month -
|
| 14 |
-
|
| 15 |
|
| 16 |
|
| 17 |
class SubscriptionStatus(str, Enum):
|
|
@@ -34,7 +34,7 @@ class Tenant(SQLModel, table=True):
|
|
| 34 |
name: str = Field(index=True)
|
| 35 |
|
| 36 |
# Subscription
|
| 37 |
-
subscription_plan: SubscriptionPlan = Field(default=SubscriptionPlan.
|
| 38 |
subscription_status: str = Field(default="trial") # trial, active, expired, cancelled
|
| 39 |
subscription_start: datetime = Field(default_factory=datetime.utcnow)
|
| 40 |
subscription_end: Optional[datetime] = None
|
|
|
|
| 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 |
name: str = Field(index=True)
|
| 35 |
|
| 36 |
# Subscription
|
| 37 |
+
subscription_plan: SubscriptionPlan = Field(default=SubscriptionPlan.TRIAL)
|
| 38 |
subscription_status: str = Field(default="trial") # trial, active, expired, cancelled
|
| 39 |
subscription_start: datetime = Field(default_factory=datetime.utcnow)
|
| 40 |
subscription_end: Optional[datetime] = None
|
backend/app/services/subscription_manager.py
CHANGED
|
@@ -25,10 +25,10 @@ class SubscriptionManager:
|
|
| 25 |
|
| 26 |
# Лимиты диалогов по планам
|
| 27 |
PLAN_LIMITS = {
|
| 28 |
-
"trial":
|
| 29 |
-
"start":
|
| 30 |
-
"growth":
|
| 31 |
-
"
|
| 32 |
}
|
| 33 |
|
| 34 |
# Лимиты каналов по планам
|
|
@@ -36,7 +36,7 @@ class SubscriptionManager:
|
|
| 36 |
"trial": 1,
|
| 37 |
"start": 1,
|
| 38 |
"growth": 3,
|
| 39 |
-
"
|
| 40 |
}
|
| 41 |
|
| 42 |
@classmethod
|
|
@@ -132,34 +132,34 @@ class SubscriptionManager:
|
|
| 132 |
"trial": {
|
| 133 |
"name": "Trial",
|
| 134 |
"price": 0,
|
| 135 |
-
"conversations_limit":
|
| 136 |
"channels_limit": 1,
|
| 137 |
"duration_days": 7,
|
| 138 |
-
"features": ["AI-ассистент", "1 канал", "Email поддержка"]
|
| 139 |
},
|
| 140 |
"start": {
|
| 141 |
"name": "Старт",
|
| 142 |
"price": 99,
|
| 143 |
-
"conversations_limit":
|
| 144 |
"channels_limit": 1,
|
| 145 |
"duration_days": 30,
|
| 146 |
-
"features": ["1 Канал (Любой)", "
|
| 147 |
},
|
| 148 |
"growth": {
|
| 149 |
"name": "Рост",
|
| 150 |
"price": 199,
|
| 151 |
-
"conversations_limit":
|
| 152 |
"channels_limit": 3,
|
| 153 |
"duration_days": 30,
|
| 154 |
-
"features": ["3 Канала (TG+WA+LI)", "
|
| 155 |
},
|
| 156 |
-
"
|
| 157 |
-
"name": "
|
| 158 |
"price": 499,
|
| 159 |
"conversations_limit": None,
|
| 160 |
"channels_limit": None,
|
| 161 |
"duration_days": 30,
|
| 162 |
-
"features": ["Безлимит каналов", "
|
| 163 |
}
|
| 164 |
}
|
| 165 |
|
|
|
|
| 25 |
|
| 26 |
# Лимиты диалогов по планам
|
| 27 |
PLAN_LIMITS = {
|
| 28 |
+
"trial": 50, # 50 диалогов за 7 дней
|
| 29 |
+
"start": 500, # 500 диалогов/месяц
|
| 30 |
+
"growth": 1500, # 1500 диалогов/месяц
|
| 31 |
+
"enterprise": None # Безлимит
|
| 32 |
}
|
| 33 |
|
| 34 |
# Лимиты каналов по планам
|
|
|
|
| 36 |
"trial": 1,
|
| 37 |
"start": 1,
|
| 38 |
"growth": 3,
|
| 39 |
+
"enterprise": None # Безлимит
|
| 40 |
}
|
| 41 |
|
| 42 |
@classmethod
|
|
|
|
| 132 |
"trial": {
|
| 133 |
"name": "Trial",
|
| 134 |
"price": 0,
|
| 135 |
+
"conversations_limit": 50,
|
| 136 |
"channels_limit": 1,
|
| 137 |
"duration_days": 7,
|
| 138 |
+
"features": ["AI-ассистент", "1 канал", "50 диалогов", "Email поддержка"]
|
| 139 |
},
|
| 140 |
"start": {
|
| 141 |
"name": "Старт",
|
| 142 |
"price": 99,
|
| 143 |
+
"conversations_limit": 500,
|
| 144 |
"channels_limit": 1,
|
| 145 |
"duration_days": 30,
|
| 146 |
+
"features": ["1 Канал (Любой)", "500 диалогов/мес", "Все AI функции", "CRM Интеграция"]
|
| 147 |
},
|
| 148 |
"growth": {
|
| 149 |
"name": "Рост",
|
| 150 |
"price": 199,
|
| 151 |
+
"conversations_limit": 1500,
|
| 152 |
"channels_limit": 3,
|
| 153 |
"duration_days": 30,
|
| 154 |
+
"features": ["3 Канала (TG+WA+LI)", "1500 диалогов/мес", "Все AI функции", "Расширенная аналитика"]
|
| 155 |
},
|
| 156 |
+
"enterprise": {
|
| 157 |
+
"name": "Enterprise",
|
| 158 |
"price": 499,
|
| 159 |
"conversations_limit": None,
|
| 160 |
"channels_limit": None,
|
| 161 |
"duration_days": 30,
|
| 162 |
+
"features": ["Безлимит каналов", "Безлимит диалогов", "Приоритетная поддержка", "Все AI функции"]
|
| 163 |
}
|
| 164 |
}
|
| 165 |
|
frontend/components/Sidebar.tsx
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
'use client'
|
| 2 |
|
|
|
|
| 3 |
import Link from 'next/link'
|
| 4 |
import { usePathname } from 'next/navigation'
|
| 5 |
import {
|
|
@@ -14,11 +15,15 @@ import {
|
|
| 14 |
Users,
|
| 15 |
Bell,
|
| 16 |
Calendar,
|
| 17 |
-
Shield
|
|
|
|
|
|
|
| 18 |
} from 'lucide-react'
|
| 19 |
import { useAuth } from '@/contexts/AuthContext'
|
| 20 |
import { motion } from 'framer-motion'
|
| 21 |
import { NavItem } from './Sidebar/NavItem'
|
|
|
|
|
|
|
| 22 |
|
| 23 |
// ============================================================================
|
| 24 |
// Animation Variants
|
|
@@ -83,6 +88,27 @@ function getInitials(name?: string | null, email?: string): string {
|
|
| 83 |
export function Sidebar() {
|
| 84 |
const pathname = usePathname()
|
| 85 |
const { user, isLoading } = useAuth()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
// Don't show sidebar on auth pages
|
| 88 |
if (pathname === '/login' || pathname === '/register' || pathname === '/') {
|
|
@@ -103,6 +129,13 @@ export function Sidebar() {
|
|
| 103 |
const visibleIntegrationItems = integrationItems.filter(item => isOwner || !item.ownerOnly)
|
| 104 |
const visibleSettingsItems = settingsItems.filter(item => isOwner || !item.ownerOnly)
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
return (
|
| 107 |
<motion.aside
|
| 108 |
initial="hidden"
|
|
@@ -207,7 +240,72 @@ export function Sidebar() {
|
|
| 207 |
</div>
|
| 208 |
</motion.div>
|
| 209 |
)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
</nav>
|
| 213 |
)}
|
|
|
|
| 1 |
'use client'
|
| 2 |
|
| 3 |
+
import { useState, useEffect } from 'react'
|
| 4 |
import Link from 'next/link'
|
| 5 |
import { usePathname } from 'next/navigation'
|
| 6 |
import {
|
|
|
|
| 15 |
Users,
|
| 16 |
Bell,
|
| 17 |
Calendar,
|
| 18 |
+
Shield,
|
| 19 |
+
Clock,
|
| 20 |
+
AlertTriangle
|
| 21 |
} from 'lucide-react'
|
| 22 |
import { useAuth } from '@/contexts/AuthContext'
|
| 23 |
import { motion } from 'framer-motion'
|
| 24 |
import { NavItem } from './Sidebar/NavItem'
|
| 25 |
+
import { api } from '@/lib/api'
|
| 26 |
+
import { cn } from '@/lib/utils'
|
| 27 |
|
| 28 |
// ============================================================================
|
| 29 |
// Animation Variants
|
|
|
|
| 88 |
export function Sidebar() {
|
| 89 |
const pathname = usePathname()
|
| 90 |
const { user, isLoading } = useAuth()
|
| 91 |
+
const [usageStats, setUsageStats] = useState<{
|
| 92 |
+
plan: string
|
| 93 |
+
conversations_used: number
|
| 94 |
+
conversations_limit: number | null
|
| 95 |
+
days_left: number
|
| 96 |
+
is_expired: boolean
|
| 97 |
+
} | null>(null)
|
| 98 |
+
|
| 99 |
+
// Load usage stats for trial/subscription display
|
| 100 |
+
useEffect(() => {
|
| 101 |
+
const loadUsage = async () => {
|
| 102 |
+
if (!user) return
|
| 103 |
+
try {
|
| 104 |
+
const res = await api.get('/api/v1/tenants/me/usage')
|
| 105 |
+
setUsageStats(res.data.usage)
|
| 106 |
+
} catch (e) {
|
| 107 |
+
console.error('Failed to load usage stats')
|
| 108 |
+
}
|
| 109 |
+
}
|
| 110 |
+
loadUsage()
|
| 111 |
+
}, [user])
|
| 112 |
|
| 113 |
// Don't show sidebar on auth pages
|
| 114 |
if (pathname === '/login' || pathname === '/register' || pathname === '/') {
|
|
|
|
| 129 |
const visibleIntegrationItems = integrationItems.filter(item => isOwner || !item.ownerOnly)
|
| 130 |
const visibleSettingsItems = settingsItems.filter(item => isOwner || !item.ownerOnly)
|
| 131 |
|
| 132 |
+
// Trial banner logic
|
| 133 |
+
const isTrial = usageStats?.plan === 'trial'
|
| 134 |
+
const isExpired = usageStats?.is_expired
|
| 135 |
+
const daysLeft = usageStats?.days_left || 0
|
| 136 |
+
const usedDialogs = usageStats?.conversations_used || 0
|
| 137 |
+
const limitDialogs = usageStats?.conversations_limit || 50
|
| 138 |
+
|
| 139 |
return (
|
| 140 |
<motion.aside
|
| 141 |
initial="hidden"
|
|
|
|
| 240 |
</div>
|
| 241 |
</motion.div>
|
| 242 |
)}
|
| 243 |
+
{/* Trial/Subscription Banner */}
|
| 244 |
+
{usageStats && isOwner && (
|
| 245 |
+
<motion.div
|
| 246 |
+
initial={{ opacity: 0 }}
|
| 247 |
+
animate={{ opacity: 1 }}
|
| 248 |
+
className={cn(
|
| 249 |
+
"mx-2 p-4 rounded-xl border mt-4",
|
| 250 |
+
isExpired
|
| 251 |
+
? "bg-red-500/10 border-red-500/30"
|
| 252 |
+
: isTrial
|
| 253 |
+
? "bg-amber-500/10 border-amber-500/30"
|
| 254 |
+
: "bg-indigo-500/10 border-indigo-500/30"
|
| 255 |
+
)}
|
| 256 |
+
>
|
| 257 |
+
<div className="flex items-center gap-2 mb-2">
|
| 258 |
+
{isExpired ? (
|
| 259 |
+
<AlertTriangle className="w-4 h-4 text-red-400" />
|
| 260 |
+
) : (
|
| 261 |
+
<Clock className="w-4 h-4 text-amber-400" />
|
| 262 |
+
)}
|
| 263 |
+
<span className={cn(
|
| 264 |
+
"text-xs font-bold uppercase",
|
| 265 |
+
isExpired ? "text-red-400" : isTrial ? "text-amber-400" : "text-indigo-400"
|
| 266 |
+
)}>
|
| 267 |
+
{isExpired ? "Подписка истекла" : isTrial ? `Trial: ${daysLeft} дн.` : usageStats.plan.toUpperCase()}
|
| 268 |
+
</span>
|
| 269 |
+
</div>
|
| 270 |
|
| 271 |
+
{/* Progress bar */}
|
| 272 |
+
{limitDialogs && (
|
| 273 |
+
<div className="mb-2">
|
| 274 |
+
<div className="flex justify-between text-[10px] text-slate-500 mb-1">
|
| 275 |
+
<span>Диалоги</span>
|
| 276 |
+
<span>{usedDialogs} / {limitDialogs}</span>
|
| 277 |
+
</div>
|
| 278 |
+
<div className="w-full bg-white/10 rounded-full h-1.5">
|
| 279 |
+
<div
|
| 280 |
+
className={cn(
|
| 281 |
+
"h-1.5 rounded-full transition-all",
|
| 282 |
+
usedDialogs >= limitDialogs
|
| 283 |
+
? "bg-red-500"
|
| 284 |
+
: usedDialogs >= limitDialogs * 0.8
|
| 285 |
+
? "bg-amber-500"
|
| 286 |
+
: "bg-indigo-500"
|
| 287 |
+
)}
|
| 288 |
+
style={{ width: `${Math.min((usedDialogs / limitDialogs) * 100, 100)}%` }}
|
| 289 |
+
/>
|
| 290 |
+
</div>
|
| 291 |
+
</div>
|
| 292 |
+
)}
|
| 293 |
+
|
| 294 |
+
{(isTrial || isExpired) && (
|
| 295 |
+
<Link
|
| 296 |
+
href="/settings/billing"
|
| 297 |
+
className={cn(
|
| 298 |
+
"block w-full py-2 text-center text-xs font-bold rounded-lg transition",
|
| 299 |
+
isExpired
|
| 300 |
+
? "bg-red-600 hover:bg-red-500 text-white"
|
| 301 |
+
: "bg-amber-600 hover:bg-amber-500 text-white"
|
| 302 |
+
)}
|
| 303 |
+
>
|
| 304 |
+
{isExpired ? "Продлить подписку" : "Выбрать тариф"}
|
| 305 |
+
</Link>
|
| 306 |
+
)}
|
| 307 |
+
</motion.div>
|
| 308 |
+
)}
|
| 309 |
|
| 310 |
</nav>
|
| 311 |
)}
|