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'] ) 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/ - **Response**: Includes details of notifications such as title, message, is read status, creation date, and update date. - **Headers**: `Authorization: Bearer ` for authentication. """ return super().get(request, *args, **kwargs) def get_queryset(self): user = self.request.user return Notification.objects.filter(user=user).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'], responses={ 200: "All notifications marked as read", 403: "Forbidden", } ) def get(self, request, *args, **kwargs): user = request.user notifications = Notification.objects.filter(user=user) notifications.update(is_read=True) return Response({'status': 'all notifications marked as read'}, status=status.HTTP_200_OK)