feat(D): Phase D — Undo/Restore komplett implementiert
Check Cross-Plugin Imports / check (push) Has been cancelled

- D-GEN: RestoreRegistry mit RestoreConfig (model_class, restore_permission, excluded_fields, special_handler)
- D-HOOK: history_hooks.py mit register_history_hooks() für after_create/update/delete
- D-CORE: Company create+update record_history in companies.py
- D-PLUG: Task/Calendar/DMS record_history in services/routes
- D-SOFT: Alle registrierten Entitäten haben deleted_at + un-delete via Registry
- D-MAIL: Mail special_handler (IMAP Trash-Move, Folder-Verify) + record_history in delete/move
- D-TRASH: GET /entity-history/trash (filterbar, paginiert) + Frontend Trash.tsx
- D-TOAST: UndoToast.tsx (5s Auto-Dismiss, useUndoToast Hook)
- D-HIST-UI: HistoryPanel.tsx (Timeline, Diff-View, Restore-Button)
- D-BULK: POST /entity-history/bulk-restore mit partial_success Semantik
- D-RET: POST /entity-history/retention/archive (GDPR hard-delete >90 Tage)
- D-TEST: 26 Tests in test_restore_registry.py, alle grün
- D-DOC: test-strategy.md + security_kernel.md aktualisiert

