1 00:00:01,520 --> 00:00:05,630 We now need to take a look at the ticket that we just fetched out the database and decide whether or 2 00:00:05,630 --> 00:00:07,350 not this thing has been reserved. 3 00:00:07,660 --> 00:00:12,560 So to do this step right here we really have to understand what it means to be reserved. 4 00:00:12,560 --> 00:00:14,470 How is a ticket reserved in general. 5 00:00:14,510 --> 00:00:16,490 What does it mean to be reserved. 6 00:00:16,490 --> 00:00:21,260 Well remember we spent a lot of time to talk about Mongoose and how we're going to associate an order 7 00:00:21,320 --> 00:00:22,100 with a ticket. 8 00:00:22,160 --> 00:00:25,000 So we spoke of a mongoose and that ref and populate feature. 9 00:00:25,010 --> 00:00:27,650 We also took a look at a couple of code snippets. 10 00:00:27,650 --> 00:00:36,380 So if a ticket is reserved that means that it has been associated with an order and on top that the 11 00:00:36,440 --> 00:00:41,110 order document must have a status of not canceled. 12 00:00:41,120 --> 00:00:43,220 So if an order is canceled the order is canceled. 13 00:00:43,220 --> 00:00:48,540 It is not valid and the ticket that it is associated with is not a valid reservation anymore. 14 00:00:48,600 --> 00:00:54,310 So that should mean that the ticket is available to be registered or reserved by another order. 15 00:00:54,310 --> 00:00:58,630 So what we really have to write out right here we have to write out some kind of query that's going 16 00:00:58,630 --> 00:01:02,050 to take a look at all the different orders inside of our order collection. 17 00:01:02,140 --> 00:01:08,290 We have to find one order where the ticket is equal to the ticket that we just reserved and where its 18 00:01:08,290 --> 00:01:11,930 status is anything but canceled. 19 00:01:12,040 --> 00:01:16,180 That's essentially a criteria we have put a comment here just to kind of iterate all that stuff that 20 00:01:16,360 --> 00:01:28,670 we need to run query to look at all orders and we want to find an order where the ticket is the ticket 21 00:01:28,700 --> 00:01:37,610 we just fetched or I should say about the one we just found on the line right above and the orders status 22 00:01:37,970 --> 00:01:50,260 is not cancelled so if we find an order from this that means the ticket is reserved so that's essentially 23 00:01:50,260 --> 00:01:50,980 a criteria. 24 00:01:51,100 --> 00:01:54,740 That's what we're trying to do right here to how we can write this query out. 25 00:01:54,870 --> 00:01:59,460 Well really just comes down to doing a little bit of research with Mongoose and whatnot. 26 00:01:59,460 --> 00:02:02,250 Let's just take a look at what the query is going to say. 27 00:02:02,260 --> 00:02:13,390 Order is a wait order dots find one we're going to try to find one order in our order collection that 28 00:02:13,390 --> 00:02:15,430 satisfies the following criteria. 29 00:02:15,430 --> 00:02:21,520 We want to find one where the ticket is equal to the ticket that we just found so mongoose is going 30 00:02:21,520 --> 00:02:22,840 to take this right here. 31 00:02:22,870 --> 00:02:26,980 It's going to essentially pull off just the idea that ticket and then take a look at all the orders 32 00:02:26,980 --> 00:02:32,160 instead of order collection and find one where the ticket is equal to the ticket we just found in the 33 00:02:32,160 --> 00:02:33,660 next part is a little bit more tricky. 34 00:02:33,660 --> 00:02:37,620 It's where we're going to take a look at all the statuses and make sure that we get an order status 35 00:02:37,620 --> 00:02:44,740 where it is not equal to canceled they're going to ride out we're going to take a look at all the statuses 36 00:02:44,860 --> 00:02:53,030 of all these different orders and we're going to add in an object here with the in operator so we've 37 00:02:53,030 --> 00:02:54,920 got dollar sign in right there. 38 00:02:54,980 --> 00:02:57,220 This is a special Mongo DV operator. 39 00:02:57,380 --> 00:03:01,340 So this is going to cause my goal to be to take a look at all of our orders and it's going to find a 40 00:03:01,340 --> 00:03:06,800 status where the status is in some set of values that we're going to write in in an array right here 41 00:03:08,530 --> 00:03:17,260 so we want to try to find an order where the status is equal to order status dot created or order status 42 00:03:17,620 --> 00:03:27,340 not awaiting payment or order status not paid or order status or we don't have a paid one do we. 43 00:03:27,340 --> 00:03:28,780 We have a complete 44 00:03:33,150 --> 00:03:37,400 I'm gonna very quickly go to the top of the file import order status from our common module and then 45 00:03:37,550 --> 00:03:41,740 describe once again what's going on with this query to backup your from my command module. 46 00:03:41,750 --> 00:03:47,930 I will get order status and all those areas should now go away OK. 47 00:03:47,980 --> 00:03:50,790 So this guy right here does pretty much what we just wrote out inside this comment. 48 00:03:50,830 --> 00:03:56,050 It's gonna try to find an order or existing inside of our database that is taking a look at the ticket 49 00:03:56,050 --> 00:04:02,500 we just found and has a status that is created awaiting payment or complete if we find an order that 50 00:04:02,500 --> 00:04:07,150 is referencing this ticket and has been created is awaiting payment or is complete. 51 00:04:07,210 --> 00:04:12,430 That means the ticket is already reserved and the user who is making this request can not attempt to 52 00:04:12,430 --> 00:04:14,830 reserve the ticket that they are trying to reserve. 53 00:04:16,440 --> 00:04:20,880 A better name for this variable right here rather than order might be something like existing order 54 00:04:21,360 --> 00:04:27,220 to say hey this is some existing order that someone else has already created it's now right after that 55 00:04:27,340 --> 00:04:31,900 if we find some existing order and we might not actually get a result from this query. 56 00:04:31,900 --> 00:04:36,760 So if we find an existing order if it's actually defined then that means that this ticket is already 57 00:04:36,760 --> 00:04:39,520 reserved and we want to return early from this root handler. 58 00:04:39,540 --> 00:04:45,700 So very similar to what we just did above we can say if existing order 59 00:04:49,540 --> 00:04:54,360 and notice in this case that we're not putting on the actual exclamation request one had an exclamation 60 00:04:54,390 --> 00:04:56,900 because we wanted to handle the case in which there was not a ticket. 61 00:04:56,940 --> 00:05:02,310 In this case we want to say if there is an order then we want to return early and we want to send back 62 00:05:02,340 --> 00:05:04,130 or throw some kind of error. 63 00:05:04,170 --> 00:05:11,860 So in this case we'll throw a new bad request error let's make sure we import that at the top as well. 64 00:05:13,060 --> 00:05:16,450 So at our command module once again we'll get bad. 65 00:05:16,450 --> 00:05:17,530 Request air 66 00:05:24,450 --> 00:05:28,590 now I've still got an error here because we are supposed to provide a message describing why this request 67 00:05:28,590 --> 00:05:33,750 failed so we could put it in some kind of message and send back to the user something like ticket is 68 00:05:33,780 --> 00:05:36,940 already reserved and that's it 69 00:05:39,940 --> 00:05:40,110 now. 70 00:05:40,120 --> 00:05:42,610 As we look at this code right here I want you to notice that. 71 00:05:42,700 --> 00:05:46,760 Well this is kind of a lot of logic to stuff into a root handler. 72 00:05:46,780 --> 00:05:51,160 In addition there might be other scenarios inside our application specifically inside this order service 73 00:05:51,360 --> 00:05:55,230 where we want to figure out whether or not a ticket has been reserved. 74 00:05:55,300 --> 00:05:56,720 So we're gonna take a quick pause right here. 75 00:05:56,740 --> 00:06:01,480 When connect the next video we're going to take this logic and reflate factor it out to a separate file 76 00:06:01,750 --> 00:06:06,130 because as you will soon see there actually are some other scenarios where we want to figure out whether 77 00:06:06,190 --> 00:06:08,220 or not a ticket has been reserved. 78 00:06:08,220 --> 00:06:10,060 So great pause secure that in just a moment.