fix: delegate tariff resets to panel
This commit is contained in:
+11
-60
@@ -1,13 +1,9 @@
|
||||
import logging
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from bot.utils.date_utils import month_start
|
||||
from config.settings import Settings
|
||||
from db.dal import subscription_dal
|
||||
from db.models import Base, Subscription
|
||||
from db.models import Base
|
||||
from .migrator import run_database_migrations
|
||||
|
||||
async_engine = None
|
||||
@@ -105,19 +101,7 @@ async def init_db(settings: Settings, session_factory: sessionmaker):
|
||||
tariff_key = COALESCE(s.tariff_key, :tariff_key),
|
||||
tier_baseline_bytes = COALESCE(s.tier_baseline_bytes, s.traffic_limit_bytes, :baseline),
|
||||
topup_balance_bytes = COALESCE(s.topup_balance_bytes, 0),
|
||||
period_start_at = COALESCE(
|
||||
s.period_start_at,
|
||||
(
|
||||
SELECT p.created_at
|
||||
FROM payments p
|
||||
WHERE p.user_id = s.user_id
|
||||
AND p.status = 'succeeded'
|
||||
ORDER BY p.created_at DESC
|
||||
LIMIT 1
|
||||
),
|
||||
s.start_date,
|
||||
NOW()
|
||||
),
|
||||
period_start_at = NULL,
|
||||
effective_monthly_price_rub = COALESCE(
|
||||
s.effective_monthly_price_rub,
|
||||
(
|
||||
@@ -141,49 +125,16 @@ async def init_db(settings: Settings, session_factory: sessionmaker):
|
||||
"default_price": default_price,
|
||||
},
|
||||
)
|
||||
month_anchor = month_start()
|
||||
active_subs = await session.execute(
|
||||
select(Subscription).where(Subscription.is_active == True)
|
||||
await session.execute(
|
||||
text(
|
||||
"""
|
||||
UPDATE subscriptions
|
||||
SET period_start_at = NULL
|
||||
WHERE is_active = TRUE
|
||||
AND tariff_key IS NOT NULL
|
||||
"""
|
||||
)
|
||||
)
|
||||
for sub in active_subs.scalars().all():
|
||||
tariff_key = sub.tariff_key or default_tariff.key
|
||||
try:
|
||||
tariff = settings.tariffs_config.require(tariff_key)
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
update_data = {
|
||||
"tariff_key": tariff.key,
|
||||
"topup_balance_bytes": int(sub.topup_balance_bytes or 0),
|
||||
}
|
||||
if tariff.billing_model == "period":
|
||||
baseline_source = (
|
||||
sub.tier_baseline_bytes
|
||||
if sub.tier_baseline_bytes is not None
|
||||
else (sub.traffic_limit_bytes if sub.traffic_limit_bytes is not None else tariff.monthly_bytes)
|
||||
)
|
||||
baseline = int(baseline_source or 0)
|
||||
update_data.update(
|
||||
{
|
||||
"tier_baseline_bytes": baseline,
|
||||
"traffic_limit_bytes": baseline + int(sub.topup_balance_bytes or 0),
|
||||
"period_start_at": month_anchor,
|
||||
"effective_monthly_price_rub": (
|
||||
sub.effective_monthly_price_rub
|
||||
if sub.effective_monthly_price_rub is not None
|
||||
else default_price
|
||||
),
|
||||
}
|
||||
)
|
||||
else:
|
||||
update_data.update(
|
||||
{
|
||||
"tier_baseline_bytes": 0,
|
||||
"period_start_at": None,
|
||||
"effective_monthly_price_rub": None,
|
||||
}
|
||||
)
|
||||
await subscription_dal.update_subscription(session, sub.subscription_id, update_data)
|
||||
await session.commit()
|
||||
except Exception:
|
||||
await session.rollback()
|
||||
|
||||
Reference in New Issue
Block a user