A local-first personal finance app
Reapply rules to split transactions when the parent changes (#2834)
Julian Dominguez-Schatza42938fa64a9
diff --git a/packages/desktop-client/e2e/rules.test.js-snapshots/Rules-creates-a-split-transaction-rule-and-makes-sure-it-is-applied-when-creating-a-transaction-3-chromium-linux.png b/packages/desktop-client/e2e/rules.test.js-snapshots/Rules-creates-a-split-transaction-rule-and-makes-sure-it-is-applied-when-creating-a-transaction-3-chromium-linux.pngindex 7490e7d331..b6bd1b7d28 100644Binary files a/packages/desktop-client/e2e/rules.test.js-snapshots/Rules-creates-a-split-transaction-rule-and-makes-sure-it-is-applied-when-creating-a-transaction-3-chromium-linux.png and b/packages/desktop-client/e2e/rules.test.js-snapshots/Rules-creates-a-split-transaction-rule-and-makes-sure-it-is-applied-when-creating-a-transaction-3-chromium-linux.png differdiff --git a/packages/desktop-client/src/components/table.tsx b/packages/desktop-client/src/components/table.tsxindex 81516a418d..b354c6b353 100644--- a/packages/desktop-client/src/components/table.tsx+++ b/packages/desktop-client/src/components/table.tsx@@ -325,6 +325,12 @@ function InputValue({...props}: InputValueProps) {const [value, setValue] = useState(defaultValue);+ const [prevDefaultValue, setPrevDefaultValue] = useState(defaultValue);++ if (prevDefaultValue !== defaultValue) {+ setValue(defaultValue);+ setPrevDefaultValue(defaultValue);+ }function onBlur_(e) {if (onBlur) {diff --git a/packages/desktop-client/src/components/transactions/TransactionList.jsx b/packages/desktop-client/src/components/transactions/TransactionList.jsxindex 95c6e66fb5..137cc61bc5 100644--- a/packages/desktop-client/src/components/transactions/TransactionList.jsx+++ b/packages/desktop-client/src/components/transactions/TransactionList.jsx@@ -137,25 +137,44 @@ export function TransactionList({return changes.diff.added[0].id;}, []);- const onApplyRules = useCallback(async transaction => {- const afterRules = await send('rules-run', { transaction });- const diff = getChangedValues(transaction, afterRules);-- const newTransaction = { ...transaction };- if (diff) {- Object.keys(diff).forEach(field => {+ const onApplyRules = useCallback(+ async (transaction, updatedFieldName = null) => {+ const afterRules = await send('rules-run', { transaction });+ const diff = getChangedValues(transaction, afterRules);++ const newTransaction = { ...transaction };+ if (diff) {+ Object.keys(diff).forEach(field => {+ if (+ newTransaction[field] == null ||+ newTransaction[field] === '' ||+ newTransaction[field] === 0 ||+ newTransaction[field] === false+ ) {+ newTransaction[field] = diff[field];+ }+ });++ // When a rule updates a parent transaction, overwrite all changes to the current field in subtransactions.if (- newTransaction[field] == null ||- newTransaction[field] === '' ||- newTransaction[field] === 0 ||- newTransaction[field] === false+ transaction.is_parent &&+ diff.subtransactions !== undefined &&+ updatedFieldName !== null) {- newTransaction[field] = diff[field];+ newTransaction.subtransactions = diff.subtransactions.map(+ (st, idx) => ({+ ...(newTransaction.subtransactions[idx] || st),+ ...(st[updatedFieldName] != null && {+ [updatedFieldName]: st[updatedFieldName],+ }),+ }),+ );}- });- }- return newTransaction;- }, []);+ }+ return newTransaction;+ },+ [],+ );const onManagePayees = useCallback(id => {navigate('/payees', { state: { selectedPayee: id } });diff --git a/packages/desktop-client/src/components/transactions/TransactionsTable.jsx b/packages/desktop-client/src/components/transactions/TransactionsTable.jsxindex 3633f8fa9b..b860929876 100644--- a/packages/desktop-client/src/components/transactions/TransactionsTable.jsx+++ b/packages/desktop-client/src/components/transactions/TransactionsTable.jsx@@ -818,7 +818,10 @@ const Transaction = memo(function Transaction({// it's always showing the formatted resultsetTransaction(serializeTransaction(deserialized, showZeroInDeposit));- onSave(deserialized, subtransactions);+ const deserializedName = ['credit', 'debit'].includes(name)+ ? 'amount'+ : name;+ onSave(deserialized, subtransactions, deserializedName);}}@@ -2053,7 +2056,7 @@ export const TransactionTable = forwardRef((props, ref) => {}, [props.onAdd, newNavigator.onEdit]);const onSave = useCallback(- async (transaction, subtransactions = null) => {+ async (transaction, subtransactions = null, updatedFieldName = null) => {savePending.current = true;let groupedTransaction = subtransactions@@ -2062,7 +2065,10 @@ export const TransactionTable = forwardRef((props, ref) => {if (isTemporaryId(transaction.id)) {if (props.onApplyRules) {- groupedTransaction = await props.onApplyRules(groupedTransaction);+ groupedTransaction = await props.onApplyRules(+ groupedTransaction,+ updatedFieldName,+ );}const newTrans = latestState.current.newTransactions;diff --git a/packages/loot-core/src/shared/transactions.ts b/packages/loot-core/src/shared/transactions.tsindex 773d59fac7..d56389ca3e 100644--- a/packages/loot-core/src/shared/transactions.ts+++ b/packages/loot-core/src/shared/transactions.ts@@ -241,7 +241,9 @@ export function updateTransaction(return replaceTransactions(transactions, transaction.id, trans => {if (trans.is_parent) {const parent = trans.id === transaction.id ? transaction : trans;- const sub = trans.subtransactions?.map(t => {+ const originalSubtransactions =+ parent.subtransactions ?? trans.subtransactions;+ const sub = originalSubtransactions?.map(t => {// Make sure to update the children to reflect the updated// properties (if the parent updated)diff --git a/upcoming-release-notes/2834.md b/upcoming-release-notes/2834.mdnew file mode 100644index 0000000000..e0a2a73c10--- /dev/null+++ b/upcoming-release-notes/2834.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [jfdoming]+---++Reapply rules to split transactions when the parent changes