This episode is for members only

Sign up to access "Build an Appointment Booking System With Livewire" right now.

Get started
Already a member? Sign in to continue
Playing
05. Employee schedules and exclusions tables

Episodes

0%
Your progress
  • Total: 3h 4m
  • Played: 0m
  • Remaining: 3h 4m
Join or sign in to track your progress

Transcript

00:00
Slightly more complicated are the schedules for the employees and the schedule exclusions, but we're going to dive more into the inner workings of this when we build up the core functionality.
00:10
Let's get the schema done, and hopefully things will start to make a little bit more sense. OK, so we're going to go ahead and create out another model. And this is going to be a schedule for an employee.
00:20
Employees are going to be allowed multiple schedules. We saw that from the introduction about how that looked in the database. Let's go ahead and create this out first of all.
00:29
Let's open up the Create Schedules table migration and start to fill this in. Now, obviously, this belongs to a particular employee. So let's go ahead and pull in a employee ID,
00:41
and let's just constrain this. And then we have that overall starts and ends at date, which is a long-term starts and ends at date. So we might want to have this start at the start of the year
00:53
and end at the end of the year. Then we want a new schedule for the next year, for example. So let's add in starts at, and then we'll do exactly the same thing for ends at.
01:03
Now, the annoying part about this is now we're going to have to define a column for every single day that the user wants to work. Now, there's a really good reason for this,
01:13
and that is that as we go through the employee's availability or their schedule and we build up availability for them inside of the class that we're eventually going to be building,
01:23
it's very easy to see which days that they work. So let's go ahead and build this out, and then like I said, we'll talk about this a little bit more later.
01:30
So we want a time in here, and we want Monday starts at. And that's going to be nullable because they might not work on a Monday. And we're going to have Monday ends at, which also
01:41
is going to be nullable. In fact, every single one of these is going to be nullable. So let's go and fill in the rest of these. OK, so I've gone ahead and added these for every single day
01:51
of the week, a starts at and an ends at. It's going to give us superfine control over the days and the times that people work or our employees work. OK, let's go ahead and run our migrations on this.
02:03
And then we can go over and set up the relationship. So if we head over to the employee model, we know that they have schedules. So let's go ahead and create out a schedules relationship
02:14
in here. And that is just going to have many. So let's just say has many and our schedule class in there. Great.
02:21
Now for the schedule model itself, we want to set up some casts in here. That's really important. So let's go and set up our cast for our starts at and ends
02:31
at date. And the reason we want to do this is we want these to be casted dates, so they are carbon instances.
02:38
So let's say starts at, and we'll set that to a date. That just means we can manipulate that really easily. And the same with ends at as well. We don't need to do this for all of the other days.
02:49
As we'll see a little bit later, we're going to do something a little bit more custom to work out whether they can actually work on them days or not. OK, so now that we've done this, let's work
02:58
on the schedule exclusions. So again, another model in here. And this is going to be a schedule exclusion. Let's create that out.
03:05
Let's open up the Create Schedule Exclusions table. And this is much easier, because we just want a starts at and an ends at date over a period of time when this is unavailable, when that employee is unavailable.
03:18
So again, we just want a foreign ID in here for the employee. And that will be constrained. And again, we just want a starts at and ends at date. That is all we need inside of here.
03:29
So let's say starts at. Let's pull this down and, of course, change this over to ends at. And we should be good.
03:35
OK, so let's go ahead and run our migrations on this. And we are good to go. So again, what we're going to do is head over to our schedule table.
03:44
Remember, we have exactly the same casts here. So we're going to go over to our exclusions. And we are going to apply exactly the same cast. So we get nice carbon objects through to here.
03:55
And we can do what we need. And again, if we head over to the employee where we added the schedules, we also have many schedule exclusions.
04:04
So let's go ahead and add that in now. So we've got that in there. And of course, change that over to schedule exclusion. So the whole idea here, just before we
04:13
start to build the core functionality, is that as we go through our schedules, we know which days and which sort of periods we're working. And we can add them to the availability.
04:23
But with the schedule exclusions, we can subtract them from the availability. So then we end up with the overall availability for the employee based on these two things.
04:33
And like I said, these are two things that are very powerful because schedules are very fine grained. And schedule exclusions will just cut out that availability from anything we've added in there.

Episode summary

In this episode, we take a hands-on approach to building two essential parts of our employee scheduling system: the employee schedules table and the schedule exclusions table.

First, we dive into designing the employee schedules. Employees can have multiple schedules—for example, one per year. Each schedule, besides referencing the employee, has start and end dates indicating the valid period, and then a column for the start and end time for each day of the week (e.g., Monday starts/ends at, Tuesday starts/ends at, etc.), all of which can be nullable to handle different working setups. This gives us super fine-grained control over when an employee is available. We run the migration and set up the model relationships, like connecting employees to their schedules and ensuring date fields are correctly cast so we can easily handle them in code.

Next up, we set up the schedule exclusions table. This table is a bit simpler—it just tracks the periods where an employee is unavailable, like vacations or leaves. Similar to the schedules table, we add start and end dates and hook up the model relationships so we can easily query exclusions for each employee.

Throughout, we point out how these two tables work together: you build up employee availability from their schedules, and then subtract stuff that's in their exclusions, resulting in the final availability we'll use later. Now that we have these database tables in place, we're ready to move on to the core scheduling logic in upcoming episodes!

Episode discussion

No comments, yet. Be the first!