Skip to content

Commit

Permalink
Merge pull request #75 from Build-Squad/muditm/eng-43-ui-orders-dashb…
Browse files Browse the repository at this point in the history
…oard

eng 43 UI orders dashboard
  • Loading branch information
muditmahajan authored Jan 10, 2024
2 parents cca01b6 + dfb2d0a commit e71bad6
Show file tree
Hide file tree
Showing 21 changed files with 1,866 additions and 229 deletions.
3 changes: 2 additions & 1 deletion src/api/marketplace/accounts/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from unicodedata import category
from marketplace.services import truncateWalletAddress
from rest_framework import serializers
from uuid import UUID

Expand Down Expand Up @@ -237,7 +238,7 @@ class WalletSerializer(serializers.ModelSerializer):

def get_wallet_address_id(self, wallet):
if wallet.wallet_address_id:
return wallet.wallet_address_id[:4] + "..." + wallet.wallet_address_id[-4:]
return truncateWalletAddress(wallet.wallet_address_id)
return None

class Meta:
Expand Down
3 changes: 2 additions & 1 deletion src/api/marketplace/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
handleNotFound,
handleDeleteNotAllowed,
JWTOperations,
truncateWalletAddress,
)
from drf_yasg.utils import swagger_auto_schema
from django.core.exceptions import ValidationError
Expand Down Expand Up @@ -1198,7 +1199,7 @@ def create_wallet(self, wallet_address_id, wallet_provider, wallet_network):
def create_user(self, wallet_address_id, role):
try:
user = User.objects.create(
username=wallet_address_id,
username=truncateWalletAddress(wallet_address_id),
role=Role.objects.get(name=role),
)
user.save()
Expand Down
23 changes: 20 additions & 3 deletions src/api/marketplace/marketplace/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rest_framework.response import Response
from rest_framework import status
from django.core.mail import send_mail
# from .logger import custom_logger
import json

# logger = custom_logger()
import logging
Expand Down Expand Up @@ -112,21 +112,32 @@ def getPageInfo(self):

def handleServerException(e):
logger.error(e)
try:
json.dumps(str(e)) # Try to serialize the error
error = str(e)
except TypeError:
error = 'Non-serializable error'

return Response({
'isSuccess': False,
'data': None,
'message': 'Internal Server Error',
'errors': e,
'errors': error,
}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)


def handleBadRequest(e):
logger.error(e)
try:
json.dumps(str(e)) # Try to serialize the error
error = str(e)
except TypeError:
error = 'Non-serializable error'
return Response({
'isSuccess': False,
'data': None,
'message': 'Bad Request',
'errors': e,
'errors': error,
}, status=status.HTTP_400_BAD_REQUEST)

def handleNotFound(resource_name):
Expand All @@ -153,3 +164,9 @@ def sendEmail(self, subject, message, from_email, recipient_list):
send_mail(subject, message, from_email, recipient_list)
except Exception as e:
pass


def truncateWalletAddress(address):
if address:
return address[:4] + "..." + address[-4:]
return None
1 change: 1 addition & 0 deletions src/api/marketplace/orders/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class OrderListFilterSerializer(serializers.Serializer):
gt_rating = serializers.FloatField(required=False)
lt_amount = serializers.FloatField(required=False)
gt_amount = serializers.FloatField(required=False)
order_by = serializers.CharField(required=False)

class OrderSerializer(serializers.ModelSerializer):

Expand Down
52 changes: 52 additions & 0 deletions src/api/marketplace/orders/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from accounts.models import Wallet
from marketplace.authentication import JWTAuthentication
from marketplace.services import (
Pagination,
Expand Down Expand Up @@ -48,6 +49,13 @@ def post(self, request):
serializer = CreateOrderSerializer(
data=request.data, context={'request': request})
if serializer.is_valid():
# Check that the requested account has atleast one wallet connected
# If not, then return error
# If yes, then create the order
wallets = Wallet.objects.filter(
user_id=self.request.user_account)
if wallets.count() == 0:
return handleBadRequest({"wallet": "No wallet is connected to the account. Please add a wallet to the account."})
serializer.save()
return Response(
{
Expand All @@ -66,6 +74,47 @@ def post(self, request):
class OrderListView(APIView):
authentication_classes = [JWTAuthentication]

def get(self, request):
# Get the role of the user and get the orders of the users.
# Return the count based on the status of the orders
# Response will be like
try:
user = request.user_account
role = request.user_account.role
if role.name == "business_owner":
orders = Order.objects.filter(
Q(buyer=user)
).distinct()
elif role.name == "influencer":
# For all the order items, there will be a package in it and the package willl have influencer id
order_items = OrderItem.objects.filter(
Q(package__influencer=user)
).distinct()
orders = Order.objects.filter(
Q(order_item_order_id__in=order_items)
).distinct()

accepted = orders.filter(status="accepted").count()
pending = orders.filter(status="pending").count()
completed = orders.filter(status="completed").count()
rejected = orders.filter(status="rejected").count()

return Response(
{
"isSuccess": True,
"data": {
"accepted": accepted,
"pending": pending,
"completed": completed,
"rejected": rejected
},
"message": "All Order Count retrieved successfully",
},
status=status.HTTP_200_OK,
)
except Exception as e:
return handleServerException(e)

@swagger_auto_schema(request_body=OrderListFilterSerializer)
def post(self, request):
try:
Expand Down Expand Up @@ -116,6 +165,9 @@ def post(self, request):
if 'gt_rating' in filters:
orders = orders.filter(review__rating__gt=filters['gt_rating'])

if 'order_by' in filters:
orders = orders.order_by(filters['order_by'])

pagination = Pagination(orders, request)
serializer = OrderSerializer(pagination.getData(), many=True)
return Response(
Expand Down
Loading

0 comments on commit e71bad6

Please sign in to comment.