A local-first personal finance app
fix: Unsplit transaction without losing the parent’s data. (#6501)
Ilia Skliarukd52f920e988f
diff --git a/packages/loot-core/src/shared/transactions.test.ts b/packages/loot-core/src/shared/transactions.test.tsindex cdd3d69e7d..aa5d6edbe1 100644--- a/packages/loot-core/src/shared/transactions.test.ts+++ b/packages/loot-core/src/shared/transactions.test.ts@@ -9,6 +9,7 @@ import {deleteTransaction,addSplitTransaction,makeChild,+ makeAsNonChildTransactions,} from './transactions';function makeTransaction(data: Partial<TransactionEntity>): TransactionEntity {@@ -225,4 +226,29 @@ describe('Transactions', () => {expect.objectContaining({ amount: 3002 }),]);});++ test('unsplitting last remaining child converts parent to regular transaction', () => {+ const [parent, child] = makeSplitTransaction(+ { id: 't1', amount: 2000, category: 'cat1' },+ [{ id: 't2', amount: 0, category: 'cat2' }],+ );++ const transactions = [parent, child];++ const result = makeAsNonChildTransactions([child], transactions);++ expect(result.updated).toHaveLength(1);+ expect(result.deleted).toHaveLength(1);++ expect(result.updated[0]).toMatchObject({+ id: 't1',+ amount: 2000,+ is_parent: false,+ category: 'cat2',+ });++ expect(result.deleted[0]).toMatchObject({+ id: 't2',+ });+ });});diff --git a/packages/loot-core/src/shared/transactions.ts b/packages/loot-core/src/shared/transactions.tsindex 7137d81981..699d1001c2 100644--- a/packages/loot-core/src/shared/transactions.ts+++ b/packages/loot-core/src/shared/transactions.ts@@ -72,6 +72,17 @@ function makeNonChild<T extends GenericTransactionEntity>(} as unknown as T;}+function makeTransactionWithChildCategory<T extends GenericTransactionEntity>(+ parent: T,+ data: Partial<TransactionEntity>,+) {+ return {+ ...parent,+ is_parent: false,+ category: data.category || null,+ } as unknown as T;+}+export function recalculateSplit(trans: TransactionEntity) {// Calculate the new total of split transactions and make sure// that it equals the parent amount@@ -375,6 +386,21 @@ export function makeAsNonChildTransactions(t =>!newNonChildTransactions.some(updatedTrans => updatedTrans.id === t.id),);+ if (+ childTransactions.length === 1 &&+ childTransactionsToUpdate.length === 1 &&+ childTransactionsToUpdate[0].id === childTransactions[0].id+ ) {+ return {+ updated: [+ makeTransactionWithChildCategory(+ parentTransaction,+ childTransactionsToUpdate[0],+ ),+ ],+ deleted: [childTransactionsToUpdate[0]],+ };+ }const nonChildTransactionsToUpdate =remainingChildTransactions.length === 1diff --git a/upcoming-release-notes/6501.md b/upcoming-release-notes/6501.mdnew file mode 100644index 0000000000..e61dd96268--- /dev/null+++ b/upcoming-release-notes/6501.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [skliaruk]+---++Unsplit transaction without losing parent's transaction information (amount, notes, etc.)