mirror of
https://github.com/BEDOLAGA-DEV/remnawave-bedolaga-telegram-bot.git
synced 2026-02-28 23:35:59 +00:00
- Migrate 660+ datetime.utcnow() across 153 files to datetime.now(UTC) - Migrate 30+ datetime.now() without UTC to datetime.now(UTC) - Convert all 170 DateTime columns to DateTime(timezone=True) - Add migrate_datetime_to_timestamptz() in universal_migration with SET LOCAL timezone='UTC' safety - Remove 70+ .replace(tzinfo=None) workarounds - Fix utcfromtimestamp → fromtimestamp(..., tz=UTC) - Fix fromtimestamp() without tz= (system_logs, backup_service, referral_diagnostics) - Fix fromisoformat/isoparse to ensure aware output (platega, yookassa, wata, miniapp, nalogo) - Fix strptime() to add .replace(tzinfo=UTC) (backup_service, referral_diagnostics) - Fix datetime.combine() to include tzinfo=UTC (remnawave_sync, traffic_monitoring) - Fix datetime.max/datetime.min sentinels with .replace(tzinfo=UTC) - Rename panel_datetime_to_naive_utc → panel_datetime_to_utc - Remove DTZ003 from ruff ignore list
74 lines
2.0 KiB
Python
74 lines
2.0 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Iterable
|
|
from datetime import UTC, datetime
|
|
from typing import Any
|
|
|
|
from sqlalchemy import and_, func, select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy.orm import selectinload
|
|
|
|
from app.database.models import SubscriptionEvent
|
|
|
|
|
|
async def create_subscription_event(
|
|
db: AsyncSession,
|
|
*,
|
|
user_id: int,
|
|
event_type: str,
|
|
subscription_id: int | None = None,
|
|
transaction_id: int | None = None,
|
|
amount_kopeks: int | None = None,
|
|
currency: str | None = None,
|
|
message: str | None = None,
|
|
occurred_at: datetime | None = None,
|
|
extra: dict[str, Any] | None = None,
|
|
) -> SubscriptionEvent:
|
|
event = SubscriptionEvent(
|
|
user_id=user_id,
|
|
event_type=event_type,
|
|
subscription_id=subscription_id,
|
|
transaction_id=transaction_id,
|
|
amount_kopeks=amount_kopeks,
|
|
currency=currency,
|
|
message=message,
|
|
occurred_at=occurred_at or datetime.now(UTC),
|
|
extra=extra or None,
|
|
)
|
|
db.add(event)
|
|
await db.commit()
|
|
await db.refresh(event)
|
|
return event
|
|
|
|
|
|
async def list_subscription_events(
|
|
db: AsyncSession,
|
|
*,
|
|
limit: int,
|
|
offset: int,
|
|
event_types: Iterable[str] | None = None,
|
|
user_id: int | None = None,
|
|
) -> tuple[list[SubscriptionEvent], int]:
|
|
base_query = select(SubscriptionEvent)
|
|
filters = []
|
|
|
|
if event_types:
|
|
filters.append(SubscriptionEvent.event_type.in_(set(event_types)))
|
|
if user_id:
|
|
filters.append(SubscriptionEvent.user_id == user_id)
|
|
|
|
if filters:
|
|
base_query = base_query.where(and_(*filters))
|
|
|
|
total_query = base_query.with_only_columns(func.count()).order_by(None)
|
|
total = await db.scalar(total_query) or 0
|
|
|
|
result = await db.execute(
|
|
base_query.options(selectinload(SubscriptionEvent.user))
|
|
.order_by(SubscriptionEvent.occurred_at.desc())
|
|
.offset(offset)
|
|
.limit(limit)
|
|
)
|
|
|
|
return result.scalars().all(), int(total)
|