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

Merged
merged 10 commits into from
Dec 24, 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
154 changes: 97 additions & 57 deletions packages/desktop-client/src/components/settings/FixSplits.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useTranslation, Trans } from 'react-i18next';

import { send } from 'loot-core/src/platform/client/fetch';
import { type Handlers } from 'loot-core/src/types/handlers';
Expand All @@ -14,53 +14,85 @@ import { Setting } from './UI';

type Results = Awaited<ReturnType<Handlers['tools/fix-split-transactions']>>;

function renderResults(results: Results) {
const { numBlankPayees, numCleared, numDeleted } = results;
let result = '';
function useRenderResults() {
const { t } = useTranslation();

if (numBlankPayees === 0 && numCleared === 0 && numDeleted === 0) {
result = 'No split transactions found needing repair.';
} else {
if (numBlankPayees > 0) {
result += `Fixed ${numBlankPayees} splits with a blank payee.`;
}
if (numCleared > 0) {
if (result !== '') {
result += '\n';
function renderResults(results: Results) {
const { numBlankPayees, numCleared, numDeleted, mismatchedSplits } =
results;
const result: string[] = [];

if (
numBlankPayees === 0 &&
numCleared === 0 &&
numDeleted === 0 &&
mismatchedSplits.length === 0
) {
result.push(t('No split transactions found needing repair.'));
} else {
if (numBlankPayees > 0) {
result.push(
t('Fixed {{count}} splits with a blank payee.', {
count: numBlankPayees,
}),
);
}
result += `Fixed ${numCleared} splits with the wrong cleared flag.`;
}
if (numDeleted > 0) {
if (result !== '') {
result += '\n';
if (numCleared > 0) {
result.push(
t('Fixed {{count}} splits with the wrong cleared flag.', {
count: numCleared,
}),
);
}
if (numDeleted > 0) {
result.push(
t('Fixed {{count}} splits that weren’t properly deleted.', {
count: numDeleted,
}),
);
}
if (mismatchedSplits.length > 0) {
const mismatchedSplitInfo = mismatchedSplits
.map(t => `- ${t.date}`)
.join('\n');

result.push(
t(
'Found {{count}} split transactions with mismatched amounts on the below dates. Please review them manually:',
{ count: mismatchedSplits.length },
) + `\n${mismatchedSplitInfo}`,
);
}
result += `Fixed ${numDeleted} splits that weren’t properly deleted.`;
}

return (
<Paragraph
style={{
color:
mismatchedSplits.length === 0
? theme.noticeTextLight
: theme.errorText,
whiteSpace: 'pre-wrap',
}}
>
{result.join('\n')}
</Paragraph>
);
}

return (
<Paragraph
style={{
color: theme.noticeTextLight,
marginBottom: 0,
marginLeft: '1em',
textAlign: 'right',
whiteSpace: 'pre-wrap',
}}
>
{result}
</Paragraph>
);
return { renderResults };
}

export function FixSplits() {
const { t } = useTranslation();
const [loading, setLoading] = useState(false);
const [results, setResults] = useState<Results | null>(null);

const { renderResults } = useRenderResults();

async function onFix() {
setLoading(true);
const res = await send('tools/fix-split-transactions');

setResults(res);
setLoading(false);
}
Expand All @@ -70,38 +102,46 @@ 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}>
{t('Repair split transactions')}
<Trans>Repair split transactions</Trans>
</ButtonWithLoading>
{results && renderResults(results)}
</View>
}
>
<Text>
<strong>{t('Repair split transactions')}</strong>
{t(
' if you are experiencing bugs relating to split transactions and the “Reset budget cache” button above does not help, this tool may fix them. Some examples of bugs include seeing blank payees on splits or incorrect account balances. This tool does two things:',
)}
</Text>
<ul style={{ margin: 0, paddingLeft: '1.5em' }}>
<li style={{ marginBottom: '0.5em' }}>
{t(
'Ensures that deleted split transactions are fully deleted. In previous versions of the app, certain split transactions may appear deleted but not all of them are actually deleted. This causes the transactions list to look correct, but certain balances may be incorrect when filtering.',
)}
</li>
<li>
{t(
'Sync the payee and cleared flag of a split transaction to the main or “parent” transaction, if appropriate. The payee will only be set if it currently doesn’t have one.',
)}
</li>
</ul>
<Trans>
<Text>
<strong>Repair split transactions</strong> if you are experiencing
bugs relating to split transactions and the “Reset budget cache”
button above does not help, this tool may fix them. Some examples of
bugs include seeing blank payees on splits or incorrect account
balances. This tool does three things:
</Text>
<ul style={{ margin: 0, paddingLeft: '1.5em' }}>
<li style={{ marginBottom: '0.5em' }}>
Ensures that deleted split transactions are fully deleted. In
previous versions of the app, certain split transactions may appear
deleted but not all of them are actually deleted. This causes the
transactions list to look correct, but certain balances may be
incorrect when filtering.
</li>
<li>
Sync the payee and cleared flag of a split transaction to the main
or “parent” transaction, if appropriate. The payee will only be set
if it currently doesn’t have one.
</li>
<li>
Checks that the sum of all child transactions adds up to the total
amount. If not, these will be flagged below to allow you to easily
locate and fix the amounts.
</li>
</ul>
</Trans>
</Setting>
);
}
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