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

extend fix splits tool to report splits with mismatched amounts #3970

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
52 changes: 29 additions & 23 deletions packages/desktop-client/src/components/settings/FixSplits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,47 @@ import { Setting } from './UI';
type Results = Awaited<ReturnType<Handlers['tools/fix-split-transactions']>>;

function renderResults(results: Results) {
const { numBlankPayees, numCleared, numDeleted } = results;
let result = '';
const { numBlankPayees, numCleared, numDeleted, mismatchedSplits } = results;
const result: string[] = [];

if (numBlankPayees === 0 && numCleared === 0 && numDeleted === 0) {
result = 'No split transactions found needing repair.';
if (
numBlankPayees === 0 &&
numCleared === 0 &&
numDeleted === 0 &&
mismatchedSplits.length === 0
) {
result.push('No split transactions found needing repair.');
} else {
if (numBlankPayees > 0) {
result += `Fixed ${numBlankPayees} splits with a blank payee.`;
result.push(`Fixed ${numBlankPayees} splits with a blank payee.`);
}
if (numCleared > 0) {
if (result !== '') {
result += '\n';
}
result += `Fixed ${numCleared} splits with the wrong cleared flag.`;
result.push(`Fixed ${numCleared} splits with the wrong cleared flag.`);
}
if (numDeleted > 0) {
if (result !== '') {
result += '\n';
}
result += `Fixed ${numDeleted} splits that weren’t properly deleted.`;
result.push(`Fixed ${numDeleted} splits that weren’t properly deleted.`);
}
if (mismatchedSplits.length > 0) {
result.push(
`Found ${mismatchedSplits.length} split transaction${mismatchedSplits.length > 1 ? 's' : ''} ` +
`with mismatched amounts on the below date${mismatchedSplits.length > 1 ? 's' : ''}. ` +
`Please fix ${mismatchedSplits.length > 1 ? 'these' : 'this'} manually:\n` +
mismatchedSplits.map(t => `- ${t.date}`).join('\n'),
);
}
}

return (
<Paragraph
style={{
color: theme.noticeTextLight,
marginBottom: 0,
marginLeft: '1em',
textAlign: 'right',
color:
mismatchedSplits.length === 0
? theme.noticeTextLight
: theme.errorText,
whiteSpace: 'pre-wrap',
}}
>
{result}
{result.join('\n')}
</Paragraph>
);
}
Expand All @@ -61,6 +68,7 @@ export function FixSplits() {
async function onFix() {
setLoading(true);
const res = await send('tools/fix-split-transactions');

setResults(res);
setLoading(false);
}
Expand All @@ -70,11 +78,9 @@ export function FixSplits() {
primaryAction={
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
maxWidth: 500,
width: '100%',
alignItems: 'center',
flexDirection: 'column',
alignItems: 'flex-start',
gap: '1em',
}}
>
<ButtonWithLoading isLoading={loading} onPress={onFix}>
Expand Down
20 changes: 20 additions & 0 deletions packages/loot-core/src/server/tools/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// @ts-strict-ignore
import { q } from '../../shared/query';
import { batchUpdateTransactions } from '../accounts/transactions';
import { createApp } from '../app';
import { runQuery } from '../aql';
import * as db from '../db';
import { runMutator } from '../mutators';

Expand Down Expand Up @@ -54,9 +56,27 @@ app.method('tools/fix-split-transactions', async () => {
await batchUpdateTransactions({ updated });
});

const splitTransactions = (
await runQuery(
q('transactions')
.options({ splits: 'grouped' })
.filter({
is_parent: true,
})
.select('*'),
)
).data;

const mismatchedSplits = splitTransactions.filter(t => {
const subValue = t.subtransactions.reduce((acc, st) => acc + st.amount, 0);

return subValue !== t.amount;
});
matt-fidd marked this conversation as resolved.
Show resolved Hide resolved

return {
numBlankPayees: blankPayeeRows.length,
numCleared: clearedRows.length,
numDeleted: deletedRows.length,
mismatchedSplits,
};
});
3 changes: 3 additions & 0 deletions packages/loot-core/src/server/tools/types/handlers.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { TransactionEntity } from './models';

export interface ToolsHandlers {
'tools/fix-split-transactions': () => Promise<{
numBlankPayees: number;
numCleared: number;
numDeleted: number;
mismatchedSplits: TransactionEntity[];
}>;
}
6 changes: 6 additions & 0 deletions upcoming-release-notes/3970.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [matt-fidd]
---

Extend fix splits tool to report splits with mismatched amounts
Loading