feat: purchases added

This commit is contained in:
Artem Tsyrulnikov
2025-11-10 16:43:20 +03:00
parent f8edd81395
commit 2549242b06
20 changed files with 1429 additions and 8 deletions

View File

@@ -222,3 +222,80 @@ class Postgres(DatabaseBase):
stmt = delete(domain.Creative).where(domain.Creative.id == creative_id)
await self.session.execute(stmt)
await self.session.flush()
async def get_purchase(self, user_id: uuid.UUID, purchase_id: uuid.UUID) -> 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.id == purchase_id, domain.Purchase.user_id == user_id)
)
result = await self.session.execute(stmt)
return result.scalar_one_or_none()
async def create_purchase(self, purchase: domain.Purchase) -> domain.Purchase:
self.session.add(purchase)
await self.session.flush()
await self.session.refresh(
purchase,
['target_channel', 'external_channel', 'creative'],
)
return purchase
async def update_purchase(self, purchase: domain.Purchase) -> domain.Purchase:
await self.session.flush()
await self.session.refresh(
purchase,
['target_channel', 'external_channel', 'creative'],
)
return purchase
async def get_user_purchases(
self,
user_id: uuid.UUID,
*,
target_channel_id: uuid.UUID | None = None,
external_channel_id: uuid.UUID | None = None,
creative_id: uuid.UUID | None = None,
include_archived: bool = False,
) -> list[domain.Purchase]:
stmt = (
select(domain.Purchase)
.options(
selectinload(domain.Purchase.target_channel),
selectinload(domain.Purchase.external_channel),
selectinload(domain.Purchase.creative),
)
.where(domain.Purchase.user_id == user_id)
)
if target_channel_id:
stmt = stmt.where(domain.Purchase.target_channel_id == target_channel_id)
if external_channel_id:
stmt = stmt.where(domain.Purchase.external_channel_id == external_channel_id)
if creative_id:
stmt = stmt.where(domain.Purchase.creative_id == creative_id)
if not include_archived:
stmt = stmt.where(domain.Purchase.status == domain.PurchaseStatus.ACTIVE)
stmt = stmt.order_by(domain.Purchase.placement_date.desc())
result = await self.session.execute(stmt)
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)
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,
)
result = await self.session.execute(stmt)
return result.scalar_one_or_none() is not None