Some checks failed
Deploy Development / deploy (push) Successful in 45s
Test Suite / pytest-backend (push) Failing after 1m10s
Test Suite / k6 /api/health Baseline (push) Has been skipped
Test Suite / playwright-smoke (push) Has been skipped
Test Suite / lint-backend (push) Successful in 1s
Test Suite / compose-smoke (push) Has been skipped
Beantwortet die Lücke zwischen parallelen Tabellen und späterem Steering Core: Graph-Read-Model, minimale Flows, sichtbarer Steuerungszustand. Co-authored-by: Cursor <cursoragent@cursor.com>
306 lines
9.4 KiB
Python
306 lines
9.4 KiB
Python
"""Blocker service — tenant-scoped CRUD (AP0.8b)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Literal, Optional
|
|
|
|
from psycopg2.extras import RealDictCursor
|
|
|
|
from db import get_connection
|
|
from services.audit import log_audit
|
|
from services.initiatives import get_initiative
|
|
|
|
BlockerStatus = Literal["open", "in_progress", "resolved", "accepted_risk", "dismissed"]
|
|
|
|
BLOCKER_STATUSES = frozenset(
|
|
{"open", "in_progress", "resolved", "accepted_risk", "dismissed"}
|
|
)
|
|
|
|
|
|
def _serialize_row(row: dict[str, Any]) -> dict[str, Any]:
|
|
result = dict(row)
|
|
for key in ("id", "tenant_id", "initiative_id", "action_id", "reported_by_actor_id"):
|
|
if result.get(key):
|
|
result[key] = str(result[key])
|
|
if result.get("created_at"):
|
|
result["created_at"] = result["created_at"].isoformat()
|
|
if result.get("updated_at"):
|
|
result["updated_at"] = result["updated_at"].isoformat()
|
|
return result
|
|
|
|
|
|
def _validate_status(status: str) -> None:
|
|
if status not in BLOCKER_STATUSES:
|
|
raise ValueError(f"Ungültiger Blocker-Status: {status}")
|
|
|
|
|
|
def _actor_in_tenant(*, tenant_id: str, actor_id: str) -> bool:
|
|
conn = get_connection()
|
|
try:
|
|
with conn.cursor() as cur:
|
|
cur.execute(
|
|
"SELECT 1 FROM actors WHERE id = %s AND tenant_id = %s AND is_active = TRUE",
|
|
(actor_id, tenant_id),
|
|
)
|
|
return cur.fetchone() is not None
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
def _action_in_initiative(*, tenant_id: str, initiative_id: str, action_id: str) -> bool:
|
|
conn = get_connection()
|
|
try:
|
|
with conn.cursor() as cur:
|
|
cur.execute(
|
|
"""
|
|
SELECT 1 FROM actions
|
|
WHERE id = %s AND tenant_id = %s AND initiative_id = %s
|
|
""",
|
|
(action_id, tenant_id, initiative_id),
|
|
)
|
|
return cur.fetchone() is not None
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
def create_blocker(
|
|
*,
|
|
tenant_id: str,
|
|
initiative_id: str,
|
|
title: str,
|
|
description: str = "",
|
|
status: BlockerStatus = "open",
|
|
action_id: Optional[str] = None,
|
|
reported_by_actor_id: Optional[str] = None,
|
|
user_id: Optional[str] = None,
|
|
set_action_blocked: bool = False,
|
|
) -> dict[str, Any]:
|
|
title = title.strip()
|
|
if not title:
|
|
raise ValueError("Titel ist erforderlich")
|
|
_validate_status(status)
|
|
if not get_initiative(tenant_id=tenant_id, initiative_id=initiative_id):
|
|
raise ValueError("Initiative nicht gefunden")
|
|
if action_id and not _action_in_initiative(
|
|
tenant_id=tenant_id, initiative_id=initiative_id, action_id=action_id
|
|
):
|
|
raise ValueError("Maßnahme gehört nicht zum Vorhaben")
|
|
if reported_by_actor_id and not _actor_in_tenant(
|
|
tenant_id=tenant_id, actor_id=reported_by_actor_id
|
|
):
|
|
raise ValueError("Reporter-Actor gehört nicht zum Tenant")
|
|
|
|
conn = get_connection()
|
|
try:
|
|
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
|
cur.execute(
|
|
"""
|
|
INSERT INTO blockers (
|
|
tenant_id, initiative_id, action_id, title, description,
|
|
status, reported_by_actor_id
|
|
)
|
|
VALUES (%s, %s, %s, %s, %s, %s, %s)
|
|
RETURNING id, tenant_id, initiative_id, action_id, title, description,
|
|
status, reported_by_actor_id, created_at, updated_at
|
|
""",
|
|
(
|
|
tenant_id,
|
|
initiative_id,
|
|
action_id,
|
|
title,
|
|
description,
|
|
status,
|
|
reported_by_actor_id,
|
|
),
|
|
)
|
|
row = _serialize_row(dict(cur.fetchone()))
|
|
if set_action_blocked and action_id:
|
|
cur.execute(
|
|
"""
|
|
UPDATE actions SET status = 'blocked', updated_at = NOW()
|
|
WHERE id = %s AND tenant_id = %s
|
|
""",
|
|
(action_id, tenant_id),
|
|
)
|
|
conn.commit()
|
|
finally:
|
|
conn.close()
|
|
|
|
log_audit(
|
|
"blocker.created",
|
|
user_id=user_id,
|
|
tenant_id=tenant_id,
|
|
details={"blocker_id": row["id"], "initiative_id": initiative_id, "title": title},
|
|
)
|
|
return row
|
|
|
|
|
|
def list_blockers_for_initiative(*, tenant_id: str, initiative_id: str) -> list[dict[str, Any]]:
|
|
if not get_initiative(tenant_id=tenant_id, initiative_id=initiative_id):
|
|
raise ValueError("Initiative nicht gefunden")
|
|
|
|
conn = get_connection()
|
|
try:
|
|
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
|
cur.execute(
|
|
"""
|
|
SELECT id, tenant_id, initiative_id, action_id, title, description,
|
|
status, reported_by_actor_id, created_at, updated_at
|
|
FROM blockers
|
|
WHERE tenant_id = %s AND initiative_id = %s
|
|
ORDER BY updated_at DESC, title
|
|
""",
|
|
(tenant_id, initiative_id),
|
|
)
|
|
return [_serialize_row(dict(r)) for r in cur.fetchall()]
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
def get_blocker(*, tenant_id: str, blocker_id: str) -> Optional[dict[str, Any]]:
|
|
conn = get_connection()
|
|
try:
|
|
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
|
cur.execute(
|
|
"""
|
|
SELECT id, tenant_id, initiative_id, action_id, title, description,
|
|
status, reported_by_actor_id, created_at, updated_at
|
|
FROM blockers
|
|
WHERE id = %s AND tenant_id = %s
|
|
""",
|
|
(blocker_id, tenant_id),
|
|
)
|
|
row = cur.fetchone()
|
|
return _serialize_row(dict(row)) if row else None
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
def update_blocker(
|
|
*,
|
|
tenant_id: str,
|
|
blocker_id: str,
|
|
user_id: Optional[str] = None,
|
|
title: Optional[str] = None,
|
|
description: Optional[str] = None,
|
|
status: Optional[BlockerStatus] = None,
|
|
action_id: Optional[str] = None,
|
|
clear_action_id: bool = False,
|
|
) -> Optional[dict[str, Any]]:
|
|
existing = get_blocker(tenant_id=tenant_id, blocker_id=blocker_id)
|
|
if not existing:
|
|
return None
|
|
|
|
old_status = existing["status"]
|
|
updates: list[str] = []
|
|
params: list[Any] = []
|
|
|
|
if title is not None:
|
|
title = title.strip()
|
|
if not title:
|
|
raise ValueError("Titel ist erforderlich")
|
|
updates.append("title = %s")
|
|
params.append(title)
|
|
if description is not None:
|
|
updates.append("description = %s")
|
|
params.append(description)
|
|
if status is not None:
|
|
_validate_status(status)
|
|
updates.append("status = %s")
|
|
params.append(status)
|
|
if clear_action_id:
|
|
updates.append("action_id = NULL")
|
|
elif action_id is not None:
|
|
if not _action_in_initiative(
|
|
tenant_id=tenant_id,
|
|
initiative_id=existing["initiative_id"],
|
|
action_id=action_id,
|
|
):
|
|
raise ValueError("Maßnahme gehört nicht zum Vorhaben")
|
|
updates.append("action_id = %s")
|
|
params.append(action_id)
|
|
|
|
if not updates:
|
|
return existing
|
|
|
|
updates.append("updated_at = NOW()")
|
|
params.extend([blocker_id, tenant_id])
|
|
|
|
conn = get_connection()
|
|
try:
|
|
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
|
cur.execute(
|
|
f"""
|
|
UPDATE blockers
|
|
SET {", ".join(updates)}
|
|
WHERE id = %s AND tenant_id = %s
|
|
RETURNING id, tenant_id, initiative_id, action_id, title, description,
|
|
status, reported_by_actor_id, created_at, updated_at
|
|
""",
|
|
params,
|
|
)
|
|
row = cur.fetchone()
|
|
if not row:
|
|
return None
|
|
result = _serialize_row(dict(row))
|
|
conn.commit()
|
|
finally:
|
|
conn.close()
|
|
|
|
log_audit(
|
|
"blocker.updated",
|
|
user_id=user_id,
|
|
tenant_id=tenant_id,
|
|
details={"blocker_id": blocker_id},
|
|
)
|
|
if status is not None and status != old_status:
|
|
log_audit(
|
|
"blocker.status_changed",
|
|
user_id=user_id,
|
|
tenant_id=tenant_id,
|
|
details={
|
|
"blocker_id": blocker_id,
|
|
"from_status": old_status,
|
|
"to_status": status,
|
|
},
|
|
)
|
|
from services.operating_transitions import after_blocker_status_change
|
|
|
|
after_blocker_status_change(
|
|
tenant_id=tenant_id,
|
|
blocker_id=blocker_id,
|
|
action_id=result.get("action_id"),
|
|
old_status=old_status,
|
|
new_status=status,
|
|
user_id=user_id,
|
|
)
|
|
return result
|
|
|
|
|
|
def delete_blocker(
|
|
*,
|
|
tenant_id: str,
|
|
blocker_id: str,
|
|
user_id: Optional[str] = None,
|
|
) -> bool:
|
|
conn = get_connection()
|
|
try:
|
|
with conn.cursor() as cur:
|
|
cur.execute(
|
|
"DELETE FROM blockers WHERE id = %s AND tenant_id = %s RETURNING id",
|
|
(blocker_id, tenant_id),
|
|
)
|
|
deleted = cur.fetchone() is not None
|
|
conn.commit()
|
|
finally:
|
|
conn.close()
|
|
|
|
if deleted:
|
|
log_audit(
|
|
"blocker.deleted",
|
|
user_id=user_id,
|
|
tenant_id=tenant_id,
|
|
details={"blocker_id": blocker_id},
|
|
)
|
|
return deleted
|