AP0.9: Operating Model Extension II (Evidence, Decision, Review, Recurring, due_at)
Deploy Development / deploy (push) Successful in 43s
Test Suite / pytest-backend (push) Successful in 1m6s
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 21s
Test Suite / playwright-smoke (push) Successful in 14s
Deploy Development / deploy (push) Successful in 43s
Test Suite / pytest-backend (push) Successful in 1m6s
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 21s
Test Suite / playwright-smoke (push) Successful in 14s
Schließt den zweiten OM-Slice entlang der Roadmap: neue Entitäten parallel im Vorhaben, erweiterte Action-Status/Fälligkeit und Attention-Regeln 7–9. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
c9101db1e1
commit
a025542545
36 files changed
+3178
-37
No files matched your search
+35
-19
@@ -10,10 +10,21 @@ from db import get_connection
|
||||
from services.audit import log_audit
|
||||
from services.initiatives import PRIORITIES, get_initiative
|
||||
|
||||
ActionStatus = Literal["open", "in_progress", "blocked", "done", "discarded"]
|
||||
ActionStatus = Literal[
|
||||
"open", "ready", "in_progress", "blocked", "review_required", "done", "discarded"
|
||||
]
|
||||
|
||||
ACTION_STATUSES = frozenset({"open", "in_progress", "blocked", "done", "discarded"})
|
||||
OPEN_ACTION_STATUSES = frozenset({"open", "in_progress", "blocked"})
|
||||
ACTION_STATUSES = frozenset(
|
||||
{"open", "ready", "in_progress", "blocked", "review_required", "done", "discarded"}
|
||||
)
|
||||
OPEN_ACTION_STATUSES = frozenset(
|
||||
{"open", "ready", "in_progress", "blocked", "review_required"}
|
||||
)
|
||||
|
||||
_ACTION_COLUMNS = """
|
||||
id, tenant_id, initiative_id, title, description,
|
||||
status, priority, due_at, created_at, updated_at
|
||||
"""
|
||||
|
||||
|
||||
def _serialize_row(row: dict[str, Any]) -> dict[str, Any]:
|
||||
@@ -25,6 +36,8 @@ def _serialize_row(row: dict[str, Any]) -> dict[str, Any]:
|
||||
result["created_at"] = result["created_at"].isoformat()
|
||||
if result.get("updated_at"):
|
||||
result["updated_at"] = result["updated_at"].isoformat()
|
||||
if result.get("due_at"):
|
||||
result["due_at"] = result["due_at"].isoformat()
|
||||
return result
|
||||
|
||||
|
||||
@@ -94,6 +107,7 @@ def create_action(
|
||||
description: str = "",
|
||||
status: ActionStatus = "open",
|
||||
priority: str = "normal",
|
||||
due_at: Optional[Any] = None,
|
||||
assigned_actor_ids: Optional[list[str]] = None,
|
||||
user_id: Optional[str] = None,
|
||||
) -> dict[str, Any]:
|
||||
@@ -114,15 +128,14 @@ def create_action(
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
f"""
|
||||
INSERT INTO actions (
|
||||
tenant_id, initiative_id, title, description, status, priority
|
||||
tenant_id, initiative_id, title, description, status, priority, due_at
|
||||
)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)
|
||||
RETURNING id, tenant_id, initiative_id, title, description,
|
||||
status, priority, created_at, updated_at
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s)
|
||||
RETURNING {_ACTION_COLUMNS}
|
||||
""",
|
||||
(tenant_id, initiative_id, title, description, status, priority),
|
||||
(tenant_id, initiative_id, title, description, status, priority, due_at),
|
||||
)
|
||||
row = _serialize_row(dict(cur.fetchone()))
|
||||
for actor_id in assigned_actor_ids:
|
||||
@@ -162,9 +175,8 @@ def list_actions_for_initiative(
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, tenant_id, initiative_id, title, description,
|
||||
status, priority, created_at, updated_at
|
||||
f"""
|
||||
SELECT {_ACTION_COLUMNS}
|
||||
FROM actions
|
||||
WHERE tenant_id = %s AND initiative_id = %s
|
||||
ORDER BY updated_at DESC, title
|
||||
@@ -182,9 +194,8 @@ def get_action(*, tenant_id: str, action_id: str) -> Optional[dict[str, Any]]:
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, tenant_id, initiative_id, title, description,
|
||||
status, priority, created_at, updated_at
|
||||
f"""
|
||||
SELECT {_ACTION_COLUMNS}
|
||||
FROM actions
|
||||
WHERE id = %s AND tenant_id = %s
|
||||
""",
|
||||
@@ -208,6 +219,8 @@ def update_action(
|
||||
description: Optional[str] = None,
|
||||
status: Optional[ActionStatus] = None,
|
||||
priority: Optional[str] = None,
|
||||
due_at: Optional[Any] = None,
|
||||
clear_due_at: bool = False,
|
||||
) -> Optional[dict[str, Any]]:
|
||||
existing = get_action(tenant_id=tenant_id, action_id=action_id)
|
||||
if not existing:
|
||||
@@ -234,6 +247,11 @@ def update_action(
|
||||
_validate_priority(priority)
|
||||
updates.append("priority = %s")
|
||||
params.append(priority)
|
||||
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 not updates:
|
||||
return existing
|
||||
@@ -249,15 +267,13 @@ def update_action(
|
||||
UPDATE actions
|
||||
SET {", ".join(updates)}
|
||||
WHERE id = %s AND tenant_id = %s
|
||||
RETURNING id, tenant_id, initiative_id, title, description,
|
||||
status, priority, created_at, updated_at
|
||||
RETURNING {_ACTION_COLUMNS}
|
||||
""",
|
||||
params,
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
return None
|
||||
result = _serialize_row(dict(row))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
@@ -374,7 +390,7 @@ def list_open_actions_for_actor(
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT a.id, a.tenant_id, a.initiative_id, a.title, a.description,
|
||||
a.status, a.priority, a.created_at, a.updated_at,
|
||||
a.status, a.priority, a.due_at, a.created_at, a.updated_at,
|
||||
i.title AS initiative_title
|
||||
FROM actions a
|
||||
JOIN action_assignments aa ON aa.action_id = a.id AND aa.tenant_id = a.tenant_id
|
||||
|
||||
@@ -0,0 +1,265 @@
|
||||
"""Decision service — tenant-scoped CRUD (AP0.9c)."""
|
||||
|
||||
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
|
||||
|
||||
DecisionStatus = Literal["proposed", "decided", "superseded"]
|
||||
|
||||
DECISION_STATUSES = frozenset({"proposed", "decided", "superseded"})
|
||||
|
||||
_DECISION_COLUMNS = """
|
||||
id, tenant_id, initiative_id, title, description, status, outcome,
|
||||
decided_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", "decided_by_actor_id"):
|
||||
if result.get(key):
|
||||
result[key] = str(result[key])
|
||||
for ts_key in ("created_at", "updated_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 DECISION_STATUSES:
|
||||
raise ValueError(f"Ungültiger Decision-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 create_decision(
|
||||
*,
|
||||
tenant_id: str,
|
||||
initiative_id: str,
|
||||
title: str,
|
||||
description: str = "",
|
||||
status: DecisionStatus = "proposed",
|
||||
outcome: str = "",
|
||||
decided_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 decided_by_actor_id and not _actor_in_tenant(
|
||||
tenant_id=tenant_id, actor_id=decided_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 decisions (
|
||||
tenant_id, initiative_id, title, description, status, outcome,
|
||||
decided_by_actor_id
|
||||
)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s)
|
||||
RETURNING {_DECISION_COLUMNS}
|
||||
""",
|
||||
(
|
||||
tenant_id,
|
||||
initiative_id,
|
||||
title,
|
||||
description,
|
||||
status,
|
||||
outcome,
|
||||
decided_by_actor_id,
|
||||
),
|
||||
)
|
||||
row = _serialize_row(dict(cur.fetchone()))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
log_audit(
|
||||
"decision.created",
|
||||
user_id=user_id,
|
||||
tenant_id=tenant_id,
|
||||
details={"decision_id": row["id"], "initiative_id": initiative_id},
|
||||
)
|
||||
return row
|
||||
|
||||
|
||||
def list_decisions_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 {_DECISION_COLUMNS}
|
||||
FROM decisions
|
||||
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_decision(*, tenant_id: str, decision_id: str) -> Optional[dict[str, Any]]:
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
f"""
|
||||
SELECT {_DECISION_COLUMNS}
|
||||
FROM decisions
|
||||
WHERE id = %s AND tenant_id = %s
|
||||
""",
|
||||
(decision_id, tenant_id),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
return _serialize_row(dict(row)) if row else None
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def update_decision(
|
||||
*,
|
||||
tenant_id: str,
|
||||
decision_id: str,
|
||||
user_id: Optional[str] = None,
|
||||
title: Optional[str] = None,
|
||||
description: Optional[str] = None,
|
||||
status: Optional[DecisionStatus] = None,
|
||||
outcome: Optional[str] = None,
|
||||
decided_by_actor_id: Optional[str] = None,
|
||||
) -> Optional[dict[str, Any]]:
|
||||
existing = get_decision(tenant_id=tenant_id, decision_id=decision_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 outcome is not None:
|
||||
updates.append("outcome = %s")
|
||||
params.append(outcome)
|
||||
if decided_by_actor_id is not None:
|
||||
if decided_by_actor_id and not _actor_in_tenant(
|
||||
tenant_id=tenant_id, actor_id=decided_by_actor_id
|
||||
):
|
||||
raise ValueError("Actor gehört nicht zum Tenant")
|
||||
updates.append("decided_by_actor_id = %s")
|
||||
params.append(decided_by_actor_id or None)
|
||||
|
||||
if not updates:
|
||||
return existing
|
||||
|
||||
updates.append("updated_at = NOW()")
|
||||
params.extend([decision_id, tenant_id])
|
||||
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
f"""
|
||||
UPDATE decisions
|
||||
SET {", ".join(updates)}
|
||||
WHERE id = %s AND tenant_id = %s
|
||||
RETURNING {_DECISION_COLUMNS}
|
||||
""",
|
||||
params,
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
return None
|
||||
result = _serialize_row(dict(row))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
log_audit(
|
||||
"decision.updated",
|
||||
user_id=user_id,
|
||||
tenant_id=tenant_id,
|
||||
details={"decision_id": decision_id},
|
||||
)
|
||||
if status is not None and status != old_status:
|
||||
log_audit(
|
||||
"decision.status_changed",
|
||||
user_id=user_id,
|
||||
tenant_id=tenant_id,
|
||||
details={
|
||||
"decision_id": decision_id,
|
||||
"from_status": old_status,
|
||||
"to_status": status,
|
||||
},
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
def delete_decision(
|
||||
*,
|
||||
tenant_id: str,
|
||||
decision_id: str,
|
||||
user_id: Optional[str] = None,
|
||||
) -> bool:
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"DELETE FROM decisions WHERE id = %s AND tenant_id = %s RETURNING id",
|
||||
(decision_id, tenant_id),
|
||||
)
|
||||
deleted = cur.fetchone() is not None
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
if deleted:
|
||||
log_audit(
|
||||
"decision.deleted",
|
||||
user_id=user_id,
|
||||
tenant_id=tenant_id,
|
||||
details={"decision_id": decision_id},
|
||||
)
|
||||
return deleted
|
||||
@@ -0,0 +1,318 @@
|
||||
"""Evidence service — tenant-scoped CRUD (AP0.9b)."""
|
||||
|
||||
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
|
||||
|
||||
EvidenceStatus = Literal["submitted", "accepted", "rejected"]
|
||||
|
||||
EVIDENCE_STATUSES = frozenset({"submitted", "accepted", "rejected"})
|
||||
|
||||
_EVIDENCE_COLUMNS = """
|
||||
id, tenant_id, initiative_id, action_id, milestone_id, title, description,
|
||||
status, submitted_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",
|
||||
"submitted_by_actor_id",
|
||||
):
|
||||
if result.get(key):
|
||||
result[key] = str(result[key])
|
||||
for ts_key in ("created_at", "updated_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 EVIDENCE_STATUSES:
|
||||
raise ValueError(f"Ungültiger Evidence-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_evidence(
|
||||
*,
|
||||
tenant_id: str,
|
||||
initiative_id: str,
|
||||
title: str,
|
||||
description: str = "",
|
||||
status: EvidenceStatus = "submitted",
|
||||
action_id: Optional[str] = None,
|
||||
milestone_id: Optional[str] = None,
|
||||
submitted_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 submitted_by_actor_id and not _actor_in_tenant(
|
||||
tenant_id=tenant_id, actor_id=submitted_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 evidence (
|
||||
tenant_id, initiative_id, action_id, milestone_id,
|
||||
title, description, status, submitted_by_actor_id
|
||||
)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
||||
RETURNING {_EVIDENCE_COLUMNS}
|
||||
""",
|
||||
(
|
||||
tenant_id,
|
||||
initiative_id,
|
||||
action_id,
|
||||
milestone_id,
|
||||
title,
|
||||
description,
|
||||
status,
|
||||
submitted_by_actor_id,
|
||||
),
|
||||
)
|
||||
row = _serialize_row(dict(cur.fetchone()))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
log_audit(
|
||||
"evidence.created",
|
||||
user_id=user_id,
|
||||
tenant_id=tenant_id,
|
||||
details={"evidence_id": row["id"], "initiative_id": initiative_id},
|
||||
)
|
||||
return row
|
||||
|
||||
|
||||
def list_evidence_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 {_EVIDENCE_COLUMNS}
|
||||
FROM evidence
|
||||
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_evidence(*, tenant_id: str, evidence_id: str) -> Optional[dict[str, Any]]:
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
f"""
|
||||
SELECT {_EVIDENCE_COLUMNS}
|
||||
FROM evidence
|
||||
WHERE id = %s AND tenant_id = %s
|
||||
""",
|
||||
(evidence_id, tenant_id),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
return _serialize_row(dict(row)) if row else None
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def update_evidence(
|
||||
*,
|
||||
tenant_id: str,
|
||||
evidence_id: str,
|
||||
user_id: Optional[str] = None,
|
||||
title: Optional[str] = None,
|
||||
description: Optional[str] = None,
|
||||
status: Optional[EvidenceStatus] = None,
|
||||
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_evidence(tenant_id=tenant_id, evidence_id=evidence_id)
|
||||
if not existing:
|
||||
return None
|
||||
|
||||
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 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([evidence_id, tenant_id])
|
||||
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
f"""
|
||||
UPDATE evidence
|
||||
SET {", ".join(updates)}
|
||||
WHERE id = %s AND tenant_id = %s
|
||||
RETURNING {_EVIDENCE_COLUMNS}
|
||||
""",
|
||||
params,
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
return None
|
||||
result = _serialize_row(dict(row))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
log_audit(
|
||||
"evidence.updated",
|
||||
user_id=user_id,
|
||||
tenant_id=tenant_id,
|
||||
details={"evidence_id": evidence_id},
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
def delete_evidence(
|
||||
*,
|
||||
tenant_id: str,
|
||||
evidence_id: str,
|
||||
user_id: Optional[str] = None,
|
||||
) -> bool:
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"DELETE FROM evidence WHERE id = %s AND tenant_id = %s RETURNING id",
|
||||
(evidence_id, tenant_id),
|
||||
)
|
||||
deleted = cur.fetchone() is not None
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
if deleted:
|
||||
log_audit(
|
||||
"evidence.deleted",
|
||||
user_id=user_id,
|
||||
tenant_id=tenant_id,
|
||||
details={"evidence_id": evidence_id},
|
||||
)
|
||||
return deleted
|
||||
@@ -0,0 +1,260 @@
|
||||
"""RecurringElement service — tenant-scoped CRUD (AP0.9e)."""
|
||||
|
||||
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
|
||||
|
||||
RecurringStatus = Literal["active", "paused", "ended"]
|
||||
|
||||
RECURRING_STATUSES = frozenset({"active", "paused", "ended"})
|
||||
|
||||
_RECURRING_COLUMNS = """
|
||||
id, tenant_id, initiative_id, title, description, status,
|
||||
interval_days, next_due_at, 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"):
|
||||
if result.get(key):
|
||||
result[key] = str(result[key])
|
||||
for ts_key in ("created_at", "updated_at", "next_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 RECURRING_STATUSES:
|
||||
raise ValueError(f"Ungültiger Recurring-Status: {status}")
|
||||
|
||||
|
||||
def create_recurring_element(
|
||||
*,
|
||||
tenant_id: str,
|
||||
initiative_id: str,
|
||||
title: str,
|
||||
description: str = "",
|
||||
status: RecurringStatus = "active",
|
||||
interval_days: Optional[int] = None,
|
||||
next_due_at: Optional[datetime] = None,
|
||||
user_id: Optional[str] = None,
|
||||
) -> dict[str, Any]:
|
||||
title = title.strip()
|
||||
if not title:
|
||||
raise ValueError("Titel ist erforderlich")
|
||||
_validate_status(status)
|
||||
if interval_days is not None and interval_days <= 0:
|
||||
raise ValueError("interval_days muss positiv sein")
|
||||
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"""
|
||||
INSERT INTO recurring_elements (
|
||||
tenant_id, initiative_id, title, description, status,
|
||||
interval_days, next_due_at
|
||||
)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s)
|
||||
RETURNING {_RECURRING_COLUMNS}
|
||||
""",
|
||||
(
|
||||
tenant_id,
|
||||
initiative_id,
|
||||
title,
|
||||
description,
|
||||
status,
|
||||
interval_days,
|
||||
next_due_at,
|
||||
),
|
||||
)
|
||||
row = _serialize_row(dict(cur.fetchone()))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
log_audit(
|
||||
"recurring.created",
|
||||
user_id=user_id,
|
||||
tenant_id=tenant_id,
|
||||
details={"recurring_id": row["id"], "initiative_id": initiative_id},
|
||||
)
|
||||
return row
|
||||
|
||||
|
||||
def list_recurring_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 {_RECURRING_COLUMNS}
|
||||
FROM recurring_elements
|
||||
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_recurring_element(
|
||||
*, tenant_id: str, recurring_id: str
|
||||
) -> Optional[dict[str, Any]]:
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
f"""
|
||||
SELECT {_RECURRING_COLUMNS}
|
||||
FROM recurring_elements
|
||||
WHERE id = %s AND tenant_id = %s
|
||||
""",
|
||||
(recurring_id, tenant_id),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
return _serialize_row(dict(row)) if row else None
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def update_recurring_element(
|
||||
*,
|
||||
tenant_id: str,
|
||||
recurring_id: str,
|
||||
user_id: Optional[str] = None,
|
||||
title: Optional[str] = None,
|
||||
description: Optional[str] = None,
|
||||
status: Optional[RecurringStatus] = None,
|
||||
interval_days: Optional[int] = None,
|
||||
clear_interval_days: bool = False,
|
||||
next_due_at: Optional[datetime] = None,
|
||||
clear_next_due_at: bool = False,
|
||||
) -> Optional[dict[str, Any]]:
|
||||
existing = get_recurring_element(tenant_id=tenant_id, recurring_id=recurring_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_interval_days:
|
||||
updates.append("interval_days = NULL")
|
||||
elif interval_days is not None:
|
||||
if interval_days <= 0:
|
||||
raise ValueError("interval_days muss positiv sein")
|
||||
updates.append("interval_days = %s")
|
||||
params.append(interval_days)
|
||||
if clear_next_due_at:
|
||||
updates.append("next_due_at = NULL")
|
||||
elif next_due_at is not None:
|
||||
updates.append("next_due_at = %s")
|
||||
params.append(next_due_at)
|
||||
|
||||
if not updates:
|
||||
return existing
|
||||
|
||||
updates.append("updated_at = NOW()")
|
||||
params.extend([recurring_id, tenant_id])
|
||||
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor(cursor_factory=RealDictCursor) as cur:
|
||||
cur.execute(
|
||||
f"""
|
||||
UPDATE recurring_elements
|
||||
SET {", ".join(updates)}
|
||||
WHERE id = %s AND tenant_id = %s
|
||||
RETURNING {_RECURRING_COLUMNS}
|
||||
""",
|
||||
params,
|
||||
)
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
return None
|
||||
result = _serialize_row(dict(row))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
log_audit(
|
||||
"recurring.updated",
|
||||
user_id=user_id,
|
||||
tenant_id=tenant_id,
|
||||
details={"recurring_id": recurring_id},
|
||||
)
|
||||
if status is not None and status != old_status:
|
||||
log_audit(
|
||||
"recurring.status_changed",
|
||||
user_id=user_id,
|
||||
tenant_id=tenant_id,
|
||||
details={
|
||||
"recurring_id": recurring_id,
|
||||
"from_status": old_status,
|
||||
"to_status": status,
|
||||
},
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
def delete_recurring_element(
|
||||
*,
|
||||
tenant_id: str,
|
||||
recurring_id: str,
|
||||
user_id: Optional[str] = None,
|
||||
) -> bool:
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
DELETE FROM recurring_elements
|
||||
WHERE id = %s AND tenant_id = %s RETURNING id
|
||||
""",
|
||||
(recurring_id, tenant_id),
|
||||
)
|
||||
deleted = cur.fetchone() is not None
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
if deleted:
|
||||
log_audit(
|
||||
"recurring.deleted",
|
||||
user_id=user_id,
|
||||
tenant_id=tenant_id,
|
||||
details={"recurring_id": recurring_id},
|
||||
)
|
||||
return deleted
|
||||
@@ -0,0 +1,340 @@
|
||||
"""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,
|
||||
},
|
||||
)
|
||||
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
|
||||
Reference in new issue
Block a user