1
00:00:00,360 --> 00:00:06,630
Great, and once we're clear on aggregation, let's set up our last model, the order one.

2
00:00:07,290 --> 00:00:13,080
And the good news is that we're pretty much in the homestretch once we're done with orders, we just

3
00:00:13,080 --> 00:00:18,000
want to make ducks, set up security and we're ready for deployment.

4
00:00:18,480 --> 00:00:23,700
So for everyone who's getting anxious or maybe tired of this project, we're almost almost there.

5
00:00:23,880 --> 00:00:26,370
Just hang with me and we'll get there in no time.

6
00:00:27,060 --> 00:00:30,930
Now, when it comes to Orosz, a big part of it is carp.

7
00:00:31,860 --> 00:00:35,420
Essentially, the items the user wants to buy, correct?

8
00:00:36,090 --> 00:00:38,070
And effectively, you've got two options.

9
00:00:38,160 --> 00:00:46,170
First of all, the card data is on the frontend and it's persistent in local storage or second option.

10
00:00:46,590 --> 00:00:49,590
We can store card data directly in a database.

11
00:00:50,250 --> 00:00:52,740
Now in this project will cover option number one.

12
00:00:53,250 --> 00:01:00,900
So we will assume that there is a frontend that is sending us all the card info and effectively in order

13
00:01:00,900 --> 00:01:01,500
to test that.

14
00:01:01,830 --> 00:01:06,270
Of course, we'll use the postman, but just to give you a visual representation.

15
00:01:06,640 --> 00:01:13,350
I'm going to be coming back to the comfrey store that we built in React and in the Yelp clone project.

16
00:01:13,680 --> 00:01:21,840
So Project Number 13, we will set up card data in a database and I'll show you how we can do that using

17
00:01:22,110 --> 00:01:23,550
express sessions.

18
00:01:24,280 --> 00:01:29,460
And like I said, we're going to keep showing you the visual representation where Daddy is following.

19
00:01:29,490 --> 00:01:37,230
Yes, there's some kind of site, various products, and I can click on any of the products.

20
00:01:37,620 --> 00:01:41,160
I can choose the quantity or consider the color added to the cart.

21
00:01:41,520 --> 00:01:48,960
There is my card and like I said, most likely, it's going to be stored in local storage.

22
00:01:49,410 --> 00:01:57,210
So if you navigate their notice vs. the card data and then once the user is ready to proceed to the

23
00:01:57,210 --> 00:01:57,780
checkout.

24
00:01:58,710 --> 00:02:00,240
This is where we do two things.

25
00:02:01,050 --> 00:02:06,900
First will communicate with Stripe because remember, we need to get that client secret.

26
00:02:07,410 --> 00:02:10,680
And second, we will set up the initial order.

27
00:02:11,520 --> 00:02:16,950
Now in this project, I'll show you something that I talked about in the Stripe project, where before

28
00:02:16,950 --> 00:02:18,630
we even communicate with Stripe.

29
00:02:19,650 --> 00:02:27,120
We want to double check wherever the data that is coming in from the front end actually makes sense.

30
00:02:27,480 --> 00:02:34,920
So we want to check whether the I.D. exists for the product and if it does exist, what is the correct

31
00:02:34,920 --> 00:02:35,400
price?

32
00:02:35,790 --> 00:02:41,400
We always, always want to double check when it comes to money, when we want to communicate with Stripe,

33
00:02:41,400 --> 00:02:43,350
and then we'll set up the order.

34
00:02:43,680 --> 00:02:49,950
And then once the payment is complete, then we'll get additional data and we'll add it to an order.

35
00:02:49,950 --> 00:02:52,770
And don't worry if some of it does not make sense.

36
00:02:52,950 --> 00:02:58,080
If you're familiar with my teachings, you know that I'll come back to the screen one too many times.

37
00:02:58,080 --> 00:03:03,960
Probably so if I'm here, if I'm in the car and if I'm the user, I go to a checkout.

38
00:03:04,440 --> 00:03:11,610
And just like in a previous project, when we communicated with Stripe to get that client secret and

39
00:03:11,610 --> 00:03:18,960
to send it back to the frontend in this case will communicate with our backend to make sure that all

40
00:03:18,960 --> 00:03:24,480
the data is correct, whether the product exists and what is the correct price and all that.

41
00:03:25,080 --> 00:03:31,290
And second, we will create an order and not just get the client secret like we did in the previous

42
00:03:31,290 --> 00:03:33,960
product will actually set up the order.

43
00:03:34,320 --> 00:03:39,750
And then once a payment is complete, then we'll get that additional data.

44
00:03:40,320 --> 00:03:46,530
So let's go back first to our project and you're looking in the lead me and we want to start like always

45
00:03:46,530 --> 00:03:47,820
with a schema.

46
00:03:48,700 --> 00:03:55,860
And we want to create order jazz in models, then we want to create the schema, we want to set up that

