Skip to content

Commit

Permalink
Don't consider payees used in rules as orphaned (#3609)
Browse files Browse the repository at this point in the history
* don't consider payees in rules as orphaned

* release note

* lint

* indentation

* ignore deleted rules
  • Loading branch information
matt-fidd authored Oct 9, 2024
1 parent 6d122c8 commit e455369
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
34 changes: 22 additions & 12 deletions packages/loot-core/src/server/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,22 +580,32 @@ export function getCommonPayees() {
`);
}

/* eslint-disable rulesdir/typography */
const orphanedPayeesQuery = `
SELECT p.id
FROM payees p
LEFT JOIN payee_mapping pm ON pm.id = p.id
LEFT JOIN v_transactions_internal_alive t ON t.payee = pm.targetId
WHERE p.tombstone = 0
AND p.transfer_acct IS NULL
AND t.id IS NULL
AND NOT EXISTS (
SELECT 1
FROM rules r,
json_each(r.conditions) as cond
WHERE r.tombstone = 0
AND json_extract(cond.value, '$.field') = 'description'
AND json_extract(cond.value, '$.value') = pm.targetId
);
`;
/* eslint-enable rulesdir/typography */

export function syncGetOrphanedPayees() {
return all(`
SELECT p.id FROM payees p
LEFT JOIN payee_mapping pm ON pm.id = p.id
LEFT JOIN v_transactions_internal_alive t ON t.payee = pm.targetId
WHERE p.tombstone = 0 AND p.transfer_acct IS NULL AND t.id IS NULL
`);
return all(orphanedPayeesQuery);
}

export async function getOrphanedPayees() {
const rows = await all(`
SELECT p.id FROM payees p
LEFT JOIN payee_mapping pm ON pm.id = p.id
LEFT JOIN v_transactions_internal_alive t ON t.payee = pm.targetId
WHERE p.tombstone = 0 AND p.transfer_acct IS NULL AND t.id IS NULL
`);
const rows = await all(orphanedPayeesQuery);
return rows.map(row => row.id);
}

Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3609.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [matt-fidd]
---

Don't consider payees used in rules as orphaned

0 comments on commit e455369

Please sign in to comment.