feat: subs and views

This commit is contained in:
Artem Tsyrulnikov
2025-11-10 17:08:56 +03:00
parent 2549242b06
commit 0bed5c266d
34 changed files with 1706 additions and 119 deletions

View File

@@ -299,3 +299,57 @@ class Postgres(DatabaseBase):
)
result = await self.session.execute(stmt)
return result.scalar_one_or_none() is not None
async def get_purchase_by_invite_link(self, invite_link: str) -> domain.Purchase | None:
stmt = (
select(domain.Purchase)
.options(
selectinload(domain.Purchase.target_channel),
selectinload(domain.Purchase.external_channel),
selectinload(domain.Purchase.creative),
)
.where(domain.Purchase.invite_link == invite_link)
)
result = await self.session.execute(stmt)
return result.scalar_one_or_none()
async def create_subscription(self, subscription: domain.Subscription) -> domain.Subscription:
self.session.add(subscription)
await self.session.flush()
await self.session.refresh(subscription, ['purchase'])
return subscription
async def get_subscription_by_user_and_purchase(
self, user_telegram_id: int, purchase_id: uuid.UUID
) -> domain.Subscription | None:
stmt = select(domain.Subscription).where(
domain.Subscription.user_telegram_id == user_telegram_id,
domain.Subscription.purchase_id == purchase_id,
)
result = await self.session.execute(stmt)
return result.scalar_one_or_none()
async def create_views_snapshot(self, snapshot: domain.ViewsSnapshot) -> domain.ViewsSnapshot:
self.session.add(snapshot)
await self.session.flush()
await self.session.refresh(snapshot, ['purchase'])
return snapshot
async def get_views_history(
self,
purchase_id: uuid.UUID,
*,
from_date: datetime.datetime | None = None,
to_date: datetime.datetime | None = None,
) -> list[domain.ViewsSnapshot]:
stmt = select(domain.ViewsSnapshot).where(domain.ViewsSnapshot.purchase_id == purchase_id)
if from_date:
stmt = stmt.where(domain.ViewsSnapshot.fetched_at >= from_date)
if to_date:
stmt = stmt.where(domain.ViewsSnapshot.fetched_at <= to_date)
stmt = stmt.order_by(domain.ViewsSnapshot.fetched_at.asc())
result = await self.session.execute(stmt)
return list(result.scalars().all())