Features: 1) Add promocode_uuid field to BuyAsBusinessOrderSerializer; 2) Enhance error handling in buy_without_registration by wrapping in a try-except block and returning appropriate HTTP

This commit is contained in:
Egor Pavlovich Gorbunov 2025-10-01 17:59:06 +03:00
parent 330177f6e4
commit b1c022b974
4 changed files with 17 additions and 13 deletions

View file

@ -1831,7 +1831,7 @@ class CustomerRelationshipManagementProvider(ExportModelOperationsMixin("crm_pro
verbose_name_plural = _("CRMs")
class OrderCrmLink(ExportModelOperationsMixin("order_crm_link"), NiceModel):
class OrderCrmLink(ExportModelOperationsMixin("order_crm_link"), NiceModel): # type: ignore
order = ForeignKey(to=Order, on_delete=PROTECT, related_name="crm_links")
crm = ForeignKey(to=CustomerRelationshipManagementProvider, on_delete=PROTECT, related_name="order_links")
crm_lead_id = CharField(max_length=30, unique=True, db_index=True)

View file

@ -177,6 +177,7 @@ class BuyUnregisteredOrderSerializer(Serializer):
class BuyAsBusinessOrderSerializer(Serializer):
products = AddOrderProductSerializer(many=True, required=True)
business_identificator = CharField(required=True)
promocode_uuid = UUIDField(required=False)
business_email = CharField(required=True)
business_phone_number = CharField(required=True)
billing_business_address_uuid = CharField(required=False)

View file

@ -455,7 +455,7 @@ class BuyAsBusinessView(APIView):
order.save()
return Response(
status=status.HTTP_400_BAD_REQUEST,
data={"error": str(e)},
data={"detail": str(e)},
)

View file

@ -846,17 +846,20 @@ class OrderViewSet(EvibesViewSet):
serializer.is_valid(raise_exception=True)
order = Order.objects.create(status="MOMENTAL")
products = [p["product_uuid"] for p in serializer.validated_data["products"]]
transaction = order.buy_without_registration(
products=products,
promocode_uuid=serializer.validated_data.get("promocode_uuid"),
customer_name=serializer.validated_data.get("customer_name"),
customer_email=serializer.validated_data.get("customer_email"),
customer_phone_number=serializer.validated_data.get("customer_phone_number"),
billing_customer_address=serializer.validated_data.get("billing_customer_address_uuid"),
shipping_customer_address=serializer.validated_data.get("shipping_customer_address_uuid"),
payment_method=serializer.validated_data.get("payment_method"),
)
return Response(status=status.HTTP_202_ACCEPTED, data=TransactionProcessSerializer(transaction).data)
try:
transaction = order.buy_without_registration(
products=products,
promocode_uuid=serializer.validated_data.get("promocode_uuid"),
customer_name=serializer.validated_data.get("customer_name"),
customer_email=serializer.validated_data.get("customer_email"),
customer_phone_number=serializer.validated_data.get("customer_phone_number"),
billing_customer_address=serializer.validated_data.get("billing_customer_address_uuid"),
shipping_customer_address=serializer.validated_data.get("shipping_customer_address_uuid"),
payment_method=serializer.validated_data.get("payment_method"),
)
return Response(status=status.HTTP_201_CREATED, data=TransactionProcessSerializer(transaction).data)
except Exception as e:
return Response(status=status.HTTP_400_BAD_REQUEST, data={"detail": str(e)})
@action(detail=True, methods=["post"], url_path="add_order_product")
def add_order_product(self, request, **kwargs):