Hey, did everyone had a share?
And welcome to the new section.
I know this course is really, really long, but whatever you wanted to learn or whatever you wanted
to master, this all is done now.
Or we are onto a system where we are going to work on the order system.
And this is exactly what we have been working so far.
So so far we know how to cross reference into the models itself by using the refs and mongoose dot schema
dot id object id rather.
So this is what we will be doing here as well.
We have also seen in the past that how we can manipulate ratings some of the fields which user don't
directly control, but it is auto calculated, so it's just some mathematical and some of the functions
that run there.
Now here also we are going to work on the same.
Remember, we have to manage the stocks as well.
So stock is nothing, just an inventory.
So as soon as the order is being delivered to the user, that means something has something needs to
be reduced and that is going to be the stock that we are taking from the user while creating the product.
So really simple.
Just like we were manipulating the reviews.
This is how we will be manipulating the stock here.
So really pretty simple.
Apart from that, nothing new.
Nothing new in this one.
So let's go ahead and check out our simple screen up here.
That's how we are managing this one.
So this is the entirety of the models.
I know this is like really a long gone sheet that we have pulled off.
So let me just walk you through.
So this is the payment route that we have already worked with that stripe key Razorpay Key Capture,
Stripe and Capture Razorpay This is the recent one that I added after recording the videos.
This is all what we have worked on.
I've added a simple highlighter on the admin routes up here and the red lines are actually the delete
requests here.
And similarly, these are the routes I forgot to show you these ones, but this is how we actually came
up with that.
So we had the products, we have individual product review review and the reviews.
So reviews all the reviews.
A red line means a simple delete request and a edit request on this one.
And similarly we have got this product, so we need to add the product, get all the products, edit
the product with the special ID that means single product and this one.
And similarly, we'll be working on this one.
So this is the really simple of the route that we have.
We want to create an order.
We want to get an individual order.
We want to get a specific order.
Get my order.
That means just grab the user from request user ID and get all the orders of that particular user itself.
Or we'll be simply filtering that, Hey, get me all the orders whose user is listed as that request
dot user id pretty simple.
And then we have admin.
So he wants to get all the orders.
He wants to update the order.
How much updation facility you want to give, that's up to you.
And we want to obviously allow the admin to delete the order.
So that's pretty much it.
Now further down the road, we will be actually grabbing all of this information that we previously
worked on and we had a lot of discussion about how to actually go ahead and work on with that.
So let me go ahead and copy all of this going back and let's create another model for the orders itself.
So let's go ahead and say new file and this one is going to be order dot JS There we go.
The first thing is we need Mongoose.
So we're going to go ahead and say, Hey, Mongoose, please give me the require as Mongoose.
Come on, suggest me.
No, not going to do that.
Okay, so now we have Mongoose available with us.
Now, I would like to actually paste all of this up here, and I'm going to go ahead and just comment
this out.
This will actually help us to just craft the orders all appear instead of going the sheets back and
forth up here.
So let's go ahead and start working with that.
Let's go ahead and create a simple order schema.
So we're going to go go ahead and call this one as simple order schema.
And that order schema will be created simply from the mongoose.
So let's go ahead and say mongoose dot schema and there we go.
And this reminds me, maybe I have done some mistake in the product.
Okay, so we can actually use the new keyword or we can actually work without the new keyword as well.
However you like to go ahead.
And with that, I will actually use the new keyword here as well to say stay consistent in here.
Now, this reminds me of one more thing, which I was looking up, and I'm pretty sure some of you might
be getting confused in that in case you have previous experience on that.
So sorry for taking this topic really late here, but you might have noticed that we have got a whole
lot of fields going on and working on that.
For example, when I walked you to with the user, we simply say it's mongoose dot schema, dot object
ID, but you are going to notice that a lot of times there is a mongoose dot schema, dot type object
ID.
So yes, I'm pretty aware of the situation because this is a common question that comes around whenever
I take this bootcamp into any of the corporates.
So notice here, this is a StackOverflow that should answer all this question that should I use the
schema type object ID or you should use the schema dot object ID when defining.
And the very first answer actually sums it all up that it doesn't matter.
Both are exactly same.
If you are actually using the console log, you'll see that they actually gives you the exact same result.
But there is further down the road and answer that Hey, I found once an issue that if you want the
backward compatibility, you want to use object schema dot object ID in case you want to follow the
newer convention, then you can go ahead and use the types dot object id.
So again they exactly means the same.
It's up to you that what you really want to grab there.
So inside this order ID in fact at the very end of it I'll just go ahead and some put some ref link.
So let's just say ref link so that later on in case you want to, you can go ahead and read this one
to.
So I'll just add this one for your reference.
Moving on, let's go up here and inside this order schema.
Let's go ahead and start working with how the order is going to be placed.
This is going to be a really long one.
So the first thing that we need is the shipping info.
Let's go ahead and copy that and bring this one here.
So the shipping info, obviously, this is going to be an object.
And inside the shipping info, we need a lot of information from the user.
So first we are going to say that, hey, give me your address.
And sometimes people take address, line one, address, line two, totally up to you.
The type is obviously going to be string and we are going to say that this is a required field, otherwise
how we are going to ship the order itself.
So there we go.
Okay.
Once I have gone this one, now let's collect some more information inside the same shipping info.
So address is there then we are going to go ahead.
In fact, let me go ahead and make a couple of copies of it.
Now, once the address is done, we obviously want to take city as well.
Maybe you want to take address line two or also that is totally fine.
Then we need to take phone number, phone number and let's go ahead and make a couple of more copies
of it.
So once I have the phone number, I have city, I will take probably state as well.
Or we can take a postal code.
You might want to take state as well.
So let's go ahead and take state as well as much information as you grab.
That's always good.
And we're going to go ahead and take country finally.
Okay.
So all these information are absolutely necessary and they are required.
Let's go ahead and shrink it down.
So this is the shipping info that we need.
Okay, let's come back.
And what else do we need?
After the shipping info, we're going to go ahead and say that we need a user.
So who is placing this order?
Let's go ahead and say that this is going to be the user who is placing the order just like this.
What is the type of the user?
The type of the user is actually mongoose dot schema dot object ID.
Again, you can use optionally types here.
So you can go ahead and say, hey, I can go ahead and say mongoose dot schema, dot type's.
Remember it is not type it is actually types object ID, there we go.
And you can actually optionally go ahead and use this one and moving further once we have this, then
we have to provide the reps as well.
So from where I should take the reference, you should take the reference from the user model itself.
Is it required?
Yes, 100%.
This is required.
There we go.
True.
Okay.
So once we have this user now, let's go ahead and take care of this order items because this is, again,
a mammoth of the object itself.
And this one is going to be an array of object because order item can have two t shirts or two types
of t shirt, maybe three products each, each of them having many variety.
So this is how important it is and make sure you keep them as an array.
So let's go ahead and say this is going to be an order item, this is going to be an array, and inside
the array will be having multiple of the objects.
So what, how the one object itself is going to look like we'll be just adding these information, name,
quantity images, just the one image, actually the price and the product itself.
What is the product here?
Let's go ahead and say, hey, I would like to first add the name here.
So what is the name of the product?
We can actually grab it from the front end that whatever the product you are calling in.
So let's go ahead and say, hey, this one is going to be type of string and this one is required also.
Otherwise, how would we know that what item to ship a ship to the user?
Okay, let's go ahead and grab a name then we have got one, two, three, four, five.
So one, is there two, three, four, five.
Okay, now moving on.
The next one is quantity.
So let's go ahead and see quantity.
The quantity is obviously going to be type of number.
This quantity represents that.
Let's just say there is a pro order t shirt and I want five t shirts maybe for some reason.
So that is why the number is there.
Then the image this says you can skip this part if you in case you don't want to handle the images.
But this is all okay.
We are going to have a type of string and we're going to require true.
Now what you're going to inject further down the road.
You don't need to have the public ID or anything of the image.
We will be just injecting the security URL in this one and that's it.
We'll call it as a day off here.
Now, after the image, we have got the price, of course.
So there we go.
So price the price is going to be number.
Now this is going to be the price of let's just say you are ordering pro order t shirt.
You have ordered five of these t shirts.
So this price is going to be 299 or whatever the t shirt price is, let's just say ₹500.
So 500 multiply by five.
So this is the price that will be going on for that individual product.
And then finally at the end, we are going to have a product.
Now this product again, we can borrow some code from here, actually copy that because this is a product
and we need to have a reference here.
So we're going to say, hey, mongoose, dot schema, dot object ID is going to be there.
And the reference that you need to take is actually from the product and required is true and that's
pretty much it.
That's all what we got.
Okay.
So this is the behemoth of the data that we need to manage up here.
And once this is all done, this is all like really the biggest of one.
Let's go ahead and work on with the rest of them.
Let me go ahead and squeeze this.
There we go and put up a comma.
There we go.
Now, what else we got up here.
So this order item is all done.
We got user done.
Now we need to take care of the payment info.
Now the reason payment info could be directly a string itself, but I would love to create an object
into this one because later on maybe it comes up that, hey, I want to take more information inside
this payment info, maybe want to create a separate dashboard for the admin, for all the payment information,
cards, user, and all the details that I can probably grab from either stripe or razorpay.
So we're going to go ahead and say, Hey, this is going to be a type of string.
That's it.
That's all for now.
But this is all kept as for the future usage.
Okay, now moving on.
We want to have as payment info is there, then we have got a whole lot of amounts.
So let's go ahead and say this is a text amount, so let's go ahead and grab the text amount.
Scroll there.
There we go.
Now how does the text amount looks like?
So there we go.
We are going to create an object on this one.
So the type is obviously going to be number in the text amount and we are going to have required as
well.
So this is required.
True.
So somebody needs to send me this text amount.
Let's go ahead and grab a copy of this because we will be having a very similar field solve this like
this.
So two copies of this.
So after the text amount, we have shipping amount.
Let's go ahead and in fact, copy this.
Now I'll write that this one is going to be shipping amount.
And after the shipping amount, we have total amount.
I can come up here and say total amount.
Okay, so this is all done.
Now we have another field which is order status.
So copy that and let's go ahead and see that.
So how does this order status is going to look like?
Now, this order status is going to be pretty simple.
It is going to be a simple string, which is going to be default as processing.
And later on you can allow the user to change it.
In fact, you can add an enum here.
That would be also fine, but I'm not going to do it.
I'll leave that as an exercise for you.
So type is going to be string.
There we go.
Come on.
A string and then we are going to have a required of true and we we'll also put up a default value.
So the default is going to be.
Processing.
I like to keep everything as lowercase.
If I need a per case or a capitalized case in the front end, I can do that from the JavaScript manipulation
later on.
Not here.
I like consistency of the lowercase all the time up here.
Further down the road we have another which is delivered at so.
Copy that and let's go ahead and paste it up here, deliver that.
So this is a really simple field.
All it takes is a type of date.
So let's go ahead and give it a date.
Somebody needs to pass me the date of whenever this is being this order is being processed as delivered.
As soon as that we are going to just pass on a date field and that's it.
And after the end of it, we are going to go ahead and have a simple created at.
So let's go ahead and copy that and let's go ahead and check it out.
Create that again, really simple.
We have done this one so many times.
Shouldn't be a big deal.
This is a type date and the default is date now always date dot now.
There we go.
Okay, so now this seems okay.
Let me take a close look.
If I haven't missed anything or there is something more that we can actually add or is it all okay,
okay.
So this seems all okay.
I have gone through again with the list that we walked in up here.
So this is what I have been stating for a while.
So it looks like everything is all in control and everything is all being done.
Okay, let's go ahead and shrink this up.
So there we go.
This looks okay now let's go ahead and export this.
So simply, we are going to go ahead and say module dot exports and we're going to export using mongoose
dot model.
And this one is going to take order again in the database.
It will be created as orders all lowercase.
I know that and this will be taken up as order schema.
So there we go.
Now, in theory at least, our model is all done and is all ready to work on whether I would like to
shrink all of this, because I will need to actually see this quite a lot when I'll be creating the
order, because there is a lot of information that is coming in and we are manipulating only a handful
of information in this case.
So there we go.
So this is all the data that we are collecting.
I know a lot of them are objects and this is too much of the data to process.
Okay, so that's it.
And let's go ahead and catch up in the next video and create controllers for this order.