send total work hrs for homie

This commit is contained in:
Bilal
2020-10-06 00:17:21 +03:00
parent e608cfd773
commit 3d63a796bd
3 changed files with 11 additions and 9 deletions

View File

@@ -21,9 +21,9 @@ class HomiesController < ApplicationController
end end
end end
def cash def info
importance = params[:importance].present? ? params[:importance].to_i : -1 importance = params[:importance].to_i
json_response(Homie.cash(importance)) json_response(Homie.info(importance))
end end
def settle def settle
@@ -32,7 +32,7 @@ class HomiesController < ApplicationController
homie = Homie.find(homie_id) homie = Homie.find(homie_id)
if homie.settle(amount) if homie.settle(amount)
cash info
else else
error_response :bad_request error_response :bad_request
end end

View File

@@ -3,12 +3,14 @@ class Homie < ApplicationRecord
has_many :work has_many :work
belongs_to :chip belongs_to :chip
def self.cash(importance) def self.info(importance)
totals = Homie.all.joins(:money_moves).group(:id).order(:id).sum(:amount) cash_totals = Homie.all.joins(:money_moves).group(:id).order(:id).sum(:amount)
work_totals = Homie.all.joins(:work).group(:id).order(:id).sum(:amount)
Homie.where(["importance > ?", importance]).map do |homie| Homie.where(["importance > ?", importance]).map do |homie|
total = totals.fetch(homie.id, 0) cash_total = cash_totals.fetch(homie.id, 0)
{ homie: homie, amount: total } work_total = work_totals.fetch(homie.id, 0)
{ homie: homie, amount: cash_total, work: work_total }
end end
end end

View File

@@ -7,7 +7,7 @@ Rails.application.routes.draw do
resources :chip_values, only: %i[create update destroy] resources :chip_values, only: %i[create update destroy]
resources :homies, param: :homie_id do resources :homies, param: :homie_id do
collection do collection do
get 'cash' get 'info'
end end
member do member do
post 'settle' post 'settle'