Open-source personal finance manager. Self-hosted, privacy-first.
Feat/payee on rule conditions (#151)
Gustavo Henrique Rodrigues Guterresa67b333f84ec
diff --git a/backend/app/schemas/rule.py b/backend/app/schemas/rule.pyindex 1066cd96..c1373447 100644--- a/backend/app/schemas/rule.py+++ b/backend/app/schemas/rule.py@@ -6,7 +6,7 @@ from pydantic import BaseModel, ConfigDictclass RuleCondition(BaseModel):- field: str # description, notes, amount, type, account_id, date+ field: str # description, notes, amount, type, account_id, payee_id, dateop: str # contains, not_contains, equals, not_equals, starts_with, ends_with, regex, gt, gte, lt, ltevalue: Any # str or number depending on fielddiff --git a/backend/tests/test_rule_engine.py b/backend/tests/test_rule_engine.pyindex 3aea6c52..65a34e58 100644--- a/backend/tests/test_rule_engine.py+++ b/backend/tests/test_rule_engine.py@@ -13,6 +13,7 @@ def make_tx(**kwargs) -> types.SimpleNamespace:id=uuid.uuid4(),user_id=uuid.uuid4(),account_id=uuid.uuid4(),+ payee_id=uuid.uuid4(),category_id=None,description="UBER TRIP",amount=Decimal("25.50"),@@ -318,6 +319,39 @@ def test_set_payee_action():assert tx.payee_id == payee_id+# --- payee_id condition tests ---+++def test_payee_id_equals_match():+ payee_id = uuid.uuid4()+ conditions = [{"field": "payee_id", "op": "equals", "value": str(payee_id)}]+ tx = make_tx(payee_id=payee_id)+ assert evaluate_conditions("and", conditions, tx) is True+++def test_payee_id_equals_no_match():+ payee_id = uuid.uuid4()+ other_payee_id = uuid.uuid4()+ conditions = [{"field": "payee_id", "op": "equals", "value": str(other_payee_id)}]+ tx = make_tx(payee_id=payee_id)+ assert evaluate_conditions("and", conditions, tx) is False+++def test_payee_id_not_equals():+ payee_id = uuid.uuid4()+ other_payee_id = uuid.uuid4()+ conditions = [{"field": "payee_id", "op": "not_equals", "value": str(other_payee_id)}]+ tx = make_tx(payee_id=payee_id)+ assert evaluate_conditions("and", conditions, tx) is True+++def test_payee_id_none_not_equals_real_payee():+ payee_id = uuid.uuid4()+ conditions = [{"field": "payee_id", "op": "not_equals", "value": str(payee_id)}]+ tx = make_tx(payee_id=None)+ assert evaluate_conditions("and", conditions, tx) is True++def test_set_payee_invalid_uuid_ignored():actions = [{"op": "set_payee", "value": "not-a-uuid"}]tx = make_tx()diff --git a/frontend/src/locales/en.json b/frontend/src/locales/en.jsonindex 9dee33b3..d3407951 100644--- a/frontend/src/locales/en.json+++ b/frontend/src/locales/en.json@@ -530,6 +530,7 @@"fieldAmount": "Amount","fieldType": "Type","fieldAccount": "Account",+ "fieldPayee": "Payee","fieldDate": "Date","opContains": "contains","opNotContains": "does not contain",@@ -539,6 +540,7 @@"opEndsWith": "ends with","opRegex": "regex","opIs": "is",+ "opIsNot": "is not","typeExpense": "Expense (debit)","typeIncome": "Income (credit)","valuePlaceholder": "Value...",@@ -808,7 +810,7 @@"searchIcon": "Search icon...","noIconsFound": "No icons found","primaryCurrency": "Primary Currency",- "showLess":"Show less",+ "showLess": "Show less","showMore": "+{{count}} more"},"privacy": {diff --git a/frontend/src/locales/pt-BR.json b/frontend/src/locales/pt-BR.jsonindex e7446fe3..6e5166e2 100644--- a/frontend/src/locales/pt-BR.json+++ b/frontend/src/locales/pt-BR.json@@ -530,6 +530,7 @@"fieldAmount": "Valor","fieldType": "Tipo","fieldAccount": "Conta",+ "fieldPayee": "Beneficiário","fieldDate": "Data","opContains": "contém","opNotContains": "não contém",@@ -539,6 +540,7 @@"opEndsWith": "termina com","opRegex": "regex","opIs": "é",+ "opIsNot": "não é","typeExpense": "Despesa (debit)","typeIncome": "Receita (credit)","valuePlaceholder": "Valor...",@@ -808,7 +810,7 @@"searchIcon": "Buscar ícone...","noIconsFound": "Nenhum ícone encontrado","primaryCurrency": "Moeda Principal",- "showLess":"Mostrar menos",+ "showLess": "Mostrar menos","showMore": "Mostrar +{{count}}"},"privacy": {diff --git a/frontend/src/pages/rules.tsx b/frontend/src/pages/rules.tsxindex 281f2ce7..c8ddc563 100644--- a/frontend/src/pages/rules.tsx+++ b/frontend/src/pages/rules.tsx@@ -43,6 +43,7 @@ const CONDITION_FIELDS = [{ value: 'amount', label: 'rules.fieldAmount' },{ value: 'type', label: 'rules.fieldType' },{ value: 'account_id', label: 'rules.fieldAccount' },+ { value: 'payee_id', label: 'rules.fieldPayee' },{ value: 'date', label: 'rules.fieldDate' },] as const@@ -67,10 +68,14 @@ const NUMERIC_OPS = [function getOpsForField(field: string) {if (field === 'amount' || field === 'date') return NUMERIC_OPSif (field === 'type') return [{ value: 'equals', label: 'rules.opIs' }]+ if (field === 'payee_id' || field === 'account_id') return [+ { value: 'equals', label: 'rules.opIs' },+ { value: 'not_equals', label: 'rules.opIsNot' },+ ]return STRING_OPS}-function conditionSummary(conditions: RuleCondition[], conditionsOp: string, t: (key: string) => string): string {+function conditionSummary(conditions: RuleCondition[], conditionsOp: string, t: (key: string) => string, payeesList: Payee[]): string {const fieldLabel = (f: string) => {const key = CONDITION_FIELDS.find(x => x.value === f)?.labelreturn key ? t(key) : f@@ -79,7 +84,14 @@ function conditionSummary(conditions: RuleCondition[], conditionsOp: string, t:const key = getOpsForField(f).find(x => x.value === op)?.labelreturn key ? t(key) : op}- const parts = conditions.map(c => `${fieldLabel(c.field)} ${opLabel(c.field, c.op)} "${c.value}"`)+ const valueLabel = (c: RuleCondition) => {+ if (c.field === 'payee_id') {+ const p = payeesList.find(p => p.id === c.value)+ return p ? p.name : String(c.value)+ }+ return String(c.value)+ }+ const parts = conditions.map(c => `${fieldLabel(c.field)} ${opLabel(c.field, c.op)} "${valueLabel(c)}"`)return parts.join(` ${conditionsOp === 'or' ? t('rules.orOp') : t('rules.andOp')} `) || t('rules.noConditions')}@@ -283,7 +295,7 @@ export default function RulesPage() {</span></div><p className="text-xs text-muted-foreground font-mono truncate">- {conditionSummary(rule.conditions, rule.conditions_op, t)}+ {conditionSummary(rule.conditions, rule.conditions_op, t, payees)}</p><p className="text-xs text-emerald-600 font-medium mt-0.5">{actionSummary(rule.actions, categories, payees, t)}@@ -458,7 +470,7 @@ function RuleDialog({return (<Dialog open={open} onOpenChange={onClose}>- <DialogContent className="max-w-2xl max-h-[90vh] overflow-y-auto">+ <DialogContent className="max-w-2xl max-h-[90vh] overflow-y-auto overflow-x-hidden"><DialogHeader><DialogTitle>{rule ? t('rules.editRule') : t('rules.newRule')}</DialogTitle></DialogHeader>@@ -497,7 +509,7 @@ function RuleDialog({</div><div className="space-y-2">{conditions.map((cond, i) => (- <div key={i} className="flex items-center gap-2">+ <div key={i} className="flex items-center gap-2 min-w-0"><selectclassName={`${selectClass} w-32 shrink-0`}value={cond.field}@@ -518,7 +530,7 @@ function RuleDialog({</select>{cond.field === 'type' ? (<select- className={`${selectClass} flex-1`}+ className={`${selectClass} w-0 flex-1 min-w-0`}value={String(cond.value)}onChange={(e) => updateCondition(i, 'value', e.target.value)}>@@ -527,7 +539,7 @@ function RuleDialog({</select>) : cond.field === 'account_id' ? (<select- className={`${selectClass} flex-1`}+ className={`${selectClass} w-0 flex-1 min-w-0`}value={String(cond.value)}onChange={(e) => updateCondition(i, 'value', e.target.value)}>@@ -536,9 +548,20 @@ function RuleDialog({<option key={acc.id} value={acc.id}>{getAccountName(acc)}</option>))}</select>+ ) : cond.field === 'payee_id' ? (+ <select+ className={`${selectClass} w-0 flex-1 min-w-0`}+ value={String(cond.value)}+ onChange={(e) => updateCondition(i, 'value', e.target.value)}+ >+ <option value="">{t('rules.selectPayee')}</option>+ {payees.map(p => (+ <option key={p.id} value={p.id}>{p.name}</option>+ ))}+ </select>) : (<Input- className="flex-1 h-8 text-sm"+ className="w-0 flex-1 min-w-0 h-8 text-sm"value={String(cond.value)}onChange={(e) => updateCondition(i, 'value', e.target.value)}placeholder={cond.field === 'amount' ? '0.00' : cond.field === 'date' ? 'YYYY-MM-DD' : t('rules.valuePlaceholder')}@@ -569,7 +592,7 @@ function RuleDialog({<Label>{t('rules.actions')}</Label><div className="space-y-2">{actions.map((action, i) => (- <div key={i} className="flex items-center gap-2">+ <div key={i} className="flex items-center gap-2 min-w-0"><selectclassName={`${selectClass} w-40 shrink-0`}value={action.op}@@ -581,7 +604,7 @@ function RuleDialog({</select>{action.op === 'set_category' ? (<select- className={`${selectClass} flex-1`}+ className={`${selectClass} w-0 flex-1 min-w-0`}value={action.value}onChange={(e) => updateAction(i, 'value', e.target.value)}required@@ -593,7 +616,7 @@ function RuleDialog({</select>) : action.op === 'set_payee' ? (<select- className={`${selectClass} flex-1`}+ className={`${selectClass} w-0 flex-1 min-w-0`}value={action.value}onChange={(e) => updateAction(i, 'value', e.target.value)}required@@ -605,7 +628,7 @@ function RuleDialog({</select>) : (<Input- className="flex-1 h-8 text-sm"+ className="w-0 flex-1 min-w-0 h-8 text-sm"value={action.value}onChange={(e) => updateAction(i, 'value', e.target.value)}placeholder="Ex: #work #reimbursable"