feat: add order views and product serializers for customer backend
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 26s
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 26s
This commit is contained in:
parent
2b30ce914a
commit
e07c1b58ff
2 changed files with 59 additions and 12 deletions
|
|
@ -158,7 +158,15 @@ class CheckoutView(APIView):
|
||||||
prod_price = float(it.get('price', total_amount))
|
prod_price = float(it.get('price', total_amount))
|
||||||
prod_qty = int(it.get('quantity', 1))
|
prod_qty = int(it.get('quantity', 1))
|
||||||
prod_id = it.get('id') or 1
|
prod_id = it.get('id') or 1
|
||||||
supplier_id = it.get('supplier_id') or 26
|
supplier_id = it.get('supplier_id')
|
||||||
|
|
||||||
|
# Try looking up product in DB to find accurate supplier_id
|
||||||
|
if not supplier_id or supplier_id == 26:
|
||||||
|
matched_prod = Product.objects.filter(Q(id=prod_id) | Q(title__iexact=prod_title) | Q(sku__iexact=str(prod_id))).first()
|
||||||
|
if matched_prod and matched_prod.supplier_id:
|
||||||
|
supplier_id = matched_prod.supplier_id
|
||||||
|
if not supplier_id:
|
||||||
|
supplier_id = 26
|
||||||
|
|
||||||
SellerOrder.objects.create(
|
SellerOrder.objects.create(
|
||||||
supplier_id=supplier_id,
|
supplier_id=supplier_id,
|
||||||
|
|
@ -175,8 +183,11 @@ class CheckoutView(APIView):
|
||||||
created_seller_orders = True
|
created_seller_orders = True
|
||||||
|
|
||||||
if not created_seller_orders:
|
if not created_seller_orders:
|
||||||
|
# Fallback seller order
|
||||||
|
first_prod = Product.objects.first()
|
||||||
|
supp_id = first_prod.supplier_id if (first_prod and first_prod.supplier_id) else 26
|
||||||
SellerOrder.objects.create(
|
SellerOrder.objects.create(
|
||||||
supplier_id=26,
|
supplier_id=supp_id,
|
||||||
order_id=f"{order.id}-1",
|
order_id=f"{order.id}-1",
|
||||||
date=now().date(),
|
date=now().date(),
|
||||||
item="Artisan Heritage Creation",
|
item="Artisan Heritage Creation",
|
||||||
|
|
@ -261,9 +272,13 @@ class OrderHistoryView(APIView):
|
||||||
})
|
})
|
||||||
|
|
||||||
# Aggregate status
|
# Aggregate status
|
||||||
if seller_orders.exists():
|
if order_data.get('status') == 'cancelled':
|
||||||
|
pass # Already marked cancelled
|
||||||
|
elif seller_orders.exists():
|
||||||
statuses = [o.status for o in seller_orders]
|
statuses = [o.status for o in seller_orders]
|
||||||
if 'Pending Acceptance' in statuses:
|
if 'Cancelled' in statuses or 'cancelled' in statuses:
|
||||||
|
order_data['status'] = 'cancelled'
|
||||||
|
elif 'Pending Acceptance' in statuses:
|
||||||
order_data['status'] = 'pending'
|
order_data['status'] = 'pending'
|
||||||
elif 'Ready to Ship' in statuses:
|
elif 'Ready to Ship' in statuses:
|
||||||
order_data['status'] = 'confirmed'
|
order_data['status'] = 'confirmed'
|
||||||
|
|
@ -632,17 +647,39 @@ class OrderCancelView(APIView):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def post(self, request, pk=None):
|
def post(self, request, pk=None):
|
||||||
order_id = pk or request.data.get('order_id')
|
raw_order_id = pk or request.data.get('order_id')
|
||||||
reason = request.data.get('reason', 'Customer requested cancellation prior to shipping')
|
reason = request.data.get('reason', 'Customer requested cancellation prior to shipping')
|
||||||
user = self.get_user(request)
|
user = self.get_user(request)
|
||||||
|
|
||||||
|
if not raw_order_id:
|
||||||
|
return Response({'error': 'order_id is required'}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
# Parse numeric ID if passed as formatted string (e.g., "TRDX-ORD-1" -> 1)
|
||||||
|
import re
|
||||||
|
numeric_id = None
|
||||||
|
if isinstance(raw_order_id, str):
|
||||||
|
digits = re.sub(r'[^0-9]', '', raw_order_id)
|
||||||
|
if digits:
|
||||||
|
numeric_id = int(digits)
|
||||||
|
elif isinstance(raw_order_id, int):
|
||||||
|
numeric_id = raw_order_id
|
||||||
|
|
||||||
|
order = None
|
||||||
|
from django.db.models import Q
|
||||||
try:
|
try:
|
||||||
|
qs = Order.objects.all()
|
||||||
if user:
|
if user:
|
||||||
order = Order.objects.get(id=order_id, user=user)
|
qs = qs.filter(user=user)
|
||||||
else:
|
|
||||||
order = Order.objects.get(id=order_id)
|
if numeric_id:
|
||||||
except Order.DoesNotExist:
|
order = qs.filter(id=numeric_id).first()
|
||||||
return Response({'error': 'Order not found'}, status=status.HTTP_404_NOT_FOUND)
|
if not order:
|
||||||
|
order = qs.filter(Q(razorpay_order_id=str(raw_order_id)) | Q(awb_number=str(raw_order_id))).first()
|
||||||
|
except Exception as e:
|
||||||
|
audit_logger.warning(f"OrderCancelView lookup error: {e}")
|
||||||
|
|
||||||
|
if not order:
|
||||||
|
return Response({'error': f'Order {raw_order_id} not found'}, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
if order.status.lower() in ['shipped', 'delivered']:
|
if order.status.lower() in ['shipped', 'delivered']:
|
||||||
return Response({
|
return Response({
|
||||||
|
|
@ -650,6 +687,11 @@ class OrderCancelView(APIView):
|
||||||
}, status=status.HTTP_400_BAD_REQUEST)
|
}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
if order.status.lower() == 'cancelled':
|
if order.status.lower() == 'cancelled':
|
||||||
|
SellerOrder.objects.filter(
|
||||||
|
Q(order_id__startswith=f"{order.id}-") |
|
||||||
|
Q(order_id=str(order.id)) |
|
||||||
|
Q(order_id=str(raw_order_id))
|
||||||
|
).update(status='Cancelled')
|
||||||
return Response({'message': 'Order is already cancelled', 'order': OrderSerializer(order).data})
|
return Response({'message': 'Order is already cancelled', 'order': OrderSerializer(order).data})
|
||||||
|
|
||||||
# Call Shadowfax cancel endpoint via shippingservice
|
# Call Shadowfax cancel endpoint via shippingservice
|
||||||
|
|
@ -674,7 +716,12 @@ class OrderCancelView(APIView):
|
||||||
order.shipping_status = 'cancelled'
|
order.shipping_status = 'cancelled'
|
||||||
order.save()
|
order.save()
|
||||||
|
|
||||||
SellerOrder.objects.filter(order_id__startswith=f"{order.id}-").update(status='Cancelled')
|
# Mark matching SellerOrders as Cancelled in database
|
||||||
|
SellerOrder.objects.filter(
|
||||||
|
Q(order_id__startswith=f"{order.id}-") |
|
||||||
|
Q(order_id=str(order.id)) |
|
||||||
|
Q(order_id=str(raw_order_id))
|
||||||
|
).update(status='Cancelled')
|
||||||
|
|
||||||
audit_logger.info(f"AUDIT - Order Cancelled: Order ID {order.id} cancelled. Reason: '{reason}'.")
|
audit_logger.info(f"AUDIT - Order Cancelled: Order ID {order.id} cancelled. Reason: '{reason}'.")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ class ProductSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Product
|
model = Product
|
||||||
fields = [
|
fields = [
|
||||||
'id', 'category', 'category_name', 'title', 'name', 'price', 'mrp',
|
'id', 'supplier_id', 'category', 'category_name', 'title', 'name', 'price', 'mrp',
|
||||||
'stock', 'sku', 'image', 'image_url', 'description', 'is_gi_tagged',
|
'stock', 'sku', 'image', 'image_url', 'description', 'is_gi_tagged',
|
||||||
'shipping_profile', 'package_weight', 'upload_source', 'status', 'is_active'
|
'shipping_profile', 'package_weight', 'upload_source', 'status', 'is_active'
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue