Skip to content

Commit

Permalink
Merge pull request #228 from Build-Squad/mudit/eng-165-highlight-non-…
Browse files Browse the repository at this point in the history
…scheduled-orders

ENG 165 highlight non scheduled orders
  • Loading branch information
muditmahajan authored Mar 13, 2024
2 parents b8a5af9 + 11e1ccf commit 9202248
Show file tree
Hide file tree
Showing 11 changed files with 1,166 additions and 137 deletions.
32 changes: 32 additions & 0 deletions src/api/marketplace/orders/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,20 @@ class Meta:
model = OrderItemMetaData
fields = '__all__'


class OrderReadSerializer(serializers.ModelSerializer):

class Meta:
model = Order
fields = '__all__'

# Serializer for the GET details of an order
class OrderItemReadSerializer(serializers.ModelSerializer):
package = PackageSerializer(read_only=True)
service_master = ServiceMasterReadSerializer(read_only=True)
currency = CurrencySerializer(read_only=True)
order_item_meta_data = serializers.SerializerMethodField()
order_id = OrderReadSerializer(read_only=True)

class Meta:
model = OrderItem
Expand Down Expand Up @@ -70,6 +78,30 @@ class OrderListFilterSerializer(serializers.Serializer):
search = serializers.CharField(required=False, allow_blank=True)


class OrderItemListFilterSerializer(serializers.Serializer):
influencers = serializers.ListField(
child=serializers.UUIDField(), required=False)
buyers = serializers.ListField(
child=serializers.UUIDField(), required=False)
status = serializers.ListField(
child=serializers.CharField(), required=False)
service_masters = serializers.ListField(
child=serializers.UUIDField(), required=False
)
order_ids = serializers.ListField(
child=serializers.UUIDField(), required=False)
lt_created_at = serializers.DateTimeField(required=False)
gt_created_at = serializers.DateTimeField(required=False)
lt_rating = serializers.FloatField(required=False)
gt_rating = serializers.FloatField(required=False)
lt_amount = serializers.FloatField(required=False)
gt_amount = serializers.FloatField(required=False)
order_by = serializers.CharField(required=False)
search = serializers.CharField(required=False, allow_blank=True)
lt_publish_date = serializers.DateTimeField(required=False)
gt_publish_date = serializers.DateTimeField(required=False)


# The response schema for the list of orders from the POST search request
class OrderSerializer(serializers.ModelSerializer):
buyer = UserSerializer(read_only=True)
Expand Down
172 changes: 153 additions & 19 deletions src/api/marketplace/orders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from .serializers import (
CreateOrderMessageSerializer,
CreateOrderSerializer,
OrderItemListFilterSerializer,
OrderItemReadSerializer,
OrderListFilterSerializer,
OrderSerializer,
OrderItemSerializer,
Expand All @@ -42,6 +44,12 @@
from rest_framework import status
from django.db.models import Q
from django.utils import timezone
from django.db.models import F, ExpressionWrapper, DateTimeField, Case, When, BooleanField
from django.db.models.functions import Now
from django.db.models import Min





# ORDER API-Endpoint
Expand Down Expand Up @@ -153,9 +161,6 @@ def post(self, request):
if "buyers" in filters:
orders = orders.filter(buyer__in=filters["buyers"])

if "status" in filters:
orders = orders.filter(status__in=filters["status"])

if "service_masters" in filters:
orders = orders.filter(
order_item_order_id__service_master__in=filters["service_masters"]
Expand Down Expand Up @@ -192,16 +197,52 @@ def post(self, request):
| Q(order_code__icontains=filters["search"])
)

if "order_by" in filters:
if "order_by" in filters and filters["order_by"] == "upcoming":
# Order by the publish date of the order items
# Should sort the publish_date closest to the current date first wrt the order
orders = orders.annotate(
min_publish_date=Min('order_item_order_id__publish_date'),
time_difference=ExpressionWrapper(
F('min_publish_date') - Now(), output_field=DateTimeField()
),
is_future=Case(
When(min_publish_date__gte=Now(), then=True),
default=False,
output_field=BooleanField(),
)
).order_by(
'-is_future',
Case(
When(is_future=True, then=F('time_difference')),
When(is_future=False, then=F('time_difference') * -1),
output_field=DateTimeField(),
)
)
elif "order_by" in filters:
orders = orders.order_by(filters["order_by"])

status_counts = {
"accepted": orders.filter(status="accepted").count(),
"pending": orders.filter(status="pending").count(),
"completed": orders.filter(status="completed").count(),
"rejected": orders.filter(status="rejected").count(),
"cancelled": orders.filter(status="cancelled").count(),
}

if "status" in filters:
orders = orders.filter(status__in=filters["status"])

pagination = Pagination(orders, request)
serializer = OrderSerializer(pagination.getData(), context={
"request": request}, many=True)
combined_data = {
"orders": serializer.data,
"status_counts": status_counts,
}
return Response(
{
"isSuccess": True,
"data": serializer.data,
"data": combined_data,
"message": "All Order retrieved successfully",
"pagination": pagination.getPageInfo(),
},
Expand Down Expand Up @@ -684,11 +725,13 @@ def post(self, request):
# ORDER-Item API-Endpoint
# List-Create-API
class OrderItemList(APIView):
authentication_classes = [JWTAuthentication]
def get(self, request):
try:
orderItems = OrderItem.objects.all()
pagination = Pagination(orderItems, request)
serializer = OrderItemSerializer(pagination.getData(), many=True)
serializer = OrderItemReadSerializer(
pagination.getData(), many=True)
return Response(
{
"isSuccess": True,
Expand All @@ -701,22 +744,113 @@ def get(self, request):
except Exception as e:
return handleServerException(e)

@swagger_auto_schema(request_body=OrderItemSerializer)
@swagger_auto_schema(request_body=OrderItemListFilterSerializer)
def post(self, request):
try:
serializer = OrderItemSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(
{
"isSuccess": True,
"data": OrderItemSerializer(serializer.instance).data,
"message": "Order Item created successfully",
},
status=status.HTTP_201_CREATED,
filter_serializer = OrderItemListFilterSerializer(
data=request.data)
filter_serializer.is_valid(raise_exception=True)
filters = filter_serializer.validated_data

user = request.user_account
role = request.user_account.role
if role.name == "business_owner":
orderItems = OrderItem.objects.filter(
Q(order_id__buyer=user), deleted_at=None
).distinct()
elif role.name == "influencer":
# For all the order items, there will be a package in it and the package willl have influencer id
orderItems = OrderItem.objects.filter(
Q(package__influencer=user), deleted_at=None
).distinct()

if "service_masters" in filters:
orderItems = orderItems.filter(
service_master__in=filters["service_masters"]
)
else:
return handleBadRequest(serializer.errors)

if "gt_created_at" in filters:
gt_created_at = filters["gt_created_at"].date()
orderItems = orderItems.filter(
created_at__date__gte=gt_created_at)

if "lt_created_at" in filters:
lt_created_at = filters["lt_created_at"].date()
orderItems = orderItems.filter(
created_at__date__lte=lt_created_at)

if "lt_rating" in filters:
orderItems = orderItems.filter(
review__rating__lt=filters["lt_rating"])

if "gt_rating" in filters:
orderItems = orderItems.filter(
review__rating__gt=filters["gt_rating"])

if "search" in filters:
orderItems = orderItems.filter(
Q(order_id__buyer__first_name__icontains=filters["search"])
| Q(order_id__buyer__last_name__icontains=filters["search"])
| Q(package__influencer__first_name__icontains=filters["search"])
| Q(package__influencer__last_name__icontains=filters["search"])
| Q(order_id__order_code__icontains=filters["search"])
)

if "buyers" in filters:
orderItems = orderItems.filter(
order_id__buyer__in=filters["buyers"])

if "order_by" in filters and filters["order_by"] == "upcoming":
orderItems = orderItems.annotate(
time_difference=ExpressionWrapper(
F('publish_date') - Now(), output_field=DateTimeField()
),
is_future=Case(
When(publish_date__gte=Now(), then=True),
default=False,
output_field=BooleanField(),
)
).order_by(
'-is_future',
Case(
When(is_future=True, then=F('time_difference')),
When(is_future=False, then=F('time_difference') * -1),
output_field=DateTimeField(),
)
)
elif "order_by" in filters:
orderItems = orderItems.order_by(filters["order_by"])

# Attach the total count of each status
status_counts = {
"accepted": orderItems.filter(status="accepted").count(),
"published": orderItems.filter(status="published").count(),
"cancelled": orderItems.filter(status="cancelled").count(),
"scheduled": orderItems.filter(status="scheduled").count(),
"rejected": orderItems.filter(status="rejected").count(),
}

if "status" in filters:
orderItems = orderItems.filter(status__in=filters["status"])

pagination = Pagination(orderItems, request)
serializer = OrderItemReadSerializer(
pagination.getData(), context={"request": request}, many=True)

combined_data = {
"order_items": serializer.data,
"status_counts": status_counts,
}

return Response(
{
"isSuccess": True,
"data": combined_data,
"message": "All Order Items retrieved successfully",
"pagination": pagination.getPageInfo(),
},
status=status.HTTP_200_OK,
)
except Exception as e:
return handleServerException(e)

Expand Down
57 changes: 29 additions & 28 deletions src/ui/app/business/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default function BusinessDashboardPage() {
ORDER_STATUS.REJECTED,
ORDER_STATUS.PENDING,
ORDER_STATUS.COMPLETED,
ORDER_STATUS.CANCELLED,
],
order_by: "-created_at",
});
Expand Down Expand Up @@ -94,7 +95,14 @@ export default function BusinessDashboardPage() {
}
);
if (isSuccess) {
setOrders(data?.data);
setOrders(data?.data?.orders);
setOrderCount({
accepted: data?.data?.status_counts?.accepted,
completed: data?.data?.status_counts?.completed,
pending: data?.data?.status_counts?.pending,
rejected: data?.data?.status_counts?.rejected,
cancelled: data?.data?.status_counts?.cancelled,
});
setPagination({
...pagination,
total_data_count: data?.pagination?.total_data_count,
Expand All @@ -108,28 +116,6 @@ export default function BusinessDashboardPage() {
}
};

const getOrdersCount = async () => {
try {
setLoading(true);
const { isSuccess, data, message } = await getService(
`orders/order-list/`
);
if (isSuccess) {
setOrderCount({
accepted: data?.data?.accepted,
completed: data?.data?.completed,
pending: data?.data?.pending,
rejected: data?.data?.rejected,
cancelled: data?.data?.cancelled,
});
} else {
notification(message ? message : "Something went wrong", "error");
}
} finally {
setLoading(false);
}
};

const cancelOrder = async (id: string) => {
const { isSuccess, data, message } = await putService(
`/orders/cancel-order/${id}/`,
Expand Down Expand Up @@ -582,10 +568,6 @@ export default function BusinessDashboardPage() {
},
];

useEffect(() => {
getOrdersCount();
}, []);

useEffect(() => {
const delayDebounceFn = setTimeout(() => {
getOrders();
Expand Down Expand Up @@ -619,6 +601,25 @@ export default function BusinessDashboardPage() {
card={card}
selectedCard={selectedCard}
orderCount={orderCount}
count={
card?.value === 0
? orderCount?.accepted +
orderCount?.completed +
orderCount?.pending +
orderCount?.rejected +
orderCount?.cancelled
: card?.value === 1
? orderCount?.accepted
: card?.value === 2
? orderCount?.completed
: card?.value === 3
? orderCount?.pending
: card?.value === 4
? orderCount?.rejected
: card?.value === 5
? orderCount?.cancelled
: 0
}
/>
</Grid>
);
Expand Down Expand Up @@ -651,7 +652,7 @@ export default function BusinessDashboardPage() {
? model?.[0]?.sort === "asc"
? `-${model?.[0]?.field}`
: `${model?.[0]?.field}`
: undefined,
: "upcoming",
}));
}}
localeText={{
Expand Down
Loading

0 comments on commit 9202248

Please sign in to comment.