mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-12-05 14:36:36 +01:00
Merge pull request #707 from officialasishkumar/fix/split-sync-run-rules
fix(splits): run rules on transactions created from split events
This commit is contained in:
2 files changed
+92
-24
No files matched your search
@@ -4,6 +4,7 @@ from django.db import transaction as db_transaction
|
||||
from django.utils import timezone
|
||||
|
||||
from apps.accounts.models import Account
|
||||
from apps.rules.signals import transaction_created
|
||||
from apps.splits.models import (
|
||||
SplitEvent,
|
||||
SplitExpense,
|
||||
@@ -44,7 +45,6 @@ def build_default_sync_description(
|
||||
return f"[{event.name}] Split transaction"
|
||||
|
||||
|
||||
@db_transaction.atomic
|
||||
def create_and_link_transaction(
|
||||
*,
|
||||
user,
|
||||
@@ -64,30 +64,36 @@ def create_and_link_transaction(
|
||||
counterparty: SplitParticipant | None = None,
|
||||
) -> SplitTransactionLink:
|
||||
tx_type = expected_transaction_type_for_kind(kind)
|
||||
tx = Transaction(
|
||||
owner=user,
|
||||
account=account,
|
||||
type=tx_type,
|
||||
is_paid=is_paid,
|
||||
date=date or timezone.localdate(),
|
||||
amount=amount,
|
||||
description=description[:500],
|
||||
notes=notes,
|
||||
category=category,
|
||||
)
|
||||
tx.save()
|
||||
tx.tags.set(tags)
|
||||
tx.entities.set(entities)
|
||||
with db_transaction.atomic():
|
||||
tx = Transaction(
|
||||
owner=user,
|
||||
account=account,
|
||||
type=tx_type,
|
||||
is_paid=is_paid,
|
||||
date=date or timezone.localdate(),
|
||||
amount=amount,
|
||||
description=description[:500],
|
||||
notes=notes,
|
||||
category=category,
|
||||
)
|
||||
tx.save()
|
||||
tx.tags.set(tags)
|
||||
tx.entities.set(entities)
|
||||
|
||||
return SplitTransactionLink.objects.create(
|
||||
event=event,
|
||||
user=user,
|
||||
transaction=tx,
|
||||
kind=kind,
|
||||
expense=expense,
|
||||
payment=payment,
|
||||
counterparty=counterparty,
|
||||
)
|
||||
link = SplitTransactionLink.objects.create(
|
||||
event=event,
|
||||
user=user,
|
||||
transaction=tx,
|
||||
kind=kind,
|
||||
expense=expense,
|
||||
payment=payment,
|
||||
counterparty=counterparty,
|
||||
)
|
||||
|
||||
# Like any other new transaction, run the user's rules and auto-tagging on it
|
||||
transaction_created.send(sender=tx)
|
||||
|
||||
return link
|
||||
|
||||
|
||||
@db_transaction.atomic
|
||||
|
||||
@@ -445,6 +445,68 @@ class WygiwyhSyncTests(SplitTestBase):
|
||||
self.assertFalse(SplitTransactionLink.objects.filter(id=link.id).exists())
|
||||
self.assertTrue(Transaction.objects.filter(id=existing_tx.id).exists())
|
||||
|
||||
@patch("apps.rules.signals.check_for_transaction_rules.defer")
|
||||
def test_created_transaction_triggers_rules(self, defer):
|
||||
self.client.force_login(self.creator)
|
||||
exp = SplitExpense.objects.create(
|
||||
event=self.event,
|
||||
paid_by=self.p_alice,
|
||||
description="Dinner",
|
||||
amount=Decimal("30.00"),
|
||||
)
|
||||
|
||||
resp = self.client.post(
|
||||
reverse(
|
||||
"split_sync_expense",
|
||||
kwargs={"event_id": self.event.id, "expense_id": exp.id},
|
||||
),
|
||||
{
|
||||
"mode": "create",
|
||||
"account": self.account.id,
|
||||
"amount": "30.00",
|
||||
"date": str(timezone.localdate()),
|
||||
"description": "Dinner",
|
||||
"is_paid": "on",
|
||||
},
|
||||
HTTP_HX_REQUEST="true",
|
||||
)
|
||||
self.assertEqual(resp.status_code, 204)
|
||||
|
||||
link = SplitTransactionLink.objects.get(event=self.event, expense=exp)
|
||||
defer.assert_called_once()
|
||||
self.assertEqual(defer.call_args.kwargs["instance_id"], link.transaction_id)
|
||||
self.assertEqual(defer.call_args.kwargs["signal"], "transaction_created")
|
||||
self.assertEqual(defer.call_args.kwargs["user_id"], self.creator.id)
|
||||
|
||||
@patch("apps.rules.signals.check_for_transaction_rules.defer")
|
||||
def test_linking_existing_transaction_does_not_trigger_rules(self, defer):
|
||||
self.client.force_login(self.creator)
|
||||
exp = SplitExpense.objects.create(
|
||||
event=self.event,
|
||||
paid_by=self.p_alice,
|
||||
description="Dinner",
|
||||
amount=Decimal("30.00"),
|
||||
)
|
||||
existing_tx = Transaction.objects.create(
|
||||
owner=self.creator,
|
||||
account=self.account,
|
||||
type=Transaction.Type.EXPENSE,
|
||||
amount=Decimal("30.00"),
|
||||
date=timezone.localdate(),
|
||||
description="Restaurant",
|
||||
)
|
||||
|
||||
resp = self.client.post(
|
||||
reverse(
|
||||
"split_sync_expense",
|
||||
kwargs={"event_id": self.event.id, "expense_id": exp.id},
|
||||
),
|
||||
{"mode": "link", "existing_transaction": existing_tx.id},
|
||||
HTTP_HX_REQUEST="true",
|
||||
)
|
||||
self.assertEqual(resp.status_code, 204)
|
||||
defer.assert_not_called()
|
||||
|
||||
def test_creator_can_remove_unused_or_deactivate_used_participant(self):
|
||||
self.client.force_login(self.creator)
|
||||
unused = SplitParticipant.objects.create(
|
||||
|
||||
Reference in new issue
Block a user