1
0
Fork 0
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:
Zach Gollwitzer 2025-06-16 18:30:34 -04:00
parent 13a64a1694
commit 12cbab035c
4 changed files with 42 additions and 1 deletions

1
.gitignore vendored
View file

@ -100,6 +100,7 @@ node_modules/
tasks.json tasks.json
.taskmaster/tasks/ .taskmaster/tasks/
.taskmaster/reports/ .taskmaster/reports/
.taskmaster/state.json
*.mcp.json *.mcp.json
scripts/ scripts/
.cursor/mcp.json .cursor/mcp.json

View file

@ -9,6 +9,13 @@ class Transaction < ApplicationRecord
accepts_nested_attributes_for :taggings, allow_destroy: true accepts_nested_attributes_for :taggings, allow_destroy: true
enum :kind, {
standard: "standard",
transfer: "transfer",
loan_payment: "loan_payment",
one_time: "one_time"
}
class << self class << self
def search(params) def search(params)
Search.new(params).build_query(all) Search.new(params).build_query(all)

View 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
View file

@ -30,7 +30,7 @@ ActiveRecord::Schema[7.2].define(version: 2025_06_10_181219) do
t.decimal "balance", precision: 19, scale: 4 t.decimal "balance", precision: 19, scale: 4
t.string "currency" t.string "currency"
t.boolean "is_active", default: true, null: false 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 "import_id"
t.uuid "plaid_account_id" t.uuid "plaid_account_id"
t.boolean "scheduled_for_deletion", default: false t.boolean "scheduled_for_deletion", default: false