Are events now include version numbers which is fantastic.
So we're now going to move over to our orders.
Service inside of our order service.
We will eventually wire up that update.
If current plug in inside of our order model but right now we're going to focus on making our ticket
work correctly.
So I want to make sure that we wire up that plug in to the ticket model inside of orders.
And I want to make sure that we execute all the appropriate logic inside of the ticket.
Updated listener.
So in other words I want to go through the process that we've described many times where we receive
some kind of event and then we run some kind of query to try to find a ticket with the same I.D. and
the version minus one.
So in this case version of to subtract one that gives us one and we would be looking for this version
right here.
We would then apply our updates to this record and when we save it we would want to see that version
number automatically incremented for us.
So that is what we're focused on right now.
We want to make sure the ticket updated events get wired up properly so to get started we're going to
first install that plugin into our order service and we're going to do that at the terminal so at my
terminal I'm inside of my orders service directory and I'll do an npm install Mongoose updates if current
count.
So it's pretty small should install it rather quickly.
Yeah looks good.
So now go back over to my ticket model file inside the orders.
Service is the order service.
I'll find the ticket model file and we're going to wire up the same series of steps so we've done it
back inside the ticket file inside of our ticket service.
We're going to import the plug at the top.
Wired up to the schema analysts this new version property inside of our ticket dock interface so at
the very top we will begin by importing update if current plugin from Mongoose update.
If current all then go down to my schema so here's ticket schema right after it we'll put in ticket
schema set version key and version and then ticket schema plugin update.
If current plugin like so
then finally up inside of our Ticket Talk interface we'll add in the version property again.
This is just here so we can eventually write out code like ticket dot version without having typescript
complain at us okay.
That is it.
We are now using this plugin with the ticket model inside the order service.
So now we're going to focus on making our listener work correctly.
We're gonna make sure that the ticket updated listener is going to receive these events it's going to
reach into the database and try to find a record with DCC Q and whatever the events version is minus
one.
So in this case we'd be looking for a version of one inside the database.
If we do not find some ticket we would throw an error and say hey something's wrong here we were not
able to find the appropriate ticket
so inside of our events listeners directory I'm gonna find a ticket.
Updated listener
we're then going to take a look at the query that we execute right here and we're going to adjust that
query so we're gonna make sure that the query we are on is what we just described find the ticket with
IDC Q and the appropriate version so I'm going to remove the find by I.D. we can't use that query or
that will help her to make the query anymore because now we want to make a query based on two different
criteria both the I.D. and the version number they're gonna change us to a find one.
Well then put in an object and list the filter or search criteria inside of here.
So you want to find a ticket with an I.D..
Notice how it's underscore I.D. of data.
I.D..
And a version of data dot version minus one.
If we mouse over ticket updated event right here or do a command click on it we should see that this
thing now has the version property.
If you do not see that you'll want to restart your code Ed because remember we did just update that
common module so that's where we are getting diversion from is contained inside the event now OK so
we're gonna run that query and again for the 20th time inside this course if we do not find that ticket
right there it means we must be processing events out of order.
And so an example of that would be if we are on Version 3 too early.
We would take version 3 subtract one that would give us version 2 and there's no record inside of our
database with the appropriate version.
So whoops we won't find the ticket and we would end up during an era.
Here's something it says ticket not found and that's pretty much it.
So now we can make our updates to the ticket.
We can set the new type title in price on there and then save the record.
And once we say that record that is why we just wired up this update.
If current plug in to the ticket model inside the order service.
So when we say this thing it's going to increment the version to match the version that just came in
the event.
That's pretty much it.
Let's save the thing and then to test it.
We're going to use postman with postman.
We're going to manually create a ticket and then tried to update it.
We're just gonna make sure that all of our code right now runs without any kind of error.
We don't really have anything to verify and say that it's handling concurrency correctly right here
but we'll take a look at an example of how it's going to fix that stuff in just a little bit.
Yeah.
So inside of postman
I'm going to first go to my tab where I am making a post request to API slash tickets.
I am of course going to make sure I'm authenticated really quickly as well so API users current user
yep I'm authenticated so post requests to API slash tickets and I'll put in a title a price I'm going
to send that off.
And here is my created ticket.
If I go over to scaffold really quickly I should see some event saying something around hey we have
published this event and receive the event as well.
So if you take a look at scaffold.
Really quick.
Yeah.
There we go.
Publish ticket created and received ticket created.
So now take the idea the ticket that I just made and I'm going to try to make an update to this ticket
so I'll go over to my put request tab
over on this put request tab.
I'll make sure that I am making a put request to API slash tickets and then going to put in the idea
of the record that we just created a moment ago.
So remember we can get that from the post request tab.
That idea right there.
I got through it at the very end of the URL.
I'll then make sure that there is some new price listed in here then finally I'm going to send this
off.
Let's go back over to scaffold and see how we're doing.
So it looks like I was able to successfully process the created and the updates as well.
And in theory this should now solve a lot of these different concurrency issues but it's kind of hard
to see that from the limited style of testing we have right now.
And take a quick pause.
We're gonna come back the next video and we're going to try to run that little test script I put together
again and see if there's still any disagreement between the records on our order service and the ticket
service.
Let's take a look at that in just a moment.