You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

105 lines
4.1 KiB

from rest_framework import generics, status
from rest_framework.response import Response
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
from rest_framework.permissions import IsAuthenticated
from apps.account.serializers import NotificationSerializer, NotificationSendSerializer
from apps.account.models import Notification
# from apps.account.fcm_notification import send_notification
class NotificationListView(generics.ListAPIView):
queryset = Notification.objects.all()
serializer_class = NotificationSerializer
permission_classes = [IsAuthenticated,]
@swagger_auto_schema(
operation_description="Retrieve a list of notifications for the authenticated user or merchant account.",
tags=['Notifications'],
manual_parameters=[
openapi.Parameter(
'service',
openapi.IN_QUERY,
description="Filter notifications by service (imam-javad or doboodi)",
type=openapi.TYPE_STRING,
enum=['imam-javad', 'doboodi'],
required=False
)
]
)
def get(self, request, *args, **kwargs):
"""
This API allows you to retrieve a list of notifications based on the authenticated user's type.
If the user is a regular user, their notifications will be fetched from the `Notification` model.
If the user is a merchant, their notifications will be fetched from the `MerchantAccountNotification` model.
- **Method**: GET
- **URL**: /api/notifications/
- **Query Parameters**:
- `service`: Optional. Filter notifications by service ('imam-javad' or 'doboodi')
- **Response**: Includes details of notifications such as title, message, is read status, service, creation date, and update date.
- **Headers**: `Authorization: Bearer <token>` for authentication.
"""
return super().get(request, *args, **kwargs)
def get_queryset(self):
user = self.request.user
queryset = Notification.objects.filter(user=user)
# Filter by service if provided in query params
service = self.request.query_params.get('service', None)
if service:
queryset = queryset.filter(service=service)
return queryset.order_by('-created_at')
class NotificationReadAllView(generics.GenericAPIView):
permission_classes = [IsAuthenticated,]
queryset = Notification.objects.all()
@swagger_auto_schema(
operation_description="Mark all notifications as read for the authenticated user or merchant account.",
tags=['Notifications'],
manual_parameters=[
openapi.Parameter(
'service',
openapi.IN_QUERY,
description="Filter notifications to mark as read by service (imam-javad or doboodi)",
type=openapi.TYPE_STRING,
enum=['imam-javad', 'doboodi'],
required=False
)
],
responses={
200: "All notifications marked as read",
}
)
def get(self, request, *args, **kwargs):
user = request.user
service = request.query_params.get('service', None)
# Get base queryset for user's notifications
notifications = Notification.objects.filter(user=user)
# Apply service filtering based on query parameter
if service == 'doboodi':
# If service is doboodi, only mark doboodi notifications as read
notifications = notifications.filter(service=Notification.ServiceChoices.DOBOODI)
status_message = 'all doboodi notifications marked as read'
else:
# Default: mark all imam-javad notifications as read (exclude doboodi)
notifications = notifications.exclude(service=Notification.ServiceChoices.DOBOODI)
status_message = 'all imam-javad notifications marked as read'
# Update the filtered notifications
notifications.update(is_read=True)
return Response({'status': status_message}, status=status.HTTP_200_OK)