1
00:00:00,080 --> 00:00:00,410
All right.

2
00:00:00,410 --> 00:00:08,730
And once we have the initial setup in place, let's start by calculating the jobs based on the job status.

3
00:00:08,750 --> 00:00:14,960
So essentially, I want to set up the correct default stats object.

4
00:00:14,960 --> 00:00:19,070
So at the moment I have the properties, but I don't have the correct values.

5
00:00:19,160 --> 00:00:26,950
And effectively we'll just group based on the job status of pending interview as well as the client.

6
00:00:26,960 --> 00:00:30,230
And of course it's going to be based on the user in my case, that is John.

7
00:00:30,230 --> 00:00:37,610
And remember that in my case I did a bunch of jobs with the macro.

8
00:00:38,200 --> 00:00:41,420
For John, as well as the test user.

9
00:00:41,440 --> 00:00:50,020
Now, during the videos, this one and next one will use MongoDB aggregation pipeline.

10
00:00:50,290 --> 00:00:55,570
If you want to find out more info about it in the Readme, you'll find a link.

11
00:00:55,720 --> 00:01:02,770
And essentially, aggregation pipeline is a way for us to process data inside MongoDB.

12
00:01:03,130 --> 00:01:11,530
Also in the Readme, you'll find a detailed explanation for pretty much each line of code.

13
00:01:11,560 --> 00:01:16,750
So if you're watching the video and you're confused about something, please reference the Readme.

14
00:01:16,780 --> 00:01:19,460
So for starters, we want to go to job controller.

15
00:01:19,480 --> 00:01:25,090
Like I said, I will leave those ones for your reference because I think it's going to be very beneficial.

16
00:01:25,510 --> 00:01:32,830
I'm going to go here with let stats and the reason why I want to go with Let is because we will overwrite

17
00:01:32,830 --> 00:01:34,630
this value a little bit later.

18
00:01:34,750 --> 00:01:41,620
Then we want to go with Await and notice here we are calling this on the model.

19
00:01:41,770 --> 00:01:45,080
So not the instance, but it's actually a model.

20
00:01:45,100 --> 00:01:48,130
We go here with dot and aggregate.

21
00:01:48,220 --> 00:01:52,950
So aggregate is a method and it's looking for an array.

22
00:01:52,960 --> 00:01:55,720
And essentially this is where we set up those stages.

23
00:01:55,750 --> 00:02:02,770
Now the first stage is going to be the match stage where I will match all the jobs that belong to the

24
00:02:02,770 --> 00:02:04,420
specific user.

25
00:02:04,420 --> 00:02:09,630
And yes, the syntax is a little bit interesting where essentially we need to set up here an object.

26
00:02:09,639 --> 00:02:13,830
Then we go with dollar sign and the keyword is match.

27
00:02:13,840 --> 00:02:15,700
So that's the first stage.

28
00:02:15,700 --> 00:02:20,830
And then we want to set it equal to an object and which property we want to use.

29
00:02:20,830 --> 00:02:23,140
Well, since created by.

30
00:02:23,990 --> 00:02:25,130
References to user.

31
00:02:25,130 --> 00:02:31,400
That's why I'm going to go with created by and equals to and now remember of course we have access to

32
00:02:31,850 --> 00:02:36,080
user user ID, but it is a string, correct.

33
00:02:36,080 --> 00:02:38,600
And we want to turn this into object ID.

34
00:02:38,840 --> 00:02:41,690
So essentially I'm going to go with new then Mongoose.

35
00:02:41,690 --> 00:02:47,000
That's why we imported the mongoose, then dot, then types, then dot, then object ID.

36
00:02:47,970 --> 00:02:52,890
And now let's pass in req user user id.

37
00:02:53,400 --> 00:03:02,520
So long story short, in this stage, I'm going to grab all of the jobs that belong to this specific

38
00:03:02,550 --> 00:03:03,120
user.

39
00:03:03,120 --> 00:03:07,610
So of course, since I'm logged in as John, I'm getting all the John's jobs.

40
00:03:07,620 --> 00:03:13,710
If I'm going to log in as Zippy, my test user, you can probably already guess that I'm going to be

41
00:03:13,710 --> 00:03:15,570
getting all the zippy jobs.

42
00:03:15,570 --> 00:03:16,890
So that's the first step.

43
00:03:16,890 --> 00:03:17,670
And you know what?

44
00:03:18,360 --> 00:03:24,410
This is going to be the case where I will log just so we can stay on the same page.

45
00:03:24,420 --> 00:03:26,310
So let's go to show stats.

