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
glue/services/order_lifecycle.py
giles 044fdd61e1 Phase 5: Add order lifecycle, cart adoption glue services and handlers
- order_lifecycle.py: claim/confirm/get entries for order (cross-domain writes)
- cart_adoption.py: adopt anonymous cart+entries for logged-in user
- login_handlers.py: handle user.logged_in event for cart adoption
- order_handlers.py: placeholder handlers for order.created/paid events
- setup.py: register new handlers at startup

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-14 17:35:04 +00:00

84 lines
2.5 KiB
Python

"""Glue service: cross-domain writes for order ↔ calendar-entry bridging.
These run in the *same* DB transaction as the caller — not event-driven —
because order creation and payment confirmation need immediate consistency.
"""
from __future__ import annotations
from sqlalchemy import select, update
from sqlalchemy.ext.asyncio import AsyncSession
from events.models.calendars import CalendarEntry, Calendar
async def claim_entries_for_order(
session: AsyncSession,
order_id: int,
user_id: int | None,
session_id: str | None,
page_post_id: int | None = None,
) -> None:
"""Mark pending CalendarEntries as 'ordered' and set order_id."""
filters = [
CalendarEntry.deleted_at.is_(None),
CalendarEntry.state == "pending",
]
if user_id is not None:
filters.append(CalendarEntry.user_id == user_id)
elif session_id is not None:
filters.append(CalendarEntry.session_id == session_id)
if page_post_id is not None:
cal_ids = select(Calendar.id).where(
Calendar.container_type == "page",
Calendar.container_id == page_post_id,
Calendar.deleted_at.is_(None),
).scalar_subquery()
filters.append(CalendarEntry.calendar_id.in_(cal_ids))
await session.execute(
update(CalendarEntry)
.where(*filters)
.values(state="ordered", order_id=order_id)
)
async def confirm_entries_for_order(
session: AsyncSession,
order_id: int,
user_id: int | None,
session_id: str | None,
) -> None:
"""Mark ordered CalendarEntries as 'provisional'. Called when payment confirms."""
filters = [
CalendarEntry.deleted_at.is_(None),
CalendarEntry.state == "ordered",
CalendarEntry.order_id == order_id,
]
if user_id is not None:
filters.append(CalendarEntry.user_id == user_id)
elif session_id is not None:
filters.append(CalendarEntry.session_id == session_id)
await session.execute(
update(CalendarEntry)
.where(*filters)
.values(state="provisional")
)
async def get_entries_for_order(
session: AsyncSession,
order_id: int,
) -> list[CalendarEntry]:
"""Return CalendarEntries for an order. Replaces Order.calendar_entries relationship."""
result = await session.execute(
select(CalendarEntry).where(
CalendarEntry.order_id == order_id,
CalendarEntry.deleted_at.is_(None),
)
)
return list(result.scalars().all())