mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-25 08:09:38 +02:00
* 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
174 lines
5.2 KiB
Ruby
174 lines
5.2 KiB
Ruby
class Family < ApplicationRecord
|
|
include Syncable, AutoTransferMatchable
|
|
|
|
DATE_FORMATS = [
|
|
[ "MM-DD-YYYY", "%m-%d-%Y" ],
|
|
[ "DD.MM.YYYY", "%d.%m.%Y" ],
|
|
[ "DD-MM-YYYY", "%d-%m-%Y" ],
|
|
[ "YYYY-MM-DD", "%Y-%m-%d" ],
|
|
[ "DD/MM/YYYY", "%d/%m/%Y" ],
|
|
[ "YYYY/MM/DD", "%Y/%m/%d" ],
|
|
[ "MM/DD/YYYY", "%m/%d/%Y" ],
|
|
[ "D/MM/YYYY", "%e/%m/%Y" ],
|
|
[ "YYYY.MM.DD", "%Y.%m.%d" ]
|
|
].freeze
|
|
|
|
has_many :users, dependent: :destroy
|
|
has_many :accounts, dependent: :destroy
|
|
has_many :plaid_items, dependent: :destroy
|
|
has_many :invitations, dependent: :destroy
|
|
|
|
has_many :imports, dependent: :destroy
|
|
|
|
has_many :entries, through: :accounts
|
|
has_many :transactions, through: :accounts
|
|
has_many :rules, dependent: :destroy
|
|
has_many :trades, through: :accounts
|
|
has_many :holdings, through: :accounts
|
|
|
|
has_many :tags, dependent: :destroy
|
|
has_many :categories, dependent: :destroy
|
|
has_many :merchants, dependent: :destroy, class_name: "FamilyMerchant"
|
|
|
|
has_many :budgets, dependent: :destroy
|
|
has_many :budget_categories, through: :budgets
|
|
|
|
validates :locale, inclusion: { in: I18n.available_locales.map(&:to_s) }
|
|
validates :date_format, inclusion: { in: DATE_FORMATS.map(&:last) }
|
|
|
|
def assigned_merchants
|
|
merchant_ids = transactions.where.not(merchant_id: nil).pluck(:merchant_id).uniq
|
|
Merchant.where(id: merchant_ids)
|
|
end
|
|
|
|
def auto_categorize_transactions_later(transactions)
|
|
AutoCategorizeJob.perform_later(self, transaction_ids: transactions.pluck(:id))
|
|
end
|
|
|
|
def auto_categorize_transactions(transaction_ids)
|
|
AutoCategorizer.new(self, transaction_ids: transaction_ids).auto_categorize
|
|
end
|
|
|
|
def auto_detect_transaction_merchants_later(transactions)
|
|
AutoDetectMerchantsJob.perform_later(self, transaction_ids: transactions.pluck(:id))
|
|
end
|
|
|
|
def auto_detect_transaction_merchants(transaction_ids)
|
|
AutoMerchantDetector.new(self, transaction_ids: transaction_ids).auto_detect
|
|
end
|
|
|
|
def balance_sheet
|
|
@balance_sheet ||= BalanceSheet.new(self)
|
|
end
|
|
|
|
def income_statement
|
|
@income_statement ||= IncomeStatement.new(self)
|
|
end
|
|
|
|
def sync_data(sync, start_date: nil)
|
|
update!(last_synced_at: Time.current)
|
|
|
|
Rails.logger.info("Syncing accounts for family #{id}")
|
|
accounts.manual.each do |account|
|
|
account.sync_later(start_date: start_date, parent_sync: sync)
|
|
end
|
|
|
|
Rails.logger.info("Syncing plaid items for family #{id}")
|
|
plaid_items.each do |plaid_item|
|
|
plaid_item.sync_later(start_date: start_date, parent_sync: sync)
|
|
end
|
|
|
|
Rails.logger.info("Applying rules for family #{id}")
|
|
rules.each do |rule|
|
|
rule.apply_later
|
|
end
|
|
end
|
|
|
|
def remove_syncing_notice!
|
|
broadcast_remove target: "syncing-notice"
|
|
end
|
|
|
|
def post_sync(sync)
|
|
auto_match_transfers!
|
|
broadcast_refresh
|
|
end
|
|
|
|
# If family has any syncs pending/syncing within the last hour, we show a persistent "syncing" notice.
|
|
# Ignore syncs older than 1 hour as they are considered "stale"
|
|
def syncing?
|
|
Sync.where(
|
|
"(syncable_type = 'Family' AND syncable_id = ?) OR
|
|
(syncable_type = 'Account' AND syncable_id IN (SELECT id FROM accounts WHERE family_id = ? AND plaid_account_id IS NULL)) OR
|
|
(syncable_type = 'PlaidItem' AND syncable_id IN (SELECT id FROM plaid_items WHERE family_id = ?))",
|
|
id, id, id
|
|
).where(status: [ "pending", "syncing" ], created_at: 1.hour.ago..).exists?
|
|
end
|
|
|
|
def eu?
|
|
country != "US" && country != "CA"
|
|
end
|
|
|
|
def get_link_token(webhooks_url:, redirect_url:, accountable_type: nil, region: :us, access_token: nil)
|
|
provider = if region.to_sym == :eu
|
|
Provider::Registry.get_provider(:plaid_eu)
|
|
else
|
|
Provider::Registry.get_provider(:plaid_us)
|
|
end
|
|
|
|
# early return when no provider
|
|
return nil unless provider
|
|
|
|
provider.get_link_token(
|
|
user_id: id,
|
|
webhooks_url: webhooks_url,
|
|
redirect_url: redirect_url,
|
|
accountable_type: accountable_type,
|
|
access_token: access_token
|
|
).link_token
|
|
end
|
|
|
|
def subscribed?
|
|
stripe_subscription_status == "active"
|
|
end
|
|
|
|
def requires_data_provider?
|
|
# If family has any trades, they need a provider for historical prices
|
|
return true if trades.any?
|
|
|
|
# If family has any accounts not denominated in the family's currency, they need a provider for historical exchange rates
|
|
return true if accounts.where.not(currency: self.currency).any?
|
|
|
|
# If family has any entries in different currencies, they need a provider for historical exchange rates
|
|
uniq_currencies = entries.pluck(:currency).uniq
|
|
return true if uniq_currencies.count > 1
|
|
return true if uniq_currencies.count > 0 && uniq_currencies.first != self.currency
|
|
|
|
false
|
|
end
|
|
|
|
def primary_user
|
|
users.order(:created_at).first
|
|
end
|
|
|
|
def oldest_entry_date
|
|
entries.order(:date).first&.date || Date.current
|
|
end
|
|
|
|
def active_accounts_count
|
|
accounts.active.count
|
|
end
|
|
|
|
# Cache key that is invalidated when any of the family's entries are updated (which affect rollups and other calculations)
|
|
def build_cache_key(key)
|
|
[
|
|
"family",
|
|
id,
|
|
key,
|
|
entries.maximum(:updated_at)
|
|
].compact.join("_")
|
|
end
|
|
|
|
def self_hoster?
|
|
Rails.application.config.app_mode.self_hosted?
|
|
end
|
|
end
|