All files / bc-mongodb-implementation-demo/src/orders order.model.js

100% Statements 6/6
100% Branches 0/0
100% Functions 1/1
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 403x   3x                   3x                     7x                           3x 3x  
const mongoose = require('mongoose')
 
const orderBookSchema = new mongoose.Schema({
  bookId: { type: mongoose.Schema.Types.ObjectId, ref: 'Book', required: true },
  quantity: { type: Number, required: true },
  priceAtPurchase: {
    type: Number,
    required: true,
    min: [0, 'Price at purchase must be a positive number.'],
  },
})
 
const orderSchema = new mongoose.Schema(
  {
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
      required: true,
    },
    books: {
      type: [orderBookSchema],
      validate: {
        validator: function (books) {
          return books.length > 0
        },
        message: 'Order must contain at least one book.',
      },
    },
    status: {
      type: String,
      enum: ['pending', 'completed', 'canceled'],
      required: [true, 'Order status is required.'],
    },
  },
  { timestamps: true },
)
 
const OrderModel = mongoose.model('Order', orderSchema)
module.exports = OrderModel