This repository has been archived on 2026-02-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
cart/bp/cart/services/clear_cart_for_order.py
giles 8527ddb84b
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 50s
Decouple cart: use shared.models for all cross-app imports
- Replace all imports from blog.models, market.models, events.models
  and bare models.* with shared.models equivalents
- Convert cart/models/order.py and page_config.py to re-export stubs
- Update shared + glue submodule pointers

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-18 20:58:10 +00:00

38 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from sqlalchemy import update, func, select
from shared.models.market import CartItem
from shared.models.market_place import MarketPlace
from shared.models.order import Order
async def clear_cart_for_order(session, order: Order, *, page_post_id: int | None = None) -> None:
"""
Soft-delete CartItem rows belonging to this order's user_id/session_id.
When *page_post_id* is given, only items whose market_place belongs to
that page are cleared. Otherwise all items are cleared (legacy behaviour).
"""
filters = [CartItem.deleted_at.is_(None)]
if order.user_id is not None:
filters.append(CartItem.user_id == order.user_id)
if order.session_id is not None:
filters.append(CartItem.session_id == order.session_id)
if len(filters) == 1:
# no user_id/session_id on order nothing to clear
return
if page_post_id is not None:
mp_ids = select(MarketPlace.id).where(
MarketPlace.container_type == "page",
MarketPlace.container_id == page_post_id,
MarketPlace.deleted_at.is_(None),
).scalar_subquery()
filters.append(CartItem.market_place_id.in_(mp_ids))
await session.execute(
update(CartItem)
.where(*filters)
.values(deleted_at=func.now())
)