47
00:03:55,870 --> 00:03:59,730
shipping fee subtotal and total, all of them are going to be numbers.

48
00:04:00,220 --> 00:04:02,710
We also want to go with or items for the time being.

49
00:04:02,720 --> 00:04:04,420
It's just going to be an empty array.

50
00:04:04,750 --> 00:04:10,990
But I'll show you something really cool where we can set up a separate schema and then just reference

51
00:04:10,990 --> 00:04:13,180
that schema here in the array.

52
00:04:13,630 --> 00:04:18,910
Then we want to have a status which is going to be enum with some possible string values.

53
00:04:19,240 --> 00:04:21,519
We want to tie this to a user.

54
00:04:21,550 --> 00:04:25,900
So we want to reference the user, just like we did with some of the previous models.

55
00:04:26,440 --> 00:04:28,540
We want to set up client secret and payment.

56
00:04:29,050 --> 00:04:34,450
So this will be that initial thing that we're getting back from the stripe when we communicate.

57
00:04:34,660 --> 00:04:37,510
That's the one that we're sending back to the frontend.

58
00:04:38,140 --> 00:04:44,980
And then once everything is complete, then front then sends us back the payment I.D., which is in

59
00:04:44,980 --> 00:04:45,940
the success object.

60
00:04:46,210 --> 00:04:51,610
Also, we want to set up time stamps and eventually export the order model.

61
00:04:51,670 --> 00:04:54,130
So let's get cracking.

62
00:04:55,030 --> 00:04:56,380
We're looking for order.

63
00:04:59,000 --> 00:04:59,460
Yes.

64
00:05:00,010 --> 00:05:06,970
And always we want to start by getting the Mongoose, which is equal to acquire.

65
00:05:08,020 --> 00:05:09,880
And we're looking for a run.

66
00:05:09,970 --> 00:05:12,310
We want to create the schema of the order schema.

67
00:05:12,550 --> 00:05:15,310
And there's going to be two schemas here or a schema.

68
00:05:15,610 --> 00:05:18,820
We're looking for a mongoose then schema.

69
00:05:19,770 --> 00:05:23,390
And here we pass in packs.

70
00:05:23,520 --> 00:05:26,580
Like I said, it's going to be a no show type.

71
00:05:27,680 --> 00:05:31,520
No, and it's going to be a required, quiet and true.

72
00:05:31,820 --> 00:05:33,560
Now we want to copy and paste this sucker.

73
00:05:34,510 --> 00:05:35,130
Three times.

74
00:05:35,260 --> 00:05:36,310
One, two, three.

75
00:05:36,790 --> 00:05:39,160
And we'll just change some hours around, say, shipping.

76
00:05:40,260 --> 00:05:42,460
The next one will be subtotal.

77
00:05:43,520 --> 00:05:52,700
And as a sign of subtotal is going to be the total for all the items where we multiply the price with

78
00:05:52,700 --> 00:05:59,780
the quantity and then the total will be everything back plus shipping fee plus the sum total.

79
00:06:00,320 --> 00:06:01,850
So let's go here with total.

80
00:06:03,430 --> 00:06:06,940
And we want to set the record items again for time being.

81
00:06:08,170 --> 00:06:14,860
Just set up them the array of items, and we'll get it here, then we want to tie this to a user server

82
00:06:14,860 --> 00:06:26,790
user here, and we'll set up type remember mongoose dot schema and an object already run.

83
00:06:26,920 --> 00:06:28,330
Let's set up a reference.

84
00:06:29,320 --> 00:06:30,850
Say here, user.

85
00:06:31,600 --> 00:06:33,790
And then lastly, we want to go with required.

86
00:06:34,870 --> 00:06:41,970
And we'll say true and then two more things we want to set up client secret and payment intent I.D.

87
00:06:42,010 --> 00:06:43,060
So let's do that.

88
00:06:43,090 --> 00:06:49,030
We're going to go with client secret and I will be a type.

89
00:06:50,470 --> 00:06:53,500
String and set it up as required.

90
00:06:54,450 --> 00:06:55,200
And true.

91
00:06:55,650 --> 00:07:00,540
And we also want to do the same thing with payment intent I.D..

92
00:07:00,810 --> 00:07:04,470
However, it's not going to be required because we'll add that one later.

93
00:07:04,980 --> 00:07:05,820
So it's more.

94
00:07:06,940 --> 00:07:08,790
And let's just go here with payment.

95
00:07:09,590 --> 00:07:09,940
Thank.

96
00:07:10,940 --> 00:07:13,940
And I.D. when we want to add timestamps.

97
00:07:15,330 --> 00:07:15,660
I'm.

98
00:07:16,920 --> 00:07:18,990
Stamps and set it equal to true.

99
00:07:20,020 --> 00:07:22,180
Of course, we want to export this module.

