Welcome back.
In this video, let's focus on the account of a user.
We are not done with the user code operation, for example, deleting, updating.
But what we want is the authorization and then the authentication.
The rest are easy.
So let's switch from users and now let's get to how we're going to create an account and then how we
are going to associate account creation with a user and a transaction with an account.
So let's get started.
So inside the controllers, let's collapse the users, and now let's focus on the account and we are
going to start with the creation of an account.
So let's get back to the models and we have the account model.
So inside the account controller less require the one as account and to import is working fine.
So next step is that we're going to pass in the payload that we want to create.
But before creating, let's look at the steps because you want to assist with account creation with
the user.
So here we go inside the try.
Let's have the roadmap.
Step number one is.
So let's start with the step number one.
Remember, we are going to protect this route by using the log in middleware.
So because of that, we're going to have the log in user as req dot user.
So here we go.
Let's bring in the user model.
And for this, the auto import isn't working.
So the trick is that, again, let's go to the file.
We save it.
Let's come back to the file where we want to require.
And then user is turned around.
It is working.
So now here we go.
So cost as user found is equal to a weight.
And then you bring in the user dot fine by ID and then the ID is under rec dot user.
So if a user is not found, we are going to throw an error.
So we need our app error class.
So again, we need to come here and then receive it and come back to the file and let's see a we have
the class perfect.
So here if user is not found, that is if there is no user found then they are going to return code
next.
Then you pass in our error class here and they pass in the message as user not found, they pass in
the status code as four or for many not found.
So at this point it means that a user is found so you can go ahead and create the account as const account
is equal to a weight and then the account model dot great.
And for this, we're going to pass in the name the initial balance, the account type and the node and
then the created by.
So looking at the model, we are going to pass in these properties.
So here we go.
So what we need here is going to be the name of the account, the initial balance,
and then the account type.
And then the nodes, that description.
So here we can pass in the name the initial balance, the account type, and then the nodes and that
is it.
And lastly, we need to add the user who is creating this account that is created by and we need the
ID of the user.
And lucky for us, we have the user and the rec dot user.
So here we go as created by and we are going to use rec dot user and that is it.
So for pushing, we are going to use user found dot account.
Remember we have account on the user.
If you check it out, this is what we want to push the account into.
So here we go.
It's going to be push, then we push the account dot I'd it.
So lastly we need to save the user as a weight user found dot save a everything is perfect.
So lastly, we can say the created account.
So here provide the status as six six.
And next is the actual data, which is the account.
And for error we can pass to next as the error we need that we need to pass in the next.
So that is it, guys.
So next step is that we need to protect this root.
So inside the root of account.
This is a root of account.
Let's bring in the log in.
Well, again, I need to save the file again.
Inside the metaverse is log in, save it.
Let's go to the account and let's import it as that's a here we go.
So this is a root I want to protect.
So I'll provide a second element to it.
So let's save it.
And now let's make a request to create an account.
So back to the tender client, let's close users and account.
Here we go.
Let's provide the properties you want to create.
So here are going to be as the name of my account.
So I'll call this one as mom's building.
Meaning I'm building a house for my mom and I want to keep track about the expenses and the income.
And then it's a balance I want to provide as initial
balance.
I will say for this one, I provide $2,000 and after that the account type for this one, make sure
that you provide the accepted value.
So inside the model, let's look at the account type.
We have savings, investment, credit card and we have projects.
So I'm going to select project here and that project.
Great.
And next is notes in in description about this account, let's say building a house for my sweet mother
and that is it for the created by.
We have access inside the controller so there is no worry about that.
So let's hit st and let's see the result.
A You can see I guess some error here, meaning that kind of read properties of undefined meaning that
we are not providing the token.
So here provide authorization and now provide the bearer and let's add some random token here and let's
st and you can see that we have the message as invalid login.
So let's go ahead and then log in and then grab that token.
So we and the users here log in and let's copy this token.
And let's go to the crate here and let's provide us that and let's hit send.
Well, I guess I'm error.
Let's see where the arrow is coming from.
Oh, I got this.
Next is not defined.
The spelling is wrong.
So let's get to the.
Controllers under account and let's see where the error is coming from.
So under account creation.
Oh, supposed to be t I believe you guys saw it.
Supposed to be next and not t.
Yeah, yeah, that is it.
So let me try again and see.
Oh, I got some error here.
Meaning that account type is required.
So let's see if in providing the account type.
Well I think I'm not destruction it inside here.
So let's see the body where we have the account type here.
The account type is supposed to be here, but why is it that it is not receiving that?
So let's check the spellings.
Well.
Is account type.
So I think I made some type of mistake here.
Yeah.
So let's say again.
A This time around, I go by my first account that I want to keep track about the expenses and income.
You can see that for this account there is no transaction without expenses or income, and I have the
details about that.
So what has left is that I can go ahead and create more account.
So for this I will say about my education.
So I will say that expenses
about my education and for the initial balance I'll provide RS 1000 and this I'm going to be the education.
So let's hit send where I got an error meaning that education is not a valid admiration.
So let's see the type of values the account type takes.
We don't have education inside here.
I think I can add it right because it's really important.
Okay.
So let me add that one to here quickly as education.
So save it and now let's create that one.
And let's see a I go back my second account.
So if I check my profile, I want to see all the account that I have created.
So let's go to file and let's hit send.
And this time around you can see that I have all the account that I've created, but I see the IDs.
So the next video, let's see how we are going to populate this ID with the real object.