from typing import Optional from sqlalchemy import func, select from sqlalchemy.ext.asyncio import AsyncSession from app.db.models import BotGroup async def upsert(session: AsyncSession, chat_id: int, title: Optional[str], active: bool) -> None: group = await session.get(BotGroup, chat_id) if group: group.title, group.active = title, active else: session.add(BotGroup(chat_id=chat_id, title=title, active=active)) await session.flush() async def count_active(session: AsyncSession) -> int: res = await session.execute(select(func.count()).select_from(BotGroup).where(BotGroup.active.is_(True))) return int(res.scalar_one())