Compare commits
No commits in common. "aff0e7733953c478b9abef9a4d2ab9bfd5277868" and "ea9474633b9d699615b3b446596a6e247e14735a" have entirely different histories.
aff0e77339
...
ea9474633b
2 changed files with 0 additions and 38 deletions
|
|
@ -1,14 +1,11 @@
|
||||||
from itertools import groupby
|
from itertools import groupby
|
||||||
|
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from django.db.models import F
|
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from rest_framework.exceptions import ValidationError
|
from rest_framework.exceptions import ValidationError
|
||||||
|
|
||||||
from utils.exceptions import Conflict
|
from utils.exceptions import Conflict
|
||||||
|
|
||||||
from apps.catalog.models import Product
|
|
||||||
|
|
||||||
from .models import Cart, Notification, Order, OrderGroup, OrderItem, OrderStatusLog
|
from .models import Cart, Notification, Order, OrderGroup, OrderItem, OrderStatusLog
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -139,16 +136,6 @@ def checkout(user, address, delivery_type, scheduled_at, payment_method, notes):
|
||||||
return order_group
|
return order_group
|
||||||
|
|
||||||
|
|
||||||
def _restock_order_items(order):
|
|
||||||
"""Reverses the stock_quantity/sold_count decrement made at checkout (see checkout())."""
|
|
||||||
for item in order.items.select_related('product').filter(product__isnull=False):
|
|
||||||
Product.objects.filter(pk=item.product_id).update(
|
|
||||||
stock_quantity=F('stock_quantity') + item.quantity,
|
|
||||||
sold_count=F('sold_count') - item.quantity,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@transaction.atomic
|
|
||||||
def _transition(order, to_status, *, changed_by=None, note='', **extra_fields):
|
def _transition(order, to_status, *, changed_by=None, note='', **extra_fields):
|
||||||
from_status = order.status
|
from_status = order.status
|
||||||
order.status = to_status
|
order.status = to_status
|
||||||
|
|
@ -160,7 +147,6 @@ def _transition(order, to_status, *, changed_by=None, note='', **extra_fields):
|
||||||
OrderStatusLog.objects.create(order=order, from_status=from_status, to_status=to_status, changed_by=changed_by, note=note)
|
OrderStatusLog.objects.create(order=order, from_status=from_status, to_status=to_status, changed_by=changed_by, note=note)
|
||||||
|
|
||||||
if to_status == Order.Status.CANCELLED:
|
if to_status == Order.Status.CANCELLED:
|
||||||
_restock_order_items(order)
|
|
||||||
notify(
|
notify(
|
||||||
recipient=order.store.owner,
|
recipient=order.store.owner,
|
||||||
type=Notification.Type.ORDER_CANCELLED,
|
type=Notification.Type.ORDER_CANCELLED,
|
||||||
|
|
|
||||||
|
|
@ -50,30 +50,6 @@ class StatusTransitionTests(OrdersTestCase):
|
||||||
self.order.refresh_from_db()
|
self.order.refresh_from_db()
|
||||||
self.assertEqual(self.order.status, Order.Status.CANCELLED)
|
self.assertEqual(self.order.status, Order.Status.CANCELLED)
|
||||||
|
|
||||||
def test_cancelling_an_order_restocks_its_products(self):
|
|
||||||
self.product1.refresh_from_db()
|
|
||||||
stock_after_checkout = self.product1.stock_quantity
|
|
||||||
sold_after_checkout = self.product1.sold_count
|
|
||||||
self.assertEqual(stock_after_checkout, 49) # base.py seeds 50, checkout bought 1
|
|
||||||
self.assertEqual(sold_after_checkout, 1)
|
|
||||||
|
|
||||||
self.client.force_authenticate(user=self.customer)
|
|
||||||
response = self.client.post(f'/api/v1/orders/{self.order.uuid}/cancel/')
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
|
|
||||||
self.product1.refresh_from_db()
|
|
||||||
self.assertEqual(self.product1.stock_quantity, stock_after_checkout + 1)
|
|
||||||
self.assertEqual(self.product1.sold_count, sold_after_checkout - 1)
|
|
||||||
|
|
||||||
def test_cancelling_after_confirm_also_restocks(self):
|
|
||||||
self._as_seller()
|
|
||||||
self.client.post(f'/api/v1/seller/orders/{self.order.uuid}/confirm/')
|
|
||||||
self.client.post(f'/api/v1/seller/orders/{self.order.uuid}/cancel/')
|
|
||||||
|
|
||||||
self.product1.refresh_from_db()
|
|
||||||
self.assertEqual(self.product1.stock_quantity, 50)
|
|
||||||
self.assertEqual(self.product1.sold_count, 0)
|
|
||||||
|
|
||||||
def test_cannot_cancel_after_handed_to_courier(self):
|
def test_cannot_cancel_after_handed_to_courier(self):
|
||||||
self._as_seller()
|
self._as_seller()
|
||||||
for action in ('confirm', 'mark-ready', 'mark-shipped'):
|
for action in ('confirm', 'mark-ready', 'mark-shipped'):
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue