|
| 1 | +"""CRUD operations for Notifications""" |
| 2 | + |
| 3 | +from typing import Any |
| 4 | +from uuid import UUID |
| 5 | + |
| 6 | +from sqlalchemy import func, select, update |
| 7 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 8 | + |
| 9 | +from app.models.notification import Notification, NotificationType |
| 10 | +from app.models.user import User |
| 11 | + |
| 12 | + |
| 13 | +async def create_notification( |
| 14 | + db: AsyncSession, |
| 15 | + user_id: UUID, |
| 16 | + type: NotificationType, |
| 17 | + title: str, |
| 18 | + message: str, |
| 19 | + metadata: dict[str, Any] | None = None, |
| 20 | +) -> Notification: |
| 21 | + """Create a single notification for a user.""" |
| 22 | + notification = Notification( |
| 23 | + user_id=user_id, |
| 24 | + type=type, |
| 25 | + title=title, |
| 26 | + message=message, |
| 27 | + metadata_=metadata, |
| 28 | + ) |
| 29 | + db.add(notification) |
| 30 | + await db.commit() |
| 31 | + await db.refresh(notification) |
| 32 | + return notification |
| 33 | + |
| 34 | + |
| 35 | +async def create_notification_for_all_users( |
| 36 | + db: AsyncSession, |
| 37 | + type: NotificationType, |
| 38 | + title: str, |
| 39 | + message: str, |
| 40 | + metadata: dict[str, Any] | None = None, |
| 41 | + exclude_user_id: UUID | None = None, |
| 42 | +) -> list[Notification]: |
| 43 | + """Create a notification for all active users, optionally excluding one.""" |
| 44 | + query = select(User).where(User.is_active.is_(True)) # type: ignore[attr-defined] |
| 45 | + if exclude_user_id is not None: |
| 46 | + query = query.where(User.id != exclude_user_id) |
| 47 | + result = await db.execute(query) |
| 48 | + users = result.scalars().all() |
| 49 | + |
| 50 | + notifications = [ |
| 51 | + Notification( |
| 52 | + user_id=u.id, |
| 53 | + type=type, |
| 54 | + title=title, |
| 55 | + message=message, |
| 56 | + metadata_=metadata, |
| 57 | + ) |
| 58 | + for u in users |
| 59 | + ] |
| 60 | + db.add_all(notifications) |
| 61 | + await db.commit() |
| 62 | + return notifications |
| 63 | + |
| 64 | + |
| 65 | +async def get_notifications( |
| 66 | + db: AsyncSession, |
| 67 | + user_id: UUID, |
| 68 | + unread_only: bool = False, |
| 69 | + limit: int = 50, |
| 70 | + offset: int = 0, |
| 71 | +) -> tuple[list[Notification], int]: |
| 72 | + """Get notifications for a user. Returns (notifications, total).""" |
| 73 | + query = select(Notification).where(Notification.user_id == user_id) |
| 74 | + if unread_only: |
| 75 | + query = query.where(Notification.read.is_(False)) # type: ignore[attr-defined] |
| 76 | + |
| 77 | + count_result = await db.execute(select(func.count()).select_from(query.subquery())) |
| 78 | + total = count_result.scalar_one() |
| 79 | + |
| 80 | + query = query.order_by(Notification.created_at.desc()).offset(offset).limit(limit) |
| 81 | + result = await db.execute(query) |
| 82 | + return list(result.scalars().all()), total |
| 83 | + |
| 84 | + |
| 85 | +async def get_unread_count(db: AsyncSession, user_id: UUID) -> int: |
| 86 | + """Get unread notification count for a user.""" |
| 87 | + result = await db.execute( |
| 88 | + select(func.count()).where(Notification.user_id == user_id, Notification.read.is_(False)) # type: ignore[attr-defined] |
| 89 | + ) |
| 90 | + return result.scalar_one() |
| 91 | + |
| 92 | + |
| 93 | +async def mark_as_read(db: AsyncSession, notification_id: UUID, user_id: UUID) -> Notification | None: |
| 94 | + """Mark a single notification as read. Returns None if not found or not owned by user.""" |
| 95 | + result = await db.execute( |
| 96 | + select(Notification).where(Notification.id == notification_id, Notification.user_id == user_id) |
| 97 | + ) |
| 98 | + notification = result.scalar_one_or_none() |
| 99 | + if notification is None: |
| 100 | + return None |
| 101 | + notification.read = True |
| 102 | + await db.commit() |
| 103 | + await db.refresh(notification) |
| 104 | + return notification |
| 105 | + |
| 106 | + |
| 107 | +async def mark_all_as_read(db: AsyncSession, user_id: UUID) -> int: |
| 108 | + """Mark all notifications as read for a user. Returns number of updated rows.""" |
| 109 | + result = await db.execute( |
| 110 | + update(Notification) |
| 111 | + .where(Notification.user_id == user_id, Notification.read.is_(False)) # type: ignore[attr-defined] |
| 112 | + .values(read=True) |
| 113 | + ) |
| 114 | + await db.commit() |
| 115 | + return result.rowcount # type: ignore[attr-defined] |
0 commit comments