Skip to content
Open
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
41 changes: 29 additions & 12 deletions app/support/reservations/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,29 +123,46 @@ def conflicting_admin_reservation
end

def satisfies_minimum_length?
diff = reserve_end_at - reserve_start_at # in seconds
return false unless product.min_reserve_mins.nil? || product.min_reserve_mins == 0 || diff / 60 >= product.min_reserve_mins
true
if product.daily_booking?
return true if product.min_reserve_days.to_i == 0
duration_days >= product.min_reserve_days
else
diff = reserve_end_at - reserve_start_at # in seconds
product.min_reserve_mins.nil? || product.min_reserve_mins == 0 || diff / 60 >= product.min_reserve_mins
end
end

def satisfies_minimum_length
errors.add(:base, :too_short, length: product.min_reserve_mins) unless satisfies_minimum_length?
if product.daily_booking?
errors.add(:base, :too_short_days, length: product.min_reserve_days) unless satisfies_minimum_length?
else
errors.add(:base, :too_short, length: product.min_reserve_mins) unless satisfies_minimum_length?
end
end

def satisfies_maximum_length?
return true if product.max_reserve_mins.to_i == 0
diff = reserve_end_at - reserve_start_at # in seconds
if product.daily_booking?
return true if product.max_reserve_days.to_i == 0
duration_days <= product.max_reserve_days
else
return true if product.max_reserve_mins.to_i == 0
diff = reserve_end_at - reserve_start_at # in seconds

# If this is updating because we're in the grace period, use the old value for checking duration
if in_grace_period? && actual_start_at && reserve_start_at_changed? && reserve_start_at_was
diff = reserve_end_at - reserve_start_at_was
end
# If this is updating because we're in the grace period, use the old value for checking duration
if in_grace_period? && actual_start_at && reserve_start_at_changed? && reserve_start_at_was
diff = reserve_end_at - reserve_start_at_was
end

diff <= product.max_reserve_mins.minutes
diff <= product.max_reserve_mins.minutes
end
end

def satisfies_maximum_length
errors.add(:base, :too_long, length: product.max_reserve_mins) unless satisfies_maximum_length?
if product.daily_booking?
errors.add(:base, :too_long_days, length: product.max_reserve_days) unless satisfies_maximum_length?
else
errors.add(:base, :too_long, length: product.max_reserve_mins) unless satisfies_maximum_length?
end
end

def allowed_in_schedule_rules?
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ en:
no_schedule_group: You do not have permission to make a reservation at this time
too_long: The reservation is too long. It cannot be longer than %{length} minutes.
too_short: The reservation is too short. It must be at least %{length} minutes.
too_long_days: The reservation is too long. It cannot be longer than %{length} days.
too_short_days: The reservation is too short. It must be at least %{length} days.
reserve_start_at:
after_cutoff: must be at least %{hours} hours in the future
in_past: must be in the future
Expand Down
80 changes: 80 additions & 0 deletions spec/models/reservations_validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,84 @@
end
end
end

describe "daily booking min/max length validations" do
subject(:reservation) do
build(
:setup_reservation,
product:,
reserve_start_at: start_at,
reserve_end_at: end_at
)
end

let(:product) { create :setup_instrument, :daily_booking }
let(:start_at) { Time.current }

describe "satisfies_maximum_length" do
context "when max_reserve_days is not set" do
let(:end_at) { start_at + 10.days }

it { is_expected.to be_valid }
end

context "when max_reserve_days is set" do
before { product.update!(max_reserve_days: 4) }

context "when reservation is within max days" do
let(:end_at) { start_at + 3.days }

it { is_expected.to be_valid }
end

context "when reservation equals max days" do
let(:end_at) { start_at + 4.days }

it { is_expected.to be_valid }
end

context "when reservation exceeds max days" do
let(:end_at) { start_at + 6.days }

it "is invalid with too_long_days error" do
is_expected.not_to be_valid
expect(reservation.errors).to be_added(:base, :too_long_days, length: 4)
end
end
end
end

describe "satisfies_minimum_length" do
context "when min_reserve_days is not set" do
let(:end_at) { start_at + 1.day }

it { is_expected.to be_valid }
end

context "when min_reserve_days is set" do
before { product.update!(min_reserve_days: 2) }

context "when reservation meets minimum days" do
let(:end_at) { start_at + 3.days }

it { is_expected.to be_valid }
end

context "when reservation equals minimum days" do
let(:end_at) { start_at + 2.days }

it { is_expected.to be_valid }
end

context "when reservation is less than minimum days" do
let(:end_at) { start_at + 1.day }

it "is invalid with too_short_days error" do
is_expected.not_to be_valid
expect(reservation.errors).to be_added(:base, :too_short_days, length: 2)
end
end
end
end
end
end