Skip to content

A local-first personal finance app

prevent closing an account into itself (#5462)

Matt Fiddaman80cd2cf347bc
diff --git a/packages/desktop-client/src/components/autocomplete/AccountAutocomplete.tsx b/packages/desktop-client/src/components/autocomplete/AccountAutocomplete.tsx
index 9178ec309b..29173fe208 100644
--- a/packages/desktop-client/src/components/autocomplete/AccountAutocomplete.tsx
+++ b/packages/desktop-client/src/components/autocomplete/AccountAutocomplete.tsx
@@ -101,6 +101,7 @@ type AccountAutocompleteProps = ComponentProps<
> & {
embedded?: boolean;
includeClosedAccounts?: boolean;
+ hiddenAccounts?: AccountEntity['id'][];
renderAccountItemGroupHeader?: (
props: ComponentPropsWithoutRef<typeof ItemHeader>,
) => ReactElement<typeof ItemHeader>;
@@ -116,6 +117,7 @@ export function AccountAutocomplete({
renderAccountItemGroupHeader,
renderAccountItem,
closeOnBlur,
+ hiddenAccounts,
...props
}: AccountAutocompleteProps) {
const accounts = useAccounts() || [];
@@ -124,7 +126,10 @@ export function AccountAutocomplete({
//then sort by closed, then offbudget
const accountSuggestions: AccountAutocompleteItem[] = accounts
.filter(item => {
- return includeClosedAccounts ? item : !item.closed;
+ return (
+ (includeClosedAccounts ? item : !item.closed) &&
+ !hiddenAccounts?.includes(item.id)
+ );
})
.sort(
(a, b) =>
diff --git a/packages/desktop-client/src/components/modals/AccountAutocompleteModal.tsx b/packages/desktop-client/src/components/modals/AccountAutocompleteModal.tsx
index 30419b0b34..73c056f313 100644
--- a/packages/desktop-client/src/components/modals/AccountAutocompleteModal.tsx
+++ b/packages/desktop-client/src/components/modals/AccountAutocompleteModal.tsx
@@ -23,6 +23,7 @@ type AccountAutocompleteModalProps = Extract<
export function AccountAutocompleteModal({
onSelect,
includeClosedAccounts,
+ hiddenAccounts,
onClose,
}: AccountAutocompleteModalProps) {
const { t } = useTranslation();
@@ -83,6 +84,7 @@ export function AccountAutocompleteModal({
{...defaultAutocompleteProps}
onSelect={onSelect}
includeClosedAccounts={includeClosedAccounts}
+ hiddenAccounts={hiddenAccounts}
value={null}
/>
</View>
diff --git a/packages/desktop-client/src/components/modals/CloseAccountModal.tsx b/packages/desktop-client/src/components/modals/CloseAccountModal.tsx
index 194abb8e49..54922e7386 100644
--- a/packages/desktop-client/src/components/modals/CloseAccountModal.tsx
+++ b/packages/desktop-client/src/components/modals/CloseAccountModal.tsx
@@ -182,6 +182,7 @@ export function CloseAccountModal({
<View style={{ marginBottom: 15 }}>
<AccountAutocomplete
includeClosedAccounts={false}
+ hiddenAccounts={[account.id]}
value={transferAccountId}
inputProps={{
placeholder: t('Select account...'),
@@ -198,6 +199,7 @@ export function CloseAccountModal({
name: 'account-autocomplete',
options: {
includeClosedAccounts: false,
+ hiddenAccounts: [account.id],
onSelect: onSelectAccount,
},
},
diff --git a/packages/desktop-client/src/modals/modalsSlice.ts b/packages/desktop-client/src/modals/modalsSlice.ts
index af324a5e6b..296da13eac 100644
--- a/packages/desktop-client/src/modals/modalsSlice.ts
+++ b/packages/desktop-client/src/modals/modalsSlice.ts
@@ -225,6 +225,7 @@ export type Modal =
options: {
onSelect: (accountId: string, accountName: string) => void;
includeClosedAccounts?: boolean;
+ hiddenAccounts?: AccountEntity['id'][];
onClose?: () => void;
};
}
diff --git a/packages/loot-core/src/server/accounts/app.ts b/packages/loot-core/src/server/accounts/app.ts
index cd3888cc7f..67da0b13f6 100644
--- a/packages/loot-core/src/server/accounts/app.ts
+++ b/packages/loot-core/src/server/accounts/app.ts
@@ -436,6 +436,10 @@ async function closeAccount({
throw APIError('balance is non-zero: transferAccountId is required');
}
+ if (id === transferAccountId) {
+ throw APIError('transfer account can not be the account being closed');
+ }
+
await db.update('accounts', { id, closed: 1 });
// If there is a balance we need to transfer it to the specified
diff --git a/upcoming-release-notes/5462.md b/upcoming-release-notes/5462.md
new file mode 100644
index 0000000000..02054cdde8
--- /dev/null
+++ b/upcoming-release-notes/5462.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [matt-fidd]
+---
+
+Prevent closing an account into itself