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

Enactment: enable triggering of rules on selected transaction form the account view. #3805

Open
wants to merge 13 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
53 changes: 53 additions & 0 deletions packages/desktop-client/src/components/accounts/Account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,58 @@
return groupById<{ id: string; balance: number }>(data);
}

onRunRules = async (ids: string[]) => {
try {
this.setState({ workingHard: true });
esseti marked this conversation as resolved.
Show resolved Hide resolved
// Bulk fetch transactions
const transactions = this.state.transactions.filter(trans =>

Check failure on line 709 in packages/desktop-client/src/components/accounts/Account.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Cannot redeclare block-scoped variable 'transactions'.
ids.includes(trans.id),
);
const changedTransactions = [];

Check failure on line 712 in packages/desktop-client/src/components/accounts/Account.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Cannot redeclare block-scoped variable 'changedTransactions'.
for (const transaction of transactions) {
const res: TransactionEntity | null = await send('rules-run', {
transaction,
});
if (res) {
changedTransactions.push(res);
}
}
// Bulk fetch transactions
const transactions = this.state.transactions.filter(trans =>

Check warning on line 722 in packages/desktop-client/src/components/accounts/Account.tsx

View workflow job for this annotation

GitHub Actions / lint

'transactions' is already defined

Check failure on line 722 in packages/desktop-client/src/components/accounts/Account.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Cannot redeclare block-scoped variable 'transactions'.
ids.includes(trans.id),
);
//call the runrules function
const changedTransactions = [];

Check warning on line 726 in packages/desktop-client/src/components/accounts/Account.tsx

View workflow job for this annotation

GitHub Actions / lint

'changedTransactions' is already defined

Check failure on line 726 in packages/desktop-client/src/components/accounts/Account.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Cannot redeclare block-scoped variable 'changedTransactions'.
for (const transaction of transactions) {
await send('rules-run', {
transaction,
}).then((res: TransactionEntity | null) => {
if (res) {
changedTransactions.push(res);
}
});
}

// If we have changed transactions, update them in the database
if (changedTransactions.length > 0) {
await send('transactions-batch-update', {
updated: changedTransactions,
});
}

// Fetch updated transactions once at the end
await this.fetchTransactions();
} catch (error) {
console.error('Error applying rules:', error);
this.props.addNotification({
type: 'error',
message: 'Failed to apply rules to transactions',
});
} finally {
this.setState({ workingHard: false });
}
};
Comment on lines +705 to +755
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix duplicate code blocks and variable redeclarations

The implementation has several critical issues that need to be addressed:

  1. Duplicate code blocks (lines 709-720 and 722-735)
  2. Variable redeclarations of transactions and changedTransactions
  3. Inconsistent async/await usage

Apply this diff to fix the issues:

 onRunRules = async (ids: string[]) => {
   try {
     this.setState({ workingHard: true });
     // Bulk fetch transactions
     const transactions = this.state.transactions.filter(trans =>
       ids.includes(trans.id),
     );
     const changedTransactions = [];
     for (const transaction of transactions) {
       const res: TransactionEntity | null = await send('rules-run', {
         transaction,
       });
       if (res) {
         changedTransactions.push(res);
       }
     }

-    // Bulk fetch transactions
-    const transactions = this.state.transactions.filter(trans =>
-      ids.includes(trans.id),
-    );
-    //call the runrules function
-    const changedTransactions = [];
-    for (const transaction of transactions) {
-      await send('rules-run', {
-        transaction,
-      }).then((res: TransactionEntity | null) => {
-        if (res) {
-          changedTransactions.push(res);
-        }
-      });
-    }

     // If we have changed transactions, update them in the database
     if (changedTransactions.length > 0) {
       await send('transactions-batch-update', {
         updated: changedTransactions,
       });
     }

     // Fetch updated transactions once at the end
     await this.fetchTransactions();
   } catch (error) {
     console.error('Error applying rules:', error);
     this.props.addNotification({
       type: 'error',
       message: 'Failed to apply rules to transactions',
     });
   } finally {
     this.setState({ workingHard: false });
   }
 };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
onRunRules = async (ids: string[]) => {
try {
this.setState({ workingHard: true });
// Bulk fetch transactions
const transactions = this.state.transactions.filter(trans =>
ids.includes(trans.id),
);
const changedTransactions = [];
for (const transaction of transactions) {
const res: TransactionEntity | null = await send('rules-run', {
transaction,
});
if (res) {
changedTransactions.push(res);
}
}
// Bulk fetch transactions
const transactions = this.state.transactions.filter(trans =>
ids.includes(trans.id),
);
//call the runrules function
const changedTransactions = [];
for (const transaction of transactions) {
await send('rules-run', {
transaction,
}).then((res: TransactionEntity | null) => {
if (res) {
changedTransactions.push(res);
}
});
}
// If we have changed transactions, update them in the database
if (changedTransactions.length > 0) {
await send('transactions-batch-update', {
updated: changedTransactions,
});
}
// Fetch updated transactions once at the end
await this.fetchTransactions();
} catch (error) {
console.error('Error applying rules:', error);
this.props.addNotification({
type: 'error',
message: 'Failed to apply rules to transactions',
});
} finally {
this.setState({ workingHard: false });
}
};
onRunRules = async (ids: string[]) => {
try {
this.setState({ workingHard: true });
// Bulk fetch transactions
const transactions = this.state.transactions.filter(trans =>
ids.includes(trans.id),
);
const changedTransactions = [];
for (const transaction of transactions) {
const res: TransactionEntity | null = await send('rules-run', {
transaction,
});
if (res) {
changedTransactions.push(res);
}
}
// If we have changed transactions, update them in the database
if (changedTransactions.length > 0) {
await send('transactions-batch-update', {
updated: changedTransactions,
});
}
// Fetch updated transactions once at the end
await this.fetchTransactions();
} catch (error) {
console.error('Error applying rules:', error);
this.props.addNotification({
type: 'error',
message: 'Failed to apply rules to transactions',
});
} finally {
this.setState({ workingHard: false });
}
};
🧰 Tools
🪛 Biome (1.9.4)

