-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifications.py
More file actions
116 lines (99 loc) · 4.58 KB
/
Copy pathnotifications.py
File metadata and controls
116 lines (99 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from typing import Annotated
from dishka import FromDishka
from dishka.integrations.fastapi import DishkaRoute
from fastapi import APIRouter, Depends, Query, Response
from app.api.dependencies import current_user
from app.domain.enums import NotificationChannel, NotificationStatus
from app.domain.notification import DomainSubscriptionUpdate
from app.domain.user import User
from app.schemas_pydantic.notification import (
DeleteNotificationResponse,
NotificationListResponse,
NotificationSubscription,
SubscriptionsResponse,
SubscriptionUpdate,
UnreadCountResponse,
)
from app.services.notification_service import NotificationService
router = APIRouter(prefix="/notifications", tags=["notifications"], route_class=DishkaRoute)
@router.get("", response_model=NotificationListResponse)
async def get_notifications(
user: Annotated[User, Depends(current_user)],
notification_service: FromDishka[NotificationService],
status: Annotated[NotificationStatus | None, Query()] = None,
include_tags: Annotated[list[str] | None, Query(description="Only notifications with any of these tags")] = None,
exclude_tags: Annotated[list[str] | None, Query(description="Exclude notifications with any of these tags")] = None,
tag_prefix: Annotated[
str | None, Query(description="Only notifications having a tag starting with this prefix")
] = None,
limit: Annotated[int, Query(ge=1, le=100)] = 50,
offset: Annotated[int, Query(ge=0)] = 0,
) -> NotificationListResponse:
"""List notifications for the authenticated user."""
result = await notification_service.list_notifications(
user_id=user.user_id,
status=status,
limit=limit,
offset=offset,
include_tags=include_tags,
exclude_tags=exclude_tags,
tag_prefix=tag_prefix,
)
return NotificationListResponse.model_validate(result)
@router.put("/{notification_id}/read", status_code=204)
async def mark_notification_read(
notification_id: str,
user: Annotated[User, Depends(current_user)],
notification_service: FromDishka[NotificationService],
) -> Response:
"""Mark a single notification as read."""
await notification_service.mark_as_read(notification_id=notification_id, user_id=user.user_id)
return Response(status_code=204)
@router.post("/mark-all-read", status_code=204)
async def mark_all_read(
user: Annotated[User, Depends(current_user)],
notification_service: FromDishka[NotificationService],
) -> Response:
"""Mark all notifications as read."""
await notification_service.mark_all_as_read(user.user_id)
return Response(status_code=204)
@router.get("/subscriptions", response_model=SubscriptionsResponse)
async def get_subscriptions(
user: Annotated[User, Depends(current_user)],
notification_service: FromDishka[NotificationService],
) -> SubscriptionsResponse:
"""Get all notification channel subscriptions for the authenticated user."""
result = await notification_service.get_subscriptions(user.user_id)
return SubscriptionsResponse.model_validate(result)
@router.put("/subscriptions/{channel}", response_model=NotificationSubscription)
async def update_subscription(
channel: NotificationChannel,
subscription: SubscriptionUpdate,
user: Annotated[User, Depends(current_user)],
notification_service: FromDishka[NotificationService],
) -> NotificationSubscription:
"""Update subscription settings for a notification channel."""
update_data = DomainSubscriptionUpdate(**subscription.model_dump())
updated_sub = await notification_service.update_subscription(
user_id=user.user_id,
channel=channel,
update_data=update_data,
)
return NotificationSubscription.model_validate(updated_sub)
@router.get("/unread-count", response_model=UnreadCountResponse)
async def get_unread_count(
user: Annotated[User, Depends(current_user)],
notification_service: FromDishka[NotificationService],
) -> UnreadCountResponse:
"""Get the count of unread notifications."""
count = await notification_service.get_unread_count(user.user_id)
return UnreadCountResponse(unread_count=count)
@router.delete("/{notification_id}", response_model=DeleteNotificationResponse)
async def delete_notification(
notification_id: str,
user: Annotated[User, Depends(current_user)],
notification_service: FromDishka[NotificationService],
) -> DeleteNotificationResponse:
"""Delete a notification."""
await notification_service.delete_notification(user_id=user.user_id, notification_id=notification_id)
return DeleteNotificationResponse(message="Notification deleted")