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_qty = int(it.get('quantity', 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(
|
||||
supplier_id=supplier_id,
|
||||
|
|
@ -175,8 +183,11 @@ class CheckoutView(APIView):
|
|||
created_seller_orders = True
|
||||
|
||||
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(
|
||||
supplier_id=26,
|
||||
supplier_id=supp_id,
|
||||
order_id=f"{order.id}-1",
|
||||
date=now().date(),
|
||||
item="Artisan Heritage Creation",
|
||||
|
|
@ -261,9 +272,13 @@ class OrderHistoryView(APIView):
|
|||
})
|
||||
|
||||
# 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]
|
||||
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'
|
||||
elif 'Ready to Ship' in statuses:
|
||||
order_data['status'] = 'confirmed'
|
||||
|
|
@ -632,17 +647,39 @@ class OrderCancelView(APIView):
|
|||
return 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')
|
||||
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:
|
||||
qs = Order.objects.all()
|
||||
if user:
|
||||
order = Order.objects.get(id=order_id, user=user)
|
||||
else:
|
||||
order = Order.objects.get(id=order_id)
|
||||
except Order.DoesNotExist:
|
||||
return Response({'error': 'Order not found'}, status=status.HTTP_404_NOT_FOUND)
|
||||
qs = qs.filter(user=user)
|
||||
|
||||
if numeric_id:
|
||||
order = qs.filter(id=numeric_id).first()
|
||||
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']:
|
||||
return Response({
|
||||
|
|
@ -650,6 +687,11 @@ class OrderCancelView(APIView):
|
|||
}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
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})
|
||||
|
||||
# Call Shadowfax cancel endpoint via shippingservice
|
||||
|
|
@ -674,7 +716,12 @@ class OrderCancelView(APIView):
|
|||
order.shipping_status = 'cancelled'
|
||||
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}'.")
|
||||
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ class ProductSerializer(serializers.ModelSerializer):
|
|||
class Meta:
|
||||
model = Product
|
||||
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',
|
||||
'shipping_profile', 'package_weight', 'upload_source', 'status', 'is_active'
|
||||
]
|
||||
|
|
|
|||
Loading…
Reference in a new issue