All checks were successful
Deploy Development / deploy (push) Successful in 44s
Test Suite / pytest-backend (push) Successful in 1m19s
Test Suite / lint-backend (push) Successful in 2s
Test Suite / compose-smoke (push) Has been skipped
Test Suite / k6 /api/health Baseline (push) Successful in 18s
Test Suite / playwright-smoke (push) Successful in 11s
Persistierter Standard Lifecycle pro Initiative, backend/steering/ Skeleton, Attention-Refactor und Hook-Dispatch als Basis für Method Registry. Co-authored-by: Cursor <cursoragent@cursor.com>
352 lines
10 KiB
Python
352 lines
10 KiB
Python
"""Review service — tenant-scoped CRUD (AP0.9d)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
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
|
|
|
|
ReviewStatus = Literal["planned", "completed", "skipped"]
|
|
|
|
REVIEW_STATUSES = frozenset({"planned", "completed", "skipped"})
|
|
|
|
_REVIEW_COLUMNS = """
|
|
id, tenant_id, initiative_id, action_id, milestone_id, title, summary,
|
|
status, due_at, reviewed_by_actor_id, created_at, updated_at
|
|
"""
|
|
|
|
|
|
def _serialize_row(row: dict[str, Any]) -> dict[str, Any]:
|
|
result = dict(row)
|
|
for key in (
|
|
"id",
|
|
"tenant_id",
|
|
"initiative_id",
|
|
"action_id",
|
|
"milestone_id",
|
|
"reviewed_by_actor_id",
|
|
):
|
|
if result.get(key):
|
|
result[key] = str(result[key])
|
|
for ts_key in ("created_at", "updated_at", "due_at"):
|
|
if result.get(ts_key):
|
|
result[ts_key] = result[ts_key].isoformat()
|
|
return result
|
|
|
|
|
|
def _validate_status(status: str) -> None:
|
|
if status not in REVIEW_STATUSES:
|
|
raise ValueError(f"Ungültiger Review-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 _milestone_in_initiative(
|
|
*, tenant_id: str, initiative_id: str, milestone_id: str
|
|
) -> bool:
|
|
conn = get_connection()
|
|
try:
|
|
with conn.cursor() as cur:
|
|
cur.execute(
|
|
"""
|
|
SELECT 1 FROM milestones
|
|
WHERE id = %s AND tenant_id = %s AND initiative_id = %s
|
|
""",
|
|
(milestone_id, tenant_id, initiative_id),
|
|
)
|
|
return cur.fetchone() is not None
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
def create_review(
|
|
*,
|
|
tenant_id: str,
|
|
initiative_id: str,
|
|
title: str,
|
|
summary: str = "",
|
|
status: ReviewStatus = "planned",
|
|
due_at: Optional[datetime] = None,
|
|
action_id: Optional[str] = None,
|
|
milestone_id: Optional[str] = None,
|
|
reviewed_by_actor_id: Optional[str] = None,
|
|
user_id: Optional[str] = None,
|
|
) -> 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 milestone_id and not _milestone_in_initiative(
|
|
tenant_id=tenant_id, initiative_id=initiative_id, milestone_id=milestone_id
|
|
):
|
|
raise ValueError("Meilenstein gehört nicht zum Vorhaben")
|
|
if reviewed_by_actor_id and not _actor_in_tenant(
|
|
tenant_id=tenant_id, actor_id=reviewed_by_actor_id
|
|
):
|
|
raise ValueError("Actor gehört nicht zum Tenant")
|
|
|
|
conn = get_connection()
|
|
try:
|
|
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
|
cur.execute(
|
|
f"""
|
|
INSERT INTO reviews (
|
|
tenant_id, initiative_id, action_id, milestone_id,
|
|
title, summary, status, due_at, reviewed_by_actor_id
|
|
)
|
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
|
|
RETURNING {_REVIEW_COLUMNS}
|
|
""",
|
|
(
|
|
tenant_id,
|
|
initiative_id,
|
|
action_id,
|
|
milestone_id,
|
|
title,
|
|
summary,
|
|
status,
|
|
due_at,
|
|
reviewed_by_actor_id,
|
|
),
|
|
)
|
|
row = _serialize_row(dict(cur.fetchone()))
|
|
conn.commit()
|
|
finally:
|
|
conn.close()
|
|
|
|
log_audit(
|
|
"review.created",
|
|
user_id=user_id,
|
|
tenant_id=tenant_id,
|
|
details={"review_id": row["id"], "initiative_id": initiative_id},
|
|
)
|
|
return row
|
|
|
|
|
|
def list_reviews_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(
|
|
f"""
|
|
SELECT {_REVIEW_COLUMNS}
|
|
FROM reviews
|
|
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_review(*, tenant_id: str, review_id: str) -> Optional[dict[str, Any]]:
|
|
conn = get_connection()
|
|
try:
|
|
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
|
cur.execute(
|
|
f"""
|
|
SELECT {_REVIEW_COLUMNS}
|
|
FROM reviews
|
|
WHERE id = %s AND tenant_id = %s
|
|
""",
|
|
(review_id, tenant_id),
|
|
)
|
|
row = cur.fetchone()
|
|
return _serialize_row(dict(row)) if row else None
|
|
finally:
|
|
conn.close()
|
|
|
|
|
|
def update_review(
|
|
*,
|
|
tenant_id: str,
|
|
review_id: str,
|
|
user_id: Optional[str] = None,
|
|
title: Optional[str] = None,
|
|
summary: Optional[str] = None,
|
|
status: Optional[ReviewStatus] = None,
|
|
due_at: Optional[datetime] = None,
|
|
clear_due_at: bool = False,
|
|
action_id: Optional[str] = None,
|
|
milestone_id: Optional[str] = None,
|
|
clear_action_id: bool = False,
|
|
clear_milestone_id: bool = False,
|
|
) -> Optional[dict[str, Any]]:
|
|
existing = get_review(tenant_id=tenant_id, review_id=review_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 summary is not None:
|
|
updates.append("summary = %s")
|
|
params.append(summary)
|
|
if status is not None:
|
|
_validate_status(status)
|
|
updates.append("status = %s")
|
|
params.append(status)
|
|
if clear_due_at:
|
|
updates.append("due_at = NULL")
|
|
elif due_at is not None:
|
|
updates.append("due_at = %s")
|
|
params.append(due_at)
|
|
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 clear_milestone_id:
|
|
updates.append("milestone_id = NULL")
|
|
elif milestone_id is not None:
|
|
if not _milestone_in_initiative(
|
|
tenant_id=tenant_id,
|
|
initiative_id=existing["initiative_id"],
|
|
milestone_id=milestone_id,
|
|
):
|
|
raise ValueError("Meilenstein gehört nicht zum Vorhaben")
|
|
updates.append("milestone_id = %s")
|
|
params.append(milestone_id)
|
|
|
|
if not updates:
|
|
return existing
|
|
|
|
updates.append("updated_at = NOW()")
|
|
params.extend([review_id, tenant_id])
|
|
|
|
conn = get_connection()
|
|
try:
|
|
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
|
cur.execute(
|
|
f"""
|
|
UPDATE reviews
|
|
SET {", ".join(updates)}
|
|
WHERE id = %s AND tenant_id = %s
|
|
RETURNING {_REVIEW_COLUMNS}
|
|
""",
|
|
params,
|
|
)
|
|
row = cur.fetchone()
|
|
if not row:
|
|
return None
|
|
result = _serialize_row(dict(row))
|
|
conn.commit()
|
|
finally:
|
|
conn.close()
|
|
|
|
log_audit(
|
|
"review.updated",
|
|
user_id=user_id,
|
|
tenant_id=tenant_id,
|
|
details={"review_id": review_id},
|
|
)
|
|
if status is not None and status != old_status:
|
|
log_audit(
|
|
"review.status_changed",
|
|
user_id=user_id,
|
|
tenant_id=tenant_id,
|
|
details={
|
|
"review_id": review_id,
|
|
"from_status": old_status,
|
|
"to_status": status,
|
|
},
|
|
)
|
|
from services.operating_transitions import after_review_status_change
|
|
|
|
after_review_status_change(
|
|
tenant_id=tenant_id,
|
|
review_id=review_id,
|
|
action_id=result.get("action_id"),
|
|
initiative_id=result.get("initiative_id"),
|
|
old_status=old_status,
|
|
new_status=status,
|
|
user_id=user_id,
|
|
)
|
|
return result
|
|
|
|
|
|
def delete_review(
|
|
*,
|
|
tenant_id: str,
|
|
review_id: str,
|
|
user_id: Optional[str] = None,
|
|
) -> bool:
|
|
conn = get_connection()
|
|
try:
|
|
with conn.cursor() as cur:
|
|
cur.execute(
|
|
"DELETE FROM reviews WHERE id = %s AND tenant_id = %s RETURNING id",
|
|
(review_id, tenant_id),
|
|
)
|
|
deleted = cur.fetchone() is not None
|
|
conn.commit()
|
|
finally:
|
|
conn.close()
|
|
|
|
if deleted:
|
|
log_audit(
|
|
"review.deleted",
|
|
user_id=user_id,
|
|
tenant_id=tenant_id,
|
|
details={"review_id": review_id},
|
|
)
|
|
return deleted
|