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.
154 lines
6.3 KiB
154 lines
6.3 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, User
|
|
from apps.account.tasks 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)
|
|
|
|
|
|
|
|
|
|
class SendNotificationView(generics.GenericAPIView):
|
|
|
|
@swagger_auto_schema(
|
|
operation_description="Send a notification to a user by user_id.",
|
|
tags=['Notifications'],
|
|
request_body=openapi.Schema(
|
|
type=openapi.TYPE_OBJECT,
|
|
required=['user_id', 'title', 'body'],
|
|
properties={
|
|
'user_id': openapi.Schema(type=openapi.TYPE_INTEGER, description='ID of the user to send notification to'),
|
|
'title': openapi.Schema(type=openapi.TYPE_STRING, description='Notification title'),
|
|
'body': openapi.Schema(type=openapi.TYPE_STRING, description='Notification body'),
|
|
'data': openapi.Schema(type=openapi.TYPE_OBJECT, description='Additional data payload', default={'slam': 'qatreh'}),
|
|
},
|
|
),
|
|
responses={
|
|
200: openapi.Response('Notification sent successfully.'),
|
|
400: openapi.Response('FCM token not available for this user.'),
|
|
404: openapi.Response('User not found.'),
|
|
500: openapi.Response('Internal server error.'),
|
|
}
|
|
)
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
user_id = request.data.get('user_id', 1)
|
|
|
|
try:
|
|
user = User.objects.get(id=user_id)
|
|
except User.DoesNotExist:
|
|
return Response({'error': 'User not found.'}, status=status.HTTP_404_NOT_FOUND)
|
|
|
|
notification_title = request.data.get('title', 'test qatreh')
|
|
notification_body = request.data.get('body', 'test qatreh body')
|
|
data_payload = request.data.get('data', {'slam':'qatreh'})
|
|
|
|
fcm_token = user.fcm # Ensure that 'fcm' is a field in your User model
|
|
|
|
if not fcm_token:
|
|
return Response({
|
|
'error': 'FCM token not available for this user.'
|
|
}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
try:
|
|
send_notification([fcm_token], notification_title, notification_body, data_payload)
|
|
return Response({
|
|
'message': 'Notification sent successfully.'
|
|
}, status=status.HTTP_200_OK)
|
|
except Exception as e:
|
|
return Response({
|
|
'error': str(e)
|
|
}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|