Files
exchange/offers/services.py
2026-01-07 09:12:35 +07:00

104 lines
3.4 KiB
Python

"""
Сервис для создания офферов через Temporal workflow.
Используется в Django admin action и в seed командах.
"""
import uuid
import logging
from dataclasses import dataclass
from decimal import Decimal
from typing import Optional, Tuple
from exchange.temporal_client import start_offer_workflow
logger = logging.getLogger(__name__)
@dataclass
class OfferData:
"""Данные для создания оффера"""
team_uuid: str
product_uuid: str
product_name: str
location_uuid: str
location_name: str
location_country: str
location_country_code: str
location_latitude: float
location_longitude: float
quantity: Decimal
unit: str = "ton"
price_per_unit: Optional[Decimal] = None
currency: str = "USD"
category_name: str = ""
description: str = ""
class OfferService:
"""Сервис для создания офферов через workflow"""
@staticmethod
def create_offer_via_workflow(data: OfferData) -> Tuple[str, str, str]:
"""
Создает оффер через Temporal workflow.
Returns:
Tuple[offer_uuid, workflow_id, run_id]
"""
offer_uuid = str(uuid.uuid4())
workflow_id, run_id = start_offer_workflow(
offer_uuid=offer_uuid,
team_uuid=data.team_uuid,
product_uuid=data.product_uuid,
product_name=data.product_name,
category_name=data.category_name,
location_uuid=data.location_uuid,
location_name=data.location_name,
location_country=data.location_country,
location_country_code=data.location_country_code,
location_latitude=data.location_latitude,
location_longitude=data.location_longitude,
quantity=data.quantity,
unit=data.unit,
price_per_unit=data.price_per_unit,
currency=data.currency,
description=data.description,
)
logger.info(f"Started offer workflow: {workflow_id} for offer {offer_uuid}")
return offer_uuid, workflow_id, run_id
@staticmethod
def resync_offer_via_workflow(offer) -> Tuple[str, str]:
"""
Пересоздает workflow для существующего оффера.
Используется для пере-синхронизации в граф.
Args:
offer: Offer model instance
Returns:
Tuple[workflow_id, run_id]
"""
workflow_id, run_id = start_offer_workflow(
offer_uuid=offer.uuid,
team_uuid=offer.team_uuid,
product_uuid=offer.product_uuid,
product_name=offer.product_name,
category_name=offer.category_name,
location_uuid=offer.location_uuid,
location_name=offer.location_name,
location_country=offer.location_country,
location_country_code=offer.location_country_code,
location_latitude=offer.location_latitude,
location_longitude=offer.location_longitude,
quantity=offer.quantity,
unit=offer.unit,
price_per_unit=offer.price_per_unit,
currency=offer.currency,
description=offer.description,
)
logger.info(f"Restarted offer workflow: {workflow_id} for offer {offer.uuid}")
return workflow_id, run_id