a90d2d9367
1. Balance-mode gift purchase leaked full 64-char token in response. Gateway path truncated to [:12] but balance path didn't. Fixed. 2. retry_stuck_pending_activation referenced GuestPurchase.updated_at which doesn't exist on the model. Changed to paid_at (mirrors retry_stuck_paid_purchases pattern). 3. clear_notifications() called db.commit() unconditionally, defeating commit=False in replace_subscription. Added commit parameter with default=True for backward compat, passed through from caller.
69 lines
1.9 KiB
Python
69 lines
1.9 KiB
Python
import structlog
|
|
from sqlalchemy import delete, select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.database.models import SentNotification
|
|
|
|
|
|
logger = structlog.get_logger(__name__)
|
|
|
|
|
|
async def notification_sent(
|
|
db: AsyncSession,
|
|
user_id: int,
|
|
subscription_id: int,
|
|
notification_type: str,
|
|
days_before: int | None = None,
|
|
) -> bool:
|
|
result = await db.execute(
|
|
select(SentNotification)
|
|
.where(
|
|
SentNotification.user_id == user_id,
|
|
SentNotification.subscription_id == subscription_id,
|
|
SentNotification.notification_type == notification_type,
|
|
SentNotification.days_before == days_before,
|
|
)
|
|
.limit(1)
|
|
)
|
|
return result.scalars().first() is not None
|
|
|
|
|
|
async def record_notification(
|
|
db: AsyncSession,
|
|
user_id: int,
|
|
subscription_id: int,
|
|
notification_type: str,
|
|
days_before: int | None = None,
|
|
) -> None:
|
|
already_exists = await notification_sent(db, user_id, subscription_id, notification_type, days_before)
|
|
if already_exists:
|
|
return
|
|
notification = SentNotification(
|
|
user_id=user_id,
|
|
subscription_id=subscription_id,
|
|
notification_type=notification_type,
|
|
days_before=days_before,
|
|
)
|
|
db.add(notification)
|
|
await db.commit()
|
|
|
|
|
|
async def clear_notifications(db: AsyncSession, subscription_id: int, *, commit: bool = True) -> None:
|
|
await db.execute(delete(SentNotification).where(SentNotification.subscription_id == subscription_id))
|
|
if commit:
|
|
await db.commit()
|
|
|
|
|
|
async def clear_notification_by_type(
|
|
db: AsyncSession,
|
|
subscription_id: int,
|
|
notification_type: str,
|
|
) -> None:
|
|
await db.execute(
|
|
delete(SentNotification).where(
|
|
SentNotification.subscription_id == subscription_id,
|
|
SentNotification.notification_type == notification_type,
|
|
)
|
|
)
|
|
await db.commit()
|