const mongoose = require('mongoose') const mongoosePaginate = require('mongoose-paginate-v2'); function image(img) { if (img) return '/uploads/images/users/' + img } const AddressesSchema = mongoose.Schema({ title : {type : String , default : null}, province: String, city: String, address: String, used : {type : Boolean , default : true}, useRate : {type : Number , default : 0}, location : { type: {type : String , default : 'Point'} , coordinates: [Number]} }) const ScoreLogSchema = mongoose.Schema({ score: Number, order_id : {type: mongoose.ObjectId, ref: 'Order'}, branchId: {type: mongoose.ObjectId, ref: 'StoreInfo'}, forWhat : { type: String, enum: ['birthDate', 'marriageDate', 'buy', 'register', 'invite', 'emailVerify'] }, action : { type: String, enum: ['increase', 'decrease'] }, }) const UserSchema = mongoose.Schema({ // mutual first_name: String, last_name: String, birth_date: Date, birthDateChange : {type : String, default : ''}, showBirthDate : {type : Boolean , default : false}, marriage_date : Date, marriageDateChange : {type : String , default : ''}, job: String, national_code: String, password: String, scope: {type: Array, default: ['user']}, branches: [{type: mongoose.ObjectId, ref: 'StoreInfo'}], token: String, currentBranch: {type: mongoose.ObjectId, ref: 'StoreInfo'}, // for user business_code: Number, mobile_number: String, invitation : Number, // discount_codes: Array // this will be an Array and will be filled while querying database, bagAmount: {type : Number , default : 0}, score: {type : Number , default : 0}, scoreLogs:[ScoreLogSchema], email: String, addresses: [AddressesSchema], registration_done: {type: Boolean, default: false}, // for admin profile_pic: {type: String, get: image}, _creator: {type: mongoose.ObjectId, ref: 'User'}, username: String, permissions: Array, branchPermission: Array, private: {type: Boolean, default: false}, deviceId : String, platform : String, deviceName : String, fcmToken : String, fromWhere :{type : String, default : 'app'}, }) UserSchema.index({'addresses.location': '2dsphere'}); UserSchema.plugin(mongoosePaginate); module.exports = mongoose.model('User', UserSchema)