Skip to content

A local-first personal finance app

add cleared to rule conditions (#8740)

Matt Fiddamane98ddcdfa44e
diff --git a/packages/desktop-client/src/components/rules/RuleEditor.tsx b/packages/desktop-client/src/components/rules/RuleEditor.tsx
index b1ec683b9a..1af6f686ac 100644
--- a/packages/desktop-client/src/components/rules/RuleEditor.tsx
+++ b/packages/desktop-client/src/components/rules/RuleEditor.tsx
@@ -1015,6 +1015,7 @@ const conditionFields = [
.concat([
['amount-inflow', mapField('amount', { inflow: true })],
['amount-outflow', mapField('amount', { outflow: true })],
+ ['cleared', mapField('cleared')],
]);
type RuleEditorProps = {
diff --git a/packages/docs/docs/budgeting/rules/index.md b/packages/docs/docs/budgeting/rules/index.md
index dcd687c491..52f53d4d66 100644
--- a/packages/docs/docs/budgeting/rules/index.md
+++ b/packages/docs/docs/budgeting/rules/index.md
@@ -42,11 +42,14 @@ Conditions can use the following fields:
- amount
- amount (inflow)
- amount (outflow)
+- cleared
`imported payee` is different from `payee` in that it is _always_ the original text of the payee or description field when the transaction was imported. `payee` references a payee in Actual. This matters because it allows you to rename a payee before it is created in Actual. You can have several rules that all check `imported payee` and set the payee to something without worrying about them stepping on each other. (Conditions can't reliably check `payee` if previous rules changed it)
The `inflow` and `outflow` versions of `amount` make it easier to work with amounts. If you only want to match expenses between 5 and 10 dollars, use `amount (outflow)` because that money is leaving the account. If you use `amount`, you'd have to use negative numbers and it's simply less convenient.
+The `cleared` field matches whether a transaction is marked as cleared. For transactions coming from bank sync, this lets a rule treat pending and booked transactions differently.
+
All strings are matched case-insensitive. An `imported payee` of "PuBlix" will match a condition that is "contains 'publix'".
Actions can set the following fields:
diff --git a/packages/loot-core/src/server/rules/index.test.ts b/packages/loot-core/src/server/rules/index.test.ts
index b985299522..2ec1219e7a 100644
--- a/packages/loot-core/src/server/rules/index.test.ts
+++ b/packages/loot-core/src/server/rules/index.test.ts
@@ -296,6 +296,29 @@ describe('Condition', () => {
expect(cond.eval({ amount: 1.67 })).toBe(false);
expect(cond.eval({ amount: 1.5 })).toBe(true);
});
+
+ test('boolean validates value', () => {
+ new Condition('is', 'cleared', true, null);
+
+ expect(() => {
+ new Condition('is', 'cleared', 'true', null);
+ }).toThrow('Value must be a boolean');
+
+ expect(() => {
+ new Condition('is', 'cleared', null, null);
+ }).toThrow('Field cannot be empty');
+ });
+
+ test('boolean works with `is` operator', () => {
+ let cond = new Condition('is', 'cleared', true, null);
+ expect(cond.eval({ cleared: true })).toBe(true);
+ expect(cond.eval({ cleared: false })).toBe(false);
+ expect(cond.eval({})).toBe(false);
+
+ cond = new Condition('is', 'cleared', false, null);
+ expect(cond.eval({ cleared: false })).toBe(true);
+ expect(cond.eval({ cleared: true })).toBe(false);
+ });
});
describe('Action', () => {
@@ -562,6 +585,18 @@ describe('Rule', () => {
expect(rule.apply({ notes: 'James2' })).toEqual({ notes: 'James2' });
});
+ test('rule with a cleared condition matches on cleared status', () => {
+ const rule = new Rule({
+ conditionsOp: 'and',
+ conditions: [{ op: 'is', field: 'cleared', value: true }],
+ actions: [{ op: 'set', field: 'notes', value: 'Sarah' }],
+ });
+
+ expect(rule.exec({ cleared: true })).toEqual({ notes: 'Sarah' });
+ expect(rule.exec({ cleared: false })).toEqual(null);
+ expect(rule.exec({})).toEqual(null);
+ });
+
test('rule with `and` conditionsOp evaluates conditions as AND', () => {
const rule = new Rule({
conditionsOp: 'and',
diff --git a/upcoming-release-notes/add-cleared-rule-condition.md b/upcoming-release-notes/add-cleared-rule-condition.md
new file mode 100644
index 0000000000..421d8aeefa
--- /dev/null
+++ b/upcoming-release-notes/add-cleared-rule-condition.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [matt-fidd]
+---
+
+Allow rules to match transactions by cleared status