This episode is for members only

Sign up to access "Building a Notification Preference System in Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
10. Setting defaults on user creation

Transcript

00:00
Okay so when a user signs up for your application it's likely you're going to want to set some sensible notification defaults for that user that's been created. Let's do that now. We're going to write a quick test for this first and then we'll fill this in with a observer.
00:16
Okay let's go ahead and start with our test. Now I'm going to use an observer for this so I'm going to create a test under an observer's directory because I'm specifically testing the user observer to make sure that when the user gets created this stuff gets filled. But you can switch this up and put this in a different directory if you want to.
00:35
So let's say user observer test and let's go ahead and get rid of the default that we have in here and let's get rid of this. Okay so let's say it sets default notification preferences when creating a user. Okay so what do we need to do? Well we need some or one it doesn't really matter how many notification preferences we have in our test suite but let's go ahead and say that
01:01
we just have one. So let's say notification factory for again we need that notification group that it belongs to. So let's say factory and let's go ahead and create one out inside of here. Okay so we know that this is going to be the project created one because we have that set up in our factory. Let's create a user so let's say user factory and create and what do we want to do?
01:26
We now just want to make sure that the user has some notification preferences when that account gets created or user gets created. So let's say I expect that the notification preferences for that user has a count of one. Now that's going to fail so let's go ahead and run test feature and observers user observer test that fails of course because we don't have anything
01:51
created. What we also want to do is the first one and the pivot channels here we want to make sure that's an array so we want to make sure that that array gets inserted and we want to make sure that this is equal to a mail for example like so. Now the reason I've done this is because when I first create a user or when a user first registers I'm only going to set the notification preferences
02:19
globally for everything to email. Now that might not be what you want to do in your application so you need to tweak around the test and also the functionality that you actually use to set these defaults. You might need to be a little bit more granular but by the end of this episode you'll get the idea and you can fill these in as you need. Okay let's go back over then and let's run this
02:39
test again of course it's going to fail so you know the notification preferences are zero so of course this one is also going to fail. So we need to observe the user somehow when they get created wherever they get created whether it's via the registration process or an admin panel we want to fill in them default notification preferences. So let's go ahead and make an observer for our user
03:03
so we create a user observer we head over to the user model we go ahead and make sure that this is observed by that so we can use the observed by attribute and we can pass in our user observer and then over in our user observer we can say well when this user gets created let's go ahead and fill in their notification preferences. How do we do this? Well we access the user we access their
03:30
notification preferences we sync the notification preferences exactly like we did when we did this within the controller and then we go ahead and pluck out the notification preferences that we want to set alongside the channels that we want these to be set for. This is where if you want this to change in your own application you'll have to either do this sort of line by line or do this
03:55
slightly differently and exclude stuff within this query. So I'm going to say notification get I'm going to map this with keys like we did within our controller and in here we'll get a closure which returns an array in here we will get the notification and what do we want to do? Well we want to return the notification id that's going to be the key
04:21
that gets passed into sync and then the pivot columns that we pass in which is going to be channels and we're going to set that to mail. So this will grab all of the notifications that are possibly available within our application it will set them all to mail and store that for that user. So now that we've done that let's go over and run our test again and see if we get anything
04:45
different and let's have a look and yeah okay so it says default notification preferences does not match expected type string. Let's head over to our user observer test and have a look here and yeah this needs to equal an array of things or we could say includes mail or only includes mail whatever we wanted to say. Let's run the test and yeah sure enough we get green.
05:10
So we're pretty confident now that's going to fill that but we can test this within our application itself. So I'm going to come over to notification user get rid of all of that for my user and I'm going to manually delete my user from the database. I'm going to come over and I'm going to register a new account here and that should fill in all of my details. So let's go register an
05:30
account and that should fill in all of the notification preferences for email and like I said you can tweak this around to do whatever you want to do just switch over your user observer where this gets created but I think it kind of makes sense to select them all for one notification type and the user can now come in disable them do whatever they want.
10 episodes1 hr 23 mins

Overview

Need to store advanced notification preferences in your application?

In this course, we cover displaying a matrix of notification groups, types and channels with the ability to sync preferences cleanly and quickly.

Once you’re done, your users will be able to choose what notifications they receive, and how.

Oh, and we’ll write tests to back everything up, look at how to use notification preferences in Notification classes, and how to set defaults for when users register.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!