mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-05 05:25:24 +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:
parent
8edd7ecef0
commit
297a695d0f
152 changed files with 4502 additions and 612 deletions
|
@ -1,6 +1,18 @@
|
|||
module Provider::LlmConcept
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
AutoCategorization = Data.define(:transaction_id, :category_name)
|
||||
|
||||
def auto_categorize(transactions)
|
||||
raise NotImplementedError, "Subclasses must implement #auto_categorize"
|
||||
end
|
||||
|
||||
AutoDetectedMerchant = Data.define(:transaction_id, :business_name, :business_url)
|
||||
|
||||
def auto_detect_merchants(transactions)
|
||||
raise NotImplementedError, "Subclasses must implement #auto_detect_merchants"
|
||||
end
|
||||
|
||||
ChatMessage = Data.define(:id, :output_text)
|
||||
ChatStreamChunk = Data.define(:type, :data)
|
||||
ChatResponse = Data.define(:id, :model, :messages, :function_requests)
|
||||
|
|
|
@ -14,6 +14,30 @@ class Provider::Openai < Provider
|
|||
MODELS.include?(model)
|
||||
end
|
||||
|
||||
def auto_categorize(transactions: [], user_categories: [])
|
||||
with_provider_response do
|
||||
raise Error, "Too many transactions to auto-categorize. Max is 25 per request." if transactions.size > 25
|
||||
|
||||
AutoCategorizer.new(
|
||||
client,
|
||||
transactions: transactions,
|
||||
user_categories: user_categories
|
||||
).auto_categorize
|
||||
end
|
||||
end
|
||||
|
||||
def auto_detect_merchants(transactions: [], user_merchants: [])
|
||||
with_provider_response do
|
||||
raise Error, "Too many transactions to auto-detect merchants. Max is 25 per request." if transactions.size > 25
|
||||
|
||||
AutoMerchantDetector.new(
|
||||
client,
|
||||
transactions: transactions,
|
||||
user_merchants: user_merchants
|
||||
).auto_detect_merchants
|
||||
end
|
||||
end
|
||||
|
||||
def chat_response(prompt, model:, instructions: nil, functions: [], function_results: [], streamer: nil, previous_response_id: nil)
|
||||
with_provider_response do
|
||||
chat_config = ChatConfig.new(
|
||||
|
|
120
app/models/provider/openai/auto_categorizer.rb
Normal file
120
app/models/provider/openai/auto_categorizer.rb
Normal file
|
@ -0,0 +1,120 @@
|
|||
class Provider::Openai::AutoCategorizer
|
||||
def initialize(client, transactions: [], user_categories: [])
|
||||
@client = client
|
||||
@transactions = transactions
|
||||
@user_categories = user_categories
|
||||
end
|
||||
|
||||
def auto_categorize
|
||||
response = client.responses.create(parameters: {
|
||||
model: "gpt-4.1-mini",
|
||||
input: [ { role: "developer", content: developer_message } ],
|
||||
text: {
|
||||
format: {
|
||||
type: "json_schema",
|
||||
name: "auto_categorize_personal_finance_transactions",
|
||||
strict: true,
|
||||
schema: json_schema
|
||||
}
|
||||
},
|
||||
instructions: instructions
|
||||
})
|
||||
|
||||
Rails.logger.info("Tokens used to auto-categorize transactions: #{response.dig("usage").dig("total_tokens")}")
|
||||
|
||||
build_response(extract_categorizations(response))
|
||||
end
|
||||
|
||||
private
|
||||
attr_reader :client, :transactions, :user_categories
|
||||
|
||||
AutoCategorization = Provider::LlmConcept::AutoCategorization
|
||||
|
||||
def build_response(categorizations)
|
||||
categorizations.map do |categorization|
|
||||
AutoCategorization.new(
|
||||
transaction_id: categorization.dig("transaction_id"),
|
||||
category_name: normalize_category_name(categorization.dig("category_name")),
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def normalize_category_name(category_name)
|
||||
return nil if category_name == "null"
|
||||
|
||||
category_name
|
||||
end
|
||||
|
||||
def extract_categorizations(response)
|
||||
response_json = JSON.parse(response.dig("output")[0].dig("content")[0].dig("text"))
|
||||
response_json.dig("categorizations")
|
||||
end
|
||||
|
||||
def json_schema
|
||||
{
|
||||
type: "object",
|
||||
properties: {
|
||||
categorizations: {
|
||||
type: "array",
|
||||
description: "An array of auto-categorizations for each transaction",
|
||||
items: {
|
||||
type: "object",
|
||||
properties: {
|
||||
transaction_id: {
|
||||
type: "string",
|
||||
description: "The internal ID of the original transaction",
|
||||
enum: transactions.map { |t| t[:id] }
|
||||
},
|
||||
category_name: {
|
||||
type: "string",
|
||||
description: "The matched category name of the transaction, or null if no match",
|
||||
enum: [ *user_categories.map { |c| c[:name] }, "null" ]
|
||||
}
|
||||
},
|
||||
required: [ "transaction_id", "category_name" ],
|
||||
additionalProperties: false
|
||||
}
|
||||
}
|
||||
},
|
||||
required: [ "categorizations" ],
|
||||
additionalProperties: false
|
||||
}
|
||||
end
|
||||
|
||||
def developer_message
|
||||
<<~MESSAGE.strip_heredoc
|
||||
Here are the user's available categories in JSON format:
|
||||
|
||||
```json
|
||||
#{user_categories.to_json}
|
||||
```
|
||||
|
||||
Use the available categories to auto-categorize the following transactions:
|
||||
|
||||
```json
|
||||
#{transactions.to_json}
|
||||
```
|
||||
MESSAGE
|
||||
end
|
||||
|
||||
def instructions
|
||||
<<~INSTRUCTIONS.strip_heredoc
|
||||
You are an assistant to a consumer personal finance app. You will be provided a list
|
||||
of the user's transactions and a list of the user's categories. Your job is to auto-categorize
|
||||
each transaction.
|
||||
|
||||
Closely follow ALL the rules below while auto-categorizing:
|
||||
|
||||
- Return 1 result per transaction
|
||||
- Correlate each transaction by ID (transaction_id)
|
||||
- Attempt to match the most specific category possible (i.e. subcategory over parent category)
|
||||
- Category and transaction classifications should match (i.e. if transaction is an "expense", the category must have classification of "expense")
|
||||
- If you don't know the category, return "null"
|
||||
- You should always favor "null" over false positives
|
||||
- Be slightly pessimistic. Only match a category if you're 60%+ confident it is the correct one.
|
||||
- Each transaction has varying metadata that can be used to determine the category
|
||||
- Note: "hint" comes from 3rd party aggregators and typically represents a category name that
|
||||
may or may not match any of the user-supplied categories
|
||||
INSTRUCTIONS
|
||||
end
|
||||
end
|
146
app/models/provider/openai/auto_merchant_detector.rb
Normal file
146
app/models/provider/openai/auto_merchant_detector.rb
Normal file
|
@ -0,0 +1,146 @@
|
|||
class Provider::Openai::AutoMerchantDetector
|
||||
def initialize(client, transactions:, user_merchants:)
|
||||
@client = client
|
||||
@transactions = transactions
|
||||
@user_merchants = user_merchants
|
||||
end
|
||||
|
||||
def auto_detect_merchants
|
||||
response = client.responses.create(parameters: {
|
||||
model: "gpt-4.1-mini",
|
||||
input: [ { role: "developer", content: developer_message } ],
|
||||
text: {
|
||||
format: {
|
||||
type: "json_schema",
|
||||
name: "auto_detect_personal_finance_merchants",
|
||||
strict: true,
|
||||
schema: json_schema
|
||||
}
|
||||
},
|
||||
instructions: instructions
|
||||
})
|
||||
|
||||
Rails.logger.info("Tokens used to auto-detect merchants: #{response.dig("usage").dig("total_tokens")}")
|
||||
|
||||
build_response(extract_categorizations(response))
|
||||
end
|
||||
|
||||
private
|
||||
attr_reader :client, :transactions, :user_merchants
|
||||
|
||||
AutoDetectedMerchant = Provider::LlmConcept::AutoDetectedMerchant
|
||||
|
||||
def build_response(categorizations)
|
||||
categorizations.map do |categorization|
|
||||
AutoDetectedMerchant.new(
|
||||
transaction_id: categorization.dig("transaction_id"),
|
||||
business_name: normalize_ai_value(categorization.dig("business_name")),
|
||||
business_url: normalize_ai_value(categorization.dig("business_url")),
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def normalize_ai_value(ai_value)
|
||||
return nil if ai_value == "null"
|
||||
|
||||
ai_value
|
||||
end
|
||||
|
||||
def extract_categorizations(response)
|
||||
response_json = JSON.parse(response.dig("output")[0].dig("content")[0].dig("text"))
|
||||
response_json.dig("merchants")
|
||||
end
|
||||
|
||||
def json_schema
|
||||
{
|
||||
type: "object",
|
||||
properties: {
|
||||
merchants: {
|
||||
type: "array",
|
||||
description: "An array of auto-detected merchant businesses for each transaction",
|
||||
items: {
|
||||
type: "object",
|
||||
properties: {
|
||||
transaction_id: {
|
||||
type: "string",
|
||||
description: "The internal ID of the original transaction",
|
||||
enum: transactions.map { |t| t[:id] }
|
||||
},
|
||||
business_name: {
|
||||
type: [ "string", "null" ],
|
||||
description: "The detected business name of the transaction, or `null` if uncertain"
|
||||
},
|
||||
business_url: {
|
||||
type: [ "string", "null" ],
|
||||
description: "The URL of the detected business, or `null` if uncertain"
|
||||
}
|
||||
},
|
||||
required: [ "transaction_id", "business_name", "business_url" ],
|
||||
additionalProperties: false
|
||||
}
|
||||
}
|
||||
},
|
||||
required: [ "merchants" ],
|
||||
additionalProperties: false
|
||||
}
|
||||
end
|
||||
|
||||
def developer_message
|
||||
<<~MESSAGE.strip_heredoc
|
||||
Here are the user's available merchants in JSON format:
|
||||
|
||||
```json
|
||||
#{user_merchants.to_json}
|
||||
```
|
||||
|
||||
Use BOTH your knowledge AND the user-generated merchants to auto-detect the following transactions:
|
||||
|
||||
```json
|
||||
#{transactions.to_json}
|
||||
```
|
||||
|
||||
Return "null" if you are not 80%+ confident in your answer.
|
||||
MESSAGE
|
||||
end
|
||||
|
||||
def instructions
|
||||
<<~INSTRUCTIONS.strip_heredoc
|
||||
You are an assistant to a consumer personal finance app.
|
||||
|
||||
Closely follow ALL the rules below while auto-detecting business names and website URLs:
|
||||
|
||||
- Return 1 result per transaction
|
||||
- Correlate each transaction by ID (transaction_id)
|
||||
- Do not include the subdomain in the business_url (i.e. "amazon.com" not "www.amazon.com")
|
||||
- User merchants are considered "manual" user-generated merchants and should only be used in 100% clear cases
|
||||
- Be slightly pessimistic. We favor returning "null" over returning a false positive.
|
||||
- NEVER return a name or URL for generic transaction names (e.g. "Paycheck", "Laundromat", "Grocery store", "Local diner")
|
||||
|
||||
Determining a value:
|
||||
|
||||
- First attempt to determine the name + URL from your knowledge of global businesses
|
||||
- If no certain match, attempt to match one of the user-provided merchants
|
||||
- If no match, return "null"
|
||||
|
||||
Example 1 (known business):
|
||||
|
||||
```
|
||||
Transaction name: "Some Amazon purchases"
|
||||
|
||||
Result:
|
||||
- business_name: "Amazon"
|
||||
- business_url: "amazon.com"
|
||||
```
|
||||
|
||||
Example 2 (generic business):
|
||||
|
||||
```
|
||||
Transaction name: "local diner"
|
||||
|
||||
Result:
|
||||
- business_name: null
|
||||
- business_url: null
|
||||
```
|
||||
INSTRUCTIONS
|
||||
end
|
||||
end
|
|
@ -121,7 +121,10 @@ class Provider::Plaid
|
|||
while has_more
|
||||
request = Plaid::TransactionsSyncRequest.new(
|
||||
access_token: item.access_token,
|
||||
cursor: cursor
|
||||
cursor: cursor,
|
||||
options: {
|
||||
include_original_description: true
|
||||
}
|
||||
)
|
||||
|
||||
response = client.transactions_sync(request)
|
||||
|
|
109
app/models/provider/plaid/category_alias_matcher.rb
Normal file
109
app/models/provider/plaid/category_alias_matcher.rb
Normal file
|
@ -0,0 +1,109 @@
|
|||
# The purpose of this matcher is to auto-match Plaid categories to
|
||||
# known internal user categories. Since we allow users to define their own
|
||||
# categories we cannot directly assign Plaid categories as this would overwrite
|
||||
# user data and create a confusing experience.
|
||||
#
|
||||
# Automated category matching in the Maybe app has a hierarchy:
|
||||
# 1. Naive string matching via CategoryAliasMatcher
|
||||
# 2. Rules-based matching set by user
|
||||
# 3. AI-powered matching (also enabled by user via rules)
|
||||
#
|
||||
# This class is simply a FAST and CHEAP way to match categories that are high confidence.
|
||||
# Edge cases will be handled by user-defined rules.
|
||||
class Provider::Plaid::CategoryAliasMatcher
|
||||
include Provider::Plaid::CategoryTaxonomy
|
||||
|
||||
def initialize(user_categories)
|
||||
@user_categories = user_categories
|
||||
end
|
||||
|
||||
def match(plaid_detailed_category)
|
||||
plaid_category_details = get_plaid_category_details(plaid_detailed_category)
|
||||
return nil unless plaid_category_details
|
||||
|
||||
# Try exact name matches first
|
||||
exact_match = normalized_user_categories.find do |category|
|
||||
category[:name] == plaid_category_details[:key].to_s
|
||||
end
|
||||
return user_categories.find { |c| c.id == exact_match[:id] } if exact_match
|
||||
|
||||
# Try detailed aliases matches with fuzzy matching
|
||||
alias_match = normalized_user_categories.find do |category|
|
||||
name = category[:name]
|
||||
plaid_category_details[:aliases].any? do |a|
|
||||
alias_str = a.to_s
|
||||
|
||||
# Try exact match
|
||||
next true if name == alias_str
|
||||
|
||||
# Try plural forms
|
||||
next true if name.singularize == alias_str || name.pluralize == alias_str
|
||||
next true if alias_str.singularize == name || alias_str.pluralize == name
|
||||
|
||||
# Try common forms
|
||||
normalized_name = name.gsub(/(and|&|\s+)/, "").strip
|
||||
normalized_alias = alias_str.gsub(/(and|&|\s+)/, "").strip
|
||||
normalized_name == normalized_alias
|
||||
end
|
||||
end
|
||||
return user_categories.find { |c| c.id == alias_match[:id] } if alias_match
|
||||
|
||||
# Try parent aliases matches with fuzzy matching
|
||||
parent_match = normalized_user_categories.find do |category|
|
||||
name = category[:name]
|
||||
plaid_category_details[:parent_aliases].any? do |a|
|
||||
alias_str = a.to_s
|
||||
|
||||
# Try exact match
|
||||
next true if name == alias_str
|
||||
|
||||
# Try plural forms
|
||||
next true if name.singularize == alias_str || name.pluralize == alias_str
|
||||
next true if alias_str.singularize == name || alias_str.pluralize == name
|
||||
|
||||
# Try common forms
|
||||
normalized_name = name.gsub(/(and|&|\s+)/, "").strip
|
||||
normalized_alias = alias_str.gsub(/(and|&|\s+)/, "").strip
|
||||
normalized_name == normalized_alias
|
||||
end
|
||||
end
|
||||
return user_categories.find { |c| c.id == parent_match[:id] } if parent_match
|
||||
|
||||
nil
|
||||
end
|
||||
|
||||
private
|
||||
attr_reader :user_categories
|
||||
|
||||
def get_plaid_category_details(plaid_category_name)
|
||||
detailed_plaid_categories.find { |c| c[:key] == plaid_category_name.downcase.to_sym }
|
||||
end
|
||||
|
||||
def detailed_plaid_categories
|
||||
CATEGORIES_MAP.flat_map do |parent_key, parent_data|
|
||||
parent_data[:detailed_categories].map do |child_key, child_data|
|
||||
{
|
||||
key: child_key,
|
||||
classification: child_data[:classification],
|
||||
aliases: child_data[:aliases],
|
||||
parent_key: parent_key,
|
||||
parent_aliases: parent_data[:aliases]
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def normalized_user_categories
|
||||
user_categories.map do |user_category|
|
||||
{
|
||||
id: user_category.id,
|
||||
classification: user_category.classification,
|
||||
name: normalize_user_category_name(user_category.name)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
def normalize_user_category_name(name)
|
||||
name.to_s.downcase.gsub(/[^a-z0-9]/, " ").strip
|
||||
end
|
||||
end
|
461
app/models/provider/plaid/category_taxonomy.rb
Normal file
461
app/models/provider/plaid/category_taxonomy.rb
Normal file
|
@ -0,0 +1,461 @@
|
|||
# https://plaid.com/documents/transactions-personal-finance-category-taxonomy.csv
|
||||
module Provider::Plaid::CategoryTaxonomy
|
||||
CATEGORIES_MAP = {
|
||||
income: {
|
||||
classification: :income,
|
||||
aliases: [ "income", "revenue", "earnings" ],
|
||||
detailed_categories: {
|
||||
income_dividends: {
|
||||
classification: :income,
|
||||
aliases: [ "dividend", "stock income", "dividend income", "dividend earnings" ]
|
||||
},
|
||||
income_interest_earned: {
|
||||
classification: :income,
|
||||
aliases: [ "interest", "bank interest", "interest earned", "interest income" ]
|
||||
},
|
||||
income_retirement_pension: {
|
||||
classification: :income,
|
||||
aliases: [ "retirement", "pension" ]
|
||||
},
|
||||
income_tax_refund: {
|
||||
classification: :income,
|
||||
aliases: [ "tax refund" ]
|
||||
},
|
||||
income_unemployment: {
|
||||
classification: :income,
|
||||
aliases: [ "unemployment" ]
|
||||
},
|
||||
income_wages: {
|
||||
classification: :income,
|
||||
aliases: [ "wage", "salary", "paycheck" ]
|
||||
},
|
||||
income_other_income: {
|
||||
classification: :income,
|
||||
aliases: [ "other income", "misc income" ]
|
||||
}
|
||||
}
|
||||
},
|
||||
loan_payments: {
|
||||
classification: :expense,
|
||||
aliases: [ "loan payment", "debt payment", "loan", "debt", "payment" ],
|
||||
detailed_categories: {
|
||||
loan_payments_car_payment: {
|
||||
classification: :expense,
|
||||
aliases: [ "car payment", "auto loan" ]
|
||||
},
|
||||
loan_payments_credit_card_payment: {
|
||||
classification: :expense,
|
||||
aliases: [ "credit card", "card payment" ]
|
||||
},
|
||||
loan_payments_personal_loan_payment: {
|
||||
classification: :expense,
|
||||
aliases: [ "personal loan", "loan payment" ]
|
||||
},
|
||||
loan_payments_mortgage_payment: {
|
||||
classification: :expense,
|
||||
aliases: [ "mortgage", "home loan" ]
|
||||
},
|
||||
loan_payments_student_loan_payment: {
|
||||
classification: :expense,
|
||||
aliases: [ "student loan", "education loan" ]
|
||||
},
|
||||
loan_payments_other_payment: {
|
||||
classification: :expense,
|
||||
aliases: [ "loan", "loan payment" ]
|
||||
}
|
||||
}
|
||||
},
|
||||
bank_fees: {
|
||||
classification: :expense,
|
||||
aliases: [ "bank fee", "service charge", "fee", "misc fees" ],
|
||||
detailed_categories: {
|
||||
bank_fees_atm_fees: {
|
||||
classification: :expense,
|
||||
aliases: [ "atm fee", "withdrawal fee" ]
|
||||
},
|
||||
bank_fees_foreign_transaction_fees: {
|
||||
classification: :expense,
|
||||
aliases: [ "foreign fee", "international fee" ]
|
||||
},
|
||||
bank_fees_insufficient_funds: {
|
||||
classification: :expense,
|
||||
aliases: [ "nsf fee", "overdraft" ]
|
||||
},
|
||||
bank_fees_interest_charge: {
|
||||
classification: :expense,
|
||||
aliases: [ "interest charge", "finance charge" ]
|
||||
},
|
||||
bank_fees_overdraft_fees: {
|
||||
classification: :expense,
|
||||
aliases: [ "overdraft fee" ]
|
||||
},
|
||||
bank_fees_other_bank_fees: {
|
||||
classification: :expense,
|
||||
aliases: [ "bank fee", "service charge" ]
|
||||
}
|
||||
}
|
||||
},
|
||||
entertainment: {
|
||||
classification: :expense,
|
||||
aliases: [ "entertainment", "recreation" ],
|
||||
detailed_categories: {
|
||||
entertainment_casinos_and_gambling: {
|
||||
classification: :expense,
|
||||
aliases: [ "casino", "gambling" ]
|
||||
},
|
||||
entertainment_music_and_audio: {
|
||||
classification: :expense,
|
||||
aliases: [ "music", "concert" ]
|
||||
},
|
||||
entertainment_sporting_events_amusement_parks_and_museums: {
|
||||
classification: :expense,
|
||||
aliases: [ "event", "amusement", "museum" ]
|
||||
},
|
||||
entertainment_tv_and_movies: {
|
||||
classification: :expense,
|
||||
aliases: [ "movie", "streaming" ]
|
||||
},
|
||||
entertainment_video_games: {
|
||||
classification: :expense,
|
||||
aliases: [ "game", "gaming" ]
|
||||
},
|
||||
entertainment_other_entertainment: {
|
||||
classification: :expense,
|
||||
aliases: [ "entertainment", "recreation" ]
|
||||
}
|
||||
}
|
||||
},
|
||||
food_and_drink: {
|
||||
classification: :expense,
|
||||
aliases: [ "food", "dining", "food and drink", "food & drink" ],
|
||||
detailed_categories: {
|
||||
food_and_drink_beer_wine_and_liquor: {
|
||||
classification: :expense,
|
||||
aliases: [ "alcohol", "liquor", "beer", "wine", "bar", "pub" ]
|
||||
},
|
||||
food_and_drink_coffee: {
|
||||
classification: :expense,
|
||||
aliases: [ "coffee", "cafe", "coffee shop" ]
|
||||
},
|
||||
food_and_drink_fast_food: {
|
||||
classification: :expense,
|
||||
aliases: [ "fast food", "takeout" ]
|
||||
},
|
||||
food_and_drink_groceries: {
|
||||
classification: :expense,
|
||||
aliases: [ "grocery", "supermarket", "grocery store" ]
|
||||
},
|
||||
food_and_drink_restaurant: {
|
||||
classification: :expense,
|
||||
aliases: [ "restaurant", "dining" ]
|
||||
},
|
||||
food_and_drink_vending_machines: {
|
||||
classification: :expense,
|
||||
aliases: [ "vending" ]
|
||||
},
|
||||
food_and_drink_other_food_and_drink: {
|
||||
classification: :expense,
|
||||
aliases: [ "food", "drink" ]
|
||||
}
|
||||
}
|
||||
},
|
||||
general_merchandise: {
|
||||
classification: :expense,
|
||||
aliases: [ "shopping", "retail" ],
|
||||
detailed_categories: {
|
||||
general_merchandise_bookstores_and_newsstands: {
|
||||
classification: :expense,
|
||||
aliases: [ "book", "newsstand" ]
|
||||
},
|
||||
general_merchandise_clothing_and_accessories: {
|
||||
classification: :expense,
|
||||
aliases: [ "clothing", "apparel" ]
|
||||
},
|
||||
general_merchandise_convenience_stores: {
|
||||
classification: :expense,
|
||||
aliases: [ "convenience" ]
|
||||
},
|
||||
general_merchandise_department_stores: {
|
||||
classification: :expense,
|
||||
aliases: [ "department store" ]
|
||||
},
|
||||
general_merchandise_discount_stores: {
|
||||
classification: :expense,
|
||||
aliases: [ "discount store" ]
|
||||
},
|
||||
general_merchandise_electronics: {
|
||||
classification: :expense,
|
||||
aliases: [ "electronic", "computer" ]
|
||||
},
|
||||
general_merchandise_gifts_and_novelties: {
|
||||
classification: :expense,
|
||||
aliases: [ "gift", "souvenir" ]
|
||||
},
|
||||
general_merchandise_office_supplies: {
|
||||
classification: :expense,
|
||||
aliases: [ "office supply" ]
|
||||
},
|
||||
general_merchandise_online_marketplaces: {
|
||||
classification: :expense,
|
||||
aliases: [ "online shopping" ]
|
||||
},
|
||||
general_merchandise_pet_supplies: {
|
||||
classification: :expense,
|
||||
aliases: [ "pet supply", "pet food" ]
|
||||
},
|
||||
general_merchandise_sporting_goods: {
|
||||
classification: :expense,
|
||||
aliases: [ "sporting good", "sport" ]
|
||||
},
|
||||
general_merchandise_superstores: {
|
||||
classification: :expense,
|
||||
aliases: [ "superstore", "retail" ]
|
||||
},
|
||||
general_merchandise_tobacco_and_vape: {
|
||||
classification: :expense,
|
||||
aliases: [ "tobacco", "smoke" ]
|
||||
},
|
||||
general_merchandise_other_general_merchandise: {
|
||||
classification: :expense,
|
||||
aliases: [ "shopping", "merchandise" ]
|
||||
}
|
||||
}
|
||||
},
|
||||
home_improvement: {
|
||||
classification: :expense,
|
||||
aliases: [ "home", "house", "house renovation", "home improvement", "renovation" ],
|
||||
detailed_categories: {
|
||||
home_improvement_furniture: {
|
||||
classification: :expense,
|
||||
aliases: [ "furniture", "furnishing" ]
|
||||
},
|
||||
home_improvement_hardware: {
|
||||
classification: :expense,
|
||||
aliases: [ "hardware", "tool" ]
|
||||
},
|
||||
home_improvement_repair_and_maintenance: {
|
||||
classification: :expense,
|
||||
aliases: [ "repair", "maintenance" ]
|
||||
},
|
||||
home_improvement_security: {
|
||||
classification: :expense,
|
||||
aliases: [ "security", "alarm" ]
|
||||
},
|
||||
home_improvement_other_home_improvement: {
|
||||
classification: :expense,
|
||||
aliases: [ "home improvement", "renovation" ]
|
||||
}
|
||||
}
|
||||
},
|
||||
medical: {
|
||||
classification: :expense,
|
||||
aliases: [ "medical", "healthcare", "health" ],
|
||||
detailed_categories: {
|
||||
medical_dental_care: {
|
||||
classification: :expense,
|
||||
aliases: [ "dental", "dentist" ]
|
||||
},
|
||||
medical_eye_care: {
|
||||
classification: :expense,
|
||||
aliases: [ "eye", "optometrist" ]
|
||||
},
|
||||
medical_nursing_care: {
|
||||
classification: :expense,
|
||||
aliases: [ "nursing", "care" ]
|
||||
},
|
||||
medical_pharmacies_and_supplements: {
|
||||
classification: :expense,
|
||||
aliases: [ "pharmacy", "prescription" ]
|
||||
},
|
||||
medical_primary_care: {
|
||||
classification: :expense,
|
||||
aliases: [ "doctor", "medical" ]
|
||||
},
|
||||
medical_veterinary_services: {
|
||||
classification: :expense,
|
||||
aliases: [ "vet", "veterinary" ]
|
||||
},
|
||||
medical_other_medical: {
|
||||
classification: :expense,
|
||||
aliases: [ "medical", "healthcare" ]
|
||||
}
|
||||
}
|
||||
},
|
||||
personal_care: {
|
||||
classification: :expense,
|
||||
aliases: [ "personal care", "grooming" ],
|
||||
detailed_categories: {
|
||||
personal_care_gyms_and_fitness_centers: {
|
||||
classification: :expense,
|
||||
aliases: [ "gym", "fitness", "exercise", "sport" ]
|
||||
},
|
||||
personal_care_hair_and_beauty: {
|
||||
classification: :expense,
|
||||
aliases: [ "salon", "beauty" ]
|
||||
},
|
||||
personal_care_laundry_and_dry_cleaning: {
|
||||
classification: :expense,
|
||||
aliases: [ "laundry", "cleaning" ]
|
||||
},
|
||||
personal_care_other_personal_care: {
|
||||
classification: :expense,
|
||||
aliases: [ "personal care", "grooming" ]
|
||||
}
|
||||
}
|
||||
},
|
||||
general_services: {
|
||||
classification: :expense,
|
||||
aliases: [ "service", "professional service" ],
|
||||
detailed_categories: {
|
||||
general_services_accounting_and_financial_planning: {
|
||||
classification: :expense,
|
||||
aliases: [ "accountant", "financial advisor" ]
|
||||
},
|
||||
general_services_automotive: {
|
||||
classification: :expense,
|
||||
aliases: [ "auto repair", "mechanic", "vehicle", "car", "car care", "car maintenance", "vehicle maintenance" ]
|
||||
},
|
||||
general_services_childcare: {
|
||||
classification: :expense,
|
||||
aliases: [ "childcare", "daycare" ]
|
||||
},
|
||||
general_services_consulting_and_legal: {
|
||||
classification: :expense,
|
||||
aliases: [ "legal", "attorney" ]
|
||||
},
|
||||
general_services_education: {
|
||||
classification: :expense,
|
||||
aliases: [ "education", "tuition" ]
|
||||
},
|
||||
general_services_insurance: {
|
||||
classification: :expense,
|
||||
aliases: [ "insurance", "premium" ]
|
||||
},
|
||||
general_services_postage_and_shipping: {
|
||||
classification: :expense,
|
||||
aliases: [ "shipping", "postage" ]
|
||||
},
|
||||
general_services_storage: {
|
||||
classification: :expense,
|
||||
aliases: [ "storage" ]
|
||||
},
|
||||
general_services_other_general_services: {
|
||||
classification: :expense,
|
||||
aliases: [ "service" ]
|
||||
}
|
||||
}
|
||||
},
|
||||
government_and_non_profit: {
|
||||
classification: :expense,
|
||||
aliases: [ "government", "non-profit" ],
|
||||
detailed_categories: {
|
||||
government_and_non_profit_donations: {
|
||||
classification: :expense,
|
||||
aliases: [ "donation", "charity", "charitable", "charitable donation", "giving", "gifts and donations", "gifts & donations" ]
|
||||
},
|
||||
government_and_non_profit_government_departments_and_agencies: {
|
||||
classification: :expense,
|
||||
aliases: [ "government", "agency" ]
|
||||
},
|
||||
government_and_non_profit_tax_payment: {
|
||||
classification: :expense,
|
||||
aliases: [ "tax payment", "tax" ]
|
||||
},
|
||||
government_and_non_profit_other_government_and_non_profit: {
|
||||
classification: :expense,
|
||||
aliases: [ "government", "non-profit" ]
|
||||
}
|
||||
}
|
||||
},
|
||||
transportation: {
|
||||
classification: :expense,
|
||||
aliases: [ "transportation", "travel" ],
|
||||
detailed_categories: {
|
||||
transportation_bikes_and_scooters: {
|
||||
classification: :expense,
|
||||
aliases: [ "bike", "scooter" ]
|
||||
},
|
||||
transportation_gas: {
|
||||
classification: :expense,
|
||||
aliases: [ "gas", "fuel" ]
|
||||
},
|
||||
transportation_parking: {
|
||||
classification: :expense,
|
||||
aliases: [ "parking" ]
|
||||
},
|
||||
transportation_public_transit: {
|
||||
classification: :expense,
|
||||
aliases: [ "transit", "bus" ]
|
||||
},
|
||||
transportation_taxis_and_ride_shares: {
|
||||
classification: :expense,
|
||||
aliases: [ "taxi", "rideshare" ]
|
||||
},
|
||||
transportation_tolls: {
|
||||
classification: :expense,
|
||||
aliases: [ "toll" ]
|
||||
},
|
||||
transportation_other_transportation: {
|
||||
classification: :expense,
|
||||
aliases: [ "transportation", "travel" ]
|
||||
}
|
||||
}
|
||||
},
|
||||
travel: {
|
||||
classification: :expense,
|
||||
aliases: [ "travel", "vacation", "trip", "sabbatical" ],
|
||||
detailed_categories: {
|
||||
travel_flights: {
|
||||
classification: :expense,
|
||||
aliases: [ "flight", "airfare" ]
|
||||
},
|
||||
travel_lodging: {
|
||||
classification: :expense,
|
||||
aliases: [ "hotel", "lodging" ]
|
||||
},
|
||||
travel_rental_cars: {
|
||||
classification: :expense,
|
||||
aliases: [ "rental car" ]
|
||||
},
|
||||
travel_other_travel: {
|
||||
classification: :expense,
|
||||
aliases: [ "travel", "trip" ]
|
||||
}
|
||||
}
|
||||
},
|
||||
rent_and_utilities: {
|
||||
classification: :expense,
|
||||
aliases: [ "utilities", "housing", "house", "home", "rent", "rent & utilities" ],
|
||||
detailed_categories: {
|
||||
rent_and_utilities_gas_and_electricity: {
|
||||
classification: :expense,
|
||||
aliases: [ "utility", "electric" ]
|
||||
},
|
||||
rent_and_utilities_internet_and_cable: {
|
||||
classification: :expense,
|
||||
aliases: [ "internet", "cable" ]
|
||||
},
|
||||
rent_and_utilities_rent: {
|
||||
classification: :expense,
|
||||
aliases: [ "rent", "lease" ]
|
||||
},
|
||||
rent_and_utilities_sewage_and_waste_management: {
|
||||
classification: :expense,
|
||||
aliases: [ "sewage", "waste" ]
|
||||
},
|
||||
rent_and_utilities_telephone: {
|
||||
classification: :expense,
|
||||
aliases: [ "phone", "telephone" ]
|
||||
},
|
||||
rent_and_utilities_water: {
|
||||
classification: :expense,
|
||||
aliases: [ "water" ]
|
||||
},
|
||||
rent_and_utilities_other_utilities: {
|
||||
classification: :expense,
|
||||
aliases: [ "utility" ]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
|
@ -159,38 +159,9 @@ class Provider::Synth < Provider
|
|||
end
|
||||
end
|
||||
|
||||
# ================================
|
||||
# Transactions
|
||||
# ================================
|
||||
|
||||
def enrich_transaction(description, amount: nil, date: nil, city: nil, state: nil, country: nil)
|
||||
with_provider_response do
|
||||
params = {
|
||||
description: description,
|
||||
amount: amount,
|
||||
date: date,
|
||||
city: city,
|
||||
state: state,
|
||||
country: country
|
||||
}.compact
|
||||
|
||||
response = client.get("#{base_url}/enrich", params)
|
||||
|
||||
parsed = JSON.parse(response.body)
|
||||
|
||||
TransactionEnrichmentData.new(
|
||||
name: parsed.dig("merchant"),
|
||||
icon_url: parsed.dig("icon"),
|
||||
category: parsed.dig("category")
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
attr_reader :api_key
|
||||
|
||||
TransactionEnrichmentData = Data.define(:name, :icon_url, :category)
|
||||
|
||||
def base_url
|
||||
ENV["SYNTH_URL"] || "https://api.synthfinance.com"
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue