Skip to content

Commit

Permalink
feat: add paymant stats for user, all_time and one_month.
Browse files Browse the repository at this point in the history
  • Loading branch information
nika-alaverdashvili committed Nov 5, 2024
1 parent 310dddc commit ab3cc1a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
17 changes: 10 additions & 7 deletions locale/ka/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -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 ""
Expand Down Expand Up @@ -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 "თქვენ არ გაქვთ ამ მოქმედების შესრულების უფლება."
2 changes: 2 additions & 0 deletions payments/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
PaymentListView,
SellerPayoutView,
TransferFundsView,
UserPaymentStatsView,
stripe_account_refresh,
stripe_account_return,
)
Expand Down Expand Up @@ -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"),
Expand Down
39 changes: 38 additions & 1 deletion payments/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
},
}
)

0 comments on commit ab3cc1a

Please sign in to comment.