1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-09 15:35:22 +02:00

Migrate valuations controller to new reconciliation methods

This commit is contained in:
Zach Gollwitzer 2025-07-10 10:31:40 -04:00
parent 25f0c78c47
commit d80cb9f812
19 changed files with 187 additions and 171 deletions

View file

@ -46,29 +46,24 @@ module AccountableResource
end
def update
# Handle balance update if provided
if account_params[:balance].present?
result = @account.update_balance(balance: account_params[:balance], currency: account_params[:currency])
unless result.success?
@error_message = result.error_message
render :edit, status: :unprocessable_entity
return
form = Account::OverviewForm.new(
account: @account,
name: account_params[:name],
currency: account_params[:currency],
current_balance: account_params[:balance],
current_cash_balance: @account.depository? ? account_params[:balance] : "0"
)
result = form.save
if result.success?
respond_to do |format|
format.html { redirect_back_or_to account_path(@account), notice: accountable_type.name.underscore.humanize + " account updated" }
format.turbo_stream { stream_redirect_to account_path(@account), notice: accountable_type.name.underscore.humanize + " account updated" }
end
end
# Update remaining account attributes
update_params = account_params.except(:return_to, :balance, :currency, :tracking_start_date)
unless @account.update(update_params)
@error_message = @account.errors.full_messages.join(", ")
else
@error_message = result.error || "Unable to update account details."
render :edit, status: :unprocessable_entity
return
end
@account.lock_saved_attributes!
respond_to do |format|
format.html { redirect_back_or_to account_path(@account), notice: accountable_type.name.underscore.humanize + " account updated" }
format.turbo_stream { stream_redirect_to account_path(@account), notice: accountable_type.name.underscore.humanize + " account updated" }
end
end

View file

@ -9,4 +9,31 @@ class CreditCardsController < ApplicationController
:annual_fee,
:expiration_date
)
def update
form = Account::OverviewForm.new(
account: @account,
name: account_params[:name],
currency: account_params[:currency],
current_balance: account_params[:balance],
current_cash_balance: @account.depository? ? account_params[:balance] : "0"
)
result = form.save
if result.success?
# Update credit card-specific attributes
if account_params[:accountable_attributes].present?
@account.credit_card.update!(account_params[:accountable_attributes])
end
respond_to do |format|
format.html { redirect_back_or_to account_path(@account), notice: "Credit card account updated" }
format.turbo_stream { stream_redirect_to account_path(@account), notice: "Credit card account updated" }
end
else
@error_message = result.error || "Unable to update account details."
render :edit, status: :unprocessable_entity
end
end
end

View file

@ -4,4 +4,31 @@ class LoansController < ApplicationController
permitted_accountable_attributes(
:id, :rate_type, :interest_rate, :term_months, :initial_balance
)
def update
form = Account::OverviewForm.new(
account: @account,
name: account_params[:name],
currency: account_params[:currency],
current_balance: account_params[:balance],
current_cash_balance: @account.depository? ? account_params[:balance] : "0"
)
result = form.save
if result.success?
# Update loan-specific attributes
if account_params[:accountable_attributes].present?
@account.loan.update!(account_params[:accountable_attributes])
end
respond_to do |format|
format.html { redirect_back_or_to account_path(@account), notice: "Loan account updated" }
format.turbo_stream { stream_redirect_to account_path(@account), notice: "Loan account updated" }
end
else
@error_message = result.error || "Unable to update account details."
render :edit, status: :unprocessable_entity
end
end
end

View file

@ -4,59 +4,55 @@ class ValuationsController < ApplicationController
def create
account = Current.family.accounts.find(params.dig(:entry, :account_id))
result = account.update_balance(
balance: entry_params[:amount],
date: entry_params[:date],
currency: entry_params[:currency],
notes: entry_params[:notes]
)
if result.success?
@success_message = result.updated? ? "Balance updated" : "No changes made. Account is already up to date."
respond_to do |format|
format.html { redirect_back_or_to account_path(account), notice: @success_message }
format.turbo_stream { stream_redirect_back_or_to(account_path(account), notice: @success_message) }
end
if entry_params[:date].to_date == Date.current
account.update_current_balance!(balance: entry_params[:amount].to_d)
else
@error_message = result.error_message
render :new, status: :unprocessable_entity
account.reconcile_balance!(
balance: entry_params[:amount].to_d,
date: entry_params[:date].to_date
)
end
account.sync_later
respond_to do |format|
format.html { redirect_back_or_to account_path(account), notice: "Account value updated" }
format.turbo_stream { stream_redirect_back_or_to(account_path(account), notice: "Account value updated") }
end
end
def update
result = @entry.account.update_balance(
date: @entry.date,
balance: entry_params[:amount],
currency: entry_params[:currency],
notes: entry_params[:notes]
# ActiveRecord::Base.transaction do
@entry.account.reconcile_balance!(
balance: entry_params[:amount].to_d,
date: entry_params[:date].to_date
)
if result.success?
@entry.reload
if entry_params[:notes].present?
@entry.update!(notes: entry_params[:notes])
end
respond_to do |format|
format.html { redirect_back_or_to account_path(@entry.account), notice: result.updated? ? "Balance updated" : "No changes made. Account is already up to date." }
format.turbo_stream do
render turbo_stream: [
turbo_stream.replace(
dom_id(@entry, :header),
partial: "valuations/header",
locals: { entry: @entry }
),
turbo_stream.replace(@entry)
]
end
@entry.account.sync_later
@entry.reload
respond_to do |format|
format.html { redirect_back_or_to account_path(@entry.account), notice: "Account value updated" }
format.turbo_stream do
render turbo_stream: [
turbo_stream.replace(
dom_id(@entry, :header),
partial: "valuations/header",
locals: { entry: @entry }
),
turbo_stream.replace(@entry)
]
end
else
@error_message = result.error_message
render :show, status: :unprocessable_entity
end
end
private
def entry_params
params.require(:entry)
.permit(:date, :amount, :currency, :notes)
params.require(:entry).permit(:date, :amount, :notes)
end
end