change domen

This commit is contained in:
Artem Tsyrulnikov
2026-01-02 15:51:54 +03:00
parent c05ac84b38
commit 0577d2a00e
63 changed files with 1941 additions and 2186 deletions

View File

@@ -257,37 +257,60 @@ class Postgres(DatabaseBase, Database):
return await query.order_by('created_at').all()
async def get_active_purchase_plan(self, project_id: uuid.UUID) -> domain.PurchasePlan | None:
return await domain.PurchasePlan.get_or_none(project_id=project_id, status=domain.PurchasePlanStatus.ACTIVE)
async def get_active_purchase(self, project_id: uuid.UUID, creative_id: uuid.UUID) -> domain.Purchase | None:
return await domain.Purchase.get_or_none(
project_id=project_id, creative_id=creative_id, status=domain.PurchaseStatus.ACTIVE
)
async def get_purchase_plan(self, workspace_id: uuid.UUID, plan_id: uuid.UUID) -> domain.PurchasePlan | None:
return await domain.PurchasePlan.get_or_none(id=plan_id, workspace_id=workspace_id)
async def get_purchase(self, workspace_id: uuid.UUID, purchase_id: uuid.UUID) -> domain.Purchase | None:
return await domain.Purchase.get_or_none(id=purchase_id, workspace_id=workspace_id)
async def create_purchase_plan(self, plan: domain.PurchasePlan) -> None:
await plan.save()
async def get_purchase_plan_channels(self, plan_id: uuid.UUID) -> list[domain.PurchasePlanChannel]:
return await domain.PurchasePlanChannel.filter(purchase_plan_id=plan_id).prefetch_related('channel').all()
async def get_purchase_plan_channel(
self, plan_channel_id: uuid.UUID, plan_id: uuid.UUID
) -> domain.PurchasePlanChannel | None:
async def get_project_purchases(self, workspace_id: uuid.UUID, project_id: uuid.UUID) -> list[domain.Purchase]:
return (
await domain.PurchasePlanChannel.filter(id=plan_channel_id, purchase_plan_id=plan_id)
await domain.Purchase.filter(workspace_id=workspace_id, project_id=project_id)
.prefetch_related('channels', 'channels__channel')
.all()
)
async def create_purchase(self, purchase: domain.Purchase) -> None:
await purchase.save()
async def get_purchase_channels(self, purchase_id: uuid.UUID) -> list[domain.PurchaseChannel]:
return await domain.PurchaseChannel.filter(purchase_id=purchase_id).prefetch_related('channel').all()
async def get_purchase_channel(
self, purchase_channel_id: uuid.UUID, purchase_id: uuid.UUID
) -> domain.PurchaseChannel | None:
return (
await domain.PurchaseChannel.filter(id=purchase_channel_id, purchase_id=purchase_id)
.prefetch_related('channel')
.first()
)
async def add_channel_to_purchase_plan(
async def get_purchase_channels_by_project(self, project_id: uuid.UUID) -> list[domain.PurchaseChannel]:
return (
await domain.PurchaseChannel.filter(
purchase__project_id=project_id, purchase__status=domain.PurchaseStatus.ACTIVE
)
.prefetch_related('channel')
.all()
)
async def add_channel_to_purchase(
self,
plan_id: uuid.UUID,
purchase_id: uuid.UUID,
channel_id: uuid.UUID,
invite_link: str,
invite_link_type: domain.purchase.InviteLinkType,
*,
status: domain.PurchasePlanChannelStatus | None = None,
status: domain.PurchaseChannelStatus | None = None,
planned_cost: float | None = None,
comment: str | None = None,
) -> domain.PurchasePlanChannel:
defaults: dict[str, object | None] = {}
) -> domain.PurchaseChannel:
defaults: dict[str, object | None] = {
'invite_link': invite_link,
'invite_link_type': invite_link_type,
}
if status is not None:
defaults['status'] = status
if planned_cost is not None:
@@ -295,24 +318,24 @@ class Postgres(DatabaseBase, Database):
if comment is not None:
defaults['comment'] = comment
plan_channel, created = await domain.PurchasePlanChannel.get_or_create(
purchase_plan_id=plan_id,
purchase_channel, created = await domain.PurchaseChannel.get_or_create(
purchase_id=purchase_id,
channel_id=channel_id,
defaults=defaults,
)
if not created and defaults:
for field, value in defaults.items():
setattr(plan_channel, field, value)
await plan_channel.save()
setattr(purchase_channel, field, value)
await purchase_channel.save()
return plan_channel
return purchase_channel
async def update_purchase_plan_channel(self, plan_channel: domain.PurchasePlanChannel) -> None:
await plan_channel.save()
async def update_purchase_channel(self, purchase_channel: domain.PurchaseChannel) -> None:
await purchase_channel.save()
async def remove_channel_from_purchase_plan(self, plan_id: uuid.UUID, channel_id: uuid.UUID) -> None:
await domain.PurchasePlanChannel.filter(purchase_plan_id=plan_id, channel_id=channel_id).delete()
async def remove_channel_from_purchase(self, purchase_id: uuid.UUID, channel_id: uuid.UUID) -> None:
await domain.PurchaseChannel.filter(purchase_id=purchase_id, channel_id=channel_id).delete()
async def get_creative(self, workspace_id: uuid.UUID, creative_id: uuid.UUID) -> domain.Creative | None:
return await domain.Creative.get_or_none(id=creative_id, workspace_id=workspace_id).prefetch_related(
@@ -485,27 +508,6 @@ class Postgres(DatabaseBase, Database):
return results
async def get_telegram_state(self, telegram_id: int) -> domain.TelegramState | None:
return await domain.TelegramState.get_or_none(telegram_id=telegram_id)
async def set_telegram_state(
self, telegram_id: int, state: domain.TelegramStateEnum, context: dict[str, typing.Any]
) -> domain.TelegramState:
existing = await self.get_telegram_state(telegram_id)
if existing:
existing.state = state
existing.context = context
existing.updated_at = timezone.now()
await existing.save()
return existing
new_state = await domain.TelegramState.create(telegram_id=telegram_id, state=state, context=context)
return new_state
async def clear_telegram_state(self, telegram_id: int) -> None:
await domain.TelegramState.filter(telegram_id=telegram_id).delete()
# Count methods
async def count_placements_by_creative(self, creative_id: uuid.UUID) -> int:
return await domain.Placement.filter(creative_id=creative_id).count()