Onto our next step where we're going to calculate an expiration date for this order.
Doug said in a lot of diagrams that we're gonna say that the order expires after 15 minutes.
Again doesn't really matter what the expiration time is it can be 15 minutes 15 seconds no difference
for this step.
All we really have to do right here is create a javascript Date object.
If we go back over to our order model file you'll recall that we said that when we create a new order
we were going to provide a date object.
That's a plain javascript Date object nothing fancy about it.
So to create an expiration date we'll say expiration is a new Date object when we create a new Date
object.
It'll be given a date value or a time essentially of.
Right now this instant in time.
So we now want to take a look at this expiration or this object we just created and we want to advance
it forward.
We want to point it say 15 minutes into the future or 15 seconds or whatever it is to do so we can write
out expiration date set seconds and then we will pass an expiration date get seconds plus some number
of seconds to add on.
So if you want this thing to expire 15 seconds in the future we'll put in just 15 if you want to this
to expire 15 minutes in the future.
We would put in 15 times 60 and that would be 15 minutes and we probably do not want to bury a very
important configuration variable like this inside of some line and set outside of some hidden route
file.
So I'm going to extract this expiration time into a really big very well declared variable at the top
of the file.
So up here at the very top I'm going to create a constant and call it expiration window seconds.
So this is going to be the number of seconds before the order expires.
I'm calling it window to say this is the window of time that a user has to actually pay for this order
so for me I'm going to make this right now 15 minutes.
So I will put in the 15 times 60 like so now even writing this out as a really big concern at the top
of the file still kind of buries it inside of our application.
This is potentially a very important setting right here because it dramatically affects how our application
behaves and how long a user has to actually build some or purchase the ticket.
So you might want to extract this thing and to possibly an environment variable that would allow you
to make changes to that window of time without having to redeploy the entire application.
You could instead just update the value inside of your Cuban net is config file inside that in a directory.
Alternatively you could even get fancier than that.
You could try to save this number of seconds as a record to the database and then put together some
kind of web UI that would allow an administrator to change it on the fly.
You might even want to have some kind of per user expiration settings.
But for right now I think this is just fine.
Good enough I got to go back down and rather than doing the plus 15 by 60 I'll put an expiration window
seconds like so and I'll collapse my sidebar so you can see that whole line
down onto the last steps right here.
We're going to build up our order and save it to the database.
Well there's going to be very similar to some code that we saw when we're discussing that Mongo or Mongoose
reference builder.
Just a moment ago we're essentially going to build up a normal model will say let's make an order using
the order dot build method we can then use typescript to remember what attributes we have to provide.
So we got give a user I.D. status while the while all that stuff let's throw in the user I.D. first.
This will come from recurrent user I.D..
Remember that typescript really does not think that that current user property is defined.
So if we mouse over it it's going to tell us Oh yes.
Current user right here.
It might possibly be undefined.
And so I look at the second error that's what I was looking for.
Object is possibly undefined but remember we've been over this several times.
We're already doing a check to make sure recurrent user is defined inside that require auth middleware.
So we feel pretty much just fine saying that yeah this thing definitely has a current user properly
assigned.
So I'll put in an exclamation like so telling typescript.
Don't worry about it.
Next up we need our status.
We technically are setting a default status on the order but our actual type or our interface is expecting
us to provide a status.
So we do have to put in a status here of order status not created which is the initial status that all
orders are going to get.
Well then set that expires at.
Which is gonna be just the entire expiration object that we just created a moment ago.
And then finally the ticket that we found that's a little bit ago.
So throw in the ticket as ticket and naturally we can condense that down to just be ticket yeah.
So looks good so after a building the order will then save it to the database.
So we'll do a wait order.
Dot Save As I mentioned a little bit ago.
We don't have any infrastructure right now to emit an event saying that an order was created.
So right now we'll just skip on to a sending back response the user will do a resident status of to
a one to say that a record was created and let's send back the entire order.
All right.
Not sure yet.
We are pretty much done with this new root handler to create a new order.
However we don't really know if this stuff works yet.
So we're gonna take a pause right here.
When come back the next video we are gonna start to write out some automated tests to make sure that
all this logic works as expected.
As I mentioned at the start of this order service not required for you to write tests around the stuff.
However I really do recommend you still download the completed test that I write and run them inside
of your project just to make sure that everything is actually working as expected as debugging the stuff.
Well it starts to go a bit tricky if you're going to debug it once you're actually using it from the
react application.
So let's do a little bit of test setup in just a moment.