WEBVTT

00:03.700 --> 00:08.530
Hey did everyone this year and this is a bit of an interesting one not interesting I would say I would

00:08.620 --> 00:13.360
rather say understanding the flow of the application and that's going to be a simple exercise for this

00:13.360 --> 00:13.780
one.

00:13.780 --> 00:15.040
So let's start one by one.

00:15.040 --> 00:18.880
First, we are going to work on these admin dots because we have already worked on three ones.

00:18.880 --> 00:23.020
Slash admin slash orders will give you all the orders available on the platform.

00:23.020 --> 00:26.830
I think anybody can code this, especially if you have been watching the course so far.

00:26.830 --> 00:32.380
I think that should be the easiest one of all and probably you can do in a little bit half sleepy mode.

00:32.380 --> 00:37.390
So anyways, let's go into the controller order controller and let's go ahead and design some of the

00:37.390 --> 00:38.080
admin routes.

00:38.080 --> 00:43.030
Let's copy this, shrink this guy and get some space.

00:43.240 --> 00:46.090
And this one is going to be admin.

00:46.090 --> 00:50.380
Get, get all orders.

00:51.200 --> 00:52.040
Orders.

00:52.070 --> 00:52.730
There we go.

00:52.880 --> 00:54.830
So how we're going to go ahead and do this one.

00:54.830 --> 00:58.650
This one, we are going to go ahead and call this one as orders, because all orders are coming in.

00:58.670 --> 01:03.830
This is going to be in L.A. and all we got to do is pass on nothing inside the find.

01:03.830 --> 01:06.350
And in fact, we don't need to even pass on empty parenthesis.

01:06.350 --> 01:10.400
We can just go ahead and say like this, and then we go, the model is going to fill up this orders

01:10.400 --> 01:12.800
with an array which is containing everything up there.

01:12.980 --> 01:14.570
Now that's all what we're going to do.

01:14.570 --> 01:19.210
We don't even need to check because if the orders are not coming in, it's going to be an empty array.

01:19.220 --> 01:22.570
There is nothing inside that to be worried about and that's it.

01:22.580 --> 01:26.750
We are going to go ahead and throw this orders, throw this orders like this.

01:26.750 --> 01:27.890
And there we go.

01:28.430 --> 01:29.570
And that's pretty much it.

01:29.570 --> 01:34.070
Don't need to do anything else in case you want to do more of the crafty stuff in that.

01:34.220 --> 01:34.910
You can do that.

01:34.910 --> 01:36.740
But I don't think that anything is there.

01:36.770 --> 01:39.980
Let's go ahead and copy this one and throw that into a route as well.

01:39.980 --> 01:42.950
We can close this abacus and this order route.

01:43.070 --> 01:46.130
So we're going to first bring in this is, of course, an admin route.

01:46.130 --> 01:49.700
So let's go ahead and make a copy of this one, have a enter.

01:49.700 --> 01:51.860
And these are all the route for admin.

01:52.520 --> 02:00.470
So admin route says, hey, slash admin slash orders is going to go ahead and give you this.

02:00.470 --> 02:02.540
We need to bring in some of the custom role as well.

02:02.540 --> 02:04.670
So copy that and bring in a custom role.

02:05.090 --> 02:05.660
There we go.

02:05.660 --> 02:10.880
The custom role is going to be of only allowed admin in case you want to do the same things for manager

02:10.880 --> 02:11.300
as well.

02:11.330 --> 02:12.380
Please go ahead and do this.

02:12.380 --> 02:14.150
Or maybe you have a sales manager role.

02:14.180 --> 02:15.800
You can go ahead and do that also.

02:16.130 --> 02:17.240
Okay, this is all good.

02:17.240 --> 02:20.210
Now let's work onto something interesting, which is order controller.

02:20.210 --> 02:23.720
Let's go ahead and grab a copy of this one again, another admin route.

02:24.020 --> 02:26.300
Now this one is going to be update the order.

02:26.300 --> 02:28.160
This is going to be a little bit interesting.

02:28.160 --> 02:33.260
So update admin, update order.

02:33.260 --> 02:36.830
And again, this can be a little bit casing.

02:36.830 --> 02:37.430
There we go.

02:37.790 --> 02:39.290
Now how this is going to work.

02:39.290 --> 02:42.290
First, let's analyze that, how the order information is coming in.

02:42.290 --> 02:44.210
So this is my order, which is my order.

02:44.210 --> 02:45.830
So I grabbed this one order.

