All right.
And once we have the initial setup in place, let's start by calculating the jobs based on the job status.
So essentially, I want to set up the correct default stats object.
So at the moment I have the properties, but I don't have the correct values.
And effectively we'll just group based on the job status of pending interview as well as the client.
And of course it's going to be based on the user in my case, that is John.
And remember that in my case I did a bunch of jobs with the macro.
For John, as well as the test user.
Now, during the videos, this one and next one will use MongoDB aggregation pipeline.
If you want to find out more info about it in the Readme, you'll find a link.
And essentially, aggregation pipeline is a way for us to process data inside MongoDB.
Also in the Readme, you'll find a detailed explanation for pretty much each line of code.
So if you're watching the video and you're confused about something, please reference the Readme.
So for starters, we want to go to job controller.
Like I said, I will leave those ones for your reference because I think it's going to be very beneficial.
I'm going to go here with let stats and the reason why I want to go with Let is because we will overwrite
this value a little bit later.
Then we want to go with Await and notice here we are calling this on the model.
So not the instance, but it's actually a model.
We go here with dot and aggregate.
So aggregate is a method and it's looking for an array.
And essentially this is where we set up those stages.
Now the first stage is going to be the match stage where I will match all the jobs that belong to the
specific user.
And yes, the syntax is a little bit interesting where essentially we need to set up here an object.
Then we go with dollar sign and the keyword is match.
So that's the first stage.
And then we want to set it equal to an object and which property we want to use.
Well, since created by.
References to user.
That's why I'm going to go with created by and equals to and now remember of course we have access to
user user ID, but it is a string, correct.
And we want to turn this into object ID.
So essentially I'm going to go with new then Mongoose.
That's why we imported the mongoose, then dot, then types, then dot, then object ID.
And now let's pass in req user user id.
So long story short, in this stage, I'm going to grab all of the jobs that belong to this specific
user.
So of course, since I'm logged in as John, I'm getting all the John's jobs.
If I'm going to log in as Zippy, my test user, you can probably already guess that I'm going to be
getting all the zippy jobs.
So that's the first step.
And you know what?
This is going to be the case where I will log just so we can stay on the same page.
So let's go to show stats.
We want to make a request.
What we're looking for here is, of course.
The console and check it out.
So essentially, I'll have just a long, long, long, long list of jobs.
Why?
Well, because we just populated the database.
Correct.
So for starters, we just get all of these jobs.
Okay, Awesome.
That's a good starting point.
Now we want to group them and we want to group them by job status, property.
And I also want to count.
So how many are pending?
How many have interviewed and declined?
And the way we do that, again, we set up a next stage.
Then this one has a keyword of group.
And effectively in here.
We want to go with underscore ID and now we want to go with the specific property and we want to set
it in a string.
Then again, dollar sign and job status here.
And then if I want to count, I'm going to go over here with Count.
And again, we'll set it equal to an object, then go with the dollar sign sum and set it equal to one.
Again, that's just a syntax.
So let's navigate to show stats.
Let's make the request one more time.
And now check it out.
If I take a look at the console, I can see that I have an array.
And there I have three objects.
Why?
Well, because those are three potential values.
One for pending, one for declined an interview.
And I can nicely count.
Well, how many jobs have status of pending and the rest of them and everything is awesome.
But I actually want to turn it into an object and that way I can access those properties and actually
set it equal to the properties in my default stats.
So long story short, I want to run the reduce and take this array and turn it into an object.
How is that going to look like?
Well, for starters, we want to go with stats.
Like I said, we'll overwrite the value, then we'll type reduce over here.
And you know what?
Let me just remove this fine here.
Then we want to pass here the callback function.
This is just straight up JavaScript.
So essentially this is just a reduce syntax where we go with accumulator and then we have the current.
Now you also probably will see the total and the current iteration or something along those lines.
So essentially the first parameter in the callback function is what we're returning from reduce.
And then this is going to be the current iteration because we need to keep in mind that when we run
reduce, we'll still iterate pretty much over each and every item over here.
Now, like I said, what we want to return back from reduce is actually an object.
So I want to take this array and turn it into an object.
Now as far as the functionality, here is how it's going to look like.
So I'm iterating.
Over my array.
And I want to pull out two things.
First of all, I want to grab the ID and I'll right away give it an alias of title.
And second thing I want to pull out from the object is the count.
And this is going to be equal to current.
Again, we're iterating over.
So I know that I'll have access to it and then I'll right away in my object create a property which
is going to be equal to title.
So either pending.
Declined or interview.
So let's go over here with dynamic property over here and let's set it equal to count.
Now, at the very, very end, it's very, very important to return the accumulator, basically to return
the result.
Otherwise it's not going to work.
So now let's navigate over here and let's log the stats, Let's make the request and check it out in
the console.
Now I have a proper object and we're pretty much good to go.
We can right away send the stats one back, but we need to keep in mind one thing.
Initially, when user signs up for the application, he or she is not going to have any jobs.
So in that case, of course we're not going to be sending any values back.
There's going to be nothing to count.
And therefore, we do need to add nice or operator where we'll add zero just in case it doesn't return
any value.
So back in the job controller, essentially I'll use the stats to make this dynamic, but I'll also
say, you know what?
If there is no value, hey, listen, at least return something.
Otherwise, if we're not returning at least zero back from the server, then our entire front end logic
is going to go bananas.
Now, technically, we will do the same thing on a front end just to avoid some weird bugs.
But again, I prefer doing that in two places so that way I know for sure.
I'm not going to get any unexpected results.
So long story short, where we have pending interview and the client, I want to go with my stats then
dot, then I'm looking for pending and if there is no value, basically if user just sign up for the
account or for some reason he doesn't have.
Any jobs that have the value of spending.
At least I'm going to go with zero in the same deal over here where I'm going to go with stats and interview.
And if there's nothing there, then I'm going to go with zero.
And then lastly, we go with stats.
Dot declined.
And if there's no value, then again, we send back zero.
Now, in this case, it looks like the comma is missing.
So now let me navigate to show stats.
Let me send it and notice now the response is exactly what we hard coded before.
But of course, now those values are dynamic.
And with this in place, we can start working on the next functionality.