Creating new order
When a user places an order, the details of the order confirmed at checkout will be used to create a new order record in the database, update or create a Stripe Customer for the user, and decrease the stock quantities of products ordered.
Order model
To store the orders, we will define a Mongoose Schema for the order model that will record the customer details along with user account reference, delivery address information, payment reference, created and updated-at timestamps, and an array of ordered products where the structure of each product will be defined in a separate subschema called CartItemSchema
.
Ordered by and for customer
To record the details of the customer who the order is meant for, we will add customer_name
and customer_email
fields to the Order
schema.
mern-marketplace/server/models/order.model.js
:
customer_name: { type: String, trim: true, required: 'Name is required' }, customer_email: { type: String, trim: true, match: [/.+\@.+\..+/, 'Please fill a...