We've got a listener put together for order created.
We're not gonna repeat that same process for order canceled.
The only thing we're going to do inside the order cancel that listener is update the status of the order
that we had previously saved to canceled.
That's pretty much it.
Then whenever a user makes a request to pay an order or essentially create a charge we're going to check
to see if the order they are trying to pay for has a status of canceled or not.
And of course if it has it as a cancelled we will reject the payment.
That's pretty much it.
Let's get started on this order canceled.
Listener feedback inside my listeners directory I'll make a new file of order canceled
listener and listener is going to vary so much.
The one we just put together.
So let's just get right to it.
At the very top we'll import our order canceled event.
Our subjects enum and the listener base class from our common module will then export a new class of
order canceled.
Listener that will extend listener and plug in our order canceled event we can then set up our subject
as subject start.
Order canceled assign it a value as well.
After that we'll import our Q Group Name
from Q Group name and then assign that in the body so cougar name is you.
Group name.
As usual you know what comes next.
Async on message.
We're going to receive a data object that has type order canceled event and then our message which is
up type message.
And again let's import our message site.
Now let's take a look at what data is available to us just as a really quick reminder on the order cancel
the event object so I'll do a command click on order cancel event at the very top.
Looks like we are told the idea of the order that was cancelled.
We're also given a version and then presumably well the status is going to be cancelled.
So all we have to do is take a look in our collection of orders.
We're going to try to find some order but to give an I.D. and the appropriate version number as well
and we're going to update it status to.
That's pretty much all we have to do so inside of on message we're going to begin by making a query
and trying to find the appropriate order for that.
We of course need our order model so I will import.
Order from up to directories.
Order at the top.
Now we can find in order as a weight order.
Find one the reason we're using a find one here.
You might remember we ran into this a little bit ago when we make this query.
We want to find a record with the appropriate I.D. So the idea that is lost inside this data object
and the appropriate version as well.
Just make sure that we're processing all these events in the correct order.
Now with the order itself it doesn't really make a big difference to worry about the version too much
does remember around in order.
All we really have is the order created in the order cancelled.
That is it.
There's not really any ordering of events per say because if we fail to find the order at all that means
that well we probably have not correctly process that create order event just yet.
And if we find the right order does really matter what the version is because there's never ever going
to be an end or in any intermediate update events inside there either so we don't strictly need the
version thing this time around or we're going to include it anyways just to possibly prepare our code
for some particular new event we might introduce into our app at some point in time the future of something
like maybe order updated or something like that.
So if we ever had some kind of update event that's in the version starts to get really important.
So we're gonna write out some code here that's just going to assume possibly at some future point time
we might have the ability to update and order okay.
So far find one right here we'll try to find some appropriate I.D. of underscore I.D. and we'll come
from data I.D. and then our version will be a data version minus one once again.
We might want to extract this find one helper method into a separate function inside of our model file
itself but I'll leave that up to you if you want to go through that process.
So after we got our order.
Well then make sure that we actually did find an order so that if there is no order let's throw a new
error and just say order not found and then.
Otherwise we'll update the status of this order manually to update the status manually.
We need to import the status or the order status you name from our common module
will then do an order set of status to order status dot cancelled and we can save the order.
So I'll do an a wait order.
Dot safe naturally right after that we will act the message and that's pretty much it.
All right.
So at this point we could definitely write a test here but very much like the last listener we put together
not super necessary.
So once again we will write a test but it is 100 percent optional.
So if you do want to write the test no problem whatsoever.
We'll take a pause right here.
Come back next video.
Go through the optional test and then move on from there.