mirror of
https://github.com/we-promise/sure.git
synced 2026-04-07 22:34:47 +00:00
* Implement providers factory * Multiple providers sync support - Proper Multi-Provider Syncing: When you click sync on an account with multiple providers (e.g., both Plaid and SimpleFin), all provider items are synced - Better API: The existing account.providers method already returns all providers, and account.provider returns the first one for backward compatibility - Correct Holdings Deletion Logic: Holdings can only be deleted if ALL providers allow it, preventing accidental deletions that would be recreated on next sync TODO: validate this is the way we want to go? We would need to check holdings belong to which account, and then check provider allows deletion. More complex - Database Constraints: The existing validations ensure an account can have at most one provider of each type (one PlaidAccount, one SimplefinAccount, etc.) * Add generic provider_import_adapter * Finish unified import strategy * Update app/models/plaid_account.rb Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: soky srm <sokysrm@gmail.com> * Update app/models/provider/factory.rb Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: soky srm <sokysrm@gmail.com> * Fix account linked by plaid_id instead of external_id * Parse numerics to BigDecimal Parse numerics to BigDecimal before computing amount; guard nils. Avoid String * String and float drift; also normalize date. * Fix incorrect usage of assert_raises. * Fix linter * Fix processor test. * Update current_balance_manager.rb * Test fixes * Fix plaid linked account test * Add support for holding per account_provider * Fix proper account access Also fix account deletion for simpefin too * FIX match tests for consistency * Some more factory updates * Fix account schema for multipe providers Can do: - Account #1 → PlaidAccount + SimplefinAccount (multiple different providers) - Account #2 → PlaidAccount only - Account #3 → SimplefinAccount only Cannot do: - Account #1 → PlaidAccount + PlaidAccount (duplicate provider type) - PlaidAccount #123 → Account #1 + Account #2 (provider linked to multiple accounts) * Fix account setup - An account CAN have multiple providers (the schema shows account_providers with unique index on [account_id, provider_type]) - Each provider should maintain its own separate entries - We should NOT update one provider's entry when another provider syncs * Fix linter and guard migration * FIX linter issues. * Fixes - Remove duplicated index - Pass account_provider_id - Guard holdings call to avoid NoMethodError * Update schema and provider import fix * Plaid doesn't allow holdings deletion * Use ClimateControl for proper env setup * No need for this in .git --------- Signed-off-by: soky srm <sokysrm@gmail.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
101 lines
2.7 KiB
Ruby
101 lines
2.7 KiB
Ruby
class Entry < ApplicationRecord
|
|
include Monetizable, Enrichable
|
|
|
|
monetize :amount
|
|
|
|
belongs_to :account
|
|
belongs_to :transfer, optional: true
|
|
belongs_to :import, optional: true
|
|
|
|
delegated_type :entryable, types: Entryable::TYPES, dependent: :destroy
|
|
accepts_nested_attributes_for :entryable
|
|
|
|
validates :date, :name, :amount, :currency, presence: true
|
|
validates :date, uniqueness: { scope: [ :account_id, :entryable_type ] }, if: -> { valuation? }
|
|
validates :date, comparison: { greater_than: -> { min_supported_date } }
|
|
validates :external_id, uniqueness: { scope: [ :account_id, :source ] }, if: -> { external_id.present? && source.present? }
|
|
|
|
scope :visible, -> {
|
|
joins(:account).where(accounts: { status: [ "draft", "active" ] })
|
|
}
|
|
|
|
scope :chronological, -> {
|
|
order(
|
|
date: :asc,
|
|
Arel.sql("CASE WHEN entries.entryable_type = 'Valuation' THEN 1 ELSE 0 END") => :asc,
|
|
created_at: :asc
|
|
)
|
|
}
|
|
|
|
scope :reverse_chronological, -> {
|
|
order(
|
|
date: :desc,
|
|
Arel.sql("CASE WHEN entries.entryable_type = 'Valuation' THEN 1 ELSE 0 END") => :desc,
|
|
created_at: :desc
|
|
)
|
|
}
|
|
|
|
def classification
|
|
amount.negative? ? "income" : "expense"
|
|
end
|
|
|
|
def lock_saved_attributes!
|
|
super
|
|
entryable.lock_saved_attributes!
|
|
end
|
|
|
|
def sync_account_later
|
|
sync_start_date = [ date_previously_was, date ].compact.min unless destroyed?
|
|
account.sync_later(window_start_date: sync_start_date)
|
|
end
|
|
|
|
def entryable_name_short
|
|
entryable_type.demodulize.underscore
|
|
end
|
|
|
|
def balance_trend(entries, balances)
|
|
Balance::TrendCalculator.new(self, entries, balances).trend
|
|
end
|
|
|
|
def linked?
|
|
external_id.present?
|
|
end
|
|
|
|
class << self
|
|
def search(params)
|
|
EntrySearch.new(params).build_query(all)
|
|
end
|
|
|
|
# arbitrary cutoff date to avoid expensive sync operations
|
|
def min_supported_date
|
|
30.years.ago.to_date
|
|
end
|
|
|
|
def bulk_update!(bulk_update_params)
|
|
bulk_attributes = {
|
|
date: bulk_update_params[:date],
|
|
notes: bulk_update_params[:notes],
|
|
entryable_attributes: {
|
|
category_id: bulk_update_params[:category_id],
|
|
merchant_id: bulk_update_params[:merchant_id],
|
|
tag_ids: bulk_update_params[:tag_ids]
|
|
}.compact_blank
|
|
}.compact_blank
|
|
|
|
return 0 if bulk_attributes.blank?
|
|
|
|
transaction do
|
|
all.each do |entry|
|
|
bulk_attributes[:entryable_attributes][:id] = entry.entryable_id if bulk_attributes[:entryable_attributes].present?
|
|
entry.update! bulk_attributes
|
|
|
|
entry.lock_saved_attributes!
|
|
entry.entryable.lock_attr!(:tag_ids) if entry.transaction? && entry.transaction.tags.any?
|
|
end
|
|
end
|
|
|
|
all.size
|
|
end
|
|
end
|
|
end
|