From 0e5682a9a07494643b1bacc2124c23057630ab67 Mon Sep 17 00:00:00 2001 From: Bilal Date: Thu, 17 Sep 2020 14:53:19 +0300 Subject: [PATCH 1/3] Handle click on settle link --- client/src/cash/Cash.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/client/src/cash/Cash.js b/client/src/cash/Cash.js index a234b69..6f22627 100644 --- a/client/src/cash/Cash.js +++ b/client/src/cash/Cash.js @@ -1,13 +1,17 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, useReducer } from 'react'; import { Button, Table } from 'react-materialize'; import './Cash.css'; import axios from 'axios'; import { MAKE_MONEY_MOVE } from '../RouteNames'; import { withRouter } from 'react-router-dom'; import { formatMoney } from '../common/formatting'; +import YesNoModal from "../common/YesNoModal"; +import { errorToast } from "../common/errorHelpers"; +import M from 'materialize-css'; const Cash = (props) => { const [homiesCash, setHomiesCash] = useState([]); + const [, forceUpdate] = useReducer(x => x + 1, 0); //const [importance, setImportance] = useState(10); useEffect( () => { @@ -22,6 +26,20 @@ const Cash = (props) => { getCashForHomies(); }, []); + const settleFlowForHomie = async (id) => { + try { + const response = await axios.delete(`/api/homies/${id}/settle`); + if (response.status === 200 && response.data) { + M.toast({ html: 'Settled' }); + }else{ + errorToast(); + } + } catch(e) { + errorToast(); + } + forceUpdate(); + } + const cashTableBody = homiesCash.map( (homieLine) => { return ( @@ -32,7 +50,13 @@ const Cash = (props) => { { formatMoney(homieLine.amount) } - [ settle ] + [ + settleFlowForHomie(homieLine.homie.id)} + triggerNode={Settle} + /> + ] ); From f459052fcb8113ddee935dbbc0c29434cb0bdfc9 Mon Sep 17 00:00:00 2001 From: Bilal Date: Thu, 17 Sep 2020 15:00:55 +0300 Subject: [PATCH 2/3] handle settling records for homie --- app/controllers/homies_controller.rb | 13 +++++++++++++ app/models/homie.rb | 14 ++++++++++++++ app/models/money_move.rb | 10 ++++++++++ config/routes.rb | 1 + 4 files changed, 38 insertions(+) diff --git a/app/controllers/homies_controller.rb b/app/controllers/homies_controller.rb index 38fc39e..8415da4 100644 --- a/app/controllers/homies_controller.rb +++ b/app/controllers/homies_controller.rb @@ -26,6 +26,19 @@ class HomiesController < ApplicationController json_response(Homie.cash(importance)) 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 def homie_params diff --git a/app/models/homie.rb b/app/models/homie.rb index f09ff31..16aab9c 100644 --- a/app/models/homie.rb +++ b/app/models/homie.rb @@ -10,4 +10,18 @@ class Homie < ApplicationRecord { homie: homie, amount: total } 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 diff --git a/app/models/money_move.rb b/app/models/money_move.rb index 41a16e1..50b7e7b 100644 --- a/app/models/money_move.rb +++ b/app/models/money_move.rb @@ -13,6 +13,16 @@ class MoneyMove < ApplicationRecord 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 private diff --git a/config/routes.rb b/config/routes.rb index 19d93cc..73f5e0e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,7 @@ Rails.application.routes.draw do collection do get 'cash' end + delete 'settle' end end end From 73eb22dbfd3a52fe371b3aafcc99357066eba33a Mon Sep 17 00:00:00 2001 From: Bilal Date: Thu, 17 Sep 2020 20:23:58 +0300 Subject: [PATCH 3/3] fix param name --- app/controllers/homies_controller.rb | 2 +- config/routes.rb | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/controllers/homies_controller.rb b/app/controllers/homies_controller.rb index 8415da4..014751e 100644 --- a/app/controllers/homies_controller.rb +++ b/app/controllers/homies_controller.rb @@ -27,7 +27,7 @@ class HomiesController < ApplicationController end def settle - homie_id = params[:homy_id].to_i + homie_id = params[:homie_id].to_i homie = Homie.find(homie_id) if homie.settle diff --git a/config/routes.rb b/config/routes.rb index 73f5e0e..1a36dfd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,11 +4,13 @@ Rails.application.routes.draw do resources :money_moves resources :chips, only: %i[index create destroy] resources :chip_values, only: %i[create update destroy] - resources :homies do + resources :homies, param: :homie_id do collection do get 'cash' end - delete 'settle' + member do + delete 'settle' + end end end end