Skip to content

Commit

Permalink
Do not allow reservations outside of working hours
Browse files Browse the repository at this point in the history
  • Loading branch information
mostlyobvious committed Oct 10, 2023
1 parent d9d8dfd commit d43be65
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
18 changes: 11 additions & 7 deletions lib/booking/day_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@ module Booking
class DaySchedule
CannotReserve = Class.new(StandardError)

def initialize
def initialize(allowed_time_range)
@reserved_slots = Set.new
@allowed_time_range = allowed_time_range
end

def reserve(time_range)
cannot_reserve unless allowed_time_range.cover?(time_range)
cannot_reserve if reserved_slots.any?(covers?(time_range))
reserved_slots << time_range
visit_scheduled(time_range)
end

private

attr_reader :reserved_slots
attr_reader :reserved_slots, :allowed_time_range

def visit_scheduled(time_range)
VisitScheduled.new(data: {
scheduled_at: time_range.first,
duration: time_range.last - time_range.first
})
VisitScheduled.new(
data: {
scheduled_at: time_range.first,
duration: time_range.last - time_range.first
}
)
end

def covers?(time_range)
Expand All @@ -31,4 +35,4 @@ def cannot_reserve
raise CannotReserve
end
end
end
end
31 changes: 25 additions & 6 deletions test/lib/booking/day_schedule_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ class DayScheduleTest < ActiveSupport::TestCase
cover DaySchedule

test "happy path" do
day_schedule = DaySchedule.new
day_schedule = DaySchedule.new(open_hours)

assert_event visit_scheduled(at_12_00..at_13_00),
day_schedule.reserve(at_12_00..at_13_00)
end

test "cannot reserve same slot twice" do
day_schedule = DaySchedule.new
day_schedule = DaySchedule.new(open_hours)
day_schedule.reserve(at_12_00..at_13_00)

assert_raises DaySchedule::CannotReserve do
Expand All @@ -21,7 +21,7 @@ class DayScheduleTest < ActiveSupport::TestCase
end

test "cannot reserve partially covered slot" do
day_schedule = DaySchedule.new
day_schedule = DaySchedule.new(open_hours)
day_schedule.reserve(at_12_00..at_13_00)

assert_raises DaySchedule::CannotReserve do
Expand All @@ -30,7 +30,7 @@ class DayScheduleTest < ActiveSupport::TestCase
end

test "cannot reserve partially covered slot, again" do
day_schedule = DaySchedule.new
day_schedule = DaySchedule.new(open_hours)
day_schedule.reserve(at_12_00..at_13_00)

assert_raises DaySchedule::CannotReserve do
Expand All @@ -39,15 +39,26 @@ class DayScheduleTest < ActiveSupport::TestCase
end

test "can reserve non-intersecting slots" do
day_schedule = DaySchedule.new
day_schedule = DaySchedule.new(open_hours)
day_schedule.reserve(at_12_00..at_12_45)

assert_event visit_scheduled(at_13_00..at_13_45),
day_schedule.reserve(at_13_00..at_13_45)
end

test "cannot reserve outside of open hours" do
day_schedule = DaySchedule.new(open_hours)
open_at, close_at = open_hours.first, open_hours.last

assert_raises { day_schedule.reserve(open_at..(close_at + 1.hour)) }
end

private

def open_hours
at_11_00..at_19_00
end

def visit_scheduled(time_range)
VisitScheduled.new(
data: {
Expand All @@ -57,6 +68,10 @@ def visit_scheduled(time_range)
)
end

def at_11_00
Time.new(2023, 10, 10, 11, 00)
end

def at_11_45
Time.new(2023, 10, 10, 11, 45)
end
Expand All @@ -76,5 +91,9 @@ def at_13_00
def at_13_45
Time.new(2023, 10, 10, 13, 45)
end

def at_19_00
Time.new(2023, 10, 10, 19, 00)
end
end
end
end

0 comments on commit d43be65

Please sign in to comment.