Merge branch 'add-settle-support' into 'master'
Add settle support See merge request saburly/gangsta/roraccounting!13
This commit was merged in pull request #13.
This commit is contained in:
@@ -26,6 +26,19 @@ class HomiesController < ApplicationController
|
||||
json_response(Homie.cash(importance))
|
||||
end
|
||||
|
||||
def settle
|
||||
homie_id = params[:homie_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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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 (
|
||||
<tr key={homieLine.homie.id}>
|
||||
@@ -32,7 +50,13 @@ const Cash = (props) => {
|
||||
{ formatMoney(homieLine.amount) }
|
||||
</td>
|
||||
<td className="cash-cell-left">
|
||||
[ settle ]
|
||||
[
|
||||
<YesNoModal
|
||||
body={"Maan, y'a sure about this? Flow history goes bye bye!"}
|
||||
yesAction={() => settleFlowForHomie(homieLine.homie.id)}
|
||||
triggerNode={<a href='#'>Settle</a>}
|
||||
/>
|
||||
]
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
|
||||
@@ -4,10 +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
|
||||
member do
|
||||
delete 'settle'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user