Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent non-MC from grabbing more than 16 hours #391

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions app/controllers/duties_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

# rubocop:disable Metrics/ClassLength
class DutiesController < ApplicationController
def index
@header_iter = generate_header_iter
Expand Down Expand Up @@ -137,17 +136,54 @@ def prepare_announcements

def grab_duty(grab_duty_ids, start_of_week)
Duty.transaction do
Duty.find(grab_duty_ids).each do |duty|
duty.update!(user: current_user, free: false, request_user: nil)
duties = Duty.find(grab_duty_ids)
can_grab = true
unless current_user.mc
weeks = separate(duties)
weeks.each do |start, wk_duties|
start_date = start.to_date
end_date = start_date + 6.days
total_hours = helpers.current_user_hours(start_date, end_date) +
helpers.sum_hours(wk_duties)
next unless total_hours > 16 && !wk_duties.empty?

redirect_to duties_path(start_date: start_of_week),
alert: 'Cannot duty for more than 16 hours!'
can_grab = false
end
end

if can_grab
duties.each do |duty|
duty.update!(user: current_user, free: false, request_user: nil)
end
redirect_to duties_path(start_date: start_of_week),
notice: 'Duty successfully grabbed!'
end
redirect_to duties_path(start_date: start_of_week),
notice: 'Duty successfully grabbed!'
end
rescue ActiveRecord::RecordInvalid
redirect_to duties_path(start_date: start_of_week),
alert: 'Error in grabbing duty! Please try again'
end

def separate(duties)
start = Time.zone.today.beginning_of_week.to_date
num_duties = duties.length
count = 0
result = {}

while count < num_duties
wk_duties = duties.select do |d|
d.date.beginning_of_week.to_date == start
end
count += wk_duties.length
result[start.to_s] = wk_duties
start += 7.days
end

result
end

def drop_duties(drop_duty_ids, start_of_week)
if can_drop_duties?(drop_duty_ids)
begin
Expand All @@ -165,4 +201,3 @@ def drop_duties(drop_duty_ids, start_of_week)
end
end
end
# rubocop:enable Metrics/ClassLength
10 changes: 7 additions & 3 deletions app/helpers/duties_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ def process_duties(start_date, end_date, first_time, last_time, places)
end
end

def current_user_hours
def current_user_hours(start_date, end_date)
relevant_duties = Duty.includes(:time_range)
.where(user_id: current_user.id,
date: @start_date..@end_date,
date: start_date..end_date,
free: false, request_user_id: nil)
total_seconds = relevant_duties.map do |d|
sum_hours(relevant_duties)
end

def sum_hours(duties)
total_seconds = duties.map do |d|
d.time_range.end_time - d.time_range.start_time
end.sum
total_hours = total_seconds / 3600
Expand Down
4 changes: 2 additions & 2 deletions app/views/duties/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<% if can?(:update_default_duties, Duty) %>
<%= link_to 'Update Default Duties', availabilities_places_path, class: "btn btn-large btn-light duty-table-button" %>
<% end %>
<div class = "duty-hours<%= current_user_hours > 16 ? " duty-hours-exceed" : "" %>">
Your hours: <%= current_user_hours %>
<div class = "duty-hours<%= current_user_hours(@start_date, @end_date) > 16 ? " duty-hours-exceed" : "" %>">
Your hours: <%= current_user_hours(@start_date, @end_date) %>
</div>
</div>

Expand Down
29 changes: 29 additions & 0 deletions spec/controllers/duties_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,35 @@
should redirect_to duties_path
expect(flash[:alert]).to be('Invalid duties to grab')
end

it 'does nothing when non-MC user tries to grab beyond 16 hours' do
timeslot = create(:timeslot, mc_only: false)
duty = create(:duty, timeslot: timeslot, free: true)
allow(controller.helpers).to receive(:current_user_hours).and_return(16)
start_of_week = duty.date.beginning_of_week

patch :grab, params: { duty_id: { duty.id => duty.id } }
should redirect_to duties_path(start_date: start_of_week)
expect(flash[:alert]).to be('Cannot duty for more than 16 hours!')
end

it 'grabs duty when MC user tries to grab beyond 16 hours' do
sign_in create(:user, mc: true)
timeslot = create(:timeslot)
duty = create(:duty, timeslot: timeslot, free: true)
allow(controller.helpers).to receive(:current_user_hours).and_return(16)
start_of_week = duty.date.beginning_of_week

expect do
patch :grab, params: { duty_id: { duty.id => duty.id } }
duty.reload
end.to change { duty.user }.to(subject.current_user)

expect(duty.free).to be(false)
expect(duty.request_user_id).to be(nil)
should redirect_to duties_path(start_date: start_of_week)
expect(flash[:notice]).to be('Duty successfully grabbed!')
end
end

describe 'POST duties#drop' do
Expand Down
21 changes: 18 additions & 3 deletions spec/helpers/duties_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,25 @@
user = create(:user)
sign_in user
duties = create_list(:duty, 10, user: user)
assign(:start_date, duties[0].date)
assign(:end_date, duties[9].date)
start_date = duties[0].date
end_date = duties[9].date

result = helper.current_user_hours
result = helper.current_user_hours(start_date, end_date)
expect(result).to eq((duties[-1].time_range.end_time -
duties[0].time_range.start_time) / 3600)

# Hack to reset time_range
create_list(:duty, 14, user: user)
end
end

describe '#sum_hours' do
it 'calculate hours correctly' do
user = create(:user)
sign_in user
duties = create_list(:duty, 10, user: user)

result = helper.sum_hours(duties)
expect(result).to eq((duties[-1].time_range.end_time -
duties[0].time_range.start_time) / 3600)

Expand Down