42 lines
1.0 KiB
Python
42 lines
1.0 KiB
Python
"""履约状态 Outbox 写入。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from typing import Any
|
||
|
||
from sqlalchemy.orm import Session
|
||
|
||
from ...models import (
|
||
PlatformFulfillmentTask,
|
||
)
|
||
from .order_site_notifier import enqueue_order_site_binding_state
|
||
from .views import customer_binding_out
|
||
|
||
|
||
def _emit_order_site_binding_state(
|
||
db: Session,
|
||
task_id: int,
|
||
*,
|
||
extra: dict[str, Any] | None = None,
|
||
) -> bool:
|
||
"""当前事务内写 Outbox;HTTP 投递由独立 worker 完成。"""
|
||
task = (
|
||
db.query(PlatformFulfillmentTask)
|
||
.filter(PlatformFulfillmentTask.id == int(task_id))
|
||
.with_for_update()
|
||
.one()
|
||
)
|
||
next_state_version = int(task.state_version or 0) + 1
|
||
payload = customer_binding_out(db, task_id)
|
||
payload["stateVersion"] = next_state_version
|
||
if extra:
|
||
payload.update(extra)
|
||
enqueued = enqueue_order_site_binding_state(
|
||
db,
|
||
payload,
|
||
)
|
||
if enqueued:
|
||
task.state_version = next_state_version
|
||
db.flush()
|
||
return enqueued
|