In this episode, we're focusing on making sure that our list of available appointment slots doesn't include times that are already booked. First, we check the current list of slots and set up a fake time in Laravel so we're just working with today for our tests. We then add a test appointment to the database (say, from 1:00 to 1:30), so we have something to filter out.
Next, we move to the code to see how we can subtract these booked appointments from the available time periods. We create a new method called removeAppointments
, which takes the set of available periods and the employee in question. The idea is to compare these periods with existing, non-cancelled appointments on that day and remove any slots that overlap.
We also talk a bit about performance and how you might want to scope your queries if you have loads of appointments, but for now, we're just iterating through them all for simplicity. We do a couple of checks and tweaks to handle appointment durations, making sure everything lines up exactly – even down to adding an extra minute just to deal with any edge cases.
After all that, we test things out by changing appointments around and checking that the correct slots show up as available only for the employees that aren't busy at those times. Finally, we get ready to add some extra helper methods and even a custom collection class to make working with availability and building the UI easier later on. Last thing, we mention that next up we'll add some tests to double-check that everything is behaving as it should.