1 00:00:00,980 --> 00:00:03,030 We've got a listener put together for order created. 2 00:00:03,050 --> 00:00:05,630 We're not gonna repeat that same process for order canceled. 3 00:00:05,630 --> 00:00:09,660 The only thing we're going to do inside the order cancel that listener is update the status of the order 4 00:00:09,660 --> 00:00:11,870 that we had previously saved to canceled. 5 00:00:11,870 --> 00:00:13,130 That's pretty much it. 6 00:00:13,310 --> 00:00:17,690 Then whenever a user makes a request to pay an order or essentially create a charge we're going to check 7 00:00:17,690 --> 00:00:21,580 to see if the order they are trying to pay for has a status of canceled or not. 8 00:00:21,590 --> 00:00:24,670 And of course if it has it as a cancelled we will reject the payment. 9 00:00:24,690 --> 00:00:26,060 That's pretty much it. 10 00:00:26,090 --> 00:00:28,330 Let's get started on this order canceled. 11 00:00:28,340 --> 00:00:35,310 Listener feedback inside my listeners directory I'll make a new file of order canceled 12 00:00:37,880 --> 00:00:41,280 listener and listener is going to vary so much. 13 00:00:41,280 --> 00:00:42,520 The one we just put together. 14 00:00:42,660 --> 00:00:44,070 So let's just get right to it. 15 00:00:44,370 --> 00:00:50,130 At the very top we'll import our order canceled event. 16 00:00:50,200 --> 00:00:58,960 Our subjects enum and the listener base class from our common module will then export a new class of 17 00:00:58,990 --> 00:01:00,920 order canceled. 18 00:01:00,970 --> 00:01:12,110 Listener that will extend listener and plug in our order canceled event we can then set up our subject 19 00:01:12,590 --> 00:01:14,080 as subject start. 20 00:01:14,180 --> 00:01:17,460 Order canceled assign it a value as well. 21 00:01:19,050 --> 00:01:21,540 After that we'll import our Q Group Name 22 00:01:25,280 --> 00:01:34,030 from Q Group name and then assign that in the body so cougar name is you. 23 00:01:34,100 --> 00:01:36,540 Group name. 24 00:01:36,770 --> 00:01:38,610 As usual you know what comes next. 25 00:01:38,750 --> 00:01:41,030 Async on message. 26 00:01:41,030 --> 00:01:49,710 We're going to receive a data object that has type order canceled event and then our message which is 27 00:01:49,710 --> 00:01:50,860 up type message. 28 00:01:51,060 --> 00:01:52,850 And again let's import our message site. 29 00:02:03,400 --> 00:02:07,840 Now let's take a look at what data is available to us just as a really quick reminder on the order cancel 30 00:02:07,860 --> 00:02:12,030 the event object so I'll do a command click on order cancel event at the very top. 31 00:02:12,280 --> 00:02:15,370 Looks like we are told the idea of the order that was cancelled. 32 00:02:15,370 --> 00:02:19,950 We're also given a version and then presumably well the status is going to be cancelled. 33 00:02:20,170 --> 00:02:23,680 So all we have to do is take a look in our collection of orders. 34 00:02:23,770 --> 00:02:28,410 We're going to try to find some order but to give an I.D. and the appropriate version number as well 35 00:02:29,240 --> 00:02:31,200 and we're going to update it status to. 36 00:02:31,240 --> 00:02:37,860 That's pretty much all we have to do so inside of on message we're going to begin by making a query 37 00:02:37,860 --> 00:02:40,680 and trying to find the appropriate order for that. 38 00:02:40,680 --> 00:02:44,950 We of course need our order model so I will import. 39 00:02:44,950 --> 00:02:48,360 Order from up to directories. 40 00:02:48,360 --> 00:02:49,370 Order at the top. 41 00:02:53,010 --> 00:02:57,460 Now we can find in order as a weight order. 42 00:02:57,520 --> 00:02:59,930 Find one the reason we're using a find one here. 43 00:02:59,950 --> 00:03:03,110 You might remember we ran into this a little bit ago when we make this query. 44 00:03:03,130 --> 00:03:07,720 We want to find a record with the appropriate I.D. So the idea that is lost inside this data object 45 00:03:07,990 --> 00:03:09,830 and the appropriate version as well. 46 00:03:09,850 --> 00:03:13,060 Just make sure that we're processing all these events in the correct order. 47 00:03:13,120 --> 00:03:17,830 Now with the order itself it doesn't really make a big difference to worry about the version too much 48 00:03:18,060 --> 00:03:19,810 does remember around in order. 49 00:03:19,810 --> 00:03:22,520 All we really have is the order created in the order cancelled. 50 00:03:22,660 --> 00:03:23,650 That is it. 51 00:03:23,650 --> 00:03:28,840 There's not really any ordering of events per say because if we fail to find the order at all that means 52 00:03:28,840 --> 00:03:33,790 that well we probably have not correctly process that create order event just yet. 53 00:03:33,910 --> 00:03:38,920 And if we find the right order does really matter what the version is because there's never ever going 54 00:03:38,920 --> 00:03:45,220 to be an end or in any intermediate update events inside there either so we don't strictly need the 55 00:03:45,220 --> 00:03:49,540 version thing this time around or we're going to include it anyways just to possibly prepare our code 56 00:03:49,750 --> 00:03:54,370 for some particular new event we might introduce into our app at some point in time the future of something 57 00:03:54,370 --> 00:03:56,960 like maybe order updated or something like that. 58 00:03:57,000 --> 00:04:01,530 So if we ever had some kind of update event that's in the version starts to get really important. 59 00:04:01,660 --> 00:04:05,680 So we're gonna write out some code here that's just going to assume possibly at some future point time 60 00:04:05,890 --> 00:04:10,570 we might have the ability to update and order okay. 61 00:04:10,680 --> 00:04:15,900 So far find one right here we'll try to find some appropriate I.D. of underscore I.D. and we'll come 62 00:04:15,900 --> 00:04:23,400 from data I.D. and then our version will be a data version minus one once again. 63 00:04:23,420 --> 00:04:29,150 We might want to extract this find one helper method into a separate function inside of our model file 64 00:04:29,150 --> 00:04:34,320 itself but I'll leave that up to you if you want to go through that process. 65 00:04:34,380 --> 00:04:35,490 So after we got our order. 66 00:04:35,490 --> 00:04:39,390 Well then make sure that we actually did find an order so that if there is no order let's throw a new 67 00:04:39,390 --> 00:04:45,100 error and just say order not found and then. 68 00:04:45,110 --> 00:04:50,620 Otherwise we'll update the status of this order manually to update the status manually. 69 00:04:50,740 --> 00:04:56,680 We need to import the status or the order status you name from our common module 70 00:05:00,340 --> 00:05:09,160 will then do an order set of status to order status dot cancelled and we can save the order. 71 00:05:09,200 --> 00:05:10,530 So I'll do an a wait order. 72 00:05:10,560 --> 00:05:17,690 Dot safe naturally right after that we will act the message and that's pretty much it. 73 00:05:21,430 --> 00:05:21,760 All right. 74 00:05:21,790 --> 00:05:26,950 So at this point we could definitely write a test here but very much like the last listener we put together 75 00:05:27,370 --> 00:05:28,780 not super necessary. 76 00:05:28,870 --> 00:05:32,470 So once again we will write a test but it is 100 percent optional. 77 00:05:32,470 --> 00:05:35,040 So if you do want to write the test no problem whatsoever. 78 00:05:35,050 --> 00:05:35,940 We'll take a pause right here. 79 00:05:35,940 --> 00:05:37,000 Come back next video. 80 00:05:37,000 --> 00:05:39,490 Go through the optional test and then move on from there.