A local-first personal finance app
[AI] Keep split child fields when updating a transaction through the API (#9307)
Alok Ranjan160b2c99aa28
diff --git a/packages/api/methods.test.ts b/packages/api/methods.test.tsindex e5cba328c0..552c6b4195 100644--- a/packages/api/methods.test.ts+++ b/packages/api/methods.test.ts@@ -954,6 +954,56 @@ describe('API CRUD operations', () => {expect(transactions).toHaveLength(1);});+ test('Transactions: updating a split child keeps its other fields', async () => {+ const accountId = await api.createAccount({ name: 'test-account' }, 0);+ const groupId = await api.createCategoryGroup({ name: 'tests' });+ const categoryId = await api.createCategory({+ name: 'test-split',+ group_id: groupId,+ });++ await api.addTransactions(accountId, [+ {+ date: '2023-11-03',+ amount: 300,+ subtransactions: [+ { amount: 100, category: categoryId, notes: 'first' },+ { amount: 200, category: categoryId, notes: 'second' },+ ],+ },+ ]);++ let [parent] = await api.getTransactions(+ accountId,+ '2023-11-01',+ '2023-11-30',+ );+ const child = parent.subtransactions?.find(t => t.notes === 'first');+ if (!child) {+ throw new Error('split child not found');+ }++ await api.updateTransaction(child.id, { notes: 'updated' });++ [parent] = await api.getTransactions(accountId, '2023-11-01', '2023-11-30');+ expect(parent.subtransactions).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ id: child.id,+ notes: 'updated',+ amount: 100,+ category: categoryId,+ }),+ expect.objectContaining({+ notes: 'second',+ amount: 200,+ category: categoryId,+ }),+ ]),+ );+ expect(parent.amount).toBe(300);+ });+// apis: mergeTransactionstest('Transactions: successfully merge two transactions', async () => {const accountId = await api.createAccount({ name: 'test-account' }, 0);diff --git a/packages/loot-core/src/server/api.ts b/packages/loot-core/src/server/api.tsindex 43d2b1c2f1..d9f62fcde9 100644--- a/packages/loot-core/src/server/api.ts+++ b/packages/loot-core/src/server/api.ts@@ -652,13 +652,14 @@ handlers['api/transaction-update'] = withMutation(async function ({);const transactions = ungroupTransactions(data);- if (transactions.length === 0) {+ const existing = transactions.find(t => t.id === id);+ if (!existing) {return [];}- // @ts-expect-error - fix me- const { diff } = updateTransaction(transactions, { id, ...fields });- return handlers['transactions-batch-update'](diff)['updated'];+ // `fields` is partial; updateTransaction replaces a split child wholesale+ const { diff } = updateTransaction(transactions, { ...existing, ...fields });+ return (await handlers['transactions-batch-update'](diff))['updated'];});handlers['api/transaction-delete'] = withMutation(async function ({ id }) {diff --git a/packages/loot-core/src/shared/transactions.test.ts b/packages/loot-core/src/shared/transactions.test.tsindex 7ca708c22f..9162d93aec 100644--- a/packages/loot-core/src/shared/transactions.test.ts+++ b/packages/loot-core/src/shared/transactions.test.ts@@ -392,7 +392,7 @@ describe('Transactions', () => {];// Mirrors `api.updateTransaction(id, { subtransactions: [...] })`, which- // reaches this reducer as `updateTransaction(transactions, { id, ...fields })`+ // reaches this reducer as `updateTransaction(transactions, { ...existing, ...fields })`// (see `api/transaction-update` in server/api.ts).const { data, diff } = updateTransaction(transactions, {id: 't1',diff --git a/upcoming-release-notes/api-update-split-child-fields.md b/upcoming-release-notes/api-update-split-child-fields.mdnew file mode 100644index 0000000000..d065ecb232--- /dev/null+++ b/upcoming-release-notes/api-update-split-child-fields.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [aloki-alok]+---++Keep the category and amount of a split transaction's child when updating some of its fields through the API