Skip to content

Commit

Permalink
feat(orders): transfer money in model
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamad-liyaghi committed Aug 9, 2024
1 parent 4478fd8 commit 53a73ca
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions apps/orders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from django.conf import settings
from django.db.models import QuerySet
from uuid import uuid4
from orders.exceptions import EmptyCartException, InsufficientBalanceException
from orders.exceptions import EmptyCartException
from orders.enums import OrderStatus
from transactions.models import Transaction
from restaurants.models import Restaurant
from products.models import Product

Expand All @@ -25,10 +26,11 @@ def __str__(self):

def save(self, *args, **kwargs):
if self.status == OrderStatus.PROCESSING and not self.is_removed_from_balance:
if self.user.balance < self.total_price:
raise InsufficientBalanceException
self.user.balance -= self.total_price
self.user.save()
Transaction.transfer(
sender=self.user,
receiver=self.restaurant.owner,
amount=self.total_price,
)
return super().save(*args, **kwargs)

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions apps/orders/views/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from rest_framework import status
from drf_spectacular.utils import extend_schema_view, OpenApiResponse, extend_schema
from orders.enums import OrderStatus
from orders.exceptions import InsufficientBalanceException
from transactions.exceptions import InsufficientBalanceError
from orders.models import Order
from orders.serializers import OrderSerializer

Expand Down Expand Up @@ -80,7 +80,7 @@ def post(self, request, *args, **kwargs):
order.save()
return Response({"message": "Order is being processed"}, status=status.HTTP_200_OK)

except InsufficientBalanceException:
except InsufficientBalanceError:
return Response({"error": "Insufficient balance"}, status=status.HTTP_400_BAD_REQUEST)


Expand Down

0 comments on commit 53a73ca

Please sign in to comment.