-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add data enrichment * Make data enrichment optional for self-hosters * Add categories to data enrichment * Only update category and merchant if nil * Fix name overrides * Lint fixes
- Loading branch information
Showing
16 changed files
with
182 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class EnrichDataJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(account) | ||
account.enrich_data | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
class Account::DataEnricher | ||
include Providable | ||
|
||
attr_reader :account | ||
|
||
def initialize(account) | ||
@account = account | ||
end | ||
|
||
def run | ||
enrich_transactions | ||
end | ||
|
||
private | ||
def enrich_transactions | ||
candidates = account.entries.account_transactions.includes(entryable: [ :merchant, :category ]) | ||
|
||
Rails.logger.info("Enriching #{candidates.count} transactions for account #{account.id}") | ||
|
||
merchants = {} | ||
categories = {} | ||
|
||
candidates.each do |entry| | ||
if entry.enriched_at.nil? || entry.entryable.merchant_id.nil? || entry.entryable.category_id.nil? | ||
begin | ||
info = self.class.synth_provider.enrich_transaction(entry.name).info | ||
|
||
next unless info.present? | ||
|
||
if info.name.present? | ||
merchant = merchants[info.name] ||= account.family.merchants.find_or_create_by(name: info.name) | ||
|
||
if info.icon_url.present? | ||
merchant.icon_url = info.icon_url | ||
end | ||
end | ||
|
||
if info.category.present? | ||
category = categories[info.category] ||= account.family.categories.find_or_create_by(name: info.category) | ||
end | ||
|
||
entryable_attributes = { id: entry.entryable_id } | ||
entryable_attributes[:merchant_id] = merchant.id if merchant.present? && entry.entryable.merchant_id.nil? | ||
entryable_attributes[:category_id] = category.id if category.present? && entry.entryable.category_id.nil? | ||
|
||
Account.transaction do | ||
merchant.save! if merchant.present? | ||
category.save! if category.present? | ||
entry.update!( | ||
enriched_at: Time.current, | ||
name: entry.enriched_at.nil? ? info.name : entry.name, | ||
entryable_attributes: entryable_attributes | ||
) | ||
end | ||
rescue => e | ||
Rails.logger.warn("Error enriching transaction #{entry.id}: #{e.message}") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
app/views/settings/hostings/_data_enrichment_settings.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<div class="space-y-4"> | ||
<div class="flex items-center justify-between"> | ||
<div class="space-y-1"> | ||
<p class="text-sm"><%= t(".title") %></p> | ||
<p class="text-gray-500 text-sm"><%= t(".description") %></p> | ||
</div> | ||
|
||
<%= styled_form_with model: Setting.new, | ||
url: settings_hosting_path, | ||
method: :patch, | ||
data: { controller: "auto-submit-form", "auto-submit-form-trigger-event-value": "blur" } do |form| %> | ||
<div class="relative inline-block select-none"> | ||
<%= form.check_box :data_enrichment_enabled, class: "sr-only peer", "data-auto-submit-form-target": "auto", "data-autosubmit-trigger-event": "input" %> | ||
<%= form.label :data_enrichment_enabled, " ".html_safe, class: "maybe-switch" %> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class AddMerchantLogo < ActiveRecord::Migration[7.2] | ||
def change | ||
add_column :merchants, :icon_url, :string | ||
add_column :merchants, :enriched_at, :datetime | ||
|
||
add_column :account_entries, :enriched_at, :datetime | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require "test_helper" | ||
|
||
class EnrichDataJobTest < ActiveJob::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |