diff --git a/app/controllers/money_moves_controller.rb b/app/controllers/money_moves_controller.rb index 2d7a83d..31a8d0e 100644 --- a/app/controllers/money_moves_controller.rb +++ b/app/controllers/money_moves_controller.rb @@ -22,6 +22,14 @@ class MoneyMovesController < ApplicationController error_response(:bad_request) end + def destroy + money_move = MoneyMove.find(params[:id]) + money_move.update!(deleted_at: DateTime.now) + index + rescue StandardError + error_response :bad_request + end + def money_move_params params.require(:money_move).permit( :description, diff --git a/app/models/homie.rb b/app/models/homie.rb index 9ad4538..eb491cf 100644 --- a/app/models/homie.rb +++ b/app/models/homie.rb @@ -7,7 +7,7 @@ class Homie < ApplicationRecord def self.info(importance, gang) # TODO: This can be improved - cash_totals = Homie.all.joins(:money_moves).group(:id).order(:id).sum(:amount) + cash_totals = Homie.all.joins(:money_moves).where(money_moves: { deleted_at: nil}).group(:id).order(:id).sum(:amount) work_totals = Homie.all.joins(:work).group(:id).order(:id).sum(:amount) Homie.where(['importance > ? and gang_id = ?', importance, gang]).map do |homie| diff --git a/client/src/homies/CashFlow.js b/client/src/homies/CashFlow.js index 5a30378..0c1e3c1 100644 --- a/client/src/homies/CashFlow.js +++ b/client/src/homies/CashFlow.js @@ -7,6 +7,7 @@ import {errorToast} from "../common/errorHelpers"; import InputModal from "../common/InputModal"; import M from "materialize-css"; import { Button } from 'react-materialize'; +import YesNoModal from "../common/YesNoModal"; const CashFlow = (props) => { const { homie_id } = useParams(); @@ -17,13 +18,16 @@ const CashFlow = (props) => { const updateData = (data) => { setCashFlow(data); - setTotalCashFlowAmount(data.reduce((sum, record) => sum + parseInt(record.amount), 0)); + setTotalCashFlowAmount(data.reduce((sum, record) => { + const amountToAdd = record.deleted_at ? 0 : parseInt(record.amount); + return sum + amountToAdd; + }, 0)); } useEffect( () => { (async () => { try { - const response = await axios.get(`/api/money_moves?homie_id=${parseInt(homie_id)}`); + const response = await axios.get(`/api/gangs/${gang.id}/homies/${homie_id}/money_moves`); if (response.status === 200 && response.data){ updateData(response.data); } @@ -33,37 +37,65 @@ const CashFlow = (props) => { })(); }, [homie_id]); - const dateBlock = (timestamp) => { timestampToDate(timestamp) } + const softDeleteMoneyMove = async (moneyMove) => { + try { + const response = await axios.delete(`/api/gangs/${gang.id}/homies/${homie_id}/money_moves/${moneyMove.id}`); + if (response.status === 200 && response.data){ + updateData(response.data); + } + } catch (e) { + errorToast(); + } + } - const flowData = cashFlow.map( (singleFlowData, index) => ( -