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