02:45.830 --> 02:51.380
So now just imagine and try to think that how much information or how much control you want to give

02:51.380 --> 02:52.970
the admin to update the things.

02:52.970 --> 02:54.800
Now there is no wrong or right.

02:54.800 --> 02:58.700
There is always a need of application, maybe the need of the client itself.

02:58.700 --> 03:03.470
So in this case shipping info, if there is any mistakenly manipulation in that, that's not good.

03:03.470 --> 03:06.620
User will not be able to ship that thing payment info.

03:06.620 --> 03:08.150
Obviously this should be all readable.

03:08.150 --> 03:11.180
Field shouldn't be allowed admin to be editable users.

03:11.180 --> 03:15.200
We probably arguably can give some control to change the users.

03:15.200 --> 03:18.740
Maybe use that as requested to change their name or email or something like that.

03:18.740 --> 03:24.170
But I think this is not touch should not be touched inside the order item also user as selected and

03:24.170 --> 03:25.190
give us all the data.

03:25.190 --> 03:26.930
So we shouldn't be touching that either.

03:26.930 --> 03:29.930
But in case you want to manipulate that, you can later on give that.

03:29.930 --> 03:34.490
Now in this case also the tags and for shipping info total amount shouldn't be changed at all.

03:34.520 --> 03:37.070
The only thing that we are going to change is the order status.

03:37.070 --> 03:40.550
So if this comes up into the body, I'm going to go ahead and change that.

03:40.550 --> 03:45.860
And we have a particular fields that processing to delivered or something like that that can come in.

03:45.860 --> 03:49.300
So that's all the information we are expecting into the body itself.

03:49.460 --> 03:55.670
First thing is user will be selecting a path of admin slash order slash ID just like we saw up here.

03:55.670 --> 03:57.830
So admin slash order slash ID.

03:57.860 --> 03:59.720
So we'll be working on this one.

04:00.410 --> 04:05.270
So the user will be sending me the order ID and in this case I expect that user will be sending me the

04:05.270 --> 04:09.710
ID that I should be updating and all the rest of the information will come up in the body itself.

04:09.710 --> 04:11.720
So let's go ahead and first find that.

04:11.720 --> 04:15.290
So we need to find a particular order in this case.

04:15.290 --> 04:16.490
So let's go ahead and see that.

04:16.490 --> 04:17.570
How we can do that.

04:17.600 --> 04:23.630
We'll be simply finding that by find, by ID and let's go ahead and grab this one.

04:23.630 --> 04:27.350
This one is going to say simply request dot params ID.

04:27.350 --> 04:29.390
This might be so much familiar to you by now.

04:29.420 --> 04:33.890
This is not going to be orders, but rather a simple order that we are trying to manipulate.

04:33.980 --> 04:38.480
Now, once this order has come to me, I want to update certain fields of it.

04:38.480 --> 04:43.310
So I'm going to go ahead and simply say that, hey, let's go ahead and order update the order status.

04:43.310 --> 04:49.670
So go into the models and this is the order status that we are expecting to change.

04:49.670 --> 04:52.580
So this order status and this will be coming up in the body.

04:52.580 --> 04:55.160
So request dot body, dot order status.

04:55.160 --> 04:59.120
So whatever the order status you are sending me in the body, I'm allowing you to change that.

04:59.240 --> 05:04.580
Now, once I have changed this one, I have to actually update this, this one here.

05:04.580 --> 05:05.930
So this is delivered at.

05:05.930 --> 05:12.320
So if the requested burrito status is delivered, then only we have to manipulate.

05:12.320 --> 05:17.000
So what we can do is once the order is delivered, I don't think anybody is going to come in and send

05:17.000 --> 05:22.280
and going to say that, hey, just undelivered it because you cannot, to be honest, you have to place

05:22.280 --> 05:22.940
another order.

05:22.940 --> 05:27.170
So let's go ahead and check in the request body if this one is coming in as delivered.

05:27.710 --> 05:32.690
So if it is already being delivered, if this part, then we are going to not allow to change this one.

05:32.690 --> 05:34.340
So let's go ahead and do that again.

05:34.340 --> 05:35.600
This is all the user flow.

05:35.600 --> 05:37.340
How would you like to go ahead and do that?

05:37.340 --> 05:42.680
So let's take this order and we are going to go ahead and say, hey, there is a order status, so let's

05:42.680 --> 05:45.170
go ahead and check what is the existing status.

05:45.530 --> 05:49.970
If my orders existing status is actually equal to delivered.

