feat(phase5.1+6): fallback material cleanup + notification batch refactor

Phase 5.1 — MATERIAL_PALETTE removal:
- Remove MATERIAL_PALETTE + _material_to_color() from step_processor.py
- build_part_colors() now returns {part→material_name} for Blender resolver

Phase 6 — Notification Center Refactor:
- Migration 051: add channel (activity|notification|alert) to audit_log,
  add frequency (immediate|daily|never) to notification_configs
- Three notification channels: activity (per-render), notification (batch
  order summaries), alert (admin infrastructure)
- Per-render emit_notification_sync calls demoted to channel=activity
- New emit_batch_render_notification_sync(): single summary notification
  when all order lines reach terminal state ("47/50 succeeded, 3 failed")
- Beat task batch_render_notifications every 60s: safety-net for missed
  batch notifications after order completion
- GET /notifications: defaults to channel IN (notification, alert);
  accepts ?channel=activity for activity feed
- Unread count badge counts only notification+alert channels
- Notifications.tsx: three tabs (Notifications | Activity | Alerts)
- NotificationSettings.tsx: frequency dropdown per event type

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-08 20:20:07 +01:00
parent 10d05bd2e7
commit 89c44b846f
14 changed files with 640 additions and 56 deletions
+71
View File
@@ -80,3 +80,74 @@ def recover_stuck_cad_files() -> None:
logger.debug("recover_stuck_cad_files: no stuck files found")
except Exception as exc:
logger.error("recover_stuck_cad_files failed: %s", exc)
@shared_task(name="app.tasks.beat_tasks.batch_render_notifications", queue="step_processing")
def batch_render_notifications() -> None:
"""Check for orders where all lines are terminal but no batch notification emitted yet.
This acts as a safety net for batch notifications that may have been missed
if the order completion hook failed or was skipped. Runs every 60 seconds.
"""
try:
from sqlalchemy import create_engine, select
from sqlalchemy.orm import Session
from app.config import settings
from app.domains.orders.models import Order, OrderStatus
from app.domains.notifications.models import AuditLog
from app.domains.notifications.service import (
emit_batch_render_notification_sync,
CHANNEL_NOTIFICATION,
)
sync_url = settings.database_url.replace("+asyncpg", "")
engine = create_engine(sync_url)
with Session(engine) as session:
# Find all completed orders
completed_order_ids: list[str] = [
str(oid) for oid in session.execute(
select(Order.id).where(Order.status == OrderStatus.completed)
).scalars().all()
]
if not completed_order_ids:
engine.dispose()
logger.debug("batch_render_notifications: no completed orders found")
return
# Find which of those already have a batch notification
notified_ids_raw = session.execute(
select(AuditLog.entity_id).where(
AuditLog.entity_type == "order",
AuditLog.action == "order.completed",
AuditLog.channel == CHANNEL_NOTIFICATION,
AuditLog.entity_id.in_(completed_order_ids),
)
).scalars().all()
notified_ids = set(notified_ids_raw)
engine.dispose()
orders_needing_notification = [
oid for oid in completed_order_ids if oid not in notified_ids
]
if not orders_needing_notification:
logger.debug("batch_render_notifications: no orders need batch notification")
return
logger.info(
"batch_render_notifications: emitting batch notifications for %d order(s)",
len(orders_needing_notification),
)
for order_id in orders_needing_notification:
try:
emit_batch_render_notification_sync(str(order_id))
except Exception as exc:
logger.error(
"batch_render_notifications: failed for order %s: %s", order_id, exc
)
except Exception as exc:
logger.error("batch_render_notifications failed: %s", exc)
+4
View File
@@ -38,5 +38,9 @@ celery_app.conf.update(
"task": "app.tasks.beat_tasks.recover_stuck_cad_files",
"schedule": 300.0, # every 5 minutes
},
"batch-render-notifications-every-60s": {
"task": "app.tasks.beat_tasks.batch_render_notifications",
"schedule": 60.0, # every 60 seconds
},
},
)
+7 -5
View File
@@ -1065,7 +1065,7 @@ def render_order_line_task(self, order_line_id: str):
except Exception:
logger.debug("WebSocket publish skipped (non-fatal)")
# Notify order creator about render result
# Emit per-render activity event (channel=activity, not shown in bell dropdown)
try:
from app.models.order import Order as OrderModel
order_row = session.execute(
@@ -1073,7 +1073,7 @@ def render_order_line_task(self, order_line_id: str):
.where(OrderModel.id == line.order_id)
).one_or_none()
if order_row:
from app.services.notification_service import emit_notification_sync
from app.services.notification_service import emit_notification_sync, CHANNEL_ACTIVITY
details: dict = {
"order_number": order_row[1],
"product_name": product_name,
@@ -1090,9 +1090,10 @@ def render_order_line_task(self, order_line_id: str):
entity_type="order",
entity_id=str(line.order_id),
details=details,
channel=CHANNEL_ACTIVITY,
)
except Exception:
logger.exception("Failed to emit render notification")
logger.exception("Failed to emit render activity event")
# Check if all lines for this order are done → auto-advance
order_id_str = str(line.order_id)
@@ -1148,7 +1149,7 @@ def render_order_line_task(self, order_line_id: str):
).one_or_none()
eng4.dispose()
if order_row2:
from app.services.notification_service import emit_notification_sync
from app.services.notification_service import emit_notification_sync, CHANNEL_ACTIVITY
emit_notification_sync(
actor_user_id=None,
target_user_id=str(order_row2[0]),
@@ -1161,9 +1162,10 @@ def render_order_line_task(self, order_line_id: str):
"output_type": "unknown",
"error": str(exc)[:300],
},
channel=CHANNEL_ACTIVITY,
)
except Exception:
logger.exception("Failed to emit render failure notification")
logger.exception("Failed to emit render failure activity event")
except Exception:
logger.exception(f"Failed to mark {order_line_id} as failed in DB")
raise