Spaces:
Sleeping
Sleeping
Mohamed Abu Basith
commited on
Commit
·
3666337
1
Parent(s):
f545fdf
DB
Browse files- Database/models/user.js +102 -103
Database/models/user.js
CHANGED
@@ -2,117 +2,116 @@ const mongoose = require('mongoose');
|
|
2 |
const { Schema } = mongoose;
|
3 |
|
4 |
const userSchema = new Schema({
|
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 |
// Pre-update hook to ensure validation happens only when email or mobileNumber are updated
|
102 |
// userSchema.pre('findOneAndUpdate', function (next) {
|
103 |
-
//
|
104 |
-
//
|
105 |
|
106 |
-
//
|
107 |
-
//
|
108 |
-
//
|
109 |
-
//
|
110 |
-
//
|
111 |
|
112 |
-
//
|
113 |
-
//
|
114 |
// });
|
115 |
|
116 |
const User = mongoose.model('User', userSchema);
|
117 |
|
118 |
-
module.exports = User;
|
|
|
2 |
const { Schema } = mongoose;
|
3 |
|
4 |
const userSchema = new Schema({
|
5 |
+
unique_id: {
|
6 |
+
type: Number,
|
7 |
+
required: true,
|
8 |
+
unique: true
|
9 |
+
},
|
10 |
+
email: {
|
11 |
+
type: String,
|
12 |
+
unique: true,
|
13 |
+
sparse: true, // Indexing only if present
|
14 |
+
// validate: {
|
15 |
+
// validator: function (value) {
|
16 |
+
// // Validate only if the email field is updated
|
17 |
+
// if (this.isModified('email') && value) {
|
18 |
+
// return !this.mobileNumber;
|
19 |
+
// }
|
20 |
+
// return true;
|
21 |
+
// },
|
22 |
+
// message: 'Mobile number must be empty if email is provided.'
|
23 |
+
// }
|
24 |
+
},
|
25 |
+
username: {
|
26 |
+
type: String,
|
27 |
+
required: true
|
28 |
+
},
|
29 |
+
dateOfBirth: {
|
30 |
+
type: String
|
31 |
+
},
|
32 |
+
mobileNumber: {
|
33 |
+
type: String,
|
34 |
+
unique: true,
|
35 |
+
sparse: true, // Indexing only if present
|
36 |
+
// validate: {
|
37 |
+
// validator: function (value) {
|
38 |
+
// // Validate only if the mobileNumber field is updated
|
39 |
+
// if (this.isModified('mobileNumber') && value) {
|
40 |
+
// return !this.email;
|
41 |
+
// }
|
42 |
+
// return true;
|
43 |
+
// },
|
44 |
+
// message: 'Email must be empty if mobile number is provided.'
|
45 |
+
// }
|
46 |
+
},
|
47 |
+
password: {
|
48 |
+
type: String,
|
49 |
+
required: true
|
50 |
+
},
|
51 |
+
pushToken: {
|
52 |
+
type: String
|
53 |
+
},
|
54 |
+
token: {
|
55 |
+
type: String
|
56 |
+
},
|
57 |
+
role: {
|
58 |
+
type: String,
|
59 |
+
enum: ['user', 'admin', 'customer'],
|
60 |
+
default: 'user'
|
61 |
+
},
|
62 |
+
googleId: {
|
63 |
+
type: String
|
64 |
+
},
|
65 |
+
profilePic: {
|
66 |
+
type: String
|
67 |
+
},
|
68 |
+
address: [
|
69 |
+
{
|
70 |
+
name: {
|
71 |
+
type: String
|
72 |
+
},
|
73 |
+
userId: {
|
74 |
+
type: String
|
75 |
+
},
|
76 |
+
mobileNumber: {
|
77 |
+
type: String
|
78 |
+
},
|
79 |
+
pinCode: {
|
80 |
+
type: String
|
81 |
+
},
|
82 |
+
address: {
|
83 |
+
type: String
|
84 |
+
},
|
85 |
+
area: {
|
86 |
+
type: String
|
87 |
+
},
|
88 |
+
landMark: {
|
89 |
+
type: String
|
90 |
+
},
|
91 |
+
alterMobileNumber: {
|
92 |
+
type: String
|
93 |
+
}
|
94 |
+
}
|
95 |
+
]
|
|
|
96 |
}, {
|
97 |
+
timestamps: true // This adds `createdAt` and `updatedAt` fields
|
98 |
});
|
99 |
|
100 |
// Pre-update hook to ensure validation happens only when email or mobileNumber are updated
|
101 |
// userSchema.pre('findOneAndUpdate', function (next) {
|
102 |
+
// const update = this.getUpdate();
|
103 |
+
// const user = this._conditions;
|
104 |
|
105 |
+
// // Check if email and mobileNumber are both in the update query
|
106 |
+
// if (update.email && update.mobileNumber) {
|
107 |
+
// const error = new Error('Email and mobile number cannot both be provided.');
|
108 |
+
// return next(error); // Return error if both fields are set
|
109 |
+
// }
|
110 |
|
111 |
+
// // Proceed with the update
|
112 |
+
// next();
|
113 |
// });
|
114 |
|
115 |
const User = mongoose.model('User', userSchema);
|
116 |
|
117 |
+
module.exports = User;
|