From e51a688b239a0a5824db808f7e613589fd13a061 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Sat, 28 Nov 2026 05:16:52 +0000 Subject: [PATCH] fix(splits): run rules on transactions created from split events Adding a split expense, payment or planned settlement to "my transactions" with "Create new WYGIWYH transaction" saved the transaction without sending transaction_created. Rules and auto-tagging never ran on it, unlike transactions created from the form, the API, imports, quick transactions and bank sync. create_and_link_transaction now sends transaction_created once the transaction, its tags and entities, and the split link are committed, the same way quick transactions do. Linking an existing transaction still doesn't trigger rules, as with bank sync matching. --- app/apps/splits/services/wygiwyh_sync.py | 54 ++++++++++++--------- app/apps/splits/tests/test_splits.py | 62 ++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 24 deletions(-) diff --git a/app/apps/splits/services/wygiwyh_sync.py b/app/apps/splits/services/wygiwyh_sync.py index f764a8ae..8e774b89 100644 --- a/app/apps/splits/services/wygiwyh_sync.py +++ b/app/apps/splits/services/wygiwyh_sync.py @@ -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 diff --git a/app/apps/splits/tests/test_splits.py b/app/apps/splits/tests/test_splits.py index 9894bbcb..7a0e5376 100644 --- a/app/apps/splits/tests/test_splits.py +++ b/app/apps/splits/tests/test_splits.py @@ -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(