05:51.620 --> 05:53.570
Delivered and did I wrote it correct?

05:53.570 --> 05:54.470
Hopefully.

05:54.500 --> 05:55.500
Yes.

05:55.520 --> 05:56.120
Nope.

05:56.930 --> 05:58.250
Delivered.

05:59.240 --> 06:00.170
There we go.

06:00.200 --> 06:03.860
Now, if this is delivered, we are going to simply return a simple response here.

06:03.860 --> 06:05.930
So we're going to go ahead and say, hey, return.

06:06.080 --> 06:11.780
Let's go ahead and wrap it up in the next new custom error.

06:11.870 --> 06:23.420
And the customer is going say order is already marked for delivered and we're going to go ahead and

06:23.420 --> 06:24.540
say 401.

06:24.560 --> 06:25.280
There we go.

06:25.640 --> 06:31.640
So now, according to this flow, we are allowing user to only send one thing which is delivered when

06:31.640 --> 06:33.680
they use when the data is not marked.

06:33.770 --> 06:39.290
Again, you can just put any little bit more of the clauses and can say that, hey, if the order is

06:39.290 --> 06:42.830
marked as processing, then we are going to only allow or something like that.

06:42.830 --> 06:45.140
It's up to you how you're going to process that.

06:45.140 --> 06:51.230
So we are going to expect that if anything else comes up other than delivered, maybe on the route processing

06:51.500 --> 06:55.340
out for shipping for all these cases, we are allowing user to update the status.

06:55.760 --> 06:55.940
Okay.

06:56.060 --> 06:56.960
So this is all done.

06:56.960 --> 07:01.850
Only thing that we have to do after that is let's go ahead and say, hey, this order needs to be saved

07:01.850 --> 07:03.560
now, so order, dot, save.

07:04.250 --> 07:04.960
There we go.

07:04.970 --> 07:06.380
So we have saved the order.

07:06.380 --> 07:07.500
Order was marked to ship.

07:07.520 --> 07:12.650
But if you remember, there was a classic requirement from the client side that, hey, please, I want

07:12.650 --> 07:14.360
to manage my inventory as well.

07:14.360 --> 07:16.120
So stock also needs to be managed.

07:16.130 --> 07:19.250
Now, if the if this was marked as delivered.

07:19.250 --> 07:20.960
So let's just say this comes up as a delivery.

07:20.960 --> 07:24.500
If the order was delivered, then we have to update the stock as well.

07:24.500 --> 07:29.090
So how we are going to update the stock, we are going to go ahead and create a simple method for that

07:29.090 --> 07:32.900
because this updation is happening into some another model itself.

07:32.900 --> 07:34.280
So let's go ahead and work with that.

07:34.430 --> 07:38.960
So this one is obviously going to be simple async because that's how it is going to be.

07:38.960 --> 07:43.730
You can create a simple arrow function or you can just go ahead and go classic like this.

07:43.730 --> 07:50.030
And we are going to go ahead and say update product stock just like that and just like that.

07:50.030 --> 07:50.870
And there we go.

07:50.900 --> 07:55.220
Now, in this one, we expect that you pass me on the product ID, so let's go ahead and say you give

07:55.220 --> 07:58.340
me the product ID and you also give me the quantity.

07:58.340 --> 08:01.070
Now, the thing is that how you're going to give me the quantity?

08:01.070 --> 08:02.480
That is also a question.

08:02.570 --> 08:05.510
So the quantity as you notice inside this order item.

08:05.510 --> 08:08.540
So order has this order items, it has this quantity field.

08:08.540 --> 08:11.300
So whatever the quantity is, I just have to update that.

08:11.600 --> 08:18.320
And if you look into the models of the product itself, I go ahead and see that I have a field here

08:18.890 --> 08:22.580
which is I don't have a stock field.

08:23.840 --> 08:24.200
Okay?

08:24.200 --> 08:28.160
So just find out that we don't even have the stock field up here.

08:28.160 --> 08:29.780
So we need to actually keep this one.

08:29.780 --> 08:33.680
So this was already mentioned in the requirement that I also want to add new product and keep a check

08:33.680 --> 08:34.280
on the stock.

08:34.280 --> 08:35.960
So we need to now inject this one.

08:35.960 --> 08:40.220
And this is why I love MongoDB, that I can go ahead and manipulate the models and it's not going to

08:40.220 --> 08:43.130
break my existing of the model I can go ahead and work on with that.

08:43.130 --> 08:43.910
So no big deal.

