From 722fc71f170c450226813d2d470b75c491113369 Mon Sep 17 00:00:00 2001 From: Bilal Date: Fri, 9 Oct 2020 16:12:44 +0300 Subject: [PATCH 1/2] implement soft delete for money moves --- app/controllers/money_moves_controller.rb | 8 ++++++++ app/models/homie.rb | 2 +- config/routes.rb | 2 +- .../20201009114126_add_deleted_at_to_money_moves.rb | 5 +++++ db/schema.rb | 3 ++- 5 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20201009114126_add_deleted_at_to_money_moves.rb 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/config/routes.rb b/config/routes.rb index 06d8614..4a079e0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,10 +8,10 @@ Rails.application.routes.draw do end member do post 'settle' + resources :money_moves end end end - resources :money_moves resources :work resources :chips, only: %i[index create destroy] resources :chip_values, only: %i[create update destroy] diff --git a/db/migrate/20201009114126_add_deleted_at_to_money_moves.rb b/db/migrate/20201009114126_add_deleted_at_to_money_moves.rb new file mode 100644 index 0000000..993fc5f --- /dev/null +++ b/db/migrate/20201009114126_add_deleted_at_to_money_moves.rb @@ -0,0 +1,5 @@ +class AddDeletedAtToMoneyMoves < ActiveRecord::Migration[5.2] + def change + add_column :money_moves, :deleted_at, :timestamp + end +end \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index 80897d6..a38451d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_10_07_213411) do +ActiveRecord::Schema.define(version: 2020_10_09_114126) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -63,6 +63,7 @@ ActiveRecord::Schema.define(version: 2020_10_07_213411) do t.integer "homie_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.datetime "deleted_at" end create_table "work", force: :cascade do |t| -- 2.47.3 From 63e292c3322f3abea91160fc14c26c5923f5d2ed Mon Sep 17 00:00:00 2001 From: Bilal Date: Fri, 9 Oct 2020 16:15:07 +0300 Subject: [PATCH 2/2] handle soft delete of money moves --- client/src/homies/CashFlow.js | 70 +++++++++++++++++++++++++--------- client/src/homies/Flow.css | 6 +++ client/src/homies/MoveMoney.js | 2 +- 3 files changed, 58 insertions(+), 20 deletions(-) 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) => ( -
  • -
    -
    -
    -
    { singleFlowData.description }
    -
    { dateBlock(singleFlowData['created_at']) }
    + const flowData = cashFlow.map((singleFlowData, index) => { + const deleted = !!singleFlowData.deleted_at; + const description = deleted ? {singleFlowData.description} : singleFlowData.description; + const formattedCreatedAtDate = timestampToDate(singleFlowData['created_at']); + const formattedDeletedAtDate = deleted ? timestampToDate(singleFlowData['deleted_at']) : null; + const dateDescription = deleted ? + `${formattedCreatedAtDate} • deleted at ${formattedDeletedAtDate}` : `${formattedCreatedAtDate}`; + + const recordClass = deleted ? 'deleted-record' : 'record'; + + return ( +
  • +
    +
    +
    +
    {description}
    +
    {dateDescription}
    +
    + +
    0 ? 'amount-green' : ''}`}>{ formatMoney(singleFlowData.amount, gang) }
    + + softDeleteMoneyMove(singleFlowData)} + triggerNode={
    - -
    0 ? 'amount-green' : ''}`}>{ formatMoney(singleFlowData.amount, gang) }
    - -
  • - )); + + ) + }); const totalStats = () => { - const totalCount = cashFlow.length; - const firstFlowRow = totalCount > 0 ? cashFlow[0] : undefined; - const lastFlowRow = totalCount > 0 ? cashFlow[cashFlow.length - 1] : undefined; + const totalNonDeletedCount = cashFlow.filter(record => !record.deleted_at).length; + + const firstFlowRow = totalNonDeletedCount > 0 ? cashFlow[0] : undefined; + const lastFlowRow = totalNonDeletedCount > 0 ? cashFlow[cashFlow.length - 1] : undefined; const fromDate = lastFlowRow ? timestampToDate(lastFlowRow['created_at']) : ''; const toDate = firstFlowRow ? timestampToDate(firstFlowRow['created_at']) : ''; + const dateRange = firstFlowRow && lastFlowRow ? ` • ${fromDate} - ${toDate}` : ''; + return (

    - {`${totalCount} Records`}{` • ${fromDate} - ${toDate}`} + {`${totalNonDeletedCount} Records`}{dateRange}
    Total cash flow: {formatMoney(totalCashFlowAmount, gang)} diff --git a/client/src/homies/Flow.css b/client/src/homies/Flow.css index cf0cb75..470f1ae 100644 --- a/client/src/homies/Flow.css +++ b/client/src/homies/Flow.css @@ -33,6 +33,12 @@ padding-left: 11px; } +.deleted-record:hover { + border-left: rgb(85, 85, 85) solid 4px; + box-sizing: border-box; + padding-left: 11px; +} + .switch-box { margin-top: 10px; margin-bottom: 20px; diff --git a/client/src/homies/MoveMoney.js b/client/src/homies/MoveMoney.js index bdf2b66..f7918d2 100644 --- a/client/src/homies/MoveMoney.js +++ b/client/src/homies/MoveMoney.js @@ -77,7 +77,7 @@ const MoveMoney = (props) => { } - const submitResponse = await axios.post('/api/money_moves', moneyMoveRequest); + const submitResponse = await axios.post(`/api/gangs/${gang.id}/homies/${homie_id}/money_moves`, moneyMoveRequest); if (submitResponse && submitResponse.status === 200 && submitResponse.data === true) { M.toast({html: "Money lounde...moved"}); -- 2.47.3