From 37f2129992170f64eb0feed4718c9c274dffa3af Mon Sep 17 00:00:00 2001 From: sandronadiradze Date: Tue, 5 Nov 2024 16:44:47 +0400 Subject: [PATCH] chore: create endpoint for buyers auction statistics used on dashboard --- auction/urls.py | 6 +++++ auction/views.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/auction/urls.py b/auction/urls.py index f96b579..767f8e3 100644 --- a/auction/urls.py +++ b/auction/urls.py @@ -5,6 +5,7 @@ BulkDeleteAuctionView, BuyerAuctionListView, BuyerDashboardListView, + BuyerDashboardStatistics, BuyerLeaderBoardStatisticsListView, CancelAuctionView, CreateBookmarkView, @@ -82,4 +83,9 @@ SellerDashboardStatistics.as_view(), name="seller-dashboard-statistics", ), + path( + "buyer/dashboard/statistics/", + BuyerDashboardStatistics.as_view(), + name="buyer-dashboard-statistics", + ), ] diff --git a/auction/views.py b/auction/views.py index f6acbe5..449bbb3 100755 --- a/auction/views.py +++ b/auction/views.py @@ -1727,7 +1727,7 @@ def list(self, request, *args, **kwargs): tags=["Statistics"], ) class SellerDashboardStatistics(APIView): - permission_classes = [IsAuthenticated] + permission_classes = [IsAuthenticated, IsSeller] def get(self, request): # Get the current user's UUID @@ -1780,3 +1780,68 @@ def get(self, request): }, status=status.HTTP_200_OK, ) + + +@extend_schema( + tags=["Statistics"], +) +class BuyerDashboardStatistics(APIView): + permission_classes = [IsAuthenticated, IsBuyer] + + def get(self, request): + # Get the current user's UUID + user_uuid = request.user.id + + # Get the current date and the start/end of the current month + now = timezone.now() + start_of_month = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0) + end_of_month = (start_of_month + timedelta(days=31)).replace(day=1) - timedelta( + seconds=1 + ) + + # Calculate the start and end of the previous month + start_of_last_month = (start_of_month - timedelta(days=1)).replace(day=1) + end_of_last_month = start_of_month - timedelta(seconds=1) + + # Count total completed auctions with winners + total_completed_auctions = AuctionStatistics.objects.filter( + auction__author=user_uuid, + auction__status=StatusChoices.COMPLETED, + winner_bid_object__isnull=False, + ).count() + + # Count completed auctions for the current month + monthly_completed_auctions = AuctionStatistics.objects.filter( + auction__author=user_uuid, + auction__status=StatusChoices.COMPLETED, + winner_bid_object__isnull=False, + auction__end_date__gte=start_of_month, + auction__end_date__lte=end_of_month, + ).count() + + # Count completed auctions for the last month + last_month_completed_auctions = AuctionStatistics.objects.filter( + auction__author=user_uuid, + auction__status=StatusChoices.COMPLETED, + winner_bid_object__isnull=False, + auction__end_date__gte=start_of_last_month, + auction__end_date__lte=end_of_last_month, + ).count() + + # Calculate percentage increase + if last_month_completed_auctions > 0: + percentage_increase = ( + (monthly_completed_auctions - last_month_completed_auctions) + / last_month_completed_auctions + ) * 100 + else: + percentage_increase = 100 if monthly_completed_auctions > 0 else 0 + + return Response( + { + "total_completed_auctions": total_completed_auctions, + "last_month_completed_auctions": monthly_completed_auctions, + "percentage_increase": str(round(percentage_increase, 2)), + }, + status=status.HTTP_200_OK, + )