1 00:00:01,230 --> 00:00:04,820 Onto our next step where we're going to calculate an expiration date for this order. 2 00:00:04,900 --> 00:00:09,210 Doug said in a lot of diagrams that we're gonna say that the order expires after 15 minutes. 3 00:00:09,210 --> 00:00:14,070 Again doesn't really matter what the expiration time is it can be 15 minutes 15 seconds no difference 4 00:00:15,020 --> 00:00:15,670 for this step. 5 00:00:15,670 --> 00:00:18,940 All we really have to do right here is create a javascript Date object. 6 00:00:18,940 --> 00:00:23,230 If we go back over to our order model file you'll recall that we said that when we create a new order 7 00:00:23,380 --> 00:00:25,620 we were going to provide a date object. 8 00:00:25,810 --> 00:00:29,480 That's a plain javascript Date object nothing fancy about it. 9 00:00:29,500 --> 00:00:37,500 So to create an expiration date we'll say expiration is a new Date object when we create a new Date 10 00:00:37,530 --> 00:00:38,040 object. 11 00:00:38,040 --> 00:00:41,050 It'll be given a date value or a time essentially of. 12 00:00:41,130 --> 00:00:43,270 Right now this instant in time. 13 00:00:43,390 --> 00:00:48,300 So we now want to take a look at this expiration or this object we just created and we want to advance 14 00:00:48,300 --> 00:00:49,110 it forward. 15 00:00:49,110 --> 00:00:55,860 We want to point it say 15 minutes into the future or 15 seconds or whatever it is to do so we can write 16 00:00:55,860 --> 00:01:05,770 out expiration date set seconds and then we will pass an expiration date get seconds plus some number 17 00:01:05,770 --> 00:01:07,360 of seconds to add on. 18 00:01:07,360 --> 00:01:12,790 So if you want this thing to expire 15 seconds in the future we'll put in just 15 if you want to this 19 00:01:12,790 --> 00:01:15,400 to expire 15 minutes in the future. 20 00:01:15,570 --> 00:01:25,140 We would put in 15 times 60 and that would be 15 minutes and we probably do not want to bury a very 21 00:01:25,140 --> 00:01:30,030 important configuration variable like this inside of some line and set outside of some hidden route 22 00:01:30,030 --> 00:01:31,050 file. 23 00:01:31,050 --> 00:01:37,320 So I'm going to extract this expiration time into a really big very well declared variable at the top 24 00:01:37,320 --> 00:01:38,330 of the file. 25 00:01:38,400 --> 00:01:45,610 So up here at the very top I'm going to create a constant and call it expiration window seconds. 26 00:01:45,630 --> 00:01:48,920 So this is going to be the number of seconds before the order expires. 27 00:01:48,930 --> 00:01:53,790 I'm calling it window to say this is the window of time that a user has to actually pay for this order 28 00:01:54,960 --> 00:01:57,380 so for me I'm going to make this right now 15 minutes. 29 00:01:57,390 --> 00:02:05,420 So I will put in the 15 times 60 like so now even writing this out as a really big concern at the top 30 00:02:05,420 --> 00:02:08,730 of the file still kind of buries it inside of our application. 31 00:02:08,750 --> 00:02:14,390 This is potentially a very important setting right here because it dramatically affects how our application 32 00:02:14,390 --> 00:02:18,860 behaves and how long a user has to actually build some or purchase the ticket. 33 00:02:18,860 --> 00:02:23,960 So you might want to extract this thing and to possibly an environment variable that would allow you 34 00:02:23,960 --> 00:02:28,180 to make changes to that window of time without having to redeploy the entire application. 35 00:02:28,220 --> 00:02:33,470 You could instead just update the value inside of your Cuban net is config file inside that in a directory. 36 00:02:33,470 --> 00:02:35,960 Alternatively you could even get fancier than that. 37 00:02:35,960 --> 00:02:40,670 You could try to save this number of seconds as a record to the database and then put together some 38 00:02:40,670 --> 00:02:44,810 kind of web UI that would allow an administrator to change it on the fly. 39 00:02:44,810 --> 00:02:48,390 You might even want to have some kind of per user expiration settings. 40 00:02:48,500 --> 00:02:51,220 But for right now I think this is just fine. 41 00:02:51,320 --> 00:03:00,140 Good enough I got to go back down and rather than doing the plus 15 by 60 I'll put an expiration window 42 00:03:00,620 --> 00:03:04,860 seconds like so and I'll collapse my sidebar so you can see that whole line 43 00:03:08,350 --> 00:03:09,810 down onto the last steps right here. 44 00:03:09,820 --> 00:03:12,970 We're going to build up our order and save it to the database. 45 00:03:12,970 --> 00:03:17,920 Well there's going to be very similar to some code that we saw when we're discussing that Mongo or Mongoose 46 00:03:17,950 --> 00:03:19,570 reference builder. 47 00:03:19,570 --> 00:03:25,800 Just a moment ago we're essentially going to build up a normal model will say let's make an order using 48 00:03:25,800 --> 00:03:31,530 the order dot build method we can then use typescript to remember what attributes we have to provide. 49 00:03:31,530 --> 00:03:37,990 So we got give a user I.D. status while the while all that stuff let's throw in the user I.D. first. 50 00:03:38,090 --> 00:03:41,670 This will come from recurrent user I.D.. 51 00:03:41,810 --> 00:03:46,870 Remember that typescript really does not think that that current user property is defined. 52 00:03:46,880 --> 00:03:51,470 So if we mouse over it it's going to tell us Oh yes. 53 00:03:51,480 --> 00:03:52,680 Current user right here. 54 00:03:52,680 --> 00:03:55,230 It might possibly be undefined. 55 00:03:55,230 --> 00:03:57,940 And so I look at the second error that's what I was looking for. 56 00:03:57,990 --> 00:04:01,340 Object is possibly undefined but remember we've been over this several times. 57 00:04:01,390 --> 00:04:05,830 We're already doing a check to make sure recurrent user is defined inside that require auth middleware. 58 00:04:05,880 --> 00:04:10,530 So we feel pretty much just fine saying that yeah this thing definitely has a current user properly 59 00:04:10,530 --> 00:04:11,070 assigned. 60 00:04:11,180 --> 00:04:13,630 So I'll put in an exclamation like so telling typescript. 61 00:04:13,680 --> 00:04:16,430 Don't worry about it. 62 00:04:16,470 --> 00:04:17,940 Next up we need our status. 63 00:04:17,940 --> 00:04:24,600 We technically are setting a default status on the order but our actual type or our interface is expecting 64 00:04:24,600 --> 00:04:26,090 us to provide a status. 65 00:04:26,160 --> 00:04:33,400 So we do have to put in a status here of order status not created which is the initial status that all 66 00:04:33,400 --> 00:04:35,060 orders are going to get. 67 00:04:35,200 --> 00:04:36,990 Well then set that expires at. 68 00:04:37,260 --> 00:04:41,400 Which is gonna be just the entire expiration object that we just created a moment ago. 69 00:04:41,400 --> 00:04:45,650 And then finally the ticket that we found that's a little bit ago. 70 00:04:45,910 --> 00:04:53,720 So throw in the ticket as ticket and naturally we can condense that down to just be ticket yeah. 71 00:04:53,770 --> 00:04:58,730 So looks good so after a building the order will then save it to the database. 72 00:04:58,750 --> 00:05:00,500 So we'll do a wait order. 73 00:05:00,520 --> 00:05:04,700 Dot Save As I mentioned a little bit ago. 74 00:05:04,710 --> 00:05:09,020 We don't have any infrastructure right now to emit an event saying that an order was created. 75 00:05:09,030 --> 00:05:15,670 So right now we'll just skip on to a sending back response the user will do a resident status of to 76 00:05:15,670 --> 00:05:23,440 a one to say that a record was created and let's send back the entire order. 77 00:05:23,510 --> 00:05:23,950 All right. 78 00:05:24,050 --> 00:05:24,510 Not sure yet. 79 00:05:26,800 --> 00:05:30,350 We are pretty much done with this new root handler to create a new order. 80 00:05:30,400 --> 00:05:33,120 However we don't really know if this stuff works yet. 81 00:05:33,220 --> 00:05:34,500 So we're gonna take a pause right here. 82 00:05:34,530 --> 00:05:38,140 When come back the next video we are gonna start to write out some automated tests to make sure that 83 00:05:38,200 --> 00:05:40,470 all this logic works as expected. 84 00:05:40,600 --> 00:05:45,220 As I mentioned at the start of this order service not required for you to write tests around the stuff. 85 00:05:45,280 --> 00:05:50,290 However I really do recommend you still download the completed test that I write and run them inside 86 00:05:50,290 --> 00:05:56,140 of your project just to make sure that everything is actually working as expected as debugging the stuff. 87 00:05:56,140 --> 00:06:00,430 Well it starts to go a bit tricky if you're going to debug it once you're actually using it from the 88 00:06:00,490 --> 00:06:02,350 react application. 89 00:06:02,380 --> 00:06:04,710 So let's do a little bit of test setup in just a moment.