Features: 1) Restructure transaction processing to handle unsaved instances using pk check; 2) Instantiate AbstractGateway to prevent potential shared reference issues;

Fixes: 1) Correct logic to properly detect new transactions for processing;

Extra: 1) Minor code tidy-up with consistent case handling;
This commit is contained in:
Egor Pavlovich Gorbunov 2025-07-06 23:17:23 +03:00
parent 4ed76b85de
commit 220e50c01b

View file

@ -14,20 +14,22 @@ def create_balance_on_user_creation_signal(instance, created, **_kwargs):
@receiver(pre_save, sender=Transaction)
def process_transaction_changes(instance, created, **_kwargs):
if created:
def process_transaction_changes(instance, **_kwargs):
is_new = instance.pk is None
if is_new:
try:
match instance.process.get("gateway", "default"):
case "gateway":
gateway = AbstractGateway
gateway = AbstractGateway()
case "default":
gateway = AbstractGateway
gateway = AbstractGateway()
case _:
gateway = AbstractGateway()
gateway.process_transaction(instance)
except Exception as e:
instance.process = {"status": "ERRORED", "error": str(e)}
if not created:
else:
status = str(instance.process.get("status", "")).lower()
success = instance.process.get("success", False)