const validator = require('validator'); import { ProductData as IProduct } from '../utils/interface/productInterface'; import { OrderData as IOrder } from '../utils/interface/orderInterface'; import { Schema, model, Types } from 'mongoose'; const OrderSchema = new Schema( { products: Array, time: { type: Date, required: [true, 'Please provide a date.'], validate(value: Date) { if (!validator.isDate(value)) { throw new Error('Not a date'); } }, }, shippingAddress: { country: { type: String, required: [true, 'Please provide a country.'], trim: true, }, city: { type: String, required: [true, 'Please provide a city.'], trim: true, }, address: { type: String, required: [true, 'Please provide an address.'], trim: true, }, address2: { type: String, trim: true, }, postcode: { type: String, required: [true, 'Please provide a postal code.'], }, email: { type: String, required: [true, 'Please provide an email.'], }, fullName: { type: String, required: [true, 'Please provide a name.'], }, }, totalPrice: { type: Number, required: [true, 'Please provide a total price.'], validate(value: number) { if (value < 0) { throw new Error('Total price must be a postive number'); } }, }, numberOfItems: { type: Number, required: [true, 'Please provide a total number of items.'], validate(value: number) { if (value < 0) { throw new Error('Number of items must be a postive number'); } }, }, fulfilled: { type: Boolean, default: false, }, owner: { type: Types.ObjectId, required: [true, 'Please provide an owner.'], ref: 'User', }, stripeCheckoutId: { type: String, required: [true, 'Please provide a stripe checkout id.'], }, }, { toJSON: { virtuals: true }, // So `res.json()` and other `JSON.stringify()` functions include virtuals toObject: { virtuals: true }, // So `console.log()` and other functions that use `toObject()` include virtuals } ); const Order = model('Order', OrderSchema, 'Order'); module.exports = Order;