All right let's open up our payment service.
We're going to create a new model file and we're going to make sure that we replicate these five properties
right here.
I should say that we are going to store these five properties tied to every order inside the payment
service.
So back inside my editor I got to find the payments directory I'll find the SRS folder inside there
and then I'll make a new directory called models and inside that I'll make a new file called Order.
Yes as usual at the very top I will import Mongoose from Mongoose and then we will define our three
separate interfaces.
It's our first interface will be order adders then order duck.
And that's going to be extending Mongoose dot document
and then order model.
And that's going to be extending Mongoose stock model.
This is a generic and we'll put in order duck like so these three interfaces they serve the same goal
they always have.
First one is a list of properties we have to provide when building in order.
The second one is the list of properties that an order has.
And then finally the list of properties that the model itself contains.
So this is going to list out any custom methods we add to the overall collection and so on.
Let's get to it.
We're going to go through and fill out each of these interfaces essentially using the same properties
that we just spoke about over here so whenever we create a new order we have to specify the idea of
that order as a string.
We have to specify the about version user idea and price will come back to status in just a moment.
So version is definitely to be a no user ideally a string and price will be a number as well diversion
is a number.
User I.D. is a string and price is a number and instead.
You might recall we had created that order status income inside of our common module.
That list out all the different possible statuses in order could have.
So for our status we're going to say that this must be one of the possible in some cases or one the
possible cases listed inside of our order status.
So for that at the very top we will import our order status enum from our common module and then use
that enum like so.
Next up will work on the order document.
Right now the order duck is going to look absolutely identical in nature except we're not going to list
out the idea right here.
No we only list I.D. inside the order adders to say that whenever we wanted to create an order we must
provide it provide an I.D. but when we write out the order duck the Mongoose document interface right
here that we are extending already includes or defines the idea of an I.D. property.
So we do not have to realestate down inside the order interface is it is essentially already listed
inside of that interface.
Will put inversion that is a number user I.D..
That is a string right.
That is a number and status that is of type order status.
Finally our order model.
So as usual we're just gonna say that this thing should have a build method.
That's going to take in some object of type order adders and then return an instance of an order duck
to the list out built.
This will take something of type order adders and then return something of type order duck.
Let's now start to define the actual schema itself.
We'll make a new order schema variable.
This will be a new Mongoose dot schema and then inside of here we're gonna list out all the different
properties.
And it's going to roughly be the same as a we list out inside of order duck.
Really big difference is that we're not going to include the version because remember the version property
is going to be maintained automatically by the Mongoose update if current module which we'll we will
eventually install and wire up to this document so we're going to list out user I.D. that's going to
be a string.
And remember because we are inside of a schema definition here we are using capital as string because
this is not an interface we are writing right now.
It is not typescript not required to put lowercase S. it must be capital S we also say that is required
to provide a user I.D. So required is true.
Next up is our price that is going to be a number also required and then finally a status and we'll
say that this is going to be a string and it is required as well.
You may recall that back on our order service and we defined the order model.
We used a little trick to make sure that the value of status we provided was one of the values inside
of the order enum.
We could definitely do that here as well.
But just to save some time I'm going to skip it gets there is our order schema or at least the different
properties it's going to have now as the second argument as usual we are going to define that to Jason
method just to make sure that we ever have to turn this thing to Jason send it over somewhere else.
We provide the appropriate properties.
We also want to make sure that we rename on the fly that underscore I.D. property as well so to defined
that we're going to throw in here
our to Jason section this allowed a transform method that receives the document and the return value
we'll say that rent I.D. is rent dot underscore I.D. and then we will delete red dot underscore i.e..
Next up let's define our build method.
We are skipping temporarily over that update of current plug in but we will come back to it.
So our build method will be order schema that statics that builds.
This is going to be a function that takes an argument of adders that must be of type order adders and
then inside of here we're going to return a new order on the fly.
We're going to pass through a couple of different properties to remember in particular this adders object
right here is going to have an I.D. property but when we pass it into the constructor we must rename
that thing to underscore I.D. instead
we're gonna say that underscore I.D. is adders dot underscore I.D. or should be just normal I.D..
There we go the version for this the adders dot version we're then going to have a price from our stock
price user I.D. from our start user I.D. and then what else are we missing here.
Take a look at order adders status something is just status and that's it.
So our status will be at our state status we are getting an air around order that will go away as soon
as we define the actual model itself which let's do that right now.
So order is going to be a mongoose dot model.
We're going to feed in our two generic so order DAC order model make a new collection called order and
then the order schema.
And then finally at the very bottom.
Export order.
And that's it.
Yeah I think we're good to go here.
So let's take a quick pause.
When we come back the next video we're gonna start to put together are two listeners which are going
to listen for the order created and order cancel events.
Whenever we receive those we're going to either create an order or cancel an existing order inside the
orders collection.