In this episode, we focus on building a system that can determine when an employee is available to work for a particular service. We start by making sure we can reference both the employee and the service when calculating availability, since the length of the service affects how scheduling works.
We pull in some test data, like the first employee and the first service, and then dig into the code. The goal is to go through the employee's schedule and figure out on which days and during which times they are available. We do this by writing a method to extract the working hours for the current day, based on the schedule data, and only add periods when there’s actually work scheduled.
We test this logic by simulating different days of the week and tweaking the employee's working hours (like giving them Fridays off, or making them work different hours on Fridays). The code ensures we skip days they don’t work, and that we only consider their real working hours for every day.
One important detail: we fine-tune the availability so that employees can’t be booked for a slot that would finish after their scheduled end time. We subtract the service's duration from their end time, making sure appointments fit snugly into their actual working hours. By the end, we end up with a set of time periods for each day that truly reflect when the employee can take a new appointment for a specific service.
This sets us up nicely to later handle booked appointments and more complex scheduling rules, but for now, we have a solid system to get real, dynamic working hours based on the employee, the day, and the service length.