Skip to content

Commit

Permalink
refactor: do some refactors about transferring funds to user
Browse files Browse the repository at this point in the history
  • Loading branch information
AmooHashem committed Nov 5, 2024
1 parent d62ef59 commit 03021ff
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 129 deletions.
15 changes: 5 additions & 10 deletions apps/attributes/models/base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from django.db import models
from polymorphic.models import PolymorphicModel
from abc import abstractmethod

from apps.attributes.models.utils import SumDict
from proxies.bank_service.bank import request_transfer
from proxies.website_service.main import get_website
from proxies.bank_service.utils import transfer_funds_to_user


class Attribute(PolymorphicModel):
Expand Down Expand Up @@ -62,15 +60,12 @@ def give_reward(self, *args, **kwargs):
if total_reward.is_zero():
return

# Get website
# Process the transfer
request = kwargs.get('request')
website_name = request.headers.get('Website')
website = get_website(website_name)

# Process the transfer
request_transfer(
sender_id=website.get('uuid'),
receiver_id=str(request.user.id),
transfer_funds_to_user(
website_name=website_name,
user_uuid=str(request.user.id),
funds=total_reward,
)

Expand Down
6 changes: 3 additions & 3 deletions apps/attributes/models/intrinsic_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Condition(IntrinsicAttribute):

def is_true(self, *args, **kwargs):
from apps.fsm.utils import AnswerSheetFacade

player = kwargs.get('player')
user = kwargs.get('user')
value = self.value
Expand All @@ -19,11 +19,11 @@ def is_true(self, *args, **kwargs):
if not player:
total_condition_result = False
else:
expected_correct_choices_in_last_answer_count = value.get(
expected_count = value.get(
'expected_correct_choices_in_last_answer_count')
facade = AnswerSheetFacade(player.answer_sheet)
total_condition_result = facade.check_expected_correct_choices_in_last_answer_count(
expected_correct_choices_in_last_answer_count)
expected_count)

if 'expected_choices' in value:
if not player:
Expand Down
6 changes: 1 addition & 5 deletions apps/attributes/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from rest_framework.routers import DefaultRouter
from django.urls import path

from apps.attributes.views.currency import get_currencies

router = DefaultRouter()

urlpatterns = [
path('currencies/', get_currencies),
]
urlpatterns = []

urlpatterns += router.urls
13 changes: 0 additions & 13 deletions apps/attributes/views/currency.py

This file was deleted.

31 changes: 12 additions & 19 deletions apps/currency/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from django.conf import settings
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework import status
from django.db import transaction
from apps.fsm.models import Object
from proxies.bank_service.bank import get_user_balances, request_transfer
from proxies.website_service.main import get_website
from proxies.bank_service.bank import get_user_balances
from proxies.bank_service.utils import transfer_funds_to_user
from .models import Spend
from django.core.exceptions import ObjectDoesNotExist

Expand Down Expand Up @@ -70,27 +69,21 @@ def spend_on_object(request):
status=status.HTTP_400_BAD_REQUEST
)

# Get website
website_name = request.headers.get('Website')
website = get_website(website_name)

# Process the transfer
transfer_response = request_transfer(
sender_id=user_uuid,
receiver_id=website.get('uuid'),
funds=funds
)

withdraw_transaction = transfer_response.get(
'transactions').get('withdraw')

# If transfer successful, create spend record
with transaction.atomic():
# Spending money
website_name = request.headers.get('Website')
response = transfer_funds_to_user(
website_name=website_name,
user_uuid=user_uuid,
funds=funds,
)

# If transfer successful, create spend record
spend = Spend.objects.create(
user=user_uuid,
object=obj.id,
fund=funds,
transaction_id=withdraw_transaction.get('id')
transaction_id=response.get('withdraw_transaction_id')
)

return Response({
Expand Down
4 changes: 2 additions & 2 deletions apps/fsm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def check_expected_choices_in_last_answer(self, expected_choice_ids: list[int]):
except:
return False

def check_expected_correct_choices_in_last_answer_count(self, expected_correct_choices_in_last_answer_count: int):
def check_expected_correct_choices_in_last_answer_count(self, expected_count: int):
try:
# Get the last multi-choice answer in a single query
from apps.fsm.models.response import MultiChoiceAnswer
Expand All @@ -252,6 +252,6 @@ def check_expected_correct_choices_in_last_answer_count(self, expected_correct_c
if choice.is_correct
)

return correct_choices_count == expected_correct_choices_in_last_answer_count
return correct_choices_count == expected_count
except:
return False
77 changes: 0 additions & 77 deletions proxies/bank_service/main.py

This file was deleted.

18 changes: 18 additions & 0 deletions proxies/bank_service/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from proxies.bank_service.bank import request_transfer
from proxies.website_service.main import get_website
from typing import TypedDict


class TransferResponse(TypedDict):
withdraw_transaction_id: str
deposit_transaction_id: str


def transfer_funds_to_user(website_name, user_uuid, funds) -> TransferResponse:
website = get_website(website_name)

return request_transfer(
sender_id=website.get('uuid'),
receiver_id=str(user_uuid),
funds=funds,
)

0 comments on commit 03021ff

Please sign in to comment.