1 00:00:00,540 --> 00:00:05,040 In the last video we started creating our new instance of a survey but we took a pause when we got to 2 00:00:05,040 --> 00:00:08,940 the creation of the sub document collection of recipients. 3 00:00:08,940 --> 00:00:12,480 So that's a reminder again and know we just looked at this but I want to make sure that it's really 4 00:00:12,480 --> 00:00:19,380 clear inside of our survey schema we said that there is a recipient's property that is an array of recipients 5 00:00:19,380 --> 00:00:21,030 schema objects. 6 00:00:21,030 --> 00:00:25,710 So if we then go and look at the recipient schema we had said that there is an e-mail property and they 7 00:00:25,710 --> 00:00:30,450 responded property and that we didn't actually have to define the respondent property because it will 8 00:00:30,450 --> 00:00:35,150 be defaulted defaults which is exactly what we want inside of our application. 9 00:00:35,190 --> 00:00:41,430 So if we start to visualize the actual relationship here the actual kind of structure of the survey 10 00:00:41,460 --> 00:00:42,600 and the recipient. 11 00:00:42,720 --> 00:00:45,550 We might end up with something that looks like this. 12 00:00:45,570 --> 00:00:52,710 So this is essentially what we want to do when we create a survey instance we want to pass in an object 13 00:00:52,770 --> 00:00:59,340 that has the title the subject the body and then for the recipient's property it's where each object 14 00:00:59,460 --> 00:01:06,710 has an email property that points out the string email for that particular recipient. 15 00:01:06,960 --> 00:01:12,180 When we pass in this array of objects mongoose will automatically create that sub document collection 16 00:01:12,180 --> 00:01:12,900 for us. 17 00:01:13,020 --> 00:01:18,420 So all we really have to do is make sure that we pass in an array of objects that contains the user's 18 00:01:18,480 --> 00:01:22,030 email address as a key of email. 19 00:01:22,110 --> 00:01:30,690 So we need to somehow get from this recipient's argument or this recipient parameter or property that's 20 00:01:30,690 --> 00:01:36,540 passed along with the request body which is a comma separated string of e-mail addresses and transform 21 00:01:36,540 --> 00:01:43,340 it into an array of objects where each object has an email property of the individual e-mail address. 22 00:01:43,350 --> 00:01:47,490 So that's essentially why I want to take the pause and say hey just to make sure we're on the same page. 23 00:01:47,490 --> 00:01:49,740 This is a little bit complicated. 24 00:01:49,830 --> 00:01:54,060 It is complicated because we have to do that transformation from the array of strings to the array of 25 00:01:54,090 --> 00:01:55,280 objects. 26 00:01:55,290 --> 00:01:58,830 So let's talk about how we're going to pull this off step by step. 27 00:01:59,580 --> 00:02:01,220 It's actually not going to be that bad. 28 00:02:01,470 --> 00:02:06,570 So we're going to do is we're going to take that list or string of email addresses that are separated 29 00:02:06,570 --> 00:02:07,590 by commas. 30 00:02:07,860 --> 00:02:13,440 We will call the Split function on that string which will take that string up here. 31 00:02:13,440 --> 00:02:19,650 It will split the entire string by every comma inside of it and return an array of strings. 32 00:02:19,710 --> 00:02:23,210 So it would then be at this step right here where we have an array of strings. 33 00:02:23,280 --> 00:02:26,640 Each string is one individual e-mail address. 34 00:02:26,640 --> 00:02:28,390 We can then use the map function. 35 00:02:28,410 --> 00:02:32,390 And if you're not familiar with map that's fine we'll walk through it exactly how it works. 36 00:02:32,520 --> 00:02:37,830 And for every element inside of this array of strings or for every string inside of that array will 37 00:02:37,830 --> 00:02:44,940 return an object with a property of email that contains the user's actual e-mail an e-mail address or 38 00:02:44,940 --> 00:02:47,420 the actual string that was in that previous array. 39 00:02:47,820 --> 00:02:49,800 So let's give this give this a shot. 40 00:02:49,840 --> 00:02:55,210 The actual code that we're going to write here is going to be very concise very concise but there's 41 00:02:55,230 --> 00:02:58,580 going to be a lot of stuff going on on a single line. 42 00:02:58,620 --> 00:03:04,800 So after our body property we're going to pass in the recipient's property and this is going to be our 43 00:03:05,100 --> 00:03:13,680 array of objects containing the e-mail addresses so we will first start off by taking the recipient's. 44 00:03:14,070 --> 00:03:17,160 We're going to split by Komba. 45 00:03:17,240 --> 00:03:22,390 So this right here is going to turn the thing into an array of strings or an array of email addresses. 46 00:03:22,610 --> 00:03:31,070 And then for every string in that array we will map over it the map function right here takes every 47 00:03:31,070 --> 00:03:32,790 single email in the array. 48 00:03:32,990 --> 00:03:38,420 It runs some function on that individual element and then it creates a new array of what we return from 49 00:03:38,420 --> 00:03:41,930 the arrow function or the function that we passed to map right here. 50 00:03:42,230 --> 00:03:52,260 So we will say for every email address in there return a new object that has an email of email like 51 00:03:52,260 --> 00:04:00,030 so so this returns our array of strings and then we say for every email address returning object. 52 00:04:00,300 --> 00:04:05,140 So here's the object with a property email that points out the user's e-mail. 53 00:04:05,160 --> 00:04:10,770 Now it can use a little bit of ESX code to dramatically simplify this arrow function right here. 54 00:04:10,770 --> 00:04:17,100 First off you'll notice that we've got an identical key and value so we can condense it to be just email 55 00:04:17,100 --> 00:04:24,410 like so and then notice how the body of the arrow function consists of one javascript expression. 56 00:04:24,480 --> 00:04:26,950 We just create the object and return it. 57 00:04:26,970 --> 00:04:33,450 So rather than putting in the outside curly braces and the return keyword because we have just one javascript 58 00:04:33,450 --> 00:04:41,020 expression inside of your we can remove the return keyword and the outside curly braces like so. 59 00:04:41,020 --> 00:04:45,150 So now we've got opening curly brace closing clearly Bretts. 60 00:04:45,240 --> 00:04:51,780 Now this will create an object with a key of e-mail and the value of whatever that e-mail address was. 61 00:04:51,870 --> 00:04:57,300 The last thing you have to do to make sure that works correctly when javascript sees this line of code 62 00:04:57,300 --> 00:04:59,370 right here when it sees the curly braces. 63 00:04:59,370 --> 00:05:04,380 It will get a little bit confuse because it will not understand if we are defining a function body or 64 00:05:04,380 --> 00:05:10,560 if we are defining a kind of shortened object right here and we are actually defining a shortened object. 65 00:05:10,560 --> 00:05:15,090 So to make sure that that is really clear to the javascript interpreter we're going to wrap it with 66 00:05:15,090 --> 00:05:19,340 a set of parentheses like so. 67 00:05:19,390 --> 00:05:27,250 So now we've got opening opening closing closing and closing like so. 68 00:05:28,010 --> 00:05:28,340 OK. 69 00:05:28,350 --> 00:05:30,810 So again this will take the list of email addresses. 70 00:05:30,810 --> 00:05:36,780 Split it into an array and then return an object for every e-mail address in there with a property of 71 00:05:36,780 --> 00:05:40,360 email and the value of the actual email address. 72 00:05:40,380 --> 00:05:45,210 So that's pretty much it for creating the survey as far as these very simple straightforward properties 73 00:05:45,210 --> 00:05:46,590 go right here. 74 00:05:46,620 --> 00:05:51,840 Now as you and I know there are several other properties that this survey expects to have. 75 00:05:51,900 --> 00:05:54,960 So we've just dealt with the floor up here so far. 76 00:05:55,050 --> 00:05:58,940 The next one that we're going to deal with is going to be the user property. 77 00:05:59,040 --> 00:06:04,080 We don't have to worry about the yes and no properties because again we've assigned default values to 78 00:06:04,080 --> 00:06:04,360 them. 79 00:06:04,380 --> 00:06:10,410 So we don't have to specifically say start out with zero yeses and zero NOS or anything like that. 80 00:06:10,410 --> 00:06:13,020 They will just be automatically defaulted for us. 81 00:06:13,320 --> 00:06:18,610 So the next property that we really have to worry about is the user relationship right here. 82 00:06:18,720 --> 00:06:24,650 Remember that underscore user is used to point at the user who owns this very particular survey. 83 00:06:24,660 --> 00:06:30,930 So when we create this new instance of a survey back inside of our survey route's file over here we 84 00:06:30,930 --> 00:06:37,400 need to make sure that we somehow link the current user to this survey that we are creating right here. 85 00:06:37,650 --> 00:06:42,870 So we're going to out on one more property I can to make sure that I put a comma at the line right above 86 00:06:42,870 --> 00:06:48,210 us and they will say underscore user because remember we are indicating that this is a relationship 87 00:06:48,210 --> 00:06:53,220 field by saying by putting the underscore in there and then all we have to do is pass in the current 88 00:06:53,220 --> 00:06:58,590 user's ID which is available as Reyk user ID. 89 00:06:58,590 --> 00:07:04,640 Now this id property right here is the id property that is automatically generated by mongoose in lango. 90 00:07:04,710 --> 00:07:09,270 So we don't have to generate this ourselves so we don't have to define it in the schema or anything 91 00:07:09,270 --> 00:07:09,960 like that. 92 00:07:10,080 --> 00:07:14,240 It is just a property that is available to us on any mongoose model. 93 00:07:14,810 --> 00:07:19,950 Now I think that the very last thing we have to put inside of your very last thing we have to make sure 94 00:07:19,950 --> 00:07:26,790 that we mark the date at which this survey was actually sent. 95 00:07:26,850 --> 00:07:29,960 So we have not yet actually sent the survey. 96 00:07:30,000 --> 00:07:35,460 But essentially you know when we create this instance right here we are close enough to the creation 97 00:07:35,460 --> 00:07:36,060 time. 98 00:07:36,060 --> 00:07:42,300 So I think that just right here we can immediately say hey here is the date at which the survey was 99 00:07:42,300 --> 00:07:42,760 sent. 100 00:07:42,760 --> 00:07:47,520 No we haven't not yet created the e-mail or any of that stuff like that but we can just kind of assume 101 00:07:47,520 --> 00:07:51,420 that we're close enough to it and say right now we have sent a survey. 102 00:07:51,810 --> 00:07:58,080 So we will out in the date sent property and we'll say that it's essentially right now and we can do 103 00:07:58,080 --> 00:08:00,460 that by saying date dot. 104 00:08:00,510 --> 00:08:02,140 Now like so. 105 00:08:02,510 --> 00:08:05,800 So this returns a date object which Mongo can handle. 106 00:08:05,860 --> 00:08:06,540 OK. 107 00:08:06,570 --> 00:08:10,780 And it just records when we have created and sent off the survey. 108 00:08:11,460 --> 00:08:12,350 OK. 109 00:08:12,720 --> 00:08:15,090 So we have now made our new survey. 110 00:08:15,090 --> 00:08:21,120 Remember this just makes an instance of a survey in memory and has not yet been persisted to our database 111 00:08:21,150 --> 00:08:22,880 or anything like that. 112 00:08:22,890 --> 00:08:26,090 So at this point we're now going to take a little bit of a break. 113 00:08:26,250 --> 00:08:30,640 When we come back we're going to look at a diagram that's going to help us understand exactly what we 114 00:08:30,640 --> 00:08:33,450 are doing with the survey model after we create it. 115 00:08:33,540 --> 00:08:39,210 And that's going to help us understand exactly how we send an e-mail off to all the people who are listed 116 00:08:39,270 --> 00:08:41,580 inside of the survey as well. 117 00:08:41,580 --> 00:08:43,290 So let's take a quick break. 118 00:08:43,290 --> 00:08:46,170 We'll come back in the next section and we'll start tackling. 119 00:08:46,220 --> 00:08:46,700 Email.