Skip to content

Commit

Permalink
chore: create endpoint for buyers auction statistics used on dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
sandronadiradze committed Nov 5, 2024
1 parent 0744b07 commit 37f2129
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
6 changes: 6 additions & 0 deletions auction/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
BulkDeleteAuctionView,
BuyerAuctionListView,
BuyerDashboardListView,
BuyerDashboardStatistics,
BuyerLeaderBoardStatisticsListView,
CancelAuctionView,
CreateBookmarkView,
Expand Down Expand Up @@ -82,4 +83,9 @@
SellerDashboardStatistics.as_view(),
name="seller-dashboard-statistics",
),
path(
"buyer/dashboard/statistics/",
BuyerDashboardStatistics.as_view(),
name="buyer-dashboard-statistics",
),
]
67 changes: 66 additions & 1 deletion auction/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)

0 comments on commit 37f2129

Please sign in to comment.