In the last video we started creating our new instance of a survey but we took a pause when we got to
the creation of the sub document collection of recipients.
So that's a reminder again and know we just looked at this but I want to make sure that it's really
clear inside of our survey schema we said that there is a recipient's property that is an array of recipients
schema objects.
So if we then go and look at the recipient schema we had said that there is an e-mail property and they
responded property and that we didn't actually have to define the respondent property because it will
be defaulted defaults which is exactly what we want inside of our application.
So if we start to visualize the actual relationship here the actual kind of structure of the survey
and the recipient.
We might end up with something that looks like this.
So this is essentially what we want to do when we create a survey instance we want to pass in an object
that has the title the subject the body and then for the recipient's property it's where each object
has an email property that points out the string email for that particular recipient.
When we pass in this array of objects mongoose will automatically create that sub document collection
for us.
So all we really have to do is make sure that we pass in an array of objects that contains the user's
email address as a key of email.
So we need to somehow get from this recipient's argument or this recipient parameter or property that's
passed along with the request body which is a comma separated string of e-mail addresses and transform
it into an array of objects where each object has an email property of the individual e-mail address.
So that's essentially why I want to take the pause and say hey just to make sure we're on the same page.
This is a little bit complicated.
It is complicated because we have to do that transformation from the array of strings to the array of
objects.
So let's talk about how we're going to pull this off step by step.
It's actually not going to be that bad.
So we're going to do is we're going to take that list or string of email addresses that are separated
by commas.
We will call the Split function on that string which will take that string up here.
It will split the entire string by every comma inside of it and return an array of strings.
So it would then be at this step right here where we have an array of strings.
Each string is one individual e-mail address.
We can then use the map function.
And if you're not familiar with map that's fine we'll walk through it exactly how it works.
And for every element inside of this array of strings or for every string inside of that array will
return an object with a property of email that contains the user's actual e-mail an e-mail address or
the actual string that was in that previous array.
So let's give this give this a shot.
The actual code that we're going to write here is going to be very concise very concise but there's
going to be a lot of stuff going on on a single line.
So after our body property we're going to pass in the recipient's property and this is going to be our
array of objects containing the e-mail addresses so we will first start off by taking the recipient's.
We're going to split by Komba.
So this right here is going to turn the thing into an array of strings or an array of email addresses.
And then for every string in that array we will map over it the map function right here takes every
single email in the array.
It runs some function on that individual element and then it creates a new array of what we return from
the arrow function or the function that we passed to map right here.
So we will say for every email address in there return a new object that has an email of email like
so so this returns our array of strings and then we say for every email address returning object.
So here's the object with a property email that points out the user's e-mail.
Now it can use a little bit of ESX code to dramatically simplify this arrow function right here.
First off you'll notice that we've got an identical key and value so we can condense it to be just email
like so and then notice how the body of the arrow function consists of one javascript expression.
We just create the object and return it.
So rather than putting in the outside curly braces and the return keyword because we have just one javascript
expression inside of your we can remove the return keyword and the outside curly braces like so.
So now we've got opening curly brace closing clearly Bretts.
Now this will create an object with a key of e-mail and the value of whatever that e-mail address was.
The last thing you have to do to make sure that works correctly when javascript sees this line of code
right here when it sees the curly braces.
It will get a little bit confuse because it will not understand if we are defining a function body or
if we are defining a kind of shortened object right here and we are actually defining a shortened object.
So to make sure that that is really clear to the javascript interpreter we're going to wrap it with
a set of parentheses like so.
So now we've got opening opening closing closing and closing like so.
OK.
So again this will take the list of email addresses.
Split it into an array and then return an object for every e-mail address in there with a property of
email and the value of the actual email address.
So that's pretty much it for creating the survey as far as these very simple straightforward properties
go right here.
Now as you and I know there are several other properties that this survey expects to have.
So we've just dealt with the floor up here so far.
The next one that we're going to deal with is going to be the user property.
We don't have to worry about the yes and no properties because again we've assigned default values to
them.
So we don't have to specifically say start out with zero yeses and zero NOS or anything like that.
They will just be automatically defaulted for us.
So the next property that we really have to worry about is the user relationship right here.
Remember that underscore user is used to point at the user who owns this very particular survey.
So when we create this new instance of a survey back inside of our survey route's file over here we
need to make sure that we somehow link the current user to this survey that we are creating right here.
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
us and they will say underscore user because remember we are indicating that this is a relationship
field by saying by putting the underscore in there and then all we have to do is pass in the current
user's ID which is available as Reyk user ID.
Now this id property right here is the id property that is automatically generated by mongoose in lango.
So we don't have to generate this ourselves so we don't have to define it in the schema or anything
like that.
It is just a property that is available to us on any mongoose model.
Now I think that the very last thing we have to put inside of your very last thing we have to make sure
that we mark the date at which this survey was actually sent.
So we have not yet actually sent the survey.
But essentially you know when we create this instance right here we are close enough to the creation
time.
So I think that just right here we can immediately say hey here is the date at which the survey was
sent.
No we haven't not yet created the e-mail or any of that stuff like that but we can just kind of assume
that we're close enough to it and say right now we have sent a survey.
So we will out in the date sent property and we'll say that it's essentially right now and we can do
that by saying date dot.
Now like so.
So this returns a date object which Mongo can handle.
OK.
And it just records when we have created and sent off the survey.
OK.
So we have now made our new survey.
Remember this just makes an instance of a survey in memory and has not yet been persisted to our database
or anything like that.
So at this point we're now going to take a little bit of a break.
When we come back we're going to look at a diagram that's going to help us understand exactly what we
are doing with the survey model after we create it.
And that's going to help us understand exactly how we send an e-mail off to all the people who are listed
inside of the survey as well.
So let's take a quick break.
We'll come back in the next section and we'll start tackling.
Email.