A local-first personal finance app
Split transactions repair tool to remove categories from parent transactions + prevent adding/updating parent transactions with categories (#4889)
Joel Jeremy Marquez82c469648e7a
diff --git a/packages/desktop-client/src/components/settings/RepairTransactions.tsx b/packages/desktop-client/src/components/settings/RepairTransactions.tsxindex 71cbad7608..cc4e6ca461 100644--- a/packages/desktop-client/src/components/settings/RepairTransactions.tsx+++ b/packages/desktop-client/src/components/settings/RepairTransactions.tsx@@ -25,6 +25,7 @@ function useRenderResults() {numTransfersFixed,mismatchedSplits,numNonParentErrorsFixed,+ numParentTransactionsWithCategoryFixed,} = results;const result: string[] = [];@@ -34,7 +35,8 @@ function useRenderResults() {numDeleted === 0 &&numTransfersFixed === 0 &&numNonParentErrorsFixed === 0 &&- mismatchedSplits.length === 0+ mismatchedSplits.length === 0 &&+ numParentTransactionsWithCategoryFixed === 0) {result.push(t('No split transactions found needing repair.'));} else {@@ -85,6 +87,13 @@ function useRenderResults() {) + `\n${mismatchedSplitInfo}`,);}+ if (numParentTransactionsWithCategoryFixed > 0) {+ result.push(+ t('Fixed {{count}} split transactions with non-null category.', {+ count: numParentTransactionsWithCategoryFixed,+ }),+ );+ }}return (@@ -170,6 +179,10 @@ export function RepairTransactions() {Check if you have any budget transfers that erroneous contain acategory, and remove the category.</li>+ <li>+ Checks for any parent transactions with a category and removes the+ category if found.+ </li></ul></Trans></Setting>diff --git a/packages/loot-core/src/server/tools/app.ts b/packages/loot-core/src/server/tools/app.tsindex ab664dd30e..6b096ee7cf 100644--- a/packages/loot-core/src/server/tools/app.ts+++ b/packages/loot-core/src/server/tools/app.ts@@ -21,6 +21,7 @@ async function fixSplitTransactions(): Promise<{numDeleted: number;numTransfersFixed: number;numNonParentErrorsFixed: number;+ numParentTransactionsWithCategoryFixed: number;mismatchedSplits: TransactionEntity[];}> {// 1. Check for child transactions that have a blank payee, and set@@ -123,12 +124,29 @@ async function fixSplitTransactions(): Promise<{await batchUpdateTransactions({ updated });});+ // 7. Clear categories of parent transactions+ const parentTransactionsWithCategory = await db.all<+ Pick<db.DbViewTransactionInternal, 'id'>+ >(`+ SELECT id FROM transactions WHERE isParent = 1 AND category IS NOT NULL+ `);++ await runMutator(async () => {+ const updated = parentTransactionsWithCategory.map(({ id }) => ({+ id,+ category: null,+ }));+ await batchUpdateTransactions({ updated });+ });+return {numBlankPayees: blankPayeeRows.length,numCleared: clearedRows.length,numDeleted: deletedRows.length,numTransfersFixed: brokenTransfers.length,numNonParentErrorsFixed: errorRows.length,+ numParentTransactionsWithCategoryFixed:+ parentTransactionsWithCategory.length,mismatchedSplits,};}diff --git a/packages/loot-core/src/server/transactions/index.ts b/packages/loot-core/src/server/transactions/index.tsindex 0814c2093b..eda7a4b311 100644--- a/packages/loot-core/src/server/transactions/index.ts+++ b/packages/loot-core/src/server/transactions/index.ts@@ -81,8 +81,9 @@ export async function batchUpdateTransactions({if (added) {addedIds = await Promise.all(added.map(async t => {+ // Offbudget account transactions and parent transactions should not have categories.const account = accounts.find(acct => acct.id === t.account);- if (account.offbudget === 1) {+ if (t.is_parent || account.offbudget === 1) {t.category = null;}return db.insertTransaction(t);@@ -107,9 +108,9 @@ export async function batchUpdateTransactions({updated.map(async t => {if (t.account) {// Moving transactions off budget should always clear the- // category+ // category. Parent transactions should not have categories.const account = accounts.find(acct => acct.id === t.account);- if (account.offbudget === 1) {+ if (t.is_parent || account.offbudget === 1) {t.category = null;}}diff --git a/upcoming-release-notes/4889.md b/upcoming-release-notes/4889.mdnew file mode 100644index 0000000000..a98f689eb2--- /dev/null+++ b/upcoming-release-notes/4889.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [joel-jeremy]+---++Add a functionality to split transactions repair tool to remove categories from parent transactions and prevent adding/update parent transactions with categories