feat: доменный нейминг изменен

This commit is contained in:
Artem Tsyrulnikov
2025-11-13 01:18:04 +03:00
parent a48d5e67cb
commit bba37fbb21
51 changed files with 1144 additions and 1154 deletions

View File

@@ -14,8 +14,8 @@ class Postgres(DatabaseBase):
return await self.session.get(domain.User, user_id)
elif telegram_id:
stmt = select(domain.User).where(domain.User.telegram_id == telegram_id)
result = await self.session.execute(stmt)
q = select(domain.User).where(domain.User.telegram_id == telegram_id)
result = await self.session.execute(q)
return result.scalar_one_or_none()
raise ValueError('Either user_id or telegram_id must be provided')
@@ -37,15 +37,15 @@ class Postgres(DatabaseBase):
return login_token
async def get_login_token(self, token: str) -> domain.LoginToken | None:
stmt = select(domain.LoginToken).where(domain.LoginToken.token == token)
q = select(domain.LoginToken).where(domain.LoginToken.token == token)
result = await self.session.execute(stmt)
result = await self.session.execute(q)
return result.scalar_one_or_none()
async def mark_token_as_used(self, token: str) -> None:
stmt = select(domain.LoginToken).where(domain.LoginToken.token == token)
q = select(domain.LoginToken).where(domain.LoginToken.token == token)
result = await self.session.execute(stmt)
result = await self.session.execute(q)
login_token = result.scalar_one_or_none()
if login_token:
login_token.used_at = datetime.datetime.now(datetime.UTC)
@@ -57,13 +57,13 @@ class Postgres(DatabaseBase):
if not channel_id and not telegram_id:
raise ValueError('Either channel_id or telegram_id must be provided')
stmt = select(domain.TargetChannel).where(domain.TargetChannel.user_id == user_id)
q = select(domain.TargetChannel).where(domain.TargetChannel.user_id == user_id)
if channel_id:
stmt = stmt.where(domain.TargetChannel.id == channel_id)
q = q.where(domain.TargetChannel.id == channel_id)
if telegram_id:
stmt = stmt.where(domain.TargetChannel.telegram_id == telegram_id)
q = q.where(domain.TargetChannel.telegram_id == telegram_id)
result = await self.session.execute(stmt)
result = await self.session.execute(q)
return result.scalar_one_or_none()
async def create_target_channel(self, channel: domain.TargetChannel) -> domain.TargetChannel:
@@ -93,11 +93,11 @@ class Postgres(DatabaseBase):
return existing
async def get_user_target_channels(self, user_id: uuid.UUID) -> list[domain.TargetChannel]:
stmt = select(domain.TargetChannel).where(
q = select(domain.TargetChannel).where(
domain.TargetChannel.status == domain.TargetChannelStatus.ACTIVE,
domain.TargetChannel.user_id == user_id,
)
result = await self.session.execute(stmt)
result = await self.session.execute(q)
return list(result.scalars().all())
async def update_target_channel(self, channel: domain.TargetChannel) -> domain.TargetChannel:
@@ -108,13 +108,13 @@ class Postgres(DatabaseBase):
async def get_external_channel(
self, user_id: uuid.UUID, channel_id: uuid.UUID | None = None, telegram_id: int | None = None
) -> domain.ExternalChannel | None:
stmt = select(domain.ExternalChannel).where(domain.ExternalChannel.user_id == user_id)
q = select(domain.ExternalChannel).where(domain.ExternalChannel.user_id == user_id)
if channel_id:
stmt = stmt.where(domain.ExternalChannel.id == channel_id)
q = q.where(domain.ExternalChannel.id == channel_id)
if telegram_id:
stmt = stmt.where(domain.ExternalChannel.telegram_id == telegram_id)
q = q.where(domain.ExternalChannel.telegram_id == telegram_id)
result = await self.session.execute(stmt)
result = await self.session.execute(q)
return result.scalar_one_or_none()
async def create_external_channel(self, channel: domain.ExternalChannel) -> domain.ExternalChannel:
@@ -124,23 +124,23 @@ class Postgres(DatabaseBase):
return channel
async def get_external_channels_for_target(self, target_channel_id: uuid.UUID) -> list[domain.ExternalChannel]:
stmt = (
q = (
select(domain.ExternalChannel)
.join(domain.ExternalChannel.target_channels)
.where(domain.TargetChannel.id == target_channel_id)
)
result = await self.session.execute(stmt)
result = await self.session.execute(q)
return list(result.scalars().all())
async def add_external_channel_to_targets(
self, external_channel_id: uuid.UUID, target_channel_ids: list[uuid.UUID]
) -> None:
stmt = (
q = (
select(domain.ExternalChannel)
.where(domain.ExternalChannel.id == external_channel_id)
.options(selectinload(domain.ExternalChannel.target_channels))
)
result = await self.session.execute(stmt)
result = await self.session.execute(q)
external_channel = result.scalar_one_or_none()
if not external_channel:
@@ -156,12 +156,12 @@ class Postgres(DatabaseBase):
async def remove_external_channel_from_target(
self, external_channel_id: uuid.UUID, target_channel_id: uuid.UUID
) -> None:
stmt = (
q = (
select(domain.ExternalChannel)
.where(domain.ExternalChannel.id == external_channel_id)
.options(selectinload(domain.ExternalChannel.target_channels))
)
result = await self.session.execute(stmt)
result = await self.session.execute(q)
external_channel = result.scalar_one_or_none()
if not external_channel:
@@ -174,8 +174,8 @@ class Postgres(DatabaseBase):
await self.session.flush()
async def delete_external_channel(self, channel_id: uuid.UUID) -> None:
stmt = delete(domain.ExternalChannel).where(domain.ExternalChannel.id == channel_id)
await self.session.execute(stmt)
q = delete(domain.ExternalChannel).where(domain.ExternalChannel.id == channel_id)
await self.session.execute(q)
await self.session.flush()
async def update_external_channel(self, channel: domain.ExternalChannel) -> domain.ExternalChannel:
@@ -184,12 +184,12 @@ class Postgres(DatabaseBase):
return channel
async def get_creative(self, user_id: uuid.UUID, creative_id: uuid.UUID) -> domain.Creative | None:
stmt = (
q = (
select(domain.Creative)
.options(selectinload(domain.Creative.target_channel))
.where(domain.Creative.id == creative_id, domain.Creative.user_id == user_id)
)
result = await self.session.execute(stmt)
result = await self.session.execute(q)
return result.scalar_one_or_none()
async def create_creative(self, creative: domain.Creative) -> domain.Creative:
@@ -206,59 +206,59 @@ class Postgres(DatabaseBase):
async def get_user_creatives(
self, user_id: uuid.UUID, *, target_channel_id: uuid.UUID | None = None, include_archived: bool = False
) -> list[domain.Creative]:
stmt = (
q = (
select(domain.Creative)
.options(selectinload(domain.Creative.target_channel))
.where(domain.Creative.user_id == user_id)
)
if target_channel_id:
stmt = stmt.where(domain.Creative.target_channel_id == target_channel_id)
q = q.where(domain.Creative.target_channel_id == target_channel_id)
if not include_archived:
stmt = stmt.where(domain.Creative.status == domain.CreativeStatus.ACTIVE)
q = q.where(domain.Creative.status == domain.CreativeStatus.ACTIVE)
stmt = stmt.order_by(domain.Creative.created_at.desc())
q = q.order_by(domain.Creative.created_at.desc())
result = await self.session.execute(stmt)
result = await self.session.execute(q)
return list(result.scalars().all())
async def delete_creative(self, creative_id: uuid.UUID) -> None:
stmt = delete(domain.Creative).where(domain.Creative.id == creative_id)
await self.session.execute(stmt)
q = delete(domain.Creative).where(domain.Creative.id == creative_id)
await self.session.execute(q)
await self.session.flush()
async def get_purchase(self, user_id: uuid.UUID, purchase_id: uuid.UUID) -> domain.Purchase | None:
stmt = (
select(domain.Purchase)
async def get_placement(self, user_id: uuid.UUID, placement_id: uuid.UUID) -> domain.Placement | None:
q = (
select(domain.Placement)
.options(
selectinload(domain.Purchase.target_channel),
selectinload(domain.Purchase.external_channel),
selectinload(domain.Purchase.creative),
selectinload(domain.Placement.target_channel),
selectinload(domain.Placement.external_channel),
selectinload(domain.Placement.creative),
)
.where(domain.Purchase.id == purchase_id, domain.Purchase.user_id == user_id)
.where(domain.Placement.id == placement_id, domain.Placement.user_id == user_id)
)
result = await self.session.execute(stmt)
result = await self.session.execute(q)
return result.scalar_one_or_none()
async def create_purchase(self, purchase: domain.Purchase) -> domain.Purchase:
self.session.add(purchase)
async def create_placement(self, placement: domain.Placement) -> domain.Placement:
self.session.add(placement)
await self.session.flush()
await self.session.refresh(
purchase,
placement,
['target_channel', 'external_channel', 'creative'],
)
return purchase
return placement
async def update_purchase(self, purchase: domain.Purchase) -> domain.Purchase:
async def update_placement(self, placement: domain.Placement) -> domain.Placement:
await self.session.flush()
await self.session.refresh(
purchase,
placement,
['target_channel', 'external_channel', 'creative'],
)
return purchase
return placement
async def get_user_purchases(
async def get_user_placements(
self,
user_id: uuid.UUID,
*,
@@ -266,109 +266,134 @@ class Postgres(DatabaseBase):
external_channel_id: uuid.UUID | None = None,
creative_id: uuid.UUID | None = None,
include_archived: bool = False,
) -> list[domain.Purchase]:
stmt = (
select(domain.Purchase)
) -> list[domain.Placement]:
q = (
select(domain.Placement)
.options(
selectinload(domain.Purchase.target_channel),
selectinload(domain.Purchase.external_channel),
selectinload(domain.Purchase.creative),
selectinload(domain.Placement.target_channel),
selectinload(domain.Placement.external_channel),
selectinload(domain.Placement.creative),
)
.where(domain.Purchase.user_id == user_id)
.where(domain.Placement.user_id == user_id)
)
if target_channel_id:
stmt = stmt.where(domain.Purchase.target_channel_id == target_channel_id)
q = q.where(domain.Placement.target_channel_id == target_channel_id)
if external_channel_id:
stmt = stmt.where(domain.Purchase.external_channel_id == external_channel_id)
q = q.where(domain.Placement.external_channel_id == external_channel_id)
if creative_id:
stmt = stmt.where(domain.Purchase.creative_id == creative_id)
q = q.where(domain.Placement.creative_id == creative_id)
if not include_archived:
stmt = stmt.where(domain.Purchase.status == domain.PurchaseStatus.ACTIVE)
q = q.where(domain.Placement.status == domain.PlacementStatus.ACTIVE)
stmt = stmt.order_by(domain.Purchase.placement_date.desc())
q = q.order_by(domain.Placement.placement_date.desc())
result = await self.session.execute(stmt)
result = await self.session.execute(q)
return list(result.scalars().all())
async def delete_purchase(self, purchase_id: uuid.UUID) -> None:
stmt = delete(domain.Purchase).where(domain.Purchase.id == purchase_id)
await self.session.execute(stmt)
async def delete_placement(self, placement_id: uuid.UUID) -> None:
q = delete(domain.Placement).where(domain.Placement.id == placement_id)
await self.session.execute(q)
await self.session.flush()
async def check_creative_in_use(self, creative_id: uuid.UUID) -> bool:
stmt = select(domain.Purchase).where(
domain.Purchase.creative_id == creative_id,
domain.Purchase.status == domain.PurchaseStatus.ACTIVE,
q = select(domain.Placement).where(
domain.Placement.creative_id == creative_id,
domain.Placement.status == domain.PlacementStatus.ACTIVE,
)
result = await self.session.execute(stmt)
result = await self.session.execute(q)
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)
async def get_placement_by_invite_link(self, invite_link: str) -> domain.Placement | None:
q = (
select(domain.Placement)
.options(
selectinload(domain.Purchase.target_channel),
selectinload(domain.Purchase.external_channel),
selectinload(domain.Purchase.creative),
selectinload(domain.Placement.target_channel),
selectinload(domain.Placement.external_channel),
selectinload(domain.Placement.creative),
)
.where(domain.Purchase.invite_link == invite_link)
.where(domain.Placement.invite_link == invite_link)
)
result = await self.session.execute(stmt)
result = await self.session.execute(q)
return result.scalar_one_or_none()
async def get_subscriber(self, telegram_id: int) -> domain.Subscriber | None:
q = select(domain.Subscriber).where(domain.Subscriber.telegram_id == telegram_id)
result = await self.session.execute(q)
return result.scalar_one_or_none()
async def create_subscriber(self, subscriber: domain.Subscriber) -> domain.Subscriber:
self.session.add(subscriber)
await self.session.flush()
await self.session.refresh(subscriber)
return subscriber
async def upsert_subscriber(self, subscriber: domain.Subscriber) -> domain.Subscriber:
existing = await self.get_subscriber(subscriber.telegram_id)
if existing:
# Обновляем данные если изменились
existing.username = subscriber.username
existing.first_name = subscriber.first_name
existing.last_name = subscriber.last_name
await self.session.flush()
await self.session.refresh(existing)
return existing
return await self.create_subscriber(subscriber)
async def create_subscription(self, subscription: domain.Subscription) -> domain.Subscription:
self.session.add(subscription)
await self.session.flush()
await self.session.refresh(subscription, ['purchase'])
await self.session.refresh(subscription, ['placement', 'subscriber'])
return subscription
async def get_subscription_by_user_and_purchase(
self, user_telegram_id: int, purchase_id: uuid.UUID
async def get_subscription_by_subscriber_and_placement(
self, subscriber_id: uuid.UUID, placement_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,
q = select(domain.Subscription).where(
domain.Subscription.subscriber_id == subscriber_id,
domain.Subscription.placement_id == placement_id,
)
result = await self.session.execute(stmt)
result = await self.session.execute(q)
return result.scalar_one_or_none()
async def create_views_snapshot(self, snapshot: domain.ViewsSnapshot) -> domain.ViewsSnapshot:
self.session.add(snapshot)
async def create_placement_views_history(
self, history: domain.PlacementViewsHistory
) -> domain.PlacementViewsHistory:
self.session.add(history)
await self.session.flush()
await self.session.refresh(snapshot, ['purchase'])
return snapshot
await self.session.refresh(history, ['placement'])
return history
async def get_views_history(
self,
purchase_id: uuid.UUID,
placement_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)
) -> list[domain.PlacementViewsHistory]:
q = select(domain.PlacementViewsHistory).where(domain.PlacementViewsHistory.placement_id == placement_id)
if from_date:
stmt = stmt.where(domain.ViewsSnapshot.fetched_at >= from_date)
q = q.where(domain.PlacementViewsHistory.fetched_at >= from_date)
if to_date:
stmt = stmt.where(domain.ViewsSnapshot.fetched_at <= to_date)
q = q.where(domain.PlacementViewsHistory.fetched_at <= to_date)
stmt = stmt.order_by(domain.ViewsSnapshot.fetched_at.asc())
q = q.order_by(domain.PlacementViewsHistory.fetched_at.asc())
result = await self.session.execute(stmt)
result = await self.session.execute(q)
return list(result.scalars().all())
async def get_active_purchases_with_urls(self) -> list[domain.Purchase]:
stmt = (
select(domain.Purchase)
async def get_active_placements_with_urls(self) -> list[domain.Placement]:
q = (
select(domain.Placement)
.where(
domain.Purchase.status == domain.PurchaseStatus.ACTIVE,
domain.Purchase.ad_post_url.isnot(None),
domain.Purchase.views_availability != domain.PostViewsAvailability.MANUAL,
domain.Placement.status == domain.PlacementStatus.ACTIVE,
domain.Placement.ad_post_url.isnot(None),
domain.Placement.views_availability != domain.PostViewsAvailability.MANUAL,
)
.order_by(domain.Purchase.created_at.asc())
.order_by(domain.Placement.created_at.asc())
)
result = await self.session.execute(stmt)
result = await self.session.execute(q)
return list(result.scalars().all())