Some checks failed
Build and Deploy / build-and-deploy (push) Has been cancelled
Phase 1 - Relations service (internal): owns ContainerRelation, exposes get-children data + attach/detach-child actions. Retargeted events, blog, market callers from cart to relations. Phase 2 - Likes service (internal): unified Like model replaces ProductLike and PostLike with generic target_type/target_slug/target_id. Exposes is-liked, liked-slugs, liked-ids data + toggle action. Phase 3 - PageConfig → blog: moved ownership to blog with direct DB queries, removed proxy endpoints from cart. Phase 4 - Orders service (public): owns Order/OrderItem + SumUp checkout flow. Cart checkout now delegates to orders via create-order action. Webhook/return routes and reconciliation moved to orders. Phase 5 - Infrastructure: docker-compose, deploy.sh, Dockerfiles updated for all 3 new services. Added orders_url helper and factory model imports. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
72 lines
2.4 KiB
Python
72 lines
2.4 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
import unicodedata
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from shared.contracts.dtos import MarketPlaceDTO
|
|
from shared.infrastructure.actions import call_action, ActionError
|
|
from shared.infrastructure.data_client import fetch_data
|
|
from shared.services.registry import services
|
|
|
|
|
|
class MarketError(ValueError):
|
|
"""Base error for market service operations."""
|
|
|
|
|
|
def slugify(value: str, max_len: int = 255) -> str:
|
|
if value is None:
|
|
value = ""
|
|
value = unicodedata.normalize("NFKD", value)
|
|
value = value.encode("ascii", "ignore").decode("ascii")
|
|
value = value.lower()
|
|
value = value.replace("/", "-")
|
|
value = re.sub(r"[^a-z0-9]+", "-", value)
|
|
value = re.sub(r"-{2,}", "-", value)
|
|
value = value.strip("-")[:max_len].strip("-")
|
|
return value or "market"
|
|
|
|
|
|
async def create_market(sess: AsyncSession, post_id: int, name: str) -> MarketPlaceDTO:
|
|
name = (name or "").strip()
|
|
if not name:
|
|
raise MarketError("Market name must not be empty.")
|
|
slug = slugify(name)
|
|
|
|
post = await services.blog.get_post_by_id(sess, post_id)
|
|
if not post:
|
|
raise MarketError(f"Post {post_id} does not exist.")
|
|
|
|
if not post.is_page:
|
|
raise MarketError("Markets can only be created on pages, not posts.")
|
|
|
|
raw_pc = await fetch_data("blog", "page-config",
|
|
params={"container_type": "page", "container_id": post_id},
|
|
required=False)
|
|
if raw_pc is None or not (raw_pc.get("features") or {}).get("market"):
|
|
raise MarketError("Market feature is not enabled for this page. Enable it in page settings first.")
|
|
|
|
try:
|
|
result = await call_action("market", "create-marketplace", payload={
|
|
"container_type": "page", "container_id": post_id,
|
|
"name": name, "slug": slug,
|
|
})
|
|
return MarketPlaceDTO(**result)
|
|
except ActionError as e:
|
|
raise MarketError(str(e)) from e
|
|
|
|
|
|
async def soft_delete_market(sess: AsyncSession, post_slug: str, market_slug: str) -> bool:
|
|
post = await services.blog.get_post_by_slug(sess, post_slug)
|
|
if not post:
|
|
return False
|
|
|
|
try:
|
|
result = await call_action("market", "soft-delete-marketplace", payload={
|
|
"container_type": "page", "container_id": post.id, "slug": market_slug,
|
|
})
|
|
return result.get("deleted", False)
|
|
except ActionError:
|
|
return False
|