Allow entering custom amount to settle #18

Merged
bilal.catic merged 4 commits from allow-entering-custom-amount-to-settle into master 2020-10-06 08:43:05 +02:00
3 changed files with 6 additions and 5 deletions
Showing only changes of commit 04abd4be78 - Show all commits

View File

@@ -28,9 +28,10 @@ class HomiesController < ApplicationController
def settle def settle
homie_id = params[:homie_id].to_i homie_id = params[:homie_id].to_i
amount = params[:amount].present? ? params[:amount].to_d : nil
homie = Homie.find(homie_id) homie = Homie.find(homie_id)
if homie.settle if homie.settle(amount)
json_response [] json_response []
else else
error_response :bad_request error_response :bad_request

View File

@@ -12,8 +12,8 @@ class Homie < ApplicationRecord
end end
end end
def settle def settle(amount)
total = MoneyMove.where(homie_id: id).all.sum(:amount) total = amount.nil? ? MoneyMove.where(homie_id: id).all.sum(:amount) : amount
all_money_moves = MoneyMove.where(homie_id: id).all all_money_moves = MoneyMove.where(homie_id: id).all
transaction do transaction do
@@ -22,7 +22,7 @@ class Homie < ApplicationRecord
MoneyMove.create_settle_record(id, total) MoneyMove.create_settle_record(id, total)
end end
true true
rescue StandardError => e rescue StandardError
false false
end end
end end

View File

@@ -10,7 +10,7 @@ Rails.application.routes.draw do
get 'cash' get 'cash'
end end
member do member do
delete 'settle' post 'settle'
end end
end end
end end