feat: notification preferences system with plugin registry
- Add NotificationType and NotificationPreference models
- Add get_notification_types() to BasePlugin for plugin registration
- Add sync_notification_types() to PluginRegistry
- Update create_notification() to check user preferences (returns Notification|None)
- Add API endpoints: GET /types, GET /preferences, PATCH /preferences/{type_key}
- Add NotificationPreferenceUpdate schema
- Mail plugin registers 10 notification types with metadata
- Add Alembic migration 0017 for new tables + seed data
- Frontend: SettingsNotifications page with toggle switches
- Frontend: Settings tab, route, hooks for notification preferences
- i18n: notification settings keys (de/en)
This commit is contained in:
@@ -6,10 +6,14 @@ import uuid
|
||||
from datetime import UTC
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import func, select, update
|
||||
from sqlalchemy import and_, func, select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models.notification import Notification
|
||||
from app.models.notification import (
|
||||
Notification,
|
||||
NotificationPreference,
|
||||
NotificationType,
|
||||
)
|
||||
|
||||
|
||||
async def create_notification(
|
||||
@@ -19,8 +23,37 @@ async def create_notification(
|
||||
type: str,
|
||||
title: str,
|
||||
body: str | None = None,
|
||||
) -> Notification:
|
||||
"""Create a new notification for a user."""
|
||||
) -> Notification | None:
|
||||
"""Create a new notification for a user if they have not disabled this type.
|
||||
|
||||
Returns None if the user has opted out of this notification type.
|
||||
"""
|
||||
# Check user preference
|
||||
pref = await db.execute(
|
||||
select(NotificationPreference).where(
|
||||
and_(
|
||||
NotificationPreference.user_id == user_id,
|
||||
NotificationPreference.type_key == type,
|
||||
NotificationPreference.tenant_id == tenant_id,
|
||||
)
|
||||
)
|
||||
)
|
||||
pref_row = pref.scalar_one_or_none()
|
||||
|
||||
# If preference exists and is disabled, skip
|
||||
if pref_row and not pref_row.is_enabled:
|
||||
return None
|
||||
|
||||
# If no preference, check if type is enabled by default
|
||||
if not pref_row:
|
||||
type_def = await db.execute(
|
||||
select(NotificationType).where(NotificationType.type_key == type)
|
||||
)
|
||||
type_row = type_def.scalar_one_or_none()
|
||||
if type_row and not type_row.is_enabled_by_default:
|
||||
return None
|
||||
|
||||
# Create notification
|
||||
notif = Notification(
|
||||
tenant_id=tenant_id,
|
||||
user_id=user_id,
|
||||
|
||||
Reference in New Issue
Block a user