In this episode, we dive into building the scheduling system for employees—including how to set up their working days, times, and handle any time-off or exclusions.
We start off by creating a schedule model and migration. This involves setting up a table where each row represents an employee's schedule, with fields to specify when the schedule starts and ends, plus individual columns for each day of the week (Monday to Sunday). For each of those days, we store optional 'starts_at' and 'ends_at' times, so we can track exactly when an employee is supposed to work each day. Yes, it’s a bit repetitive, but doing it this way keeps things super simple when adding and updating schedules, especially from an admin panel.
After setting up the migration, we create the model relationships—an employee can have many schedules—and make sure our schedule model casts the overall start and end dates as date objects for easy manipulation. We also build a factory to help populate the weekly schedule times for our tests (think 9-to-5, Monday through Friday).
Then, we talk about schedule exclusions—these are periods when an employee shouldn't be considered available, like holidays or special time off. We set up a simple model and table for these, tied to the employee, with just a 'starts_at' and 'ends_at' timestamp. The idea is that these exclusions can "cut out" periods from the main schedule, making availability checks really flexible.
By the end of this episode, we've got all the tables, models, and basic relationships in place to handle recurring employee schedules and any exceptions. We’re ready to move on to the next part, where we'll actually use these to build out functionality!