handle settling records for homie
This commit is contained in:
@@ -26,6 +26,19 @@ class HomiesController < ApplicationController
|
|||||||
json_response(Homie.cash(importance))
|
json_response(Homie.cash(importance))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def settle
|
||||||
|
homie_id = params[:homy_id].to_i
|
||||||
|
homie = Homie.find(homie_id)
|
||||||
|
|
||||||
|
if homie.settle
|
||||||
|
json_response []
|
||||||
|
else
|
||||||
|
error_response :bad_request
|
||||||
|
end
|
||||||
|
rescue StandardError
|
||||||
|
error_response :bad_request
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def homie_params
|
def homie_params
|
||||||
|
|||||||
@@ -10,4 +10,18 @@ class Homie < ApplicationRecord
|
|||||||
{ homie: homie, amount: total }
|
{ homie: homie, amount: total }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def settle
|
||||||
|
total = MoneyMove.where(homie_id: id).all.sum(:amount)
|
||||||
|
all_money_moves = MoneyMove.where(homie_id: id).all
|
||||||
|
|
||||||
|
transaction do
|
||||||
|
all_money_moves.destroy_all
|
||||||
|
|
||||||
|
MoneyMove.create_settle_record(id, total)
|
||||||
|
end
|
||||||
|
true
|
||||||
|
rescue StandardError => e
|
||||||
|
false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -13,6 +13,16 @@ class MoneyMove < ApplicationRecord
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.create_settle_record(homie_id, amount)
|
||||||
|
new_desc = "Settled on #{DateTime.now.utc}"
|
||||||
|
settle_record = MoneyMove.new(
|
||||||
|
homie_id: homie_id,
|
||||||
|
amount: amount,
|
||||||
|
description: new_desc
|
||||||
|
)
|
||||||
|
settle_record.save
|
||||||
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
private
|
private
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ Rails.application.routes.draw do
|
|||||||
collection do
|
collection do
|
||||||
get 'cash'
|
get 'cash'
|
||||||
end
|
end
|
||||||
|
delete 'settle'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user