Skip to content
Merged
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
1 change: 1 addition & 0 deletions app/controllers/reservations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def new
raise ActiveRecord::RecordNotFound unless @reservation.nil?

@reservation = NextAvailableReservationFinder.new(@instrument).next_available_for(current_user, acting_user)

@reservation.order_detail = @order_detail

authorize! :new, @reservation
Expand Down
9 changes: 8 additions & 1 deletion app/controllers/single_reservations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SingleReservationsController < ApplicationController

def new
@reservation = NextAvailableReservationFinder
.new(@instrument)
.new(@instrument, start_at: start_at_param)
.next_available_for(current_user, acting_user)

@reservation.order_detail = @order_detail
Expand Down Expand Up @@ -64,4 +64,11 @@ def set_windows
@reservation_window = ReservationWindow.new(@reservation, current_user)
end

def start_at_param
return if (start_at = params[:start_at]).blank?

Time.zone.parse(start_at)
rescue ArgumentError
nil
end
end
23 changes: 13 additions & 10 deletions app/services/next_available_reservation_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
# Used by the reservations controllers to find the default
# reservation times to display
class NextAvailableReservationFinder
def initialize(product)
attr_reader :product, :start_at

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Is the product accessor being used?


def initialize(product, start_at: nil)
@product = product
@start_at = start_at || 1.minute.from_now
end

def next_available_for(current_user, acting_user)
options = current_user.can_override_restrictions?(@product) ? {} : { user: acting_user }
next_available = @product.next_available_reservation(
after: 1.minute.from_now,
options = current_user.can_override_restrictions?(product) ? {} : { user: acting_user }
next_available = product.next_available_reservation(
after: start_at,
duration: default_duration,
options:
)
Expand All @@ -21,16 +24,16 @@ def next_available_for(current_user, acting_user)
private

def default_reservation
Reservation.new(product: @product,
reserve_start_at: Time.current,
reserve_end_at: default_duration.from_now)
Reservation.new(product:,
reserve_start_at: start_at,
reserve_end_at: start_at + default_duration)
end

def default_duration
if @product.daily_booking?
(@product.min_reserve_days || 1).days
if product.daily_booking?
(product.min_reserve_days || 1).days
else
(@product.min_reserve_mins.to_i > 0 ? @product.min_reserve_mins : 30).minutes
(product.min_reserve_mins.to_i > 0 ? product.min_reserve_mins : 30).minutes
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= html("body",
user_name: @user.full_name,
new_reservation_url: new_facility_instrument_single_reservation_url(@product.facility, @product),
new_reservation_url: new_facility_instrument_single_reservation_url(@product.facility, @product, start_at: @time_range.start_at&.iso8601),
time_range: @time_range,
product: @product,
facility: @product.facility,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= text("body",
user_name: @user.full_name,
new_reservation_url: new_facility_instrument_single_reservation_url(@product.facility, @product),
new_reservation_url: new_facility_instrument_single_reservation_url(@product.facility, @product, start_at: @time_range.start_at&.iso8601),
time_range: @time_range,
product: @product,
facility: @product.facility)
Expand Down
2 changes: 1 addition & 1 deletion config/locales/en.models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ en:
product_display_group:
one: Product Group
other: Product Groups
product_notification: Product Notification
product_notification: User Notification
research_safety_certificate:
one: Certificate
other: Certificates
Expand Down
4 changes: 2 additions & 2 deletions config/locales/views/en.facility_product_notifications.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ en:
no_results: No users found
selected_users: Selected Users
select_products_hint: Select instruments
select_users_hint: Search and select users one by one
select_users_hint: Search and select users to receive notifications
hints:
email_subject: Override default email subject
reservation_days: Reservations canceled within this amount of days will trigger the notification
reservation_days: Reservations canceled within this number of days from today will trigger the notification.
slot_available: Notify users when a Reservation or Admin Hold is canceled for any of the selected instruments
user_search_results:
add_user: Add
Expand Down
6 changes: 5 additions & 1 deletion config/locales/views/en.product_notification_mailer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ en:
body: |
Hello %{user_name},

Time slot %{time_range} became available for %{product} at %{facility}.
There has been a cancellation on %{product}.

The following time slot is now available:

%{time_range}

[Make a reservation](%{new_reservation_url})
2 changes: 1 addition & 1 deletion spec/requests/facility_product_notifications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

expect(page).to have_content("No Product Notifications have been created")
expect(page).to have_link(
"Add Product Notification",
"Add User Notification",
href: new_facility_product_notification_path(facility),
)
end
Expand Down
47 changes: 47 additions & 0 deletions spec/requests/single_reservations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe "SingleReservationsController" do
describe "new" do
let(:user) { create(:user) }
let(:instrument) { create(:setup_instrument) }
let(:facility) { instrument.facility }

let(:action) do
lambda do |params = {}|
get new_facility_instrument_single_reservation_path(facility, instrument, **params)
end
end

before do
login_as user
end

context "when start_at param is provided" do
let(:start_at) { 1.day.from_now }

it "finds next available with that value" do
expect(NextAvailableReservationFinder).to(
receive(:new)
.with(instrument, start_at:)
).and_call_original

action.call(start_at:)
end

context "when start at is invalid" do
let(:start_at) { "Some invalid date" }

it "finds next available with start_at nil" do
expect(NextAvailableReservationFinder).to(
receive(:new)
.with(instrument, start_at: nil)
).and_call_original

action.call(start_at:)
end
end
end
end
end
29 changes: 28 additions & 1 deletion spec/services/next_available_reservation_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

RSpec.describe NextAvailableReservationFinder do
let(:user) { build(:user) }
subject(:reservation) { described_class.new(instrument).next_available_for(user, user) }
subject(:reservation) do
described_class.new(instrument).next_available_for(user, user)
end

describe "time scheduled instrument" do
let(:instrument) { build(:instrument, min_reserve_mins: 0) }
Expand Down Expand Up @@ -66,4 +68,29 @@
end
end
end

describe "start time specified" do
let(:start_at) { 10.days.from_now }
let(:instrument) { build(:instrument, min_reserve_mins: 0) }
let(:finder) do
described_class
.new(instrument, start_at:)
end
let(:reservation) do
finder.next_available_for(user, user)
end

it "returns a reservation starting on start_at" do
expect(reservation.reserve_start_at).to eq(start_at)
end

it "calls next available reservation after start_at" do
expect(instrument).to(
receive(:next_available_reservation)
.with(a_hash_including(after: start_at))
)

finder.next_available_for(user, user)
end
end
end
Loading