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
13. Removing schedule exclusions

Episodes

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

Transcript

00:00
While we're inside of this iteration for each of the days and we add the availability for the schedule, we can also inside of here subtract
00:08
any schedule exclusions. So holidays, lunch breaks, whatever you want to do. OK, so we already know that we've got the relationship set up in here.
00:17
So just after we've added the availability from the schedule, let's go ahead and access the employee. Let's access the schedule exclusions. And let's go ahead and iterate over each of these.
00:28
Now, it's important to note at this point when we're iterating over these, we're not scoping these by the date that we have selected. We might go ahead and update that a little bit later
00:37
and that's going to speed things up a little bit. But since we're accessing the collection here, we're not going to end up with an N plus one problem. OK, so now that we've got the exclusion in here,
00:45
let's pull this into this. So let's just call this, well, we'll call it schedule exclusion. And again, we're going to go ahead
00:52
and defer to another method here. So let's go and say this subtract schedule exclusion. And let's pass that schedule exclusion directly in. OK, let's create out a method to deal with this.
01:06
So subtract schedule exclusion. We pass in the schedule exclusion. And then we just want to do exactly what we did here, but we want to say subtract.
01:18
So let's grab all of this code just to save us a little bit of time. Let's pop this inside of here. And this time we want to subtract the schedule exclusion.
01:27
So for the start date, that is basically just going to be the schedule exclusion starts at date, which we have in the database.
01:35
And then we're going to have the same thing down here. And that's going to be the ends at date. And remember these are carbon objects because over in the schedule exclusion
01:44
a little bit earlier, we cast these. So this is going to beautifully just start to work together. OK, so now that we've done this, just before we start to test this out,
01:52
I'm going to go over to the web and I'm going to change around the period that we're accessing. I'm just going to keep this simple and just do end of day.
02:00
We'll just keep this within a day period, just so it makes sense. OK, so if we come over to the browser, give this a refresh, nothing has changed here.
02:08
We've still got an included start and end date at nine till four minus the service. And we're going to add an exclusion now. So over in the database,
02:18
let's go over to our schedule exclusions. Let's set this for my employee, add in a starts and ends at date. And let's go ahead and change these around.
02:26
So we're going to do it for today. And this is actually a date. Let's just take out a look at the structure. Yes, this is a date.
02:32
I'm going to switch this to a date time. Let's save that out. And just while we're here as well, so we don't forget, let's go over to our create schedule exclusions table
02:42
and make sure we set this to a date time and this to a date time as well. And also over in our exclusion model, let's cast this to a date time.
02:52
So that was my bad. Okay, so now that we've done that, we can go ahead and actually set a date and a time in here. And I'm going to say, well, I want my lunch break.
03:00
So I'm going to set this to 12 to 13 for today. Okay, so back over to the browser, let's give this a refresh. And now we have two periods.
03:11
And that's absolutely normal because the way that we split period collections up means we end up with a period before lunch and a period after lunch.
03:19
Let's check this out. Okay, so we've got a start at nine. That's when I start, but now I finished just before my lunch.
03:25
And then on my second period, you can see that I start just after my lunch. And then of course I finish just before the time that the service takes.

Episode summary

In this episode, we're tackling how to handle schedule exclusions like holidays, lunch breaks, or any time an employee shouldn't be available. We dive into the code where we build out employee availability, and we extend it so that after adding up available time, we also loop through all the schedule exclusions for each employee.

We'll walk through subtracting these exclusions from their schedules—so if someone has a lunch break or a day off, they won't be shown as available. There's some discussion about improving performance by scoping exclusions more narrowly (something we might come back to), but for now, we safely avoid any nasty database performance issues.

After tweaking the logic, we also remember to update our database structure, making sure the schedule exclusion fields are set to handle date and time properly—not just dates! Once that's set, we add a sample lunch break directly in the database and refresh the browser to see the effect.

You'll see that the available period is now automatically split around that lunch break, showing two blocks of time: before and after the break. Everything works as expected: first, you see availability from the start of the shift until lunch, then from after lunch until the end. Nice and clean!

Episode discussion

No comments, yet. Be the first!