From ab3cc1a99cb9a91bc100edeb41a379c3d879696e Mon Sep 17 00:00:00 2001 From: Nika Alaverdashvili Date: Tue, 5 Nov 2024 13:31:07 +0400 Subject: [PATCH] feat: add paymant stats for user, all_time and one_month. --- locale/ka/LC_MESSAGES/django.po | 17 ++++++++------ payments/urls.py | 2 ++ payments/views.py | 39 ++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/locale/ka/LC_MESSAGES/django.po b/locale/ka/LC_MESSAGES/django.po index a120a6d..585362d 100755 --- a/locale/ka/LC_MESSAGES/django.po +++ b/locale/ka/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-11-05 12:33+0400\n" +"POT-Creation-Date: 2024-11-05 13:27+0400\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -48,12 +48,6 @@ msgstr "" "თქვენ არ გაქვთ ამ მოქმედების შესრულების უფლება, მომხმარებელი უნდა იყოს " "მყიდველი." -#: apis/permissions.py:77 -#, fuzzy -#| msgid "You do not have permission to perform this action." -msgid "You do not have permission to access this payment." -msgstr "თქვენ არ გაქვთ ამ მოქმედების შესრულების უფლება." - #: apis/serializers.py:75 apis/serializers.py:310 apis/serializers.py:902 #: apis/serializers.py:921 apis/serializers.py:1085 msgid "" @@ -439,6 +433,15 @@ msgstr "" "თქვენ ხელთავიდან გამოგეგზავნათ ორ ფაქტორიანი ავთენტიფიკაციისთვის განკუთვნილი " "ერთჯერადი კოდი." +#: payments/views.py:587 payments/views.py:602 +msgid "No payments found for this user." +msgstr "" + #: templates/password_reset_with_email.html:28 msgid "Thanks for using our site!" msgstr "მადლობას გიხდით ჩვენი ვებ-გვერდის გამოყენებისთვის!" + +#, fuzzy +#~| msgid "You do not have permission to perform this action." +#~ msgid "You do not have permission to access this payment." +#~ msgstr "თქვენ არ გაქვთ ამ მოქმედების შესრულების უფლება." diff --git a/payments/urls.py b/payments/urls.py index eae044b..dd2b5d6 100644 --- a/payments/urls.py +++ b/payments/urls.py @@ -8,6 +8,7 @@ PaymentListView, SellerPayoutView, TransferFundsView, + UserPaymentStatsView, stripe_account_refresh, stripe_account_return, ) @@ -36,6 +37,7 @@ path("transfer/", TransferFundsView.as_view(), name="transfer-funds"), path("payout/", SellerPayoutView.as_view(), name="seller_payout"), path("paymants/", PaymentListView.as_view(), name="payment_list"), + path("paymants/stats/", UserPaymentStatsView.as_view(), name="payment_stats"), # Webhook Endpoints for handling stripe responses! path("webhook/", stripe_webhook, name="deposit_webhook"), path("connect/webhook/", stripe_connect_webhook, name="connect_webhook"), diff --git a/payments/views.py b/payments/views.py index f80979d..a7ce37d 100755 --- a/payments/views.py +++ b/payments/views.py @@ -1,8 +1,10 @@ import stripe from django.contrib.auth import get_user_model from django.db import transaction +from django.db.models import Count, Sum from django.shortcuts import get_object_or_404, redirect from django.urls import reverse +from django.utils import timezone from django.utils.translation import gettext_lazy as _ from drf_spectacular.utils import extend_schema from rest_framework import status @@ -580,8 +582,43 @@ def get_queryset(self): "-created_at" ) - # Raise 404 if there are no payments if not queryset.exists(): raise NotFound(detail=_("No payments found for this user.")) return queryset + + +@extend_schema( + tags=["Payment"], +) +class UserPaymentStatsView(APIView): + permission_classes = [IsAuthenticated] + + def get(self, request): + user_payments = Payment.objects.filter(customer=request.user) + + if not user_payments.exists(): + raise NotFound(detail=_("No payments found for this user.")) + + all_time_stats = user_payments.aggregate( + total_amount=Sum("amount"), transaction_count=Count("id") + ) + + one_month_ago = timezone.now() - timezone.timedelta(days=30) + + last_month_stats = user_payments.filter(created_at__gte=one_month_ago).aggregate( + total_amount=Sum("amount"), transaction_count=Count("id") + ) + + return Response( + { + "all_time": { + "total_amount": all_time_stats["total_amount"] or 0, + "transaction_count": all_time_stats["transaction_count"] or 0, + }, + "last_month": { + "total_amount": last_month_stats["total_amount"] or 0, + "transaction_count": last_month_stats["transaction_count"] or 0, + }, + } + )