100
00:07:22,690 --> 00:07:31,600
Module exports, I'm not an equal to a Mongoose model and I don't pass in the order as well as or schema.

101
00:07:31,810 --> 00:07:36,280
And once we have set up proper exports, there's two more things that I want to do.

102
00:07:36,910 --> 00:07:40,870
First, I want to set up status for our order.

103
00:07:41,410 --> 00:07:43,440
And again, you can place it anywhere you want.

104
00:07:43,450 --> 00:07:47,300
In my case, I'm going to place it in between the user and cart items.

105
00:07:47,320 --> 00:07:54,730
So we go with status property and no one will be equal to a type string type string.

106
00:07:54,740 --> 00:08:01,510
And in this case, we will use the numbers set type string and, you know, not as equal to our array

107
00:08:01,840 --> 00:08:05,800
start with bending and actually bending will be the default.

108
00:08:06,040 --> 00:08:10,480
So in this case, I'll say default, and that is equal to a binding one.

109
00:08:11,080 --> 00:08:15,700
Then let's add a few more value so water possibly can be failed.

110
00:08:16,420 --> 00:08:18,490
Paid, delivered.

111
00:08:20,360 --> 00:08:27,380
Delivered and also it can be cancelled, and then I want to work on carte items, and if you remember

112
00:08:27,680 --> 00:08:35,990
as far as array, one of the options we looked at, I believe that was a product, correct?

113
00:08:36,820 --> 00:08:42,789
Here we looked at type and then we set up a string and then we set up some kind of default.

114
00:08:43,240 --> 00:08:44,320
Now what's really cool?

115
00:08:44,320 --> 00:08:46,720
We can also pass here a schema.

116
00:08:47,320 --> 00:08:51,010
So when it comes to cart items, there's going to be more later.

117
00:08:51,520 --> 00:08:54,970
So each item is going to have a name, price amount and product.

118
00:08:54,970 --> 00:08:58,870
And technically we can set it up directly here in the cart items.

119
00:08:58,870 --> 00:09:07,210
But I prefer setting up as a separate schema where I go with const single cart item.

120
00:09:07,780 --> 00:09:12,460
And let's call this schema that is equal to a mouse schema.

121
00:09:12,730 --> 00:09:14,820
And here let's set up some properties.

122
00:09:14,830 --> 00:09:23,290
We'll start with the name I will be equal to you type string and required to run and copy and paste,

123
00:09:23,290 --> 00:09:25,520
and we're going to have four of them again.

124
00:09:25,630 --> 00:09:27,790
Pretty typical values for cart.

125
00:09:28,360 --> 00:09:31,810
So most likely, you're looking for the image.

126
00:09:31,840 --> 00:09:35,560
Name a price, as well as the amount.

127
00:09:36,010 --> 00:09:41,740
Now what We need to keep in mind, though, that we'll be getting this data from our database and you'll

128
00:09:41,740 --> 00:09:43,180
see that once we create the order.

129
00:09:43,630 --> 00:09:46,330
So if we navigate back, I just want to change these values around.

130
00:09:46,700 --> 00:09:49,470
This is going to be an image run.

131
00:09:49,500 --> 00:09:52,480
We want to do the same thing with price.

132
00:09:52,750 --> 00:09:54,700
Now I want to set it up as no, no.

133
00:09:56,030 --> 00:10:03,030
No, and I also want to do it here where I'll say amount that is equal to a no run.

134
00:10:03,350 --> 00:10:05,780
We want to tie this to a product.

135
00:10:06,640 --> 00:10:09,200
So not only have name, image and price.

136
00:10:09,270 --> 00:10:11,680
And, by the way, should be lowercase.

137
00:10:12,100 --> 00:10:15,490
I also want to set up an I.D., a product.

138
00:10:16,300 --> 00:10:18,670
So we do that this way when we go with product.

139
00:10:19,060 --> 00:10:22,600
And now in order to speed this up, I'll just take these lines of code.

140
00:10:23,760 --> 00:10:24,320
Scroll up.

141
00:10:24,590 --> 00:10:29,330
Copy and paste, and that instead of referencing the user, we're referencing a product.

142
00:10:29,660 --> 00:10:30,020
Correct.

143
00:10:30,560 --> 00:10:36,080
So we save all of this and once we have our schema for each single card item.

144
00:10:36,710 --> 00:10:43,190
Well, I just set it up over here and I was going to happen as we're going to be setting up the order.

145
00:10:43,700 --> 00:10:46,880
We have a nice validation for card items.

146
00:10:47,300 --> 00:10:53,780
So if any of them are going to be missing or there's going to be some hanky panky with product ready,

147
00:10:54,170 --> 00:10:59,930
then we'll get back the error and the user won't be able to submit the order.

148
00:11:00,440 --> 00:11:07,610
And with all of this in place now, I can move on and start setting up a general structure for the orders.

