feat(work-orders): synchronize peripheral changes through events

This commit is contained in:
2026-10-08 11:50:15 +08:00
parent afcbd52a5a
commit b70283c2ff
8 changed files with 600 additions and 13 deletions
+25 -1
View File
@@ -1,4 +1,4 @@
"""PostgreSQL persistence isolated to peripheral_sample, never existing business writes."""
"""Peripheral persistence and transactional work-order change signals."""
from contextlib import contextmanager
from copy import deepcopy
from datetime import datetime, timedelta, timezone
@@ -306,8 +306,31 @@ class PostgresStore:
conn.execute("""INSERT INTO peripheral_sample.work_order_revisions
(project_id,work_order_id,revision,document) VALUES (%s,%s,%s,%s)""",
(who.project_id, document["work_order_id"], document["revision"], Jsonb(document)))
self._work_order_event(conn, who, document)
return document
def _work_order_event(self, conn, who: Identity, document: dict[str, Any], *, deleted: bool = False) -> None:
# This rollout is restricted to the provisioned tjwater_v2 event log.
# Task projections remain owned by the Server's authenticated APIs.
if self.settings.db_name != "tjwater_v2":
return
metadata = {
"source": "peripheral_sample",
"work_order_id": document["work_order_id"],
"change": "deleted" if deleted else "created" if document["revision"] == 1 else "updated",
}
row = conn.execute(
"""INSERT INTO workbench.domain_event_log
(project_id,event_id,topic,aggregate_type,aggregate_id,
aggregate_revision,occurred_at,metadata)
VALUES (%s,%s,'workbench.work_order.changed','peripheral_work_order',%s,%s,%s,%s)
RETURNING event_seq""",
(who.project_id, uuid4(), document["work_order_id"], document["revision"],
document["updated_at"], Jsonb(metadata)),
).fetchone()
conn.execute("SELECT pg_notify('workbench_events', %s)",
(f"{who.project_id}:{row['event_seq']}",))
def list_work_orders(self, who: Identity, limit: int, cursor: UUID | None = None) -> dict[str, Any]:
return self._list_managed(who, "work_orders", "work_order_id", limit, cursor)
@@ -385,6 +408,7 @@ class PostgresStore:
conn.execute("""INSERT INTO peripheral_sample.work_order_revisions
(project_id,work_order_id,revision,document) VALUES (%s,%s,%s,%s)""",
(who.project_id, work_order_id, revision, Jsonb(tombstone)))
self._work_order_event(conn, who, tombstone, deleted=True)
return result
return self._mutate(who, f"delete_work_order:{work_order_id}", command, write)