Spaces:
Running
Running
File size: 3,140 Bytes
97f53b4 3666337 40c5a52 898f6e2 f50648d 898f6e2 f50648d 898f6e2 97f53b4 3666337 97f53b4 3666337 97f53b4 3666337 97f53b4 3666337 97f53b4 3666337 |
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 116 117 118 119 120 121 |
const mongoose = require('mongoose');
const { Schema } = mongoose;
const userSchema = new Schema({
unique_id: {
type: Number,
required: true,
unique: true
},
email: {
type: String,
unique: true,
sparse: true, // Indexing only if present
// validate: {
// validator: function (value) {
// // Validate only if the email field is updated
// if (this.isModified('email') && value) {
// return !this.mobileNumber;
// }
// return true;
// },
// message: 'Mobile number must be empty if email is provided.'
// }
},
username: {
type: String,
required: true
},
dateOfBirth: {
type: String
},
mobileNumber: {
type: String,
unique: true,
sparse: true, // Indexing only if present
// validate: {
// validator: function (value) {
// // Validate only if the mobileNumber field is updated
// if (this.isModified('mobileNumber') && value) {
// return !this.email;
// }
// return true;
// },
// message: 'Email must be empty if mobile number is provided.'
// }
},
password: {
type: String,
required: true
},
pushToken: {
type: String
},
token: {
type: String
},
role: {
type: String,
enum: ['user', 'admin', 'customer'],
default: 'user'
},
googleId: {
type: String
},
profilePic: {
type: String
},
addresses: [{
name: {
type: String,
required: true
},
mobileNumber: {
type: String,
required: true
},
pinCode: {
type: String,
required: true
},
address: {
type: String,
required: true
},
area: {
type: String,
required: true
},
landMark: {
type: String
},
alterMobileNumber: {
type: String
},
_id: { // Add if you want unique IDs for addresses
type: mongoose.Schema.Types.ObjectId,
auto: true
}
}]
}, {
timestamps: true // This adds `createdAt` and `updatedAt` fields
});
// Pre-update hook to ensure validation happens only when email or mobileNumber are updated
// userSchema.pre('findOneAndUpdate', function (next) {
// const update = this.getUpdate();
// const user = this._conditions;
// // Check if email and mobileNumber are both in the update query
// if (update.email && update.mobileNumber) {
// const error = new Error('Email and mobile number cannot both be provided.');
// return next(error); // Return error if both fields are set
// }
// // Proceed with the update
// next();
// });
const User = mongoose.model('User', userSchema);
module.exports = User; |