Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug - Manually entered split transactions are not cleared on import #200 #1465

Merged
merged 17 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/loot-core/src/server/accounts/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,20 @@ export async function reconcileGoCardlessTransactions(acctId, transactions) {
await createNewPayees(payeesToCreate, [...added, ...updated]);
await batchUpdateTransactions({ added, updated });

// Make sure the "cleared" flag is synced up with the parent
// transactions
let clearedRows = await db.all(`
SELECT t.id, p.cleared FROM v_transactions t
LEFT JOIN v_transactions p ON t.parent_id = p.id
WHERE t.is_child = 1 AND t.cleared != p.cleared
`);

let updatedClearedRows = clearedRows.map(row => ({
id: row.id,
cleared: row.cleared === 1,
}));
await batchUpdateTransactions({ updated: updatedClearedRows });

Copy link
Contributor

@kyrias kyrias Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having to query every transaction like this seems very inefficient. I think the following would be a better solution:

diff --git a/packages/loot-core/src/server/accounts/sync.ts b/packages/loot-core/src/server/accounts/sync.ts
index 4733dc6f..4cbea6d5 100644
--- a/packages/loot-core/src/server/accounts/sync.ts
+++ b/packages/loot-core/src/server/accounts/sync.ts
@@ -431,7 +431,7 @@ export async function reconcileGoCardlessTransactions(acctId, transactions) {
       // matched transaction. See the final pass below for the needed
       // fields.
       fuzzyDataset = await db.all(
-        `SELECT id, date, imported_id, payee, category, notes FROM v_transactions
+        `SELECT id, is_parent, date, imported_id, payee, category, notes FROM v_transactions
            WHERE date >= ? AND date <= ? AND amount = ? AND account = ? AND is_child = 0`,
         [
           db.toDateRepr(monthUtils.subDays(trans.date, 4)),
@@ -509,6 +509,17 @@ export async function reconcileGoCardlessTransactions(acctId, transactions) {
       if (hasFieldsChanged(existing, updates, Object.keys(updates))) {
         updated.push({ id: existing.id, ...updates });
       }
+
+      if (existing.is_parent && existing.cleared !== updates.cleared) {
+        const children = await db.all(
+          'SELECT id FROM v_transactions WHERE parent_id = ?',
+          [existing.id],
+        );
+        for (const child of children) {
+          updated.push({ id: child.id, cleared: updates.cleared });
+        }
+      }
     } else {
       // Insert a new transaction
       let finalTransaction = {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Thanks for for the suggestion

I've updated it in both the reconcileGoCardlessTransactions and reconcileTransactions functions

Please review

return {
added: added.map(trans => trans.id),
updated: updated.map(trans => trans.id),
Expand Down Expand Up @@ -682,6 +696,20 @@ export async function reconcileTransactions(acctId, transactions) {
await createNewPayees(payeesToCreate, [...added, ...updated]);
await batchUpdateTransactions({ added, updated });

// Make sure the "cleared" flag is synced up with the parent
// transactions
let clearedRows = await db.all(`
SELECT t.id, p.cleared FROM v_transactions t
LEFT JOIN v_transactions p ON t.parent_id = p.id
WHERE t.is_child = 1 AND t.cleared != p.cleared
`);

let updatedClearedRows = clearedRows.map(row => ({
id: row.id,
cleared: row.cleared === 1,
}));
await batchUpdateTransactions({ updated: updatedClearedRows });

return {
added: added.map(trans => trans.id),
updated: updated.map(trans => trans.id),
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/1465.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [kstockk]
---

Fixed clearing split transactions when importing matched transactions
Loading