[error] 722-722: Shouldn't redeclare 'transactions'. Consider to delete it or rename it.

'transactions' is defined here:

(lint/suspicious/noRedeclare)


[error] 726-726: Shouldn't redeclare 'changedTransactions'. Consider to delete it or rename it.

'changedTransactions' is defined here:

(lint/suspicious/noRedeclare)

🪛 GitHub Check: typecheck

[failure] 709-709:
Cannot redeclare block-scoped variable 'transactions'.


[failure] 712-712:
Cannot redeclare block-scoped variable 'changedTransactions'.


[failure] 722-722:
Cannot redeclare block-scoped variable 'transactions'.


[failure] 726-726:
Cannot redeclare block-scoped variable 'changedTransactions'.

🪛 GitHub Check: lint

[warning] 722-722:
'transactions' is already defined


[warning] 726-726:
'changedTransactions' is already defined


onAddTransaction = () => {
this.setState({ isAdding: true });
};
Expand Down Expand Up @@ -1730,6 +1782,7 @@
onImport={this.onImport}
onBatchDelete={this.onBatchDelete}
onBatchDuplicate={this.onBatchDuplicate}
onRunRules={this.onRunRules}
onBatchEdit={this.onBatchEdit}
onBatchLinkSchedule={this.onBatchLinkSchedule}
onBatchUnlinkSchedule={this.onBatchUnlinkSchedule}
Expand Down
3 changes: 3 additions & 0 deletions packages/desktop-client/src/components/accounts/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type AccountHeaderProps = {
onMenuSelect: AccountMenuProps['onMenuSelect'];
onReconcile: ComponentProps<typeof ReconcileMenu>['onReconcile'];
onBatchEdit: ComponentProps<typeof SelectedTransactionsButton>['onEdit'];
onRunRules: ComponentProps<typeof SelectedTransactionsButton>['onRunRules'];
onBatchDelete: ComponentProps<typeof SelectedTransactionsButton>['onDelete'];
onBatchDuplicate: ComponentProps<
typeof SelectedTransactionsButton
Expand Down Expand Up @@ -177,6 +178,7 @@ export function AccountHeader({
onDeleteFilter,
onScheduleAction,
onSetTransfer,
onRunRules,
onMakeAsSplitTransaction,
onMakeAsNonSplitTransactions,
}: AccountHeaderProps) {
Expand Down Expand Up @@ -359,6 +361,7 @@ export function AccountHeader({
onDuplicate={onBatchDuplicate}
onDelete={onBatchDelete}
onEdit={onBatchEdit}
onRunRules={onRunRules}
onLinkSchedule={onBatchLinkSchedule}
onUnlinkSchedule={onBatchUnlinkSchedule}
onCreateRule={onCreateRule}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type SelectedTransactionsButtonProps = {
onLinkSchedule: (selectedIds: string[]) => void;
onUnlinkSchedule: (selectedIds: string[]) => void;
onCreateRule: (selectedIds: string[]) => void;
onRunRules: (selectedIds: string[]) => void;
onSetTransfer: (selectedIds: string[]) => void;
onScheduleAction: (
action: 'post-transaction' | 'skip',
Expand All @@ -50,6 +51,7 @@ export function SelectedTransactionsButton({
onLinkSchedule,
onUnlinkSchedule,
onCreateRule,
onRunRules,
onSetTransfer,
onScheduleAction,
showMakeTransfer,
Expand Down Expand Up @@ -193,6 +195,10 @@ export function SelectedTransactionsButton({
onEdit,
selectedIds,
]);
useHotkeys('r', () => onRunRules(selectedIds), hotKeyOptions, [
onRunRules,
selectedIds,
]);
useHotkeys(
's',
() =>
Expand Down Expand Up @@ -253,7 +259,13 @@ export function SelectedTransactionsButton({
name: 'create-rule',
text: t('Create rule'),
} as const,
{
name: 'run-rules',
text: t('Run Rules'),
key: 'R',
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this action will need a hotkey.

} as const,
]),

...(showMakeTransfer
? [
{
Expand Down Expand Up @@ -325,6 +337,9 @@ export function SelectedTransactionsButton({
case 'create-rule':
onCreateRule(selectedIds);
break;
case 'run-rules':
onRunRules(selectedIds);
break;
case 'set-transfer':
onSetTransfer(selectedIds);
break;
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3805.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [esseti]
---

Enables rule activation from the account view via dropdown menu or by pressing 'R'
Loading