File size: 2,834 Bytes
c2efbe6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const mongoose = require("mongoose");
const bcrypt = require('bcryptjs');

const userSchema = mongoose.Schema({
  name: { type: String, required: true },
  email: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  
  isActive: { type: Boolean, default: true },
  
  lastLogin: { type: Date },
  createdAt: { type: Date, default: Date.now },
  dateOfBirth: { type: String},
  gender: { type: String },
  country: { type: String },
  wishlist: [
    {
      productId: { type: mongoose.Schema.Types.ObjectId, ref: "Product" },
      name: String,
      price: Number,
      category: String,
      seller: String,
      stock: Number,
      image: String,
      ratings: Number,
      createdAt: { type: Date, default: Date.now }
    }
  ],
  orders: [
    {
      createdAt: { type: Date, default: Date.now },
      amount: Number,
      subtotal: Number,
      shipping: Number,
      tax: Number,
      discount: Number,
      status: String,
      estimatedDelivery: Date,
      shippingAddress: {
        fullName: String,
        email: String,
        phone: String,
        address: String,
        city: String,
        state: String,
        zipCode: String,
        country: String
      },
      paymentMethod: String,
      preferences: {
        giftWrapping: Boolean,
        expressDelivery: Boolean,
        specialInstructions: String
      },
      orderItems: [
        {
          productId: { type: mongoose.Schema.Types.ObjectId, ref: "Product" },
          name: String,
          price: Number,
          qty: Number,
          category: String,
          seller: String,
          stock: Number,
          image: String,
          ratings: Number
        }
      ],
    }
  ],
  reviews: [
    {
      product: { type: mongoose.Schema.Types.ObjectId, ref: "Product" },
      rating: { type: Number },
      comment: { type: String },
      createdAt: { type: Date, default: Date.now }
    }
  ],
  carts: [
    {
      createdAt: { type: Date, default: Date.now },
      amount: Number,
      status: String,
      cartItems: [
        {
          productId: { type: mongoose.Schema.Types.ObjectId, ref: "Product" },
          name: String,
          price: Number,
          qty: Number,
          category: String,
          seller: String,
          stock: Number,
          image: String,
          ratings: Number
        }
      ],
    }
  ],
});

userSchema.pre('save', async function(next) {
  if (!this.isModified('password')) {
    next();
  }
  
  const salt = await bcrypt.genSalt(10);
  this.password = await bcrypt.hash(this.password, salt);
});

userSchema.methods.checkPassword = async function(givenPassword) {
  return await bcrypt.compare(givenPassword, this.password);
};

const User = mongoose.model("User", userSchema);

module.exports = User;