46
00:03:26,340 --> 00:03:27,930
We want to make a request.

47
00:03:27,960 --> 00:03:31,230
What we're looking for here is, of course.

48
00:03:32,540 --> 00:03:34,920
The console and check it out.

49
00:03:34,940 --> 00:03:39,590
So essentially, I'll have just a long, long, long, long list of jobs.

50
00:03:39,590 --> 00:03:39,920
Why?

51
00:03:39,920 --> 00:03:42,530
Well, because we just populated the database.

52
00:03:42,560 --> 00:03:43,150
Correct.

53
00:03:43,160 --> 00:03:45,770
So for starters, we just get all of these jobs.

54
00:03:45,800 --> 00:03:47,180
Okay, Awesome.

55
00:03:48,130 --> 00:03:49,570
That's a good starting point.

56
00:03:49,600 --> 00:03:54,670
Now we want to group them and we want to group them by job status, property.

57
00:03:55,350 --> 00:03:57,050
And I also want to count.

58
00:03:57,060 --> 00:03:59,460
So how many are pending?

59
00:03:59,460 --> 00:04:02,100
How many have interviewed and declined?

60
00:04:02,250 --> 00:04:07,560
And the way we do that, again, we set up a next stage.

61
00:04:07,770 --> 00:04:11,820
Then this one has a keyword of group.

62
00:04:12,420 --> 00:04:14,280
And effectively in here.

63
00:04:15,350 --> 00:04:22,820
We want to go with underscore ID and now we want to go with the specific property and we want to set

64
00:04:22,820 --> 00:04:23,850
it in a string.

65
00:04:23,870 --> 00:04:27,830
Then again, dollar sign and job status here.

66
00:04:27,860 --> 00:04:31,850
And then if I want to count, I'm going to go over here with Count.

67
00:04:31,850 --> 00:04:39,810
And again, we'll set it equal to an object, then go with the dollar sign sum and set it equal to one.

68
00:04:39,830 --> 00:04:41,660
Again, that's just a syntax.

69
00:04:41,660 --> 00:04:44,030
So let's navigate to show stats.

70
00:04:44,060 --> 00:04:46,100
Let's make the request one more time.

71
00:04:46,740 --> 00:04:48,030
And now check it out.

72
00:04:48,060 --> 00:04:51,360
If I take a look at the console, I can see that I have an array.

73
00:04:52,170 --> 00:04:54,270
And there I have three objects.

74
00:04:54,300 --> 00:04:54,620
Why?

75
00:04:54,630 --> 00:04:57,410
Well, because those are three potential values.

76
00:04:57,420 --> 00:05:00,300
One for pending, one for declined an interview.

77
00:05:00,600 --> 00:05:02,760
And I can nicely count.

78
00:05:02,790 --> 00:05:09,900
Well, how many jobs have status of pending and the rest of them and everything is awesome.

79
00:05:09,900 --> 00:05:17,160
But I actually want to turn it into an object and that way I can access those properties and actually

80
00:05:17,190 --> 00:05:21,810
set it equal to the properties in my default stats.

81
00:05:21,810 --> 00:05:29,640
So long story short, I want to run the reduce and take this array and turn it into an object.

82
00:05:29,670 --> 00:05:31,430
How is that going to look like?

83
00:05:31,440 --> 00:05:33,600
Well, for starters, we want to go with stats.

84
00:05:33,630 --> 00:05:38,790
Like I said, we'll overwrite the value, then we'll type reduce over here.

85
00:05:39,330 --> 00:05:39,970
And you know what?

86
00:05:40,020 --> 00:05:41,970
Let me just remove this fine here.

87
00:05:42,510 --> 00:05:45,930
Then we want to pass here the callback function.

88
00:05:46,080 --> 00:05:47,850
This is just straight up JavaScript.

89
00:05:47,850 --> 00:05:54,970
So essentially this is just a reduce syntax where we go with accumulator and then we have the current.

90
00:05:55,000 --> 00:06:02,620
Now you also probably will see the total and the current iteration or something along those lines.

91
00:06:02,620 --> 00:06:08,080
So essentially the first parameter in the callback function is what we're returning from reduce.

92
00:06:08,080 --> 00:06:12,940
And then this is going to be the current iteration because we need to keep in mind that when we run

93
00:06:12,940 --> 00:06:18,190
reduce, we'll still iterate pretty much over each and every item over here.

94
00:06:18,190 --> 00:06:22,780
Now, like I said, what we want to return back from reduce is actually an object.

95
00:06:22,780 --> 00:06:27,310
So I want to take this array and turn it into an object.

