1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-05 13:35:21 +02:00

Transaction rules engine V1 (#1900)

* Domain model sketch

* Scaffold out rules domain

* Migrations

* Remove existing data enrichment for clean slate

* Sketch out business logic and basic tests

* Simplify rule scope building and action executions

* Get generator working again

* Basic implementation + tests

* Remove manual merchant management (rules will replace)

* Revert "Remove manual merchant management (rules will replace)"

This reverts commit 83dcbd9ff0.

* Family and Provider merchants model

* Fix brakeman warnings

* Fix notification loader

* Update notification position

* Add Rule action and condition registries

* Rule form with compound conditions and tests

* Split out notification types, add CTA type

* Rules form builder and Stimulus controller

* Clean up rule registry domain

* Clean up rules stimulus controller

* CTA message for rule when user changes transaction category

* Fix tests

* Lint updates

* Centralize notifications in Notifiable concern

* Implement category rule prompts with auto backoff and option to disable

* Fix layout bug caused by merge conflict

* Initialize rule with correct action for category CTA

* Add rule deletions, get rules working

* Complete dynamic rule form, split Stimulus controllers by resource

* Fix failing tests

* Change test password to avoid chromium conflicts

* Update integration tests

* Centralize all test password references

* Add re-apply rule action

* Rule confirm modal

* Run migrations

* Trigger rule notification after inline category updates

* Clean up rule styles

* Basic attribute locking for rules

* Apply attribute locks on user edits

* Log data enrichments, only apply rules to unlocked attributes

* Fix merge errors

* Additional merge conflict fixes

* Form UI improvements, ignore attribute locks on manual rule application

* Batch AI auto-categorization of transactions

* Auto merchant detection, ai enrichment in batches

* Fix Plaid merchant assignments

* Plaid category matching

* Cleanup 1

* Test cleanup

* Remove stale route

* Fix desktop chat UI issues

* Fix mobile nav styling issues
This commit is contained in:
Zach Gollwitzer 2025-04-18 11:39:58 -04:00 committed by GitHub
parent 8edd7ecef0
commit 297a695d0f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
152 changed files with 4502 additions and 612 deletions

View file

@ -0,0 +1,20 @@
<%# locals: (cta:) %>
<% message = "Updated to #{cta[:category_name]}" %>
<% description = "You can create a rule to automatically categorize transactions like this one" %>
<%= render "shared/notifications/cta", message: message, description: description do %>
<%= form_with model: Current.user, url: rule_prompt_settings_user_path(Current.user), method: :patch do |f| %>
<div class="flex gap-2 items-center mb-3 -mt-1">
<%= f.check_box :rule_prompts_disabled, class: "checkbox checkbox--light" %>
<%= f.label :rule_prompts_disabled, "Don't show this again", class: "text-xs text-secondary" %>
</div>
<%= f.hidden_field :rule_prompt_dismissed_at, value: Time.current %>
<%= tag.div class:"flex gap-2 justify-end" do %>
<%= f.submit "Dismiss", class: "btn btn--secondary" %>
<%= tag.a "Create rule", href: new_rule_path(resource_type: "transaction", action_type: "set_transaction_category", action_value: cta[:category_id]), class: "btn btn--primary", data: { turbo_frame: "modal" } %>
<% end %>
<% end %>
<% end %>

View file

@ -0,0 +1,98 @@
<%# locals: (rule:) %>
<%= styled_form_with model: rule, class: "space-y-4 w-[550px]",
data: { controller: "rules", rule_registry_value: rule.registry.to_json } do |f| %>
<%= f.hidden_field :resource_type, value: rule.resource_type %>
<% if @rule.errors.any? %>
<%= render "shared/form_errors", model: @rule %>
<% end %>
<section class="space-y-4">
<h3 class="text-sm font-medium">If <%= rule.resource_type %></h3>
<%# Condition template, used by Stimulus controller to add new conditions dynamically %>
<template data-rules-target="conditionGroupTemplate">
<%= f.fields_for :conditions, Rule::Condition.new(rule: rule, condition_type: "compound", operator: "and"), child_index: "IDX_PLACEHOLDER" do |cf| %>
<%= render "rule/conditions/condition_group", form: cf %>
<% end %>
</template>
<%# Condition template, used by Stimulus controller to add new conditions dynamically %>
<template data-rules-target="conditionTemplate">
<%= f.fields_for :conditions, Rule::Condition.new(rule: rule, condition_type: rule.condition_filters.first.key), child_index: "IDX_PLACEHOLDER" do |cf| %>
<%= render "rule/conditions/condition", form: cf %>
<% end %>
</template>
<ul data-rules-target="conditionsList" class="space-y-3 mb-4">
<%= f.fields_for :conditions do |cf| %>
<% if cf.object.compound? %>
<%= render "rule/conditions/condition_group", form: cf %>
<% else %>
<%= render "rule/conditions/condition", form: cf %>
<% end %>
<% end %>
</ul>
<div class="flex items-center gap-2">
<button type="button" data-action="rules#addCondition" class="btn btn--ghost">
<%= icon("plus") %>
<span>Add condition</span>
</button>
<button type="button" data-action="rules#addConditionGroup" class="btn btn--ghost">
<%= icon("boxes") %>
<span>Add condition group</span>
</button>
</div>
</section>
<section class="space-y-4">
<h3 class="text-sm font-medium">Then</h3>
<%# Action template, used by Stimulus controller to add new actions dynamically %>
<template data-rules-target="actionTemplate">
<%= f.fields_for :actions, Rule::Action.new(rule: rule, action_type: rule.action_executors.first.key), child_index: "IDX_PLACEHOLDER" do |af| %>
<%= render "rule/actions/action", form: af %>
<% end %>
</template>
<ul data-rules-target="actionsList" class="space-y-3">
<%= f.fields_for :actions do |af| %>
<%= render "rule/actions/action", form: af %>
<% end %>
</ul>
<button
type="button"
data-action="rules#addAction"
class="btn btn--ghost">
<%= icon("plus") %>
<span>Add action</span>
</button>
</section>
<section class="space-y-4">
<h3 class="text-sm font-medium">Apply this</h3>
<div class="space-y-2">
<div class="flex items-center gap-2">
<%= f.radio_button :effective_date_enabled, false, checked: rule.effective_date.nil?, data: { action: "rules#clearEffectiveDate" } %>
<%= f.label :effective_date_enabled_false, "To all past and future #{rule.resource_type}s", class: "text-sm" %>
</div>
<div class="flex items-center gap-2">
<div class="flex items-center gap-2">
<%= f.radio_button :effective_date_enabled, true, checked: rule.effective_date.present? %>
<%= f.label :effective_date_enabled_true, "Starting from", class: "text-sm" %>
</div>
<%= f.date_field :effective_date, container_class: "w-fit", data: { rules_target: "effectiveDateInput" } %>
</div>
</div>
</section>
<%= f.submit %>
<% end %>

View file

@ -0,0 +1,41 @@
<%# locals: (rule:) %>
<div class="flex justify-between items-center gap-4 bg-white shadow-border-xs rounded-md p-4">
<div class="text-sm space-y-1.5">
<p class="flex items-center flex-wrap gap-1.5">
<span class="px-2 py-1 border border-alpha-black-200 rounded-full">
<%= rule.actions.first.executor.label %>
</span>
<% if rule.actions.count > 1 %>
and <%= rule.actions.count - 1 %> more <%= rule.actions.count - 1 == 1 ? "action" : "actions" %>
<% end %>
</p>
<p class="flex items-center flex-wrap gap-1.5">
<% if rule.effective_date.nil? %>
To all past and future <%= rule.resource_type.pluralize %>
<% else %>
To all <%= rule.resource_type.pluralize %> on or after <%= rule.effective_date %>
<% end %>
</p>
</div>
<div class="flex items-center gap-4">
<%= render "shared/toggle_form", model: rule, attribute: :active %>
<%= contextual_menu icon: "more-vertical", id: "chat-menu" do %>
<%= contextual_menu_item "Edit", url: edit_rule_path(rule), icon: "pencil", turbo_frame: "modal" %>
<%= contextual_menu_item "Re-apply rule", url: confirm_rule_path(rule), turbo_frame: "modal", icon: "refresh-cw" %>
<% turbo_confirm = {
title: "Delete rule",
body: "Are you sure you want to delete this rule? Data affected by this rule will no longer be automatically updated. This action cannot be undone.",
accept: "Delete rule",
} %>
<%= contextual_menu_destructive_item "Delete", rule_path(rule), turbo_confirm: turbo_confirm %>
<% end %>
</div>
</div>

View file

@ -0,0 +1,20 @@
<%= modal(reload_on_close: true) do %>
<div class="space-y-4 p-4 max-w-[400px]">
<div>
<div class="flex justify-between mb-2 gap-4">
<h3 class="font-medium text-md">Confirm changes</h3>
<button data-action="mousedown->modal#close">
<%= lucide_icon("x", class: "w-5 h-5 shrink-0 text-secondary") %>
</button>
</div>
<div class="text-secondary text-sm">
<p>
You are about to apply this rule to
<span class="text-primary font-medium"><%= @rule.affected_resource_count %> <%= @rule.resource_type.pluralize %></span>
that meet the specified rule criteria. Please confirm if you wish to proceed with this change.
</p>
</div>
</div>
<%= button_to "Confirm changes", apply_rule_path(@rule), class: "btn btn--primary w-full justify-center", data: { turbo_frame: "_top"} %>
</div>
<% end %>

View file

@ -0,0 +1,5 @@
<%= link_to "Back to rules", rules_path %>
<%= modal_form_wrapper title: "Edit #{@rule.resource_type} rule" do %>
<%= render "rules/form", rule: @rule %>
<% end %>

View file

@ -0,0 +1,61 @@
<header class="flex items-center justify-between">
<h1 class="text-primary text-xl font-medium">Rules</h1>
<% turbo_confirm = {
title: "Delete all rules",
body: "Are you sure you want to delete all rules? This action cannot be undone.",
accept: "Delete all rules",
} %>
<div class="flex items-center gap-2">
<% if @rules.any? %>
<%= contextual_menu do %>
<%= contextual_menu_destructive_item "Delete all rules", destroy_all_rules_path, turbo_confirm: turbo_confirm %>
<% end %>
<% end %>
<%= link_to new_rule_path(resource_type: "transaction"), class: "btn btn--primary flex items-center gap-1 justify-center", data: { turbo_frame: :modal } do %>
<%= lucide_icon "plus", class: "w-5 h-5" %>
<p>New rule</p>
<% end %>
</div>
</header>
<% if self_hosted? %>
<div class="flex items-center gap-2 mb-2 py-4">
<%= lucide_icon("circle-alert", class: "w-4 h-4 text-secondary") %>
<p class="text-sm text-secondary">
AI-enabled rule actions will cost money. Be sure to filter as narrowly as possible to avoid unnecessary costs.
</p>
</div>
<% end %>
<div class="bg-white shadow-border-xs rounded-xl p-4">
<% if @rules.any? %>
<div class="rounded-xl bg-gray-25 space-y-1">
<div class="flex items-center gap-1.5 px-4 py-2 text-xs font-medium text-secondary uppercase">
<p>Rules</p>
<span class="text-subdued">&middot;</span>
<p><%= @rules.count %></p>
</div>
<div class="space-y-1 p-1">
<%= render @rules %>
</div>
</div>
<% else %>
<div class="flex justify-center items-center py-20">
<div class="text-center flex flex-col items-center max-w-[500px]">
<p class="text-sm text-primary font-medium mb-1">No rules yet</p>
<p class="text-sm text-secondary mb-4">Set up rules to perform actions to your transactions and other data on every account sync.</p>
<div class="flex items-center gap-2">
<%= link_to new_rule_path(resource_type: "transaction"), class: "btn btn--primary flex items-center gap-1", data: { turbo_frame: "modal" } do %>
<%= lucide_icon("plus", class: "w-5 h-5") %>
<span>New rule</span>
<% end %>
</div>
</div>
</div>
<% end %>
</div>

View file

@ -0,0 +1,5 @@
<%= link_to "Back to rules", rules_path %>
<%= modal_form_wrapper title: "New #{@rule.resource_type} rule" do %>
<%= render "rules/form", rule: @rule %>
<% end %>