Skip to content

Commit 48abe75

Browse files
committed
Fix daily booking issue
1 parent 6398a4e commit 48abe75

File tree

3 files changed

+111
-12
lines changed

3 files changed

+111
-12
lines changed

app/support/reservations/validations.rb

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,29 +123,46 @@ def conflicting_admin_reservation
123123
end
124124

125125
def satisfies_minimum_length?
126-
diff = reserve_end_at - reserve_start_at # in seconds
127-
return false unless product.min_reserve_mins.nil? || product.min_reserve_mins == 0 || diff / 60 >= product.min_reserve_mins
128-
true
126+
if product.daily_booking?
127+
return true if product.min_reserve_days.to_i == 0
128+
duration_days >= product.min_reserve_days
129+
else
130+
diff = reserve_end_at - reserve_start_at # in seconds
131+
product.min_reserve_mins.nil? || product.min_reserve_mins == 0 || diff / 60 >= product.min_reserve_mins
132+
end
129133
end
130134

131135
def satisfies_minimum_length
132-
errors.add(:base, :too_short, length: product.min_reserve_mins) unless satisfies_minimum_length?
136+
if product.daily_booking?
137+
errors.add(:base, :too_short_days, length: product.min_reserve_days) unless satisfies_minimum_length?
138+
else
139+
errors.add(:base, :too_short, length: product.min_reserve_mins) unless satisfies_minimum_length?
140+
end
133141
end
134142

135143
def satisfies_maximum_length?
136-
return true if product.max_reserve_mins.to_i == 0
137-
diff = reserve_end_at - reserve_start_at # in seconds
144+
if product.daily_booking?
145+
return true if product.max_reserve_days.to_i == 0
146+
duration_days <= product.max_reserve_days
147+
else
148+
return true if product.max_reserve_mins.to_i == 0
149+
diff = reserve_end_at - reserve_start_at # in seconds
138150

139-
# If this is updating because we're in the grace period, use the old value for checking duration
140-
if in_grace_period? && actual_start_at && reserve_start_at_changed? && reserve_start_at_was
141-
diff = reserve_end_at - reserve_start_at_was
142-
end
151+
# If this is updating because we're in the grace period, use the old value for checking duration
152+
if in_grace_period? && actual_start_at && reserve_start_at_changed? && reserve_start_at_was
153+
diff = reserve_end_at - reserve_start_at_was
154+
end
143155

144-
diff <= product.max_reserve_mins.minutes
156+
diff <= product.max_reserve_mins.minutes
157+
end
145158
end
146159

147160
def satisfies_maximum_length
148-
errors.add(:base, :too_long, length: product.max_reserve_mins) unless satisfies_maximum_length?
161+
if product.daily_booking?
162+
errors.add(:base, :too_long_days, length: product.max_reserve_days) unless satisfies_maximum_length?
163+
else
164+
errors.add(:base, :too_long, length: product.max_reserve_mins) unless satisfies_maximum_length?
165+
end
149166
end
150167

151168
def allowed_in_schedule_rules?

config/locales/en.models.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ en:
112112
no_schedule_group: You do not have permission to make a reservation at this time
113113
too_long: The reservation is too long. It cannot be longer than %{length} minutes.
114114
too_short: The reservation is too short. It must be at least %{length} minutes.
115+
too_long_days: The reservation is too long. It cannot be longer than %{length} days.
116+
too_short_days: The reservation is too short. It must be at least %{length} days.
115117
reserve_start_at:
116118
after_cutoff: must be at least %{hours} hours in the future
117119
in_past: must be in the future

spec/models/reservations_validations_spec.rb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,84 @@
151151
end
152152
end
153153
end
154+
155+
describe "daily booking min/max length validations" do
156+
subject(:reservation) do
157+
build(
158+
:setup_reservation,
159+
product:,
160+
reserve_start_at: start_at,
161+
reserve_end_at: end_at
162+
)
163+
end
164+
165+
let(:product) { create :setup_instrument, :daily_booking }
166+
let(:start_at) { Time.current }
167+
168+
describe "satisfies_maximum_length" do
169+
context "when max_reserve_days is not set" do
170+
let(:end_at) { start_at + 10.days }
171+
172+
it { is_expected.to be_valid }
173+
end
174+
175+
context "when max_reserve_days is set" do
176+
before { product.update!(max_reserve_days: 4) }
177+
178+
context "when reservation is within max days" do
179+
let(:end_at) { start_at + 3.days }
180+
181+
it { is_expected.to be_valid }
182+
end
183+
184+
context "when reservation equals max days" do
185+
let(:end_at) { start_at + 4.days }
186+
187+
it { is_expected.to be_valid }
188+
end
189+
190+
context "when reservation exceeds max days" do
191+
let(:end_at) { start_at + 6.days }
192+
193+
it "is invalid with too_long_days error" do
194+
is_expected.not_to be_valid
195+
expect(reservation.errors).to be_added(:base, :too_long_days, length: 4)
196+
end
197+
end
198+
end
199+
end
200+
201+
describe "satisfies_minimum_length" do
202+
context "when min_reserve_days is not set" do
203+
let(:end_at) { start_at + 1.day }
204+
205+
it { is_expected.to be_valid }
206+
end
207+
208+
context "when min_reserve_days is set" do
209+
before { product.update!(min_reserve_days: 2) }
210+
211+
context "when reservation meets minimum days" do
212+
let(:end_at) { start_at + 3.days }
213+
214+
it { is_expected.to be_valid }
215+
end
216+
217+
context "when reservation equals minimum days" do
218+
let(:end_at) { start_at + 2.days }
219+
220+
it { is_expected.to be_valid }
221+
end
222+
223+
context "when reservation is less than minimum days" do
224+
let(:end_at) { start_at + 1.day }
225+
226+
it "is invalid with too_short_days error" do
227+
is_expected.not_to be_valid
228+
expect(reservation.errors).to be_added(:base, :too_short_days, length: 2)
229+
end
230+
end
231+
end
232+
end
233+
end
154234
end

0 commit comments

Comments
 (0)