08:43.910 --> 08:49.160
We have this brand, brand ratings, a number of reviews and all of these things.

08:49.160 --> 08:51.590
Let's go ahead and add a stock update up here.

08:51.590 --> 08:54.410
So let's go ahead and add another field, which is going to be simple stock.

08:54.410 --> 08:56.690
We have done this many times, so shouldn't be a big deal.

08:56.720 --> 08:58.850
Let's go ahead and add a comma here as well.

08:58.850 --> 09:00.650
So the stock is pretty easy.

09:00.650 --> 09:04.220
We are going to go ahead and say this is going to be of stock is usually numbers.

09:04.220 --> 09:09.290
So we're going to go ahead and say, hey, this is a type of number and we don't need to provide any

09:09.290 --> 09:09.950
default value.

09:09.950 --> 09:13.940
We are going to go ahead and mark this one as required and let's go ahead and use an array.

09:13.940 --> 09:22.190
So required is true and we're going to provide a message as well that please add a number in stock.

09:23.510 --> 09:27.830
So really you can use a better one than this one and that's pretty much it.

09:27.830 --> 09:31.130
I'm going to go ahead and save this one and that's it.

09:31.130 --> 09:36.260
Now, I have a stock in the field of this number and I have already discussed that how you're going

09:36.260 --> 09:38.840
to grab the quantity and the product ID itself.

09:38.840 --> 09:45.500
So assuming that the guy who is actually working on this one has given me this product ID and this one

09:45.500 --> 09:49.850
again, both of the fields, just to remind you again, both of the fields are available here in the

09:49.850 --> 09:50.480
order items.

09:50.480 --> 09:53.600
So product is here, this is product ID and this is the quantity.

09:53.600 --> 09:58.850
I just have to subtract, subtract these two things by using these two things into the product model.

09:58.850 --> 10:00.560
So let's go ahead and grab this.

10:00.560 --> 10:03.260
So first, we need to find the product, how we're going to do that.

10:03.260 --> 10:07.820
We have this product, how to find a product by the ID pretty much couldn't be much more simpler than

10:07.820 --> 10:08.390
this one.

10:08.390 --> 10:13.100
So find buy ID and we are going to just go ahead and pass on this product ID.

10:13.850 --> 10:14.570
There we go.

10:14.570 --> 10:16.550
Now we need to hold this one into available.

10:16.550 --> 10:18.770
So let's go ahead and call this one as product.

10:18.770 --> 10:19.580
There we go.

10:19.580 --> 10:23.000
And this is a database operation, so this is going to take some time.

10:23.120 --> 10:23.590
Okay.

10:24.080 --> 10:28.070
Now, the only thing we have to do is find the stock in the product.

10:28.070 --> 10:34.670
So we can do that by simply saying a product dot stock and we need to subtract that.

10:34.670 --> 10:36.290
How we're going to subtract that.

10:36.320 --> 10:41.930
Let's go ahead and see that whatever the existing product is and we're going to go ahead and product

10:41.930 --> 10:47.000
DOT stock and we are going to subtract the quantity, whatever you are passing up to me.

10:47.330 --> 10:50.450
Now, remember here, we haven't done any check.

10:50.720 --> 10:54.890
Whether the product, stock and quantity are matching or not because we should be doing it.

10:54.890 --> 10:58.910
Rather, in case this would be an application where I would be working for six months, I would love

10:58.910 --> 11:04.160
to put a check here that while placing the order I should be first checking that the whether the product

11:04.160 --> 11:05.750
is actually in the stock or not.

11:05.750 --> 11:09.590
I should be checking that, but I cannot go for six month long in this course.

11:09.590 --> 11:11.510
So that is why I'm just sharing these ideas.

11:11.510 --> 11:14.120
These are something that you should be doing on your own.

11:14.390 --> 11:15.680
Let's go ahead and save this one.

11:15.680 --> 11:19.610
So we're going to go ahead and say product, dot save.

11:19.640 --> 11:20.480
There we go.

11:20.480 --> 11:28.730
And once I'm having this save, I'm going to go ahead and save validate before save because I don't

11:28.730 --> 11:31.400
want to turn on any of the flags like required or anything.

11:31.400 --> 11:34.340
So let's turn this off and this is going to take some time.

11:34.340 --> 11:36.050
So let's go ahead and await this one.

11:37.220 --> 11:39.410
Copy this and paste it up here.

11:39.410 --> 11:40.070
There we go.

11:40.340 --> 11:40.620
Okay.