Backend: 10 Dateien, Frontend: 7 Dateien, Tests: 1 Datei, Docs: 3 Dateien
26/26 Tests passed, TSC 0 errors, App import 492 routes
This commit is contained in:
Agent Zero
2026-08-13 23:08:29 +02:00
parent 29410f19d3
commit a4d0f0c35d
22 changed files with 2175 additions and 91 deletions
+220 -66
View File
@@ -107,8 +107,13 @@ async def restore_from_history(
For 'update' actions: revert entity fields to snapshot_before.
For 'create' actions: soft-delete the entity (undo creation).
Uses the RestoreRegistry to find the entity configuration.
Only explicitly registered entity types can be restored.
Returns the restored data dict.
"""
from app.core.restore_registry import get_restore_registry
entry = await get_history_entry(db, tenant_id, history_id, user_id, is_system_admin)
if entry is None:
raise ValueError("History entry not found")
@@ -117,76 +122,105 @@ async def restore_from_history(
entity_id = entry.entity_id
action = entry.action
# Import here to avoid circular imports
from app.models.contact import Contact
# Look up restore configuration from registry
config = get_restore_registry().get(entity_type)
if config is None:
raise ValueError(f"Unsupported entity type for restore: {entity_type}")
# Load entity from database
model_class = config.model_class
q = select(model_class).where(
model_class.id == entity_id,
model_class.tenant_id == tenant_id,
)
result = await db.execute(q)
entity = result.scalar_one_or_none()
# Use special handler if registered (e.g. Mail with IMAP semantics)
if config.special_handler is not None:
context = {
"user_id": user_id,
"tenant_id": tenant_id,
"is_system_admin": is_system_admin,
"history_entry": entry,
}
snapshot = entry.snapshot_before if entry.snapshot_before else {}
return await config.special_handler(db, entity, action, snapshot, context)
# Generic restore logic
from datetime import datetime, timezone
if action == "delete":
if entity is None:
raise ValueError("Entity not found for restore")
entity.deleted_at = None
if hasattr(entity, "updated_by"):
entity.updated_by = user_id
await db.flush()
return _serialize_entity(entity, entity_type)
elif action == "update":
if entity is None:
raise ValueError("Entity not found for restore")
if entry.snapshot_before is None:
raise ValueError("No snapshot_before available for restore")
excluded = config.all_excluded_fields
for key, value in entry.snapshot_before.items():
if hasattr(entity, key) and key not in excluded:
setattr(entity, key, value)
if hasattr(entity, "updated_by"):
entity.updated_by = user_id
await db.flush()
return _serialize_entity(entity, entity_type)
elif action == "create":
if entity is None:
raise ValueError("Entity not found for restore")
entity.deleted_at = datetime.now(timezone.utc)
if hasattr(entity, "updated_by"):
entity.updated_by = user_id
await db.flush()
return _serialize_entity(entity, entity_type)
raise ValueError(f"Unsupported action for restore: {action}")
def _serialize_entity(entity: Any, entity_type: str) -> dict[str, Any]:
"""Serialize an entity to a dict for restore response.
For contacts, uses the detailed serializer with contact_persons.
For other entities, uses a generic column-based serializer.
"""
if entity_type == "contact":
q = select(Contact).where(
Contact.id == entity_id,
Contact.tenant_id == tenant_id,
from app.services.contact_service import _serialize_contact_detail
from sqlalchemy.orm import selectinload
from app.models.contact import Contact
from sqlalchemy import select as sa_select
# Re-query with selectinload for contact_persons
q = (
sa_select(Contact)
.options(selectinload(Contact.contact_persons))
.where(Contact.id == entity.id)
)
result = await db.execute(q)
contact = result.scalar_one_or_none()
# This is called after flush, entity is still in session
# but we need to reload with the relationship
return _serialize_contact_detail(entity)
if action == "delete":
# Un-delete: clear deleted_at
if contact is None:
raise ValueError("Entity not found for restore")
contact.deleted_at = None
contact.updated_by = user_id
await db.flush()
from app.services.contact_service import _serialize_contact_detail
from sqlalchemy.orm import selectinload
q2 = (
select(Contact)
.options(selectinload(Contact.contact_persons))
.where(Contact.id == contact.id)
)
result2 = await db.execute(q2)
contact = result2.scalar_one()
return _serialize_contact_detail(contact)
elif action == "update":
# Revert to snapshot_before
if contact is None:
raise ValueError("Entity not found for restore")
if entry.snapshot_before is None:
raise ValueError("No snapshot_before available for restore")
for key, value in entry.snapshot_before.items():
if hasattr(contact, key) and key not in ("id", "tenant_id", "created_at", "updated_at", "deleted_at"):
setattr(contact, key, value)
contact.updated_by = user_id
await db.flush()
from app.services.contact_service import _serialize_contact_detail
from sqlalchemy.orm import selectinload
q2 = (
select(Contact)
.options(selectinload(Contact.contact_persons))
.where(Contact.id == contact.id)
)
result2 = await db.execute(q2)
contact = result2.scalar_one()
return _serialize_contact_detail(contact)
elif action == "create":
# Undo creation: soft-delete the entity
if contact is None:
raise ValueError("Entity not found for restore")
contact.deleted_at = datetime.now(timezone.utc)
contact.updated_by = user_id
await db.flush()
from app.services.contact_service import _serialize_contact_detail
from sqlalchemy.orm import selectinload
q2 = (
select(Contact)
.options(selectinload(Contact.contact_persons))
.where(Contact.id == contact.id)
)
result2 = await db.execute(q2)
contact = result2.scalar_one()
return _serialize_contact_detail(contact)
raise ValueError(f"Unsupported entity type for restore: {entity_type}")
# Generic serialization: extract column values
result: dict[str, Any] = {}
for column in entity.__table__.columns:
val = getattr(entity, column.name, None)
if val is not None:
if hasattr(val, "isoformat"):
result[column.name] = val.isoformat()
elif hasattr(val, "hex"):
result[column.name] = str(val)
else:
result[column.name] = val
result["_entity_type"] = entity_type
result["_restored"] = True
return result
async def undo_last_action(
@@ -222,3 +256,123 @@ async def undo_last_action(
raise ValueError("No history found for this entity")
return await restore_from_history(db, tenant_id, entry.id, user_id, is_system_admin)
async def list_trash(
db: AsyncSession,
tenant_id: uuid.UUID,
entity_type: str | None = None,
limit: int = 50,
offset: int = 0,
user_id: uuid.UUID | None = None,
is_system_admin: bool = False,
) -> dict[str, Any]:
"""List deleted entities from history (delete actions only).
Returns paginated trash items with entity_type filter.
"""
q = (
select(EntityHistory)
.where(
EntityHistory.tenant_id == tenant_id,
EntityHistory.action == "delete",
)
.order_by(EntityHistory.created_at.desc())
)
if entity_type:
q = q.where(EntityHistory.entity_type == entity_type)
if user_id and not is_system_admin:
q = await apply_visibility_filter(
db, q, "entity_history", EntityHistory, user_id, tenant_id, is_system_admin
)
# Count total
from sqlalchemy import func as sa_func
count_q = select(sa_func.count()).select_from(q.subquery())
total = (await db.execute(count_q)).scalar() or 0
# Paginate
q = q.offset(offset).limit(limit)
result = await db.execute(q)
entries = result.scalars().all()
items = []
for e in entries:
items.append({
"history_id": str(e.id),
"entity_type": e.entity_type,
"entity_id": str(e.entity_id),
"snapshot_before": e.snapshot_before,
"deleted_at": e.created_at.isoformat() if e.created_at else None,
"user_id": str(e.user_id) if e.user_id else None,
})
return {"items": items, "total": total, "limit": limit, "offset": offset}
async def bulk_restore(
db: AsyncSession,
tenant_id: uuid.UUID,
history_ids: list[uuid.UUID],
user_id: uuid.UUID,
is_system_admin: bool = False,
) -> dict[str, Any]:
"""Restore multiple entities from history entries.
Returns partial_success semantics: if some fail, others still succeed.
"""
results = []
succeeded = 0
failed = 0
for hid in history_ids:
try:
restored = await restore_from_history(db, tenant_id, hid, user_id, is_system_admin)
succeeded += 1
results.append({
"history_id": str(hid),
"success": True,
"entity_type": restored.get("_entity_type") or restored.get("entity_type"),
"entity_id": restored.get("id"),
"error": None,
})
except Exception as exc:
failed += 1
results.append({
"history_id": str(hid),
"success": False,
"entity_type": None,
"entity_id": None,
"error": str(exc),
})
return {
"total": len(history_ids),
"succeeded": succeeded,
"failed": failed,
"results": results,
"partial_success": failed > 0 and succeeded > 0,
}
async def archive_old_history(
db: AsyncSession,
tenant_id: uuid.UUID,
days: int = 90,
) -> int:
"""Archive (hard-delete) EntityHistory entries older than *days* days.
GDPR compliance: after retention period, history snapshots are purged.
Returns the number of archived entries.
"""
from datetime import datetime, timezone, timedelta
from sqlalchemy import delete as sa_delete
cutoff = datetime.now(timezone.utc) - timedelta(days=days)
stmt = sa_delete(EntityHistory).where(
EntityHistory.tenant_id == tenant_id,
EntityHistory.created_at < cutoff,
)
result = await db.execute(stmt)
await db.flush()
return result.rowcount or 0