96
00:06:27,610 --> 00:06:30,850
Now as far as the functionality, here is how it's going to look like.

97
00:06:30,850 --> 00:06:32,410
So I'm iterating.

98
00:06:33,130 --> 00:06:34,360
Over my array.

99
00:06:34,360 --> 00:06:36,940
And I want to pull out two things.

100
00:06:36,970 --> 00:06:42,160
First of all, I want to grab the ID and I'll right away give it an alias of title.

101
00:06:42,310 --> 00:06:47,160
And second thing I want to pull out from the object is the count.

102
00:06:47,170 --> 00:06:49,030
And this is going to be equal to current.

103
00:06:49,060 --> 00:06:50,440
Again, we're iterating over.

104
00:06:50,440 --> 00:06:58,900
So I know that I'll have access to it and then I'll right away in my object create a property which

105
00:06:58,900 --> 00:07:00,190
is going to be equal to title.

106
00:07:00,190 --> 00:07:01,840
So either pending.

107
00:07:02,490 --> 00:07:03,780
Declined or interview.

108
00:07:03,810 --> 00:07:09,720
So let's go over here with dynamic property over here and let's set it equal to count.

109
00:07:09,750 --> 00:07:16,980
Now, at the very, very end, it's very, very important to return the accumulator, basically to return

110
00:07:16,980 --> 00:07:17,550
the result.

111
00:07:17,550 --> 00:07:19,280
Otherwise it's not going to work.

112
00:07:19,290 --> 00:07:28,560
So now let's navigate over here and let's log the stats, Let's make the request and check it out in

113
00:07:28,560 --> 00:07:29,040
the console.

114
00:07:29,040 --> 00:07:34,170
Now I have a proper object and we're pretty much good to go.

115
00:07:34,200 --> 00:07:39,990
We can right away send the stats one back, but we need to keep in mind one thing.

116
00:07:40,230 --> 00:07:47,550
Initially, when user signs up for the application, he or she is not going to have any jobs.

117
00:07:47,670 --> 00:07:52,530
So in that case, of course we're not going to be sending any values back.

118
00:07:52,800 --> 00:07:54,480
There's going to be nothing to count.

119
00:07:54,480 --> 00:08:03,100
And therefore, we do need to add nice or operator where we'll add zero just in case it doesn't return

120
00:08:03,100 --> 00:08:03,970
any value.

121
00:08:03,970 --> 00:08:10,750
So back in the job controller, essentially I'll use the stats to make this dynamic, but I'll also

122
00:08:10,750 --> 00:08:11,620
say, you know what?

123
00:08:11,620 --> 00:08:14,830
If there is no value, hey, listen, at least return something.

124
00:08:14,860 --> 00:08:21,040
Otherwise, if we're not returning at least zero back from the server, then our entire front end logic

125
00:08:21,040 --> 00:08:22,390
is going to go bananas.

126
00:08:22,420 --> 00:08:29,140
Now, technically, we will do the same thing on a front end just to avoid some weird bugs.

127
00:08:29,140 --> 00:08:34,240
But again, I prefer doing that in two places so that way I know for sure.

128
00:08:34,960 --> 00:08:38,169
I'm not going to get any unexpected results.

129
00:08:38,169 --> 00:08:44,740
So long story short, where we have pending interview and the client, I want to go with my stats then

130
00:08:44,770 --> 00:08:53,500
dot, then I'm looking for pending and if there is no value, basically if user just sign up for the

131
00:08:53,500 --> 00:08:56,560
account or for some reason he doesn't have.

132
00:08:57,300 --> 00:08:59,880
Any jobs that have the value of spending.

133
00:09:00,270 --> 00:09:07,170
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.

134
00:09:08,240 --> 00:09:11,090
And if there's nothing there, then I'm going to go with zero.

135
00:09:11,550 --> 00:09:13,670
And then lastly, we go with stats.

136
00:09:13,670 --> 00:09:15,890
Dot declined.

137
00:09:15,920 --> 00:09:19,310
And if there's no value, then again, we send back zero.

138
00:09:19,310 --> 00:09:22,670
Now, in this case, it looks like the comma is missing.

139
00:09:23,060 --> 00:09:26,300
So now let me navigate to show stats.

140
00:09:26,450 --> 00:09:32,870
Let me send it and notice now the response is exactly what we hard coded before.

141
00:09:32,870 --> 00:09:35,910
But of course, now those values are dynamic.

142
00:09:35,930 --> 00:09:40,010
And with this in place, we can start working on the next functionality.

