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

Don't consider payees used in rules as orphaned #3609

Merged
merged 6 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This shouldn't be necessary, but lint is complaining

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