mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
Transfer and Payment auto-matching, model and UI improvements (#1585)
* Transfer data model migration * Transfers and payment modeling and UI improvements * Fix CI * Transfer matching flow * Better UI for transfers * Auto transfer matching, approve, reject flow * Mark transfers created from form as confirmed * Account filtering * Excluded rejected transfers from calculations * Calculation tweaks with transfer exclusions * Clean up migration
This commit is contained in:
parent
46e129308f
commit
307a3687e8
78 changed files with 1161 additions and 682 deletions
75
db/migrate/20241231140709_reverse_transfer_relations.rb
Normal file
75
db/migrate/20241231140709_reverse_transfer_relations.rb
Normal file
|
@ -0,0 +1,75 @@
|
|||
class ReverseTransferRelations < ActiveRecord::Migration[7.2]
|
||||
def change
|
||||
create_table :transfers, id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
|
||||
t.references :inflow_transaction, null: false, foreign_key: { to_table: :account_transactions }, type: :uuid
|
||||
t.references :outflow_transaction, null: false, foreign_key: { to_table: :account_transactions }, type: :uuid
|
||||
t.string :status, null: false, default: "pending"
|
||||
t.text :notes
|
||||
|
||||
t.index [ :inflow_transaction_id, :outflow_transaction_id ], unique: true
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
reversible do |dir|
|
||||
dir.up do
|
||||
execute <<~SQL
|
||||
INSERT INTO transfers (inflow_transaction_id, outflow_transaction_id, status, created_at, updated_at)
|
||||
SELECT
|
||||
CASE WHEN e1.amount <= 0 THEN e1.entryable_id ELSE e2.entryable_id END as inflow_transaction_id,
|
||||
CASE WHEN e1.amount <= 0 THEN e2.entryable_id ELSE e1.entryable_id END as outflow_transaction_id,
|
||||
'confirmed' as status,
|
||||
e1.created_at,
|
||||
e1.updated_at
|
||||
FROM account_entries e1
|
||||
JOIN account_entries e2 ON
|
||||
e1.transfer_id = e2.transfer_id AND
|
||||
e1.id != e2.id AND
|
||||
e1.id < e2.id -- Ensures we don't duplicate transfers from both sides
|
||||
JOIN accounts a1 ON e1.account_id = a1.id
|
||||
JOIN accounts a2 ON e2.account_id = a2.id
|
||||
WHERE
|
||||
e1.entryable_type = 'Account::Transaction' AND
|
||||
e2.entryable_type = 'Account::Transaction' AND
|
||||
e1.transfer_id IS NOT NULL AND
|
||||
a1.family_id = a2.family_id;
|
||||
SQL
|
||||
end
|
||||
|
||||
dir.down do
|
||||
execute <<~SQL
|
||||
WITH new_transfers AS (
|
||||
INSERT INTO account_transfers (created_at, updated_at)
|
||||
SELECT created_at, updated_at
|
||||
FROM transfers
|
||||
RETURNING id, created_at
|
||||
),
|
||||
transfer_pairs AS (
|
||||
SELECT
|
||||
nt.id as transfer_id,
|
||||
ae_in.id as inflow_entry_id,
|
||||
ae_out.id as outflow_entry_id
|
||||
FROM transfers t
|
||||
JOIN new_transfers nt ON nt.created_at = t.created_at
|
||||
JOIN account_entries ae_in ON ae_in.entryable_id = t.inflow_transaction_id
|
||||
JOIN account_entries ae_out ON ae_out.entryable_id = t.outflow_transaction_id
|
||||
WHERE
|
||||
ae_in.entryable_type = 'Account::Transaction' AND
|
||||
ae_out.entryable_type = 'Account::Transaction'
|
||||
)
|
||||
UPDATE account_entries ae
|
||||
SET transfer_id = tp.transfer_id
|
||||
FROM transfer_pairs tp
|
||||
WHERE ae.id IN (tp.inflow_entry_id, tp.outflow_entry_id);
|
||||
SQL
|
||||
end
|
||||
end
|
||||
|
||||
remove_foreign_key :account_entries, :account_transfers, column: :transfer_id
|
||||
remove_column :account_entries, :transfer_id, :uuid
|
||||
remove_column :account_entries, :marked_as_transfer, :boolean
|
||||
|
||||
drop_table :account_transfers, id: :uuid, default: -> { "gen_random_uuid()" } do |t|
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
25
db/schema.rb
generated
25
db/schema.rb
generated
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[7.2].define(version: 2024_12_27_142333) do
|
||||
ActiveRecord::Schema[7.2].define(version: 2024_12_31_140709) do
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "pgcrypto"
|
||||
enable_extension "plpgsql"
|
||||
|
@ -42,8 +42,6 @@ ActiveRecord::Schema[7.2].define(version: 2024_12_27_142333) do
|
|||
t.string "name", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.uuid "transfer_id"
|
||||
t.boolean "marked_as_transfer", default: false, null: false
|
||||
t.uuid "import_id"
|
||||
t.text "notes"
|
||||
t.boolean "excluded", default: false
|
||||
|
@ -52,7 +50,6 @@ ActiveRecord::Schema[7.2].define(version: 2024_12_27_142333) do
|
|||
t.string "enriched_name"
|
||||
t.index ["account_id"], name: "index_account_entries_on_account_id"
|
||||
t.index ["import_id"], name: "index_account_entries_on_import_id"
|
||||
t.index ["transfer_id"], name: "index_account_entries_on_transfer_id"
|
||||
end
|
||||
|
||||
create_table "account_holdings", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
|
||||
|
@ -89,11 +86,6 @@ ActiveRecord::Schema[7.2].define(version: 2024_12_27_142333) do
|
|||
t.index ["merchant_id"], name: "index_account_transactions_on_merchant_id"
|
||||
end
|
||||
|
||||
create_table "account_transfers", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
create_table "account_valuations", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
|
@ -606,6 +598,18 @@ ActiveRecord::Schema[7.2].define(version: 2024_12_27_142333) do
|
|||
t.index ["family_id"], name: "index_tags_on_family_id"
|
||||
end
|
||||
|
||||
create_table "transfers", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
|
||||
t.uuid "inflow_transaction_id", null: false
|
||||
t.uuid "outflow_transaction_id", null: false
|
||||
t.string "status", default: "pending", null: false
|
||||
t.text "notes"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["inflow_transaction_id", "outflow_transaction_id"], name: "idx_on_inflow_transaction_id_outflow_transaction_id_8cd07a28bd", unique: true
|
||||
t.index ["inflow_transaction_id"], name: "index_transfers_on_inflow_transaction_id"
|
||||
t.index ["outflow_transaction_id"], name: "index_transfers_on_outflow_transaction_id"
|
||||
end
|
||||
|
||||
create_table "users", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
|
||||
t.uuid "family_id", null: false
|
||||
t.string "first_name"
|
||||
|
@ -634,7 +638,6 @@ ActiveRecord::Schema[7.2].define(version: 2024_12_27_142333) do
|
|||
end
|
||||
|
||||
add_foreign_key "account_balances", "accounts", on_delete: :cascade
|
||||
add_foreign_key "account_entries", "account_transfers", column: "transfer_id"
|
||||
add_foreign_key "account_entries", "accounts"
|
||||
add_foreign_key "account_entries", "imports"
|
||||
add_foreign_key "account_holdings", "accounts"
|
||||
|
@ -663,5 +666,7 @@ ActiveRecord::Schema[7.2].define(version: 2024_12_27_142333) do
|
|||
add_foreign_key "sessions", "users"
|
||||
add_foreign_key "taggings", "tags"
|
||||
add_foreign_key "tags", "families"
|
||||
add_foreign_key "transfers", "account_transactions", column: "inflow_transaction_id"
|
||||
add_foreign_key "transfers", "account_transactions", column: "outflow_transaction_id"
|
||||
add_foreign_key "users", "families"
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue