|
| 1 | +import uuid |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +from fastapi import APIRouter, HTTPException |
| 5 | +from sqlmodel import col, func, select |
| 6 | + |
| 7 | +from app.api.deps import CurrentUser, SessionDep |
| 8 | +from app.core.websocket import manager |
| 9 | +from app.models import ( |
| 10 | + Message, |
| 11 | + Notification, |
| 12 | + NotificationCreate, |
| 13 | + NotificationPublic, |
| 14 | + NotificationsPublic, |
| 15 | + NotificationUpdate, |
| 16 | +) |
| 17 | + |
| 18 | +router = APIRouter(prefix="/notifications", tags=["notifications"]) |
| 19 | + |
| 20 | + |
| 21 | +@router.get("/", response_model=NotificationsPublic) |
| 22 | +def read_notifications( |
| 23 | + session: SessionDep, |
| 24 | + current_user: CurrentUser, |
| 25 | + skip: int = 0, |
| 26 | + limit: int = 100, |
| 27 | + unread_only: bool = False, |
| 28 | +) -> Any: |
| 29 | + """ |
| 30 | + Retrieve notifications for the current user. |
| 31 | + """ |
| 32 | + if current_user.is_superuser: |
| 33 | + count_statement = select(func.count()).select_from(Notification) |
| 34 | + if unread_only: |
| 35 | + count_statement = count_statement.where(Notification.is_read == False) |
| 36 | + count = session.exec(count_statement).one() |
| 37 | + |
| 38 | + unread_count_statement = select(func.count()).select_from(Notification).where( |
| 39 | + Notification.is_read == False |
| 40 | + ) |
| 41 | + unread_count = session.exec(unread_count_statement).one() |
| 42 | + |
| 43 | + statement = ( |
| 44 | + select(Notification) |
| 45 | + .order_by(col(Notification.created_at).desc()) |
| 46 | + .offset(skip) |
| 47 | + .limit(limit) |
| 48 | + ) |
| 49 | + if unread_only: |
| 50 | + statement = statement.where(Notification.is_read == False) |
| 51 | + notifications = session.exec(statement).all() |
| 52 | + else: |
| 53 | + count_statement = ( |
| 54 | + select(func.count()) |
| 55 | + .select_from(Notification) |
| 56 | + .where(Notification.user_id == current_user.id) |
| 57 | + ) |
| 58 | + if unread_only: |
| 59 | + count_statement = count_statement.where(Notification.is_read == False) |
| 60 | + count = session.exec(count_statement).one() |
| 61 | + |
| 62 | + unread_count_statement = ( |
| 63 | + select(func.count()) |
| 64 | + .select_from(Notification) |
| 65 | + .where(Notification.user_id == current_user.id) |
| 66 | + .where(Notification.is_read == False) |
| 67 | + ) |
| 68 | + unread_count = session.exec(unread_count_statement).one() |
| 69 | + |
| 70 | + statement = ( |
| 71 | + select(Notification) |
| 72 | + .where(Notification.user_id == current_user.id) |
| 73 | + .order_by(col(Notification.created_at).desc()) |
| 74 | + .offset(skip) |
| 75 | + .limit(limit) |
| 76 | + ) |
| 77 | + if unread_only: |
| 78 | + statement = statement.where(Notification.is_read == False) |
| 79 | + notifications = session.exec(statement).all() |
| 80 | + |
| 81 | + notifications_public = [ |
| 82 | + NotificationPublic.model_validate(notification) |
| 83 | + for notification in notifications |
| 84 | + ] |
| 85 | + return NotificationsPublic( |
| 86 | + data=notifications_public, count=count, unread_count=unread_count |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +@router.get("/{id}", response_model=NotificationPublic) |
| 91 | +def read_notification( |
| 92 | + session: SessionDep, current_user: CurrentUser, id: uuid.UUID |
| 93 | +) -> Any: |
| 94 | + """ |
| 95 | + Get notification by ID. |
| 96 | + """ |
| 97 | + notification = session.get(Notification, id) |
| 98 | + if not notification: |
| 99 | + raise HTTPException(status_code=404, detail="Notification not found") |
| 100 | + if not current_user.is_superuser and (notification.user_id != current_user.id): |
| 101 | + raise HTTPException(status_code=403, detail="Not enough permissions") |
| 102 | + return notification |
| 103 | + |
| 104 | + |
| 105 | +@router.post("/", response_model=NotificationPublic) |
| 106 | +async def create_notification( |
| 107 | + *, session: SessionDep, current_user: CurrentUser, notification_in: NotificationCreate |
| 108 | +) -> Any: |
| 109 | + """ |
| 110 | + Create new notification. |
| 111 | + Only superusers can create notifications for other users. |
| 112 | + Regular users can only create notifications for themselves. |
| 113 | + """ |
| 114 | + if not current_user.is_superuser and notification_in.user_id != current_user.id: |
| 115 | + raise HTTPException( |
| 116 | + status_code=403, detail="Not enough permissions to create notifications for other users" |
| 117 | + ) |
| 118 | + |
| 119 | + notification = Notification.model_validate(notification_in) |
| 120 | + session.add(notification) |
| 121 | + session.commit() |
| 122 | + session.refresh(notification) |
| 123 | + |
| 124 | + notification_public = NotificationPublic.model_validate(notification) |
| 125 | + await manager.send_notification(notification_public, notification.user_id) |
| 126 | + |
| 127 | + return notification |
| 128 | + |
| 129 | + |
| 130 | +@router.put("/{id}", response_model=NotificationPublic) |
| 131 | +def update_notification( |
| 132 | + *, |
| 133 | + session: SessionDep, |
| 134 | + current_user: CurrentUser, |
| 135 | + id: uuid.UUID, |
| 136 | + notification_in: NotificationUpdate, |
| 137 | +) -> Any: |
| 138 | + """ |
| 139 | + Update a notification. |
| 140 | + """ |
| 141 | + notification = session.get(Notification, id) |
| 142 | + if not notification: |
| 143 | + raise HTTPException(status_code=404, detail="Notification not found") |
| 144 | + if not current_user.is_superuser and (notification.user_id != current_user.id): |
| 145 | + raise HTTPException(status_code=403, detail="Not enough permissions") |
| 146 | + |
| 147 | + update_dict = notification_in.model_dump(exclude_unset=True) |
| 148 | + notification.sqlmodel_update(update_dict) |
| 149 | + session.add(notification) |
| 150 | + session.commit() |
| 151 | + session.refresh(notification) |
| 152 | + return notification |
| 153 | + |
| 154 | + |
| 155 | +@router.delete("/{id}") |
| 156 | +def delete_notification( |
| 157 | + session: SessionDep, current_user: CurrentUser, id: uuid.UUID |
| 158 | +) -> Message: |
| 159 | + """ |
| 160 | + Delete a notification. |
| 161 | + """ |
| 162 | + notification = session.get(Notification, id) |
| 163 | + if not notification: |
| 164 | + raise HTTPException(status_code=404, detail="Notification not found") |
| 165 | + if not current_user.is_superuser and (notification.user_id != current_user.id): |
| 166 | + raise HTTPException(status_code=403, detail="Not enough permissions") |
| 167 | + session.delete(notification) |
| 168 | + session.commit() |
| 169 | + return Message(message="Notification deleted successfully") |
| 170 | + |
| 171 | + |
| 172 | +@router.post("/{id}/mark-read", response_model=NotificationPublic) |
| 173 | +def mark_notification_read( |
| 174 | + session: SessionDep, current_user: CurrentUser, id: uuid.UUID |
| 175 | +) -> Any: |
| 176 | + """ |
| 177 | + Mark a notification as read. |
| 178 | + """ |
| 179 | + notification = session.get(Notification, id) |
| 180 | + if not notification: |
| 181 | + raise HTTPException(status_code=404, detail="Notification not found") |
| 182 | + if not current_user.is_superuser and (notification.user_id != current_user.id): |
| 183 | + raise HTTPException(status_code=403, detail="Not enough permissions") |
| 184 | + |
| 185 | + notification.is_read = True |
| 186 | + session.add(notification) |
| 187 | + session.commit() |
| 188 | + session.refresh(notification) |
| 189 | + return notification |
| 190 | + |
| 191 | + |
| 192 | +@router.post("/mark-all-read", response_model=Message) |
| 193 | +def mark_all_notifications_read( |
| 194 | + session: SessionDep, current_user: CurrentUser |
| 195 | +) -> Message: |
| 196 | + """ |
| 197 | + Mark all notifications as read for the current user. |
| 198 | + """ |
| 199 | + statement = select(Notification).where( |
| 200 | + Notification.user_id == current_user.id, |
| 201 | + Notification.is_read == False, |
| 202 | + ) |
| 203 | + notifications = session.exec(statement).all() |
| 204 | + |
| 205 | + for notification in notifications: |
| 206 | + notification.is_read = True |
| 207 | + session.add(notification) |
| 208 | + |
| 209 | + session.commit() |
| 210 | + return Message(message=f"Marked {len(notifications)} notifications as read") |
0 commit comments