All right my friends in this video we're going to take care of our last listener inside of the ticket
service.
So the order service at some point time is going to emit the order canceled events as soon as the ticket
service sees that that is assigned to say that a given ticket is no longer reserved.
So we're going to want to find a ticket inside of our tickets collection and remove the order idea property
and say hey no longer is this ticket reserved.
We can now make changes to it once again putting this listener together is going to be pretty easy and
straightforward.
It's going to be just about a total clone of the listener.
We just wrote.
So let's get to it right away.
So back inside my ticket service of the listeners directory and inside there I'll make a new file of
order canceled.
Listener T.S.
then at the very top.
Once again we will get our listener base class and our order canceled event from our common module
then export a class of order canceled listener.
And that's going to be extending the listener base class and the generic type is going to be order canceled.
Listener not worry cancel listener hurry cancel event.
These terms really blend together at a certain point in time.
That is why typescript is so fantastic here.
It helps us catch those silly little typos.
All right.
So once again touch you it's going to tell us God.
Define on message.
Q Your name and subject let's take care a subject first.
We'll get our subjects now from the com module our subject that we're going to listen for is the order
or subject Scott order canceled and we'll sign the actual value of subject stock order canceled.
Next up is our Q group name.
Remember we had the separate file for that so we will import Q Group name at the top from Q Group name
and then assign Q Group Name is Q Group name and then finally our async on message function that is
going to take some data argument that is of type order canceled event at data and some message as well.
And the message is coming from the no doubt streaming library so let's import that type at the very
top.
Okay.
So inside of here as I mentioned just about identical logic to a listener we just put together of Order
creator listener.
We want to once again find a ticket if we can't find a ticket throw in there or then going to unset
the order ideas property save the ticket publishing event saying that a ticket has been updated act
the message and that's it.
There should be pretty straightforward in order to find the ticket itself.
We are going to import ticket from up to directories models ticket
then down inside of on a message we'll make a new variable of ticket and I will come from a weight ticket
dot and how we can actually find the ticket right here.
Remember in the past it's been a really easy time finding all the relevant tickets because we could
use find by idea or something similar.
But we need to just keep in mind what information we actually have access to inside of this event.
So to get a reminder of what information we have access to.
We'll do a command click on order canceled event and that will tell us that we've got access to the
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
that this order had reserved.
That's how we can find the ticket inside of our tickets collection we'll reference data ticket dot I.D.
So back over here we're going to run a ticket dot find by I.D. data that ticket dot I.D. that should
find us the relevant ticket
okay.
So then after that we'll say that if we fail to find this ticket.
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
know.
They get not found simple enough.
Then after that we will make our update.
We'll do a ticket set of the order ideas property and we want to clear this out.
We want to say ticket not reserved anymore.
You're going to change it back over to undefined
we could potentially also put in NOLA right here.
But you might recall it there are a couple of locations where we are doing kind of an existence check
with typescript by using a question mark or an if statement or something similar that optional value
optional values with typescript don't work terribly well with no.
And so instead we are just gonna simply use undefined well then do it in a wait take it out save after
that we will then publish an event saying that this ticket has been updated so backup at the top will
once again get my ticket updated.
Publisher from up one directory publishers ticket updated publisher
then right after the save will do and await new ticket updated publisher.
I'll pass in this client.
We've been over that quite a bit now.
We'll publish and events and once again we're gonna throw in a whole bunch of information from the ticket
itself.
So in total the idea of the ticket The New Order idea take care of that right away.
That'll be a ticket order i.e. what else we've got our user I.D.
our price our title and is that it.
I feel like I'm missing of one property on this thing what is its version.
There we go
there we go.
Very good and then finally at the bottom a message dot AK Like so s that should be at well this listener
came together rather quickly and as I mentioned many times once we do something one time in the course
hopefully all following times get a little bit faster so we should probably write out a quick test or
two just to make sure that this thing works.
I know testing these listeners has been pretty darn repetitive.
It's really just the same thing again and again.
But we've also seen several times where those tests help us catch a lot of different errors as well.
Specifically a lot of errors coming from TypeScript and whatnot but nonetheless it helps helped us catch
those.
So even though I know the tests are a little bit tedious I really do think they are absolutely worth
it.
They also have to help us capture and test out some kind of corner cases as well.
So do a quick pause right here.
Come back next video and we're gonna write out lightning quick a quick test around this listener.