Begin implementing better payment simulation

This commit is contained in:
2023-05-05 13:41:00 +01:00
parent 64fd072f2f
commit 35607898f0
6 changed files with 200 additions and 52 deletions

View File

@@ -1,5 +1,6 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponse
from django.urls import reverse
from mixins.views import (
ObjectCreate,
ObjectDelete,
@@ -10,8 +11,19 @@ from mixins.views import (
from rest_framework import status
from two_factor.views.mixins import OTPRequiredMixin
from core.clients.aggregators.nordigen import NordigenClient
from core.clients.platforms.agora import AgoraClient
from core.forms import LinkGroupForm
from core.models import Aggregator, LinkGroup, Platform, Requisition
from core.lib.money import Money
from core.models import (
Aggregator,
LinkGroup,
OperatorWallets,
Platform,
Requisition,
User,
)
from core.views.helpers import synchronize_async_helper
class LinkGroupInfo(LoginRequiredMixin, OTPRequiredMixin, ObjectRead):
@@ -32,6 +44,25 @@ class LinkGroupInfo(LoginRequiredMixin, OTPRequiredMixin, ObjectRead):
def get_context_data(self):
context = super().get_context_data()
self.extra_buttons = [
{
"url": "#",
"action": "withdraw",
"method": "get",
"label": "Withdraw profit",
"icon": "fa-solid fa-money-bill-transfer",
},
{
"url": reverse(
"linkgroup_simulate", kwargs={"pk": self.object.id, "type": "modal"}
),
"action": "simulate",
"method": "get",
"label": "Simulate withdrawal",
"icon": "fa-solid fa-play",
},
]
aggregators = Aggregator.objects.filter(
user=self.request.user,
link_group=self.object,
@@ -50,8 +81,8 @@ class LinkGroupInfo(LoginRequiredMixin, OTPRequiredMixin, ObjectRead):
context["linkgroup"] = self.object
payees = self.object.payees()
simulation = {}
profit = 1000
profit_platform = profit * (self.object.platform_owner_cut_percentage / 100)
profit_requisition = profit * (
@@ -95,12 +126,35 @@ class LinkGroupInfo(LoginRequiredMixin, OTPRequiredMixin, ObjectRead):
pay_list[payee] = dict(cast)
requisition_pay_list.append(cast)
operator_pay_list = []
staff = User.objects.filter(
is_staff=True,
)
for user in staff:
wallets, _ = OperatorWallets.objects.get_or_create(user=user)
total_wallets = len(wallets.payees.all())
# Select all OperatorWallet instances with any distinct user attributes
for payee in wallets.payees.all():
cast = {
"name": payee.name,
"address": payee.address,
"amount": profit_operator / total_wallets,
"max": profit_operator,
}
print("CAST", cast)
if user not in pay_list:
pay_list[payee] = {}
if "amount" in pay_list[payee]:
pay_list[payee]["amount"] += cast["amount"]
else:
pay_list[payee] = dict(cast)
operator_pay_list.append(cast)
simulation[("Platform", profit_platform)] = platform_pay_list
simulation[("Requisition", profit_requisition)] = requisition_pay_list
simulation[("Operator", profit_operator)] = []
simulation[("Operator", profit_operator)] = operator_pay_list
context["pay_list"] = pay_list
context["simulation"] = simulation
return context
@@ -134,3 +188,50 @@ class LinkGroupUpdate(LoginRequiredMixin, OTPRequiredMixin, ObjectUpdate):
class LinkGroupDelete(LoginRequiredMixin, OTPRequiredMixin, ObjectDelete):
model = LinkGroup
class LinkGroupSimulation(LoginRequiredMixin, OTPRequiredMixin, ObjectRead):
context_object_name_singular = "linkgroupsim"
context_object_name = "linkgroupsim"
detail_template = "partials/linkgroup-info-sim.html"
def get_object(self, **kwargs):
pk = self.kwargs.get("pk")
linkgroup = LinkGroup.objects.filter(
user=self.request.user,
id=pk,
).first()
if not linkgroup:
return HttpResponse(status=status.HTTP_404_NOT_FOUND)
money = Money()
checks = synchronize_async_helper(
money.check_all(
user=self.request.user, nordigen=NordigenClient, agora=AgoraClient
)
)
print("CHECKS", checks)
aggregators = Aggregator.objects.filter(
user=self.request.user,
link_group=self.object,
)
platforms = Platform.objects.filter(
user=self.request.user,
link_group=self.object,
)
requisitions = Requisition.objects.filter(
user=self.request.user,
aggregator__in=aggregators,
)
pay_list = money.get_pay_list(
linkgroup,
requisitions,
platforms,
self.request.user,
checks["total_profit"],
)
print("PAY LIST", pay_list)
collapsed = money.collapse_pay_list(pay_list)
print("COLLAPSED", collapsed)
return collapsed