1 00:00:01,320 --> 00:00:04,680 All right my friends in this video we're going to take care of our last listener inside of the ticket 2 00:00:04,680 --> 00:00:05,650 service. 3 00:00:05,650 --> 00:00:10,650 So the order service at some point time is going to emit the order canceled events as soon as the ticket 4 00:00:10,650 --> 00:00:15,470 service sees that that is assigned to say that a given ticket is no longer reserved. 5 00:00:15,510 --> 00:00:20,580 So we're going to want to find a ticket inside of our tickets collection and remove the order idea property 6 00:00:20,670 --> 00:00:23,240 and say hey no longer is this ticket reserved. 7 00:00:23,250 --> 00:00:27,870 We can now make changes to it once again putting this listener together is going to be pretty easy and 8 00:00:27,870 --> 00:00:28,400 straightforward. 9 00:00:28,410 --> 00:00:31,050 It's going to be just about a total clone of the listener. 10 00:00:31,050 --> 00:00:31,960 We just wrote. 11 00:00:32,050 --> 00:00:35,250 So let's get to it right away. 12 00:00:35,360 --> 00:00:39,790 So back inside my ticket service of the listeners directory and inside there I'll make a new file of 13 00:00:40,120 --> 00:00:42,110 order canceled. 14 00:00:42,190 --> 00:00:43,300 Listener T.S. 15 00:00:46,040 --> 00:00:46,940 then at the very top. 16 00:00:46,940 --> 00:00:58,870 Once again we will get our listener base class and our order canceled event from our common module 17 00:01:03,170 --> 00:01:08,030 then export a class of order canceled listener. 18 00:01:08,180 --> 00:01:15,670 And that's going to be extending the listener base class and the generic type is going to be order canceled. 19 00:01:15,700 --> 00:01:21,370 Listener not worry cancel listener hurry cancel event. 20 00:01:21,370 --> 00:01:24,730 These terms really blend together at a certain point in time. 21 00:01:24,790 --> 00:01:26,700 That is why typescript is so fantastic here. 22 00:01:26,700 --> 00:01:29,350 It helps us catch those silly little typos. 23 00:01:29,350 --> 00:01:29,610 All right. 24 00:01:29,620 --> 00:01:31,700 So once again touch you it's going to tell us God. 25 00:01:31,740 --> 00:01:32,860 Define on message. 26 00:01:32,890 --> 00:01:35,980 Q Your name and subject let's take care a subject first. 27 00:01:35,980 --> 00:01:42,220 We'll get our subjects now from the com module our subject that we're going to listen for is the order 28 00:01:43,080 --> 00:01:51,690 or subject Scott order canceled and we'll sign the actual value of subject stock order canceled. 29 00:01:51,690 --> 00:01:53,510 Next up is our Q group name. 30 00:01:53,580 --> 00:01:59,250 Remember we had the separate file for that so we will import Q Group name at the top from Q Group name 31 00:02:01,210 --> 00:02:11,510 and then assign Q Group Name is Q Group name and then finally our async on message function that is 32 00:02:11,510 --> 00:02:21,120 going to take some data argument that is of type order canceled event at data and some message as well. 33 00:02:21,370 --> 00:02:25,210 And the message is coming from the no doubt streaming library so let's import that type at the very 34 00:02:25,210 --> 00:02:25,450 top. 35 00:02:32,460 --> 00:02:32,870 Okay. 36 00:02:32,940 --> 00:02:37,770 So inside of here as I mentioned just about identical logic to a listener we just put together of Order 37 00:02:37,770 --> 00:02:38,810 creator listener. 38 00:02:39,030 --> 00:02:45,180 We want to once again find a ticket if we can't find a ticket throw in there or then going to unset 39 00:02:45,180 --> 00:02:50,690 the order ideas property save the ticket publishing event saying that a ticket has been updated act 40 00:02:50,700 --> 00:02:52,450 the message and that's it. 41 00:02:52,580 --> 00:02:56,850 There should be pretty straightforward in order to find the ticket itself. 42 00:02:56,850 --> 00:03:03,060 We are going to import ticket from up to directories models ticket 43 00:03:05,980 --> 00:03:10,870 then down inside of on a message we'll make a new variable of ticket and I will come from a weight ticket 44 00:03:11,020 --> 00:03:14,050 dot and how we can actually find the ticket right here. 45 00:03:14,060 --> 00:03:17,680 Remember in the past it's been a really easy time finding all the relevant tickets because we could 46 00:03:17,680 --> 00:03:19,720 use find by idea or something similar. 47 00:03:19,720 --> 00:03:24,900 But we need to just keep in mind what information we actually have access to inside of this event. 48 00:03:25,300 --> 00:03:27,610 So to get a reminder of what information we have access to. 49 00:03:27,640 --> 00:03:31,870 We'll do a command click on order canceled event and that will tell us that we've got access to the 50 00:03:31,870 --> 00:03:38,110 orders I.D. we've got access to the order's version and we've also got access to the I.D. of the ticket 51 00:03:38,140 --> 00:03:40,180 that this order had reserved. 52 00:03:40,210 --> 00:03:45,070 That's how we can find the ticket inside of our tickets collection we'll reference data ticket dot I.D. 53 00:03:47,680 --> 00:03:54,900 So back over here we're going to run a ticket dot find by I.D. data that ticket dot I.D. that should 54 00:03:54,900 --> 00:03:55,920 find us the relevant ticket 55 00:03:59,070 --> 00:03:59,590 okay. 56 00:03:59,650 --> 00:04:04,300 So then after that we'll say that if we fail to find this ticket. 57 00:04:04,390 --> 00:04:10,540 So if there is no ticket then once again let's throw a new air now we'll just say something like I don't 58 00:04:10,540 --> 00:04:11,230 know. 59 00:04:11,230 --> 00:04:13,720 They get not found simple enough. 60 00:04:15,410 --> 00:04:17,470 Then after that we will make our update. 61 00:04:17,600 --> 00:04:23,120 We'll do a ticket set of the order ideas property and we want to clear this out. 62 00:04:23,120 --> 00:04:25,300 We want to say ticket not reserved anymore. 63 00:04:25,310 --> 00:04:27,800 You're going to change it back over to undefined 64 00:04:31,340 --> 00:04:33,560 we could potentially also put in NOLA right here. 65 00:04:33,710 --> 00:04:38,150 But you might recall it there are a couple of locations where we are doing kind of an existence check 66 00:04:38,150 --> 00:04:42,680 with typescript by using a question mark or an if statement or something similar that optional value 67 00:04:43,340 --> 00:04:47,000 optional values with typescript don't work terribly well with no. 68 00:04:47,000 --> 00:04:54,230 And so instead we are just gonna simply use undefined well then do it in a wait take it out save after 69 00:04:54,230 --> 00:05:01,480 that we will then publish an event saying that this ticket has been updated so backup at the top will 70 00:05:01,620 --> 00:05:04,110 once again get my ticket updated. 71 00:05:04,140 --> 00:05:09,460 Publisher from up one directory publishers ticket updated publisher 72 00:05:12,830 --> 00:05:19,830 then right after the save will do and await new ticket updated publisher. 73 00:05:19,860 --> 00:05:21,110 I'll pass in this client. 74 00:05:21,110 --> 00:05:22,460 We've been over that quite a bit now. 75 00:05:23,180 --> 00:05:27,230 We'll publish and events and once again we're gonna throw in a whole bunch of information from the ticket 76 00:05:27,230 --> 00:05:28,340 itself. 77 00:05:28,370 --> 00:05:35,440 So in total the idea of the ticket The New Order idea take care of that right away. 78 00:05:35,490 --> 00:05:40,080 That'll be a ticket order i.e. what else we've got our user I.D. 79 00:05:42,860 --> 00:05:47,940 our price our title and is that it. 80 00:05:47,950 --> 00:05:53,850 I feel like I'm missing of one property on this thing what is its version. 81 00:05:53,850 --> 00:05:54,310 There we go 82 00:05:57,460 --> 00:05:57,950 there we go. 83 00:05:57,960 --> 00:06:07,560 Very good and then finally at the bottom a message dot AK Like so s that should be at well this listener 84 00:06:07,560 --> 00:06:12,510 came together rather quickly and as I mentioned many times once we do something one time in the course 85 00:06:12,750 --> 00:06:18,510 hopefully all following times get a little bit faster so we should probably write out a quick test or 86 00:06:18,510 --> 00:06:20,190 two just to make sure that this thing works. 87 00:06:20,190 --> 00:06:23,130 I know testing these listeners has been pretty darn repetitive. 88 00:06:23,130 --> 00:06:25,080 It's really just the same thing again and again. 89 00:06:25,530 --> 00:06:30,780 But we've also seen several times where those tests help us catch a lot of different errors as well. 90 00:06:30,780 --> 00:06:34,800 Specifically a lot of errors coming from TypeScript and whatnot but nonetheless it helps helped us catch 91 00:06:34,800 --> 00:06:35,640 those. 92 00:06:35,700 --> 00:06:40,230 So even though I know the tests are a little bit tedious I really do think they are absolutely worth 93 00:06:40,230 --> 00:06:40,610 it. 94 00:06:40,620 --> 00:06:45,090 They also have to help us capture and test out some kind of corner cases as well. 95 00:06:45,090 --> 00:06:46,280 So do a quick pause right here. 96 00:06:46,290 --> 00:06:50,460 Come back next video and we're gonna write out lightning quick a quick test around this listener.