A local-first personal finance app
[AI] Fix updateTransaction crash when converting a transaction to a split (#8467)
Atur Dana5980acd5a40f
diff --git a/packages/loot-core/src/shared/transactions.test.ts b/packages/loot-core/src/shared/transactions.test.tsindex a28cd2e536..7ca708c22f 100644--- a/packages/loot-core/src/shared/transactions.test.ts+++ b/packages/loot-core/src/shared/transactions.test.ts@@ -383,4 +383,42 @@ describe('Transactions', () => {id: 't2',});});++ test('converting a simple transaction into a split stamps children with the parent account (#8207)', () => {+ const transactions = [+ makeTransaction({ amount: 2001 }),+ makeTransaction({ id: 't1', amount: 5000, account: 'acc-id-1' }),+ makeTransaction({ amount: 3002 }),+ ];++ // Mirrors `api.updateTransaction(id, { subtransactions: [...] })`, which+ // reaches this reducer as `updateTransaction(transactions, { id, ...fields })`+ // (see `api/transaction-update` in server/api.ts).+ const { data, diff } = updateTransaction(transactions, {+ id: 't1',+ subtransactions: [{ amount: 4000 }, { amount: 1000 }],+ } as TransactionEntity);++ const parent = data.find(d => d.id === 't1');+ expect(parent?.is_parent).toBe(true);++ const children = data.filter(t => t.parent_id === 't1');+ expect(children).toHaveLength(2);+ // Regression (#8207): children used to be emitted without an account/date,+ // so the DB rejected the insert with+ // `"account" is required for table "transactions"`.+ for (const child of children) {+ expect(child.account).toBe('acc-id-1');+ expect(child.date).toBe('2020-01-05');+ expect(child.is_child).toBe(true);+ expect(child.parent_id).toBe('t1');+ }+ expect(children.map(c => c.amount).sort((a, b) => b - a)).toEqual([+ 4000, 1000,+ ]);++ // The new child rows are inserted, and every one must carry the account.+ expect(diff.added).toHaveLength(2);+ expect(diff.added.every(t => t.account === 'acc-id-1')).toBe(true);+ });});diff --git a/packages/loot-core/src/shared/transactions.ts b/packages/loot-core/src/shared/transactions.tsindex 549985c89e..9adbac62ac 100644--- a/packages/loot-core/src/shared/transactions.ts+++ b/packages/loot-core/src/shared/transactions.ts@@ -300,6 +300,31 @@ export function updateTransaction(...parent,...(sub && { subtransactions: sub }),});+ } else if (+ transaction.subtransactions &&+ transaction.subtransactions.length > 0+ ) {+ // Converting a simple (non-split) transaction into a split — e.g.+ // `api.updateTransaction(id, { subtransactions: [...] })`. Mark it as a+ // parent and materialise each subtransaction as a proper child so it+ // inherits the parent's account/date; otherwise the children are inserted+ // without an `account` and the DB rejects them (#8207).+ const parent = {+ ...trans,+ ...transaction,+ is_parent: true,+ is_child: false,+ parent_id: undefined,+ };+ return recalculateSplit({+ ...parent,+ subtransactions: transaction.subtransactions.map((sub, index) =>+ makeChild(parent, {+ ...sub,+ sort_order: sub.sort_order ?? -(index + 1),+ }),+ ),+ });} else {return transaction;}diff --git a/upcoming-release-notes/fix-api-update-transaction-split.md b/upcoming-release-notes/fix-api-update-transaction-split.mdnew file mode 100644index 0000000000..fdbb867a77--- /dev/null+++ b/upcoming-release-notes/fix-api-update-transaction-split.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [aturzone]+---++Fixed a crash when converting a regular transaction into a split via the API.