question packs instead of monthly/yearly plans; hearts are real

- packs (20 / 60 / 150 questions for 45,000 / 119,000 / 259,000 toman) never expire and
  give 7 / 30 / 60 days of unlimited hearts; old subscription plans stay for existing
  subscribers but are no longer on sale
- credit ledger (subscription.QuestionCredit): +N per paid pack (once per order), -1 per
  tutor question that reaches the API; the free daily questions are spent first; questions
  stopped by the server filter, cached answers and failed answers cost nothing
- unlimited hearts from packs live in HeartsPass, so buying a pack never rewrites a
  running subscription
- Message.charge records how each question was paid; GET /api/ai/quota and the ask
  responses report free-left-today and credits
- HEARTS_ENFORCED now defaults to on: zero hearts stops answering until refill or a pack

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-02 11:41:39 +03:30
parent 2edc56fc08
commit 58c8e6d014
18 changed files with 572 additions and 55 deletions

View File

@@ -0,0 +1,58 @@
# Generated by Django 5.2.17 on 2026-10-02 07:20
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('subscription', '0008_paid_quota_ten_a_day'),
]
operations = [
migrations.CreateModel(
name='HeartsPass',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('user_id', models.IntegerField(db_index=True, unique=True)),
('expires_at', models.DateTimeField()),
],
),
migrations.AddField(
model_name='plan',
name='hearts_days',
field=models.PositiveSmallIntegerField(default=0, help_text='فقط بسته: چند روز قلبِ نامحدود هدیه.'),
),
migrations.AddField(
model_name='plan',
name='kind',
field=models.CharField(choices=[('pack', 'بسته\u200cی سوال'), ('subscription', 'اشتراکِ زمانی')], default='subscription', max_length=15),
),
migrations.AddField(
model_name='plan',
name='on_sale',
field=models.BooleanField(default=True, help_text='در صفحه\u200cی خرید نشان داده شود.'),
),
migrations.AddField(
model_name='plan',
name='question_credits',
field=models.PositiveIntegerField(default=0, help_text='فقط بسته: چند سوال از دستیار به اعتبارِ خریدار اضافه می\u200cشود.'),
),
migrations.CreateModel(
name='QuestionCredit',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('user_id', models.IntegerField(db_index=True)),
('delta', models.IntegerField()),
('reason', models.CharField(choices=[('purchase', 'خرید'), ('question', 'سوال'), ('grant', 'هدیه\u200cی ادمین')], max_length=10)),
('order_id', models.IntegerField(blank=True, null=True)),
('message_id', models.IntegerField(blank=True, db_index=True, null=True)),
('note', models.CharField(blank=True, max_length=100)),
('created_at', models.DateTimeField(auto_now_add=True)),
],
options={
'ordering': ['-created_at'],
'constraints': [models.UniqueConstraint(condition=models.Q(('reason', 'purchase')), fields=('order_id',), name='uniq_credit_per_paid_order')],
},
),
]

View File

@@ -0,0 +1,50 @@
"""
بسته‌های سوال به‌جای اشتراکِ ماهانه/سالانه (۲۰۲۶-۱۰-۰۲، تصمیمِ صاحبِ پروژه).
چرا: همه هر روز نمی‌آیند و با اقتصادِ امروز ۱۷۹ هزار تومان در ماه برای خیلی‌ها زیاد است. با بسته
هر کس به اندازه‌ی مصرفش می‌خرد و هر سوال جدا سود دارد؛ مشترکی که سقف را هر روز پر می‌کرد دیگر
زیان نمی‌سازد. هزینه‌ی هر سوال برای ما ~۶۰۰ تومان است (دبیرستان ~۷۷۰)؛ سهمِ هزینه از درآمدِ خالص
پس از ۱۵٪یِ کافه‌بازار: ۲۰ سوال ۳۱٪، ۶۰ سوال ۳۶٪، ۱۵۰ سوال ۴۱٪. قیمت‌ها عمداً دستِ بالا گرفته شده‌اند.
اعتبار منقضی نمی‌شود. هر بسته چند روز قلبِ نامحدود هم می‌دهد (۷ / ۳۰ / ۶۰ روز)، که از همان روز
کم شدنِ قلب روشن است (HEARTS_ENFORCED) واقعاً ارزش دارد.
پلن‌های اشتراکی پاک نمی‌شوند؛ فقط از صفحه‌ی خرید برداشته می‌شوند (on_sale=False) تا اشتراکِ
فعالِ کسی که پیش‌تر خریده با همان سقفِ روزانه بماند.
"""
from django.db import migrations
PACKS = [
{'title': '۲۰ سوال', 'price': 45_000, 'question_credits': 20, 'hearts_days': 7,
'badge': '', 'popular': False},
{'title': '۶۰ سوال', 'price': 119_000, 'question_credits': 60, 'hearts_days': 30,
'badge': 'محبوب‌ترین', 'popular': True},
{'title': '۱۵۰ سوال', 'price': 259_000, 'question_credits': 150, 'hearts_days': 60,
'badge': 'به‌صرفه‌ترین', 'popular': False},
]
def seed(apps, schema_editor):
Plan = apps.get_model('subscription', 'Plan')
Plan.objects.filter(kind='subscription').update(on_sale=False)
for pack in PACKS:
if not Plan.objects.filter(kind='pack', question_credits=pack['question_credits']).exists():
Plan.objects.create(kind='pack', currency='IRT', discount_percent=0, duration_days=0,
ai_daily_quota=0, features=[], on_sale=True, **pack)
def undo(apps, schema_editor):
Plan = apps.get_model('subscription', 'Plan')
Plan.objects.filter(kind='pack', subscriptions__isnull=True).delete()
Plan.objects.filter(kind='subscription').update(on_sale=True)
class Migration(migrations.Migration):
dependencies = [
('subscription', '0009_question_packs'),
]
operations = [
migrations.RunPython(seed, undo),
]