mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-08 06:55:21 +02:00
add kind to transaction model
This commit is contained in:
parent
13a64a1694
commit
12cbab035c
4 changed files with 42 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -100,6 +100,7 @@ node_modules/
|
|||
tasks.json
|
||||
.taskmaster/tasks/
|
||||
.taskmaster/reports/
|
||||
.taskmaster/state.json
|
||||
*.mcp.json
|
||||
scripts/
|
||||
.cursor/mcp.json
|
||||
|
|
|
@ -9,6 +9,13 @@ class Transaction < ApplicationRecord
|
|||
|
||||
accepts_nested_attributes_for :taggings, allow_destroy: true
|
||||
|
||||
enum :kind, {
|
||||
standard: "standard",
|
||||
transfer: "transfer",
|
||||
loan_payment: "loan_payment",
|
||||
one_time: "one_time"
|
||||
}
|
||||
|
||||
class << self
|
||||
def search(params)
|
||||
Search.new(params).build_query(all)
|
||||
|
|
33
db/migrate/20250616183654_add_kind_to_transactions.rb
Normal file
33
db/migrate/20250616183654_add_kind_to_transactions.rb
Normal file
|
@ -0,0 +1,33 @@
|
|||
class AddKindToTransactions < ActiveRecord::Migration[7.2]
|
||||
def change
|
||||
add_column :transactions, :kind, :string, null: false, default: "standard"
|
||||
add_index :transactions, :kind
|
||||
|
||||
reversible do |dir|
|
||||
dir.up do
|
||||
# Update transaction kinds based on transfer relationships
|
||||
execute <<~SQL
|
||||
UPDATE transactions
|
||||
SET kind = CASE
|
||||
WHEN loan_accounts.accountable_type = 'Loan'
|
||||
AND entries.amount > 0
|
||||
THEN 'loan_payment'
|
||||
ELSE 'transfer'
|
||||
END
|
||||
FROM transfers t
|
||||
JOIN entries ON (
|
||||
entries.entryable_id = t.inflow_transaction_id OR
|
||||
entries.entryable_id = t.outflow_transaction_id
|
||||
)
|
||||
LEFT JOIN entries inflow_entries ON (
|
||||
inflow_entries.entryable_id = t.inflow_transaction_id
|
||||
AND inflow_entries.entryable_type = 'Transaction'
|
||||
)
|
||||
LEFT JOIN accounts loan_accounts ON loan_accounts.id = inflow_entries.account_id
|
||||
WHERE transactions.id = entries.entryable_id
|
||||
AND entries.entryable_type = 'Transaction'
|
||||
SQL
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
2
db/schema.rb
generated
2
db/schema.rb
generated
|
@ -30,7 +30,7 @@ ActiveRecord::Schema[7.2].define(version: 2025_06_10_181219) do
|
|||
t.decimal "balance", precision: 19, scale: 4
|
||||
t.string "currency"
|
||||
t.boolean "is_active", default: true, null: false
|
||||
t.virtual "classification", type: :string, as: "\nCASE\n WHEN ((accountable_type)::text = ANY (ARRAY[('Loan'::character varying)::text, ('CreditCard'::character varying)::text, ('OtherLiability'::character varying)::text])) THEN 'liability'::text\n ELSE 'asset'::text\nEND", stored: true
|
||||
t.virtual "classification", type: :string, as: "\nCASE\n WHEN ((accountable_type)::text = ANY ((ARRAY['Loan'::character varying, 'CreditCard'::character varying, 'OtherLiability'::character varying])::text[])) THEN 'liability'::text\n ELSE 'asset'::text\nEND", stored: true
|
||||
t.uuid "import_id"
|
||||
t.uuid "plaid_account_id"
|
||||
t.boolean "scheduled_for_deletion", default: false
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue