feat: added CRUD operation for issue subscriber

This commit is contained in:
NarayanBavisetti 2023-06-22 18:27:40 +05:30
parent 9c0a2b74b3
commit cf4bb02a9f
3 changed files with 109 additions and 7 deletions

View File

@ -534,11 +534,11 @@ class IssueLiteSerializer(BaseSerializer):
class IssueSubscriberSerializer(BaseSerializer):
project = serializers.PrimaryKeyRelatedField(read_only=True)
workspace = serializers.PrimaryKeyRelatedField(read_only=True)
issue = serializers.PrimaryKeyRelatedField(read_only=True)
subscriber = serializers.PrimaryKeyRelatedField(read_only=True)
class Meta:
model = IssueSubscriber
fields = "__all__"
read_only_fields = [
"workspace",
"project",
"issue",
]

View File

@ -801,16 +801,34 @@ urlpatterns = [
## End IssueComments
# Issue Subscribers
path(
"workspaces/<str:slug>/projects/<uuid:project_id>/issues/<uuid:issue_id>/subscribers/",
"workspaces/<str:slug>/projects/<uuid:project_id>/issues/<uuid:issue_id>/issue-subscribers/",
IssueSubscriberViewSet.as_view(
{
"get": "list",
"post": "create",
}
),
name="project-issue-subscriber",
),
path(
"workspaces/<str:slug>/projects/<uuid:project_id>/issues/<uuid:issue_id>/issue-subscribers/<uuid:subscriber_id>",
IssueSubscriberViewSet.as_view(
{
"delete": "destroy"
}
),
name="project-issue-subscriber",
),
path(
"workspaces/<str:slug>/projects/<uuid:project_id>/issues/<uuid:issue_id>/subscribe/",
IssueSubscriberViewSet.as_view(
{
"get": "subscription_status",
"post": "subscribe",
}
),
name="project-issue-subscriber",
),
## End Issue Subscribers
## IssueProperty
path(

View File

@ -856,7 +856,8 @@ class IssueSubscriberViewSet(BaseViewSet):
def perform_create(self, serializer):
serializer.save(
subscriber_id=self.request.user.id, issue_id=self.kwargs.get("issue_id")
project_id=self.kwargs.get("project_id"),
issue_id=self.kwargs.get("issue_id"),
)
def get_queryset(self):
@ -870,3 +871,86 @@ class IssueSubscriberViewSet(BaseViewSet):
.order_by("-created_at")
.distinct()
)
def destroy(self, request, slug, project_id, issue_id, subscriber_id):
try:
issue_subscriber = IssueSubscriber.objects.get(
project=project_id,
subscriber=subscriber_id,
workspace__slug=slug,
issue=issue_id,
)
issue_subscriber.delete()
return Response(
{"message": "Removed Subscription"},
status=status.HTTP_200_OK,
)
except IssueSubscriber.DoesNotExist:
return Response(
{"error": "User is not subscribed to this issue"},
status=status.HTTP_400_BAD_REQUEST,
)
except Exception as e:
capture_exception(e)
return Response(
{"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST,
)
def subscribe(self, request, slug, project_id, issue_id):
try:
if IssueSubscriber.objects.filter(
issue_id=issue_id,
subscriber=request.user,
workspace__slug=slug,
project=project_id,
).exists():
return Response(
{"message": "User already subscribed to the issue."},
status=status.HTTP_400_BAD_REQUEST,
)
issue = Issue.objects.get(id=issue_id)
subscriber = IssueSubscriber.objects.create(
issue_id=issue_id,
subscriber_id=request.user.id,
project_id=project_id,
)
serilaizer = IssueSubscriberSerializer(subscriber)
return Response(serilaizer.data, status=status.HTTP_201_CREATED)
except Issue.DoesNotExist:
return Response(
{"error": "Issue does not exists"},
status=status.HTTP_400_BAD_REQUEST,
)
except Exception as e:
capture_exception(e)
return Response(
{"error": "Something went wrong, please try again later"},
status=status.HTTP_400_BAD_REQUEST,
)
def subscription_status(self, request, slug, project_id, issue_id):
try:
issue_subscriber = IssueSubscriber.objects.filter(
issue=issue_id,
subscriber=request.user,
workspace__slug=slug,
project=project_id,
).exists()
return Response({"subscribed": issue_subscriber}, status=status.HTTP_200_OK)
except Exception as e:
capture_exception(e)
return Response(
{"error": "Something went wrong, please try again later"},
status=status.HTTP_400_BAD_REQUEST,
)