Hey did everyone this year and this is a bit of an interesting one not interesting I would say I would
rather say understanding the flow of the application and that's going to be a simple exercise for this
one.
So let's start one by one.
First, we are going to work on these admin dots because we have already worked on three ones.
Slash admin slash orders will give you all the orders available on the platform.
I think anybody can code this, especially if you have been watching the course so far.
I think that should be the easiest one of all and probably you can do in a little bit half sleepy mode.
So anyways, let's go into the controller order controller and let's go ahead and design some of the
admin routes.
Let's copy this, shrink this guy and get some space.
And this one is going to be admin.
Get, get all orders.
Orders.
There we go.
So how we're going to go ahead and do this one.
This one, we are going to go ahead and call this one as orders, because all orders are coming in.
This is going to be in L.A. and all we got to do is pass on nothing inside the find.
And in fact, we don't need to even pass on empty parenthesis.
We can just go ahead and say like this, and then we go, the model is going to fill up this orders
with an array which is containing everything up there.
Now that's all what we're going to do.
We don't even need to check because if the orders are not coming in, it's going to be an empty array.
There is nothing inside that to be worried about and that's it.
We are going to go ahead and throw this orders, throw this orders like this.
And there we go.
And that's pretty much it.
Don't need to do anything else in case you want to do more of the crafty stuff in that.
You can do that.
But I don't think that anything is there.
Let's go ahead and copy this one and throw that into a route as well.
We can close this abacus and this order route.
So we're going to first bring in this is, of course, an admin route.
So let's go ahead and make a copy of this one, have a enter.
And these are all the route for admin.
So admin route says, hey, slash admin slash orders is going to go ahead and give you this.
We need to bring in some of the custom role as well.
So copy that and bring in a custom role.
There we go.
The custom role is going to be of only allowed admin in case you want to do the same things for manager
as well.
Please go ahead and do this.
Or maybe you have a sales manager role.
You can go ahead and do that also.
Okay, this is all good.
Now let's work onto something interesting, which is order controller.
Let's go ahead and grab a copy of this one again, another admin route.
Now this one is going to be update the order.
This is going to be a little bit interesting.
So update admin, update order.
And again, this can be a little bit casing.
There we go.
Now how this is going to work.
First, let's analyze that, how the order information is coming in.
So this is my order, which is my order.
So I grabbed this one order.
So now just imagine and try to think that how much information or how much control you want to give
the admin to update the things.
Now there is no wrong or right.
There is always a need of application, maybe the need of the client itself.
So in this case shipping info, if there is any mistakenly manipulation in that, that's not good.
User will not be able to ship that thing payment info.
Obviously this should be all readable.
Field shouldn't be allowed admin to be editable users.
We probably arguably can give some control to change the users.
Maybe use that as requested to change their name or email or something like that.
But I think this is not touch should not be touched inside the order item also user as selected and
give us all the data.
So we shouldn't be touching that either.
But in case you want to manipulate that, you can later on give that.
Now in this case also the tags and for shipping info total amount shouldn't be changed at all.
The only thing that we are going to change is the order status.
So if this comes up into the body, I'm going to go ahead and change that.
And we have a particular fields that processing to delivered or something like that that can come in.
So that's all the information we are expecting into the body itself.
First thing is user will be selecting a path of admin slash order slash ID just like we saw up here.
So admin slash order slash ID.
So we'll be working on this one.
So the user will be sending me the order ID and in this case I expect that user will be sending me the
ID that I should be updating and all the rest of the information will come up in the body itself.
So let's go ahead and first find that.
So we need to find a particular order in this case.
So let's go ahead and see that.
How we can do that.
We'll be simply finding that by find, by ID and let's go ahead and grab this one.
This one is going to say simply request dot params ID.
This might be so much familiar to you by now.
This is not going to be orders, but rather a simple order that we are trying to manipulate.
Now, once this order has come to me, I want to update certain fields of it.
So I'm going to go ahead and simply say that, hey, let's go ahead and order update the order status.
So go into the models and this is the order status that we are expecting to change.
So this order status and this will be coming up in the body.
So request dot body, dot order status.
So whatever the order status you are sending me in the body, I'm allowing you to change that.
Now, once I have changed this one, I have to actually update this, this one here.
So this is delivered at.
So if the requested burrito status is delivered, then only we have to manipulate.
So what we can do is once the order is delivered, I don't think anybody is going to come in and send
and going to say that, hey, just undelivered it because you cannot, to be honest, you have to place
another order.
So let's go ahead and check in the request body if this one is coming in as delivered.
So if it is already being delivered, if this part, then we are going to not allow to change this one.
So let's go ahead and do that again.
This is all the user flow.
How would you like to go ahead and do that?
So let's take this order and we are going to go ahead and say, hey, there is a order status, so let's
go ahead and check what is the existing status.
If my orders existing status is actually equal to delivered.
Delivered and did I wrote it correct?
Hopefully.
Yes.
Nope.
Delivered.
There we go.
Now, if this is delivered, we are going to simply return a simple response here.
So we're going to go ahead and say, hey, return.
Let's go ahead and wrap it up in the next new custom error.
And the customer is going say order is already marked for delivered and we're going to go ahead and
say 401.
There we go.
So now, according to this flow, we are allowing user to only send one thing which is delivered when
they use when the data is not marked.
Again, you can just put any little bit more of the clauses and can say that, hey, if the order is
marked as processing, then we are going to only allow or something like that.
It's up to you how you're going to process that.
So we are going to expect that if anything else comes up other than delivered, maybe on the route processing
out for shipping for all these cases, we are allowing user to update the status.
Okay.
So this is all done.
Only thing that we have to do after that is let's go ahead and say, hey, this order needs to be saved
now, so order, dot, save.
There we go.
So we have saved the order.
Order was marked to ship.
But if you remember, there was a classic requirement from the client side that, hey, please, I want
to manage my inventory as well.
So stock also needs to be managed.
Now, if the if this was marked as delivered.
So let's just say this comes up as a delivery.
If the order was delivered, then we have to update the stock as well.
So how we are going to update the stock, we are going to go ahead and create a simple method for that
because this updation is happening into some another model itself.
So let's go ahead and work with that.
So this one is obviously going to be simple async because that's how it is going to be.
You can create a simple arrow function or you can just go ahead and go classic like this.
And we are going to go ahead and say update product stock just like that and just like that.
And there we go.
Now, in this one, we expect that you pass me on the product ID, so let's go ahead and say you give
me the product ID and you also give me the quantity.
Now, the thing is that how you're going to give me the quantity?
That is also a question.
So the quantity as you notice inside this order item.
So order has this order items, it has this quantity field.
So whatever the quantity is, I just have to update that.
And if you look into the models of the product itself, I go ahead and see that I have a field here
which is I don't have a stock field.
Okay?
So just find out that we don't even have the stock field up here.
So we need to actually keep this one.
So this was already mentioned in the requirement that I also want to add new product and keep a check
on the stock.
So we need to now inject this one.
And this is why I love MongoDB, that I can go ahead and manipulate the models and it's not going to
break my existing of the model I can go ahead and work on with that.
So no big deal.
We have this brand, brand ratings, a number of reviews and all of these things.
Let's go ahead and add a stock update up here.
So let's go ahead and add another field, which is going to be simple stock.
We have done this many times, so shouldn't be a big deal.
Let's go ahead and add a comma here as well.
So the stock is pretty easy.
We are going to go ahead and say this is going to be of stock is usually numbers.
So we're going to go ahead and say, hey, this is a type of number and we don't need to provide any
default value.
We are going to go ahead and mark this one as required and let's go ahead and use an array.
So required is true and we're going to provide a message as well that please add a number in stock.
So really you can use a better one than this one and that's pretty much it.
I'm going to go ahead and save this one and that's it.
Now, I have a stock in the field of this number and I have already discussed that how you're going
to grab the quantity and the product ID itself.
So assuming that the guy who is actually working on this one has given me this product ID and this one
again, both of the fields, just to remind you again, both of the fields are available here in the
order items.
So product is here, this is product ID and this is the quantity.
I just have to subtract, subtract these two things by using these two things into the product model.
So let's go ahead and grab this.
So first, we need to find the product, how we're going to do that.
We have this product, how to find a product by the ID pretty much couldn't be much more simpler than
this one.
So find buy ID and we are going to just go ahead and pass on this product ID.
There we go.
Now we need to hold this one into available.
So let's go ahead and call this one as product.
There we go.
And this is a database operation, so this is going to take some time.
Okay.
Now, the only thing we have to do is find the stock in the product.
So we can do that by simply saying a product dot stock and we need to subtract that.
How we're going to subtract that.
Let's go ahead and see that whatever the existing product is and we're going to go ahead and product
DOT stock and we are going to subtract the quantity, whatever you are passing up to me.
Now, remember here, we haven't done any check.
Whether the product, stock and quantity are matching or not because we should be doing it.
Rather, in case this would be an application where I would be working for six months, I would love
to put a check here that while placing the order I should be first checking that the whether the product
is actually in the stock or not.
I should be checking that, but I cannot go for six month long in this course.
So that is why I'm just sharing these ideas.
These are something that you should be doing on your own.
Let's go ahead and save this one.
So we're going to go ahead and say product, dot save.
There we go.
And once I'm having this save, I'm going to go ahead and save validate before save because I don't
want to turn on any of the flags like required or anything.
So let's turn this off and this is going to take some time.
So let's go ahead and await this one.
Copy this and paste it up here.
There we go.
Okay.
Now, as soon as you are going to call this method with the given all these things, this should be
all good now.
Okay, now let's come back up here before we hit save on this order.
And this order is marked as delivered.
Let's go ahead and use this method.
So let's go ahead and do this.
And we have to do this for every single of the item, because right now here in the order items, remember
we are having just one product, but there can be more product.
That's why this is Eddi.
So let's go ahead and use this order item.
So before I save this one, I'm going to go ahead and say, hey, order dot order items.
Remember, there can be more up here.
We're going to use a classic for each loop.
This for each loop is going to give you each and every item.
So this is going to be called as a product.
So let's call this one a simple prod for the product in the items and for each of the product.
Now, again, this is a database operation, so this might take some time here as well.
So this is going to be a simple a sync for each of the product.
Let's go ahead and do something.
What is the something that you want to do?
I simply want to go ahead and update my stock.
So I'm going to go ahead and say update product stock.
There we go.
Now in the update, product stock, how we are going to work.
First, give me the product ID so how you can go ahead and do this one.
So this product now remember, I am inside this particular field so I can go ahead and say prod dot
product to give the product ID and I can go ahead and say product quantity to give this quantity.
So let's go ahead and do that.
So finding this, I can go ahead and say prod dot product.
This is the product ID and I can go ahead and say prod dot quantity.
So product dot product will give me the ID and product quantity will give me the quantity of that.
And since this is an async function itself, I'm going to go ahead and wait for this one to process
all the things that it wants to do.
And this is all done.
Let's save this one.
This obviously is going to take some time because it's going into the model and stuff and now this is
saving.
Now, again, as we discussed, a whole lot of things can be done.
But again, I don't have six months or a year to design this entire application while placing or updating
the order.
We can check for each of the item, whether it is delivered, whether it is a processing on the way
and all of that and do further down these things.
We can also check while creating the order itself whether the stock is in the quantity or something,
because this was obviously one of the requirement from the clients and we have seen that how we can
go back and update some of the stocks and something like this.
So I'm going to go ahead and put up a comment here as well.
Updated in order.
Videos later.
So there we go.
So I can mark this.
This field was updated in order videos later.
So there we go.
So stock is being updated later on.
But again, you get the idea that how this is being done and if any other requirement comes in, you
can always go ahead and play around.
So there is a lot to learn in this video.
Let's go ahead and catch up in the next video.