We now need to take a look at the ticket that we just fetched out the database and decide whether or
not this thing has been reserved.
So to do this step right here we really have to understand what it means to be reserved.
How is a ticket reserved in general.
What does it mean to be reserved.
Well remember we spent a lot of time to talk about Mongoose and how we're going to associate an order
with a ticket.
So we spoke of a mongoose and that ref and populate feature.
We also took a look at a couple of code snippets.
So if a ticket is reserved that means that it has been associated with an order and on top that the
order document must have a status of not canceled.
So if an order is canceled the order is canceled.
It is not valid and the ticket that it is associated with is not a valid reservation anymore.
So that should mean that the ticket is available to be registered or reserved by another order.
So what we really have to write out right here we have to write out some kind of query that's going
to take a look at all the different orders inside of our order collection.
We have to find one order where the ticket is equal to the ticket that we just reserved and where its
status is anything but canceled.
That's essentially a criteria we have put a comment here just to kind of iterate all that stuff that
we need to run query to look at all orders and we want to find an order where the ticket is the ticket
we just fetched or I should say about the one we just found on the line right above and the orders status
is not cancelled so if we find an order from this that means the ticket is reserved so that's essentially
a criteria.
That's what we're trying to do right here to how we can write this query out.
Well really just comes down to doing a little bit of research with Mongoose and whatnot.
Let's just take a look at what the query is going to say.
Order is a wait order dots find one we're going to try to find one order in our order collection that
satisfies the following criteria.
We want to find one where the ticket is equal to the ticket that we just found so mongoose is going
to take this right here.
It's going to essentially pull off just the idea that ticket and then take a look at all the orders
instead of order collection and find one where the ticket is equal to the ticket we just found in the
next part is a little bit more tricky.
It's where we're going to take a look at all the statuses and make sure that we get an order status
where it is not equal to canceled they're going to ride out we're going to take a look at all the statuses
of all these different orders and we're going to add in an object here with the in operator so we've
got dollar sign in right there.
This is a special Mongo DV operator.
So this is going to cause my goal to be to take a look at all of our orders and it's going to find a
status where the status is in some set of values that we're going to write in in an array right here
so we want to try to find an order where the status is equal to order status dot created or order status
not awaiting payment or order status not paid or order status or we don't have a paid one do we.
We have a complete
I'm gonna very quickly go to the top of the file import order status from our common module and then
describe once again what's going on with this query to backup your from my command module.
I will get order status and all those areas should now go away OK.
So this guy right here does pretty much what we just wrote out inside this comment.
It's gonna try to find an order or existing inside of our database that is taking a look at the ticket
we just found and has a status that is created awaiting payment or complete if we find an order that
is referencing this ticket and has been created is awaiting payment or is complete.
That means the ticket is already reserved and the user who is making this request can not attempt to
reserve the ticket that they are trying to reserve.
A better name for this variable right here rather than order might be something like existing order
to say hey this is some existing order that someone else has already created it's now right after that
if we find some existing order and we might not actually get a result from this query.
So if we find an existing order if it's actually defined then that means that this ticket is already
reserved and we want to return early from this root handler.
So very similar to what we just did above we can say if existing order
and notice in this case that we're not putting on the actual exclamation request one had an exclamation
because we wanted to handle the case in which there was not a ticket.
In this case we want to say if there is an order then we want to return early and we want to send back
or throw some kind of error.
So in this case we'll throw a new bad request error let's make sure we import that at the top as well.
So at our command module once again we'll get bad.
Request air
now I've still got an error here because we are supposed to provide a message describing why this request
failed so we could put it in some kind of message and send back to the user something like ticket is
already reserved and that's it
now.
As we look at this code right here I want you to notice that.
Well this is kind of a lot of logic to stuff into a root handler.
In addition there might be other scenarios inside our application specifically inside this order service
where we want to figure out whether or not a ticket has been reserved.
So we're gonna take a quick pause right here.
When connect the next video we're going to take this logic and reflate factor it out to a separate file
because as you will soon see there actually are some other scenarios where we want to figure out whether
or not a ticket has been reserved.
So great pause secure that in just a moment.