fix: InaccessibleMessage middleware / bulk restore_trials / auto-renew reset addons
This commit is contained in:
@@ -11,7 +11,7 @@ from aiogram.types import (
|
|||||||
)
|
)
|
||||||
from aiogram.utils.formatting import BlockQuote, Bold, Text
|
from aiogram.utils.formatting import BlockQuote, Bold, Text
|
||||||
from aiogram.utils.keyboard import InlineKeyboardBuilder
|
from aiogram.utils.keyboard import InlineKeyboardBuilder
|
||||||
from sqlalchemy import func, select, update
|
from sqlalchemy import exists, func, select, update
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from database import (
|
from database import (
|
||||||
@@ -287,25 +287,22 @@ async def confirm_restore_trials(callback_query: types.CallbackQuery):
|
|||||||
IsAdminFilter(),
|
IsAdminFilter(),
|
||||||
)
|
)
|
||||||
async def restore_trials(callback_query: types.CallbackQuery, session: AsyncSession):
|
async def restore_trials(callback_query: types.CallbackQuery, session: AsyncSession):
|
||||||
users_result = await session.execute(select(User.tg_id).where(User.trial == 1))
|
stmt = (
|
||||||
users_with_trial_used = [row[0] for row in users_result.all()]
|
update(User)
|
||||||
|
.where(
|
||||||
users_to_reset = []
|
User.trial == 1,
|
||||||
for tg_id in users_with_trial_used:
|
~exists(select(Key.tg_id).where(Key.tg_id == User.tg_id)),
|
||||||
has_keys = await session.execute(select(Key.tg_id).where(Key.tg_id == tg_id).limit(1))
|
)
|
||||||
if not has_keys.scalar():
|
.values(trial=0)
|
||||||
users_to_reset.append(tg_id)
|
)
|
||||||
|
result = await session.execute(stmt)
|
||||||
if users_to_reset:
|
|
||||||
stmt = update(User).where(User.tg_id.in_(users_to_reset)).values(trial=0)
|
|
||||||
await session.execute(stmt)
|
|
||||||
await session.commit()
|
await session.commit()
|
||||||
|
|
||||||
builder = InlineKeyboardBuilder()
|
builder = InlineKeyboardBuilder()
|
||||||
builder.row(build_admin_back_btn())
|
builder.row(build_admin_back_btn())
|
||||||
|
|
||||||
await callback_query.message.edit_text(
|
await callback_query.message.edit_text(
|
||||||
text=f"✅ Пробники восстановлены для {len(users_to_reset)} пользователей без подписок.",
|
text=f"✅ Пробники восстановлены для {result.rowcount} пользователей без подписок.",
|
||||||
reply_markup=builder.as_markup(),
|
reply_markup=builder.as_markup(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -379,6 +379,17 @@ async def try_auto_renew(ctx: NotificationContext, key) -> tuple[bool, Optional[
|
|||||||
plan=current_tariff["id"],
|
plan=current_tariff["id"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
new_tariff_device_limit = current_tariff.get("device_limit")
|
||||||
|
new_tariff_traffic_limit = current_tariff.get("traffic_limit")
|
||||||
|
reset_values = {
|
||||||
|
"selected_device_limit": new_tariff_device_limit,
|
||||||
|
"current_device_limit": new_tariff_device_limit,
|
||||||
|
"selected_traffic_limit": new_tariff_traffic_limit,
|
||||||
|
"current_traffic_limit": new_tariff_traffic_limit,
|
||||||
|
"selected_price_rub": int(current_tariff["price_rub"]) if current_tariff.get("price_rub") is not None else None,
|
||||||
|
}
|
||||||
|
await ctx.session.execute(update(Key).where(Key.client_id == client_id).values(**reset_values))
|
||||||
|
|
||||||
if ctx.bulk_updates is not None:
|
if ctx.bulk_updates is not None:
|
||||||
if tg_id in ctx.bulk_updates["balance_changes"]:
|
if tg_id in ctx.bulk_updates["balance_changes"]:
|
||||||
ctx.bulk_updates["balance_changes"][tg_id] -= renewal_cost
|
ctx.bulk_updates["balance_changes"][tg_id] -= renewal_cost
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ from collections.abc import Awaitable, Callable
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from aiogram import BaseMiddleware
|
from aiogram import BaseMiddleware
|
||||||
from aiogram.types import CallbackQuery, TelegramObject
|
from aiogram.types import CallbackQuery, InaccessibleMessage, TelegramObject
|
||||||
|
|
||||||
|
from bot import bot
|
||||||
|
|
||||||
|
|
||||||
class CallbackAnswerMiddleware(BaseMiddleware):
|
class CallbackAnswerMiddleware(BaseMiddleware):
|
||||||
@@ -14,4 +16,7 @@ class CallbackAnswerMiddleware(BaseMiddleware):
|
|||||||
) -> Any:
|
) -> Any:
|
||||||
if isinstance(event, CallbackQuery):
|
if isinstance(event, CallbackQuery):
|
||||||
await event.answer()
|
await event.answer()
|
||||||
|
if isinstance(event.message, InaccessibleMessage):
|
||||||
|
new_message = await bot.send_message(event.message.chat.id, "⏳")
|
||||||
|
object.__setattr__(event, "message", new_message)
|
||||||
return await handler(event, data)
|
return await handler(event, data)
|
||||||
|
|||||||
Reference in New Issue
Block a user