1 00:00:00,540 --> 00:00:03,660 All right my friends we now have put together our survey schema. 2 00:00:03,660 --> 00:00:06,000 It is linked up to our recipient schema. 3 00:00:06,000 --> 00:00:06,700 Before we move on. 4 00:00:06,700 --> 00:00:08,670 There's one quick thing I want to mention to you. 5 00:00:08,730 --> 00:00:13,450 Remember that we hooked up the survey model inside of our index G-S file. 6 00:00:13,470 --> 00:00:15,890 So here's the required statement for it right here. 7 00:00:15,930 --> 00:00:20,910 Now I had previously said that we need to make sure that we always import our models somewhere into 8 00:00:20,910 --> 00:00:22,350 our project structure. 9 00:00:22,500 --> 00:00:23,770 So you can see right here. 10 00:00:23,860 --> 00:00:29,610 Here's the user and here's the survey we do not have to do something similar for the recipient sub document 11 00:00:29,610 --> 00:00:35,090 model right here because we are already requiring that into the survey get G-S file. 12 00:00:35,160 --> 00:00:38,080 So here's the required statement for recipient right here. 13 00:00:38,100 --> 00:00:41,270 So just a quick reminder we don't have to take care of it in this case. 14 00:00:41,620 --> 00:00:41,970 OK. 15 00:00:42,000 --> 00:00:48,450 So now that our survey model is all set up we're now going to move on over to a new routes file that 16 00:00:48,450 --> 00:00:54,480 we are going to create whose sole purpose will be to implement these different routes that we need to 17 00:00:54,510 --> 00:00:57,810 hook up all the different survey logic inside of our application. 18 00:00:57,810 --> 00:01:00,350 So we're going to make a survey route's file. 19 00:01:00,540 --> 00:01:06,400 We will then define the post request handler to API slash service. 20 00:01:06,510 --> 00:01:08,970 So let's get to it. 21 00:01:08,970 --> 00:01:13,050 Back in my code editor I mean to start off by closing all the different files I have open right now 22 00:01:13,110 --> 00:01:16,660 just to clean things up and then going to find my route's directory. 23 00:01:16,800 --> 00:01:21,340 And inside of there I'll make a new file called survey wraps. 24 00:01:21,450 --> 00:01:22,710 J.S.. 25 00:01:22,950 --> 00:01:26,240 Now we've spoken about the structure of these routes files several times. 26 00:01:26,240 --> 00:01:32,820 Now remember we define an arrow function we immediately export it and then we wire that up to our express 27 00:01:32,880 --> 00:01:36,620 an object inside of the top level index not G-S file. 28 00:01:36,990 --> 00:01:44,280 So at the top of the survey route's will say module that exports is the app and then whatever associated 29 00:01:44,280 --> 00:01:46,750 routes we want to attach to it. 30 00:01:46,890 --> 00:01:51,570 Now all immediately save this file I'll flip back over to the index file. 31 00:01:51,630 --> 00:01:54,690 We're going to scroll down to where we hooked up all the other routes. 32 00:01:54,720 --> 00:01:55,790 Right around here. 33 00:01:55,950 --> 00:01:58,760 And we'll set up this new route handler. 34 00:01:59,030 --> 00:02:06,250 So we'll say require dot slash routes survey routes and then pass in the app. 35 00:02:06,330 --> 00:02:10,350 So we've spoken several times now about the kind of syntax we have going on here. 36 00:02:10,350 --> 00:02:14,200 Remember we are requiring in the out of the route function. 37 00:02:14,220 --> 00:02:15,060 There we go. 38 00:02:15,060 --> 00:02:19,160 And then we immediately call that route function with the app object. 39 00:02:19,620 --> 00:02:23,730 And that's pretty much all of the set up we have to do for creating a new route file. 40 00:02:24,060 --> 00:02:27,690 So now inside if we get to create the new route handler itself. 41 00:02:27,690 --> 00:02:36,650 Remember this time around we're taking a post request to slash API slash surveys will say app dot post. 42 00:02:36,720 --> 00:02:46,360 So if any post request comes in to API slash surveys then run this arrow function right here. 43 00:02:46,980 --> 00:02:52,680 Now before we even start worrying about creating a survey in here even remotely before we even dive 44 00:02:52,680 --> 00:02:53,810 into that aspect. 45 00:02:53,880 --> 00:02:59,190 There's two very important things that we want to kind of vet about the user before we start to create 46 00:02:59,250 --> 00:03:00,870 an actual survey. 47 00:03:00,870 --> 00:03:04,440 The first thing we want to do is we want to make sure that the user is logged in. 48 00:03:04,560 --> 00:03:09,150 So without a doubt we want to make sure that if you are attempting to create a survey you've got to 49 00:03:09,150 --> 00:03:09,740 be logged in. 50 00:03:09,750 --> 00:03:11,210 There's no two ways about it. 51 00:03:11,490 --> 00:03:17,160 Secondly if you are logged in we also want to check to make sure that you have enough credits on hand 52 00:03:17,400 --> 00:03:19,220 to actually send out a survey. 53 00:03:19,220 --> 00:03:23,470 So remember that's how all the billing inside of our application works when you create a survey. 54 00:03:23,490 --> 00:03:29,310 It takes one credit to send out that mass bold e-mail to all these different users. 55 00:03:29,310 --> 00:03:33,600 So we want to make sure that a user is logged in and that they have enough credits. 56 00:03:33,600 --> 00:03:37,980 Let's first do the check to make sure that a user is logged in and authenticated and then we'll figure 57 00:03:37,980 --> 00:03:41,110 out exactly how to do the credit stuff after that. 58 00:03:41,130 --> 00:03:47,220 So recall a couple of videos ago we put together this middle weares directory and inside there we created 59 00:03:47,220 --> 00:03:53,100 a file called require loggin and the sole purpose of this function right here was to assert and make 60 00:03:53,100 --> 00:03:58,820 sure that a user was logged in before we allow some particular route handler to actually be executed. 61 00:03:59,070 --> 00:04:04,980 So we can very easily reuse this code with this new API slash surveys throughout. 62 00:04:04,980 --> 00:04:15,600 Handler will first require in that middleware is require up one directory middleware as require log 63 00:04:15,600 --> 00:04:22,860 in and then we can wire up this middleware to the second argument of apt post right here. 64 00:04:22,860 --> 00:04:27,060 And remember when I say second argument it doesn't necessary. 65 00:04:27,060 --> 00:04:31,430 I don't really mean to indicate that it has to be some very particular argument in here. 66 00:04:31,560 --> 00:04:37,620 Remember that we can pass an arbitrary number of functions to this request handler right here and they 67 00:04:37,620 --> 00:04:39,660 will all be executed in line. 68 00:04:39,720 --> 00:04:43,360 So if we have other function functions in here as well that's totally fine. 69 00:04:43,470 --> 00:04:49,320 They will just all be executed until it eventually gets to a function that somehow takes the response 70 00:04:49,320 --> 00:04:53,310 object and sends a response back to the user. 71 00:04:53,340 --> 00:04:53,720 OK. 72 00:04:53,800 --> 00:04:57,090 Lesing I want to remind you about is the required log in function itself. 73 00:04:57,090 --> 00:04:58,740 We are not calling it. 74 00:04:58,740 --> 00:05:04,690 So we are not out the set of parentheses on there because we don't yet have the request or a response 75 00:05:04,870 --> 00:05:05,830 object in hand. 76 00:05:05,830 --> 00:05:08,530 We are simply telling our express app object. 77 00:05:08,530 --> 00:05:10,830 Hey if someone makes a request to this route right here. 78 00:05:10,960 --> 00:05:15,550 Here is a reference to a function that you should run to make sure that the user is actually logged 79 00:05:15,550 --> 00:05:16,970 in. 80 00:05:16,980 --> 00:05:19,720 OK so that's making sure the user is authenticated. 81 00:05:19,720 --> 00:05:28,090 Now step number two is to make sure that a user actually has enough credits to send out a survey so 82 00:05:28,180 --> 00:05:32,960 the check to make sure that the user has enough credits is going to be pretty straightforward. 83 00:05:32,960 --> 00:05:38,260 Like if we go and look at their required log in statement right here remember that our user model which 84 00:05:38,260 --> 00:05:41,470 sits on user has a credits property. 85 00:05:41,470 --> 00:05:47,200 And really we just want to make sure that the user has at least one credit if we are going to let them 86 00:05:47,230 --> 00:05:48,570 create a new survey. 87 00:05:48,940 --> 00:05:54,060 So I'm kind of thinking that we could almost reuse the same type of middleware right here. 88 00:05:54,340 --> 00:05:59,440 And rather than just saying hey make sure that a user exists we could say hey make sure that the user 89 00:05:59,470 --> 00:06:03,340 has enough credits and then if they don't we'll kick them out early. 90 00:06:03,460 --> 00:06:06,220 Otherwise we can allow requests to run through. 91 00:06:06,220 --> 00:06:10,960 So this is going to be another location where we can easily encapsulate all this logic to make sure 92 00:06:10,960 --> 00:06:14,050 that the user has enough credits inside of a middleware. 93 00:06:14,050 --> 00:06:18,340 Now one thing I want to remind you about before we actually create that middleware is that one of the 94 00:06:18,340 --> 00:06:24,400 ideas behind middleware are that it's a little reusable function that we can reuse with many different 95 00:06:24,400 --> 00:06:26,860 route handlers inside of our application. 96 00:06:26,860 --> 00:06:32,000 Now at this point we only have exactly one route and for our application in general. 97 00:06:32,020 --> 00:06:37,300 We're only going to have one route that really deducts or checks to make sure that a user has enough 98 00:06:37,300 --> 00:06:40,720 credits and that is the creation of a new survey right here. 99 00:06:40,720 --> 00:06:45,970 However I can very easily imagine expanding our application in the future to adding more features that 100 00:06:45,970 --> 00:06:47,580 require some amount of billing. 101 00:06:47,770 --> 00:06:53,740 So I do feel pretty secure in saying yes this really should be a middleware because we might have many 102 00:06:53,740 --> 00:06:58,060 different locations inside of our up inside the future where we want to make sure that the user has 103 00:06:58,060 --> 00:07:00,480 some minimum number of credits. 104 00:07:00,520 --> 00:07:02,680 So with that in mind let's take a break. 105 00:07:02,750 --> 00:07:07,000 We come back we're going to create a new middleware to just validate and make sure that the current 106 00:07:07,030 --> 00:07:12,150 user has enough credits to actually send out a survey. 107 00:07:12,280 --> 00:07:14,630 So quick break and I'll see you in the next video.