11:40.640 --> 11:45.230
Now, as soon as you are going to call this method with the given all these things, this should be

11:45.230 --> 11:46.130
all good now.

11:46.580 --> 11:50.870
Okay, now let's come back up here before we hit save on this order.

11:50.870 --> 11:53.180
And this order is marked as delivered.

11:53.180 --> 11:54.980
Let's go ahead and use this method.

11:54.980 --> 11:56.420
So let's go ahead and do this.

11:56.420 --> 12:01.820
And we have to do this for every single of the item, because right now here in the order items, remember

12:01.820 --> 12:04.460
we are having just one product, but there can be more product.

12:04.610 --> 12:05.860
That's why this is Eddi.

12:05.870 --> 12:07.940
So let's go ahead and use this order item.

12:07.940 --> 12:13.370
So before I save this one, I'm going to go ahead and say, hey, order dot order items.

12:13.370 --> 12:15.230
Remember, there can be more up here.

12:15.230 --> 12:17.570
We're going to use a classic for each loop.

12:17.570 --> 12:20.450
This for each loop is going to give you each and every item.

12:20.450 --> 12:23.330
So this is going to be called as a product.

12:23.330 --> 12:29.000
So let's call this one a simple prod for the product in the items and for each of the product.

12:29.000 --> 12:33.440
Now, again, this is a database operation, so this might take some time here as well.

12:33.440 --> 12:36.950
So this is going to be a simple a sync for each of the product.

12:36.950 --> 12:38.540
Let's go ahead and do something.

12:38.540 --> 12:40.280
What is the something that you want to do?

12:40.310 --> 12:43.070
I simply want to go ahead and update my stock.

12:43.070 --> 12:45.620
So I'm going to go ahead and say update product stock.

12:45.620 --> 12:46.370
There we go.

12:46.760 --> 12:49.340
Now in the update, product stock, how we are going to work.

12:49.340 --> 12:52.700
First, give me the product ID so how you can go ahead and do this one.

12:52.700 --> 12:59.450
So this product now remember, I am inside this particular field so I can go ahead and say prod dot

12:59.450 --> 13:04.820
product to give the product ID and I can go ahead and say product quantity to give this quantity.

13:04.820 --> 13:06.260
So let's go ahead and do that.

13:06.260 --> 13:11.150
So finding this, I can go ahead and say prod dot product.

13:11.390 --> 13:17.180
This is the product ID and I can go ahead and say prod dot quantity.

13:17.690 --> 13:23.060
So product dot product will give me the ID and product quantity will give me the quantity of that.

13:23.060 --> 13:29.030
And since this is an async function itself, I'm going to go ahead and wait for this one to process

13:29.030 --> 13:30.890
all the things that it wants to do.

13:31.340 --> 13:32.600
And this is all done.

13:32.600 --> 13:33.650
Let's save this one.

13:33.650 --> 13:37.790
This obviously is going to take some time because it's going into the model and stuff and now this is

13:37.790 --> 13:38.330
saving.

13:38.330 --> 13:41.600
Now, again, as we discussed, a whole lot of things can be done.

13:41.600 --> 13:46.910
But again, I don't have six months or a year to design this entire application while placing or updating

13:46.910 --> 13:47.180
the order.

13:47.180 --> 13:51.890
We can check for each of the item, whether it is delivered, whether it is a processing on the way

13:51.890 --> 13:54.020
and all of that and do further down these things.

13:54.020 --> 13:58.700
We can also check while creating the order itself whether the stock is in the quantity or something,

13:58.700 --> 14:02.720
because this was obviously one of the requirement from the clients and we have seen that how we can

14:02.720 --> 14:05.480
go back and update some of the stocks and something like this.

14:05.480 --> 14:08.750
So I'm going to go ahead and put up a comment here as well.

14:09.620 --> 14:14.120
Updated in order.

14:15.930 --> 14:17.940
Videos later.

14:17.940 --> 14:18.810
So there we go.

14:18.810 --> 14:20.940
So I can mark this.

14:21.300 --> 14:25.890
This field was updated in order videos later.

14:25.890 --> 14:26.640
So there we go.

14:26.640 --> 14:28.230
So stock is being updated later on.

14:28.230 --> 14:32.340
But again, you get the idea that how this is being done and if any other requirement comes in, you

14:32.340 --> 14:33.810
can always go ahead and play around.

14:33.810 --> 14:35.640
So there is a lot to learn in this video.

14:35.640 --> 14:37.470
Let's go ahead and catch up in the next video.
