feature: Add local subscription notification worker
This commit is contained in:
@@ -9,7 +9,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.future import select
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from db.models import Subscription
|
||||
from db.models import Subscription, SubscriptionNotification
|
||||
|
||||
INSTALL_SHARE_TOKEN_BYTES = 16
|
||||
|
||||
@@ -318,6 +318,45 @@ async def update_subscription_notification_time(
|
||||
)
|
||||
|
||||
|
||||
async def has_subscription_notification(
|
||||
session: AsyncSession,
|
||||
subscription_id: int,
|
||||
notification_key: str,
|
||||
) -> bool:
|
||||
stmt = (
|
||||
select(SubscriptionNotification.notification_id)
|
||||
.where(
|
||||
SubscriptionNotification.subscription_id == subscription_id,
|
||||
SubscriptionNotification.notification_key == notification_key,
|
||||
)
|
||||
.limit(1)
|
||||
)
|
||||
result = await session.execute(stmt)
|
||||
return result.scalar_one_or_none() is not None
|
||||
|
||||
|
||||
async def record_subscription_notification(
|
||||
session: AsyncSession,
|
||||
subscription_id: int,
|
||||
notification_key: str,
|
||||
*,
|
||||
sent_at: Optional[datetime] = None,
|
||||
) -> None:
|
||||
if sent_at is None:
|
||||
sent_at = datetime.now(timezone.utc)
|
||||
existing = await has_subscription_notification(session, subscription_id, notification_key)
|
||||
if existing:
|
||||
return
|
||||
session.add(
|
||||
SubscriptionNotification(
|
||||
subscription_id=subscription_id,
|
||||
notification_key=notification_key,
|
||||
sent_at=sent_at,
|
||||
)
|
||||
)
|
||||
await update_subscription_notification_time(session, subscription_id, sent_at)
|
||||
|
||||
|
||||
async def find_subscription_for_notification_update(
|
||||
session: AsyncSession, user_id: int, subscription_end_date_to_match: datetime
|
||||
) -> Optional[Subscription]:
|
||||
|
||||
@@ -18,6 +18,7 @@ from ..models import (
|
||||
Payment,
|
||||
PromoCodeActivation,
|
||||
Subscription,
|
||||
SubscriptionNotification,
|
||||
SupportTicket,
|
||||
SupportTicketMessage,
|
||||
TariffChange,
|
||||
@@ -779,6 +780,11 @@ async def delete_user_and_relations(session: AsyncSession, user_id: int) -> bool
|
||||
await session.execute(
|
||||
delete(TrafficWarning).where(TrafficWarning.subscription_id.in_(subscription_ids))
|
||||
)
|
||||
await session.execute(
|
||||
delete(SubscriptionNotification).where(
|
||||
SubscriptionNotification.subscription_id.in_(subscription_ids)
|
||||
)
|
||||
)
|
||||
await session.execute(
|
||||
delete(SupportTicketMessage).where(SupportTicketMessage.ticket_id.in_(support_ticket_ids))
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user