2020-01-13 23:52:51 +01:00
|
|
|
const bcrypt = require('bcryptjs');
|
2018-12-24 20:31:02 +01:00
|
|
|
const mongoose = require('mongoose');
|
|
|
|
|
const validator = require('validator');
|
|
|
|
|
const jwt = require('jsonwebtoken');
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
2019-02-09 18:03:14 +01:00
|
|
|
const tools = require('../tools/general');
|
|
|
|
|
|
2020-01-20 01:48:25 +01:00
|
|
|
const { Settings } = require('../models/settings');
|
|
|
|
|
|
2019-10-14 20:31:57 +02:00
|
|
|
const shared_consts = require('../tools/shared_nodejs');
|
|
|
|
|
const queryclass = require('../classes/queryclass');
|
|
|
|
|
|
2019-02-05 03:40:22 +01:00
|
|
|
mongoose.Promise = global.Promise;
|
|
|
|
|
|
|
|
|
|
mongoose.level = "F";
|
2018-12-27 18:13:43 +01:00
|
|
|
// Resolving error Unknown modifier: $pushAll
|
2019-03-04 19:18:54 +01:00
|
|
|
mongoose.plugin(schema => {
|
|
|
|
|
schema.options.usePushEach = true
|
|
|
|
|
});
|
2018-12-24 20:31:02 +01:00
|
|
|
|
2018-12-27 20:09:40 +01:00
|
|
|
mongoose.set('debug', process.env.DEBUG);
|
|
|
|
|
|
2020-01-13 23:52:51 +01:00
|
|
|
const UserSchema = new mongoose.Schema({
|
2019-02-05 03:40:22 +01:00
|
|
|
userId: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2018-12-24 20:31:02 +01:00
|
|
|
email: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: true,
|
|
|
|
|
trim: true,
|
|
|
|
|
minlength: 1,
|
2019-10-13 20:44:05 +02:00
|
|
|
unique: false,
|
2018-12-27 18:13:43 +01:00
|
|
|
/*validate: {
|
2018-12-24 20:31:02 +01:00
|
|
|
validator: validator.isEmail,
|
|
|
|
|
message: '{VALUE} is not a valid email'
|
2018-12-27 18:13:43 +01:00
|
|
|
}*/
|
2018-12-24 20:31:02 +01:00
|
|
|
},
|
|
|
|
|
idapp: {
|
2019-10-12 23:34:32 +02:00
|
|
|
type: String,
|
2018-12-24 20:31:02 +01:00
|
|
|
required: true,
|
|
|
|
|
},
|
2020-01-13 23:52:51 +01:00
|
|
|
ind_order: {
|
|
|
|
|
type: Number
|
|
|
|
|
},
|
2018-12-24 20:31:02 +01:00
|
|
|
username: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: true,
|
|
|
|
|
trim: true,
|
|
|
|
|
minlength: 6,
|
2019-10-13 20:44:05 +02:00
|
|
|
unique: false,
|
2018-12-24 20:31:02 +01:00
|
|
|
},
|
2019-10-05 20:01:56 +02:00
|
|
|
name: {
|
|
|
|
|
type: String,
|
|
|
|
|
trim: true,
|
|
|
|
|
},
|
|
|
|
|
surname: {
|
|
|
|
|
type: String,
|
|
|
|
|
trim: true,
|
|
|
|
|
},
|
2018-12-24 20:31:02 +01:00
|
|
|
password: {
|
|
|
|
|
type: String,
|
|
|
|
|
require: true,
|
|
|
|
|
minlength: 6,
|
|
|
|
|
},
|
|
|
|
|
lang: {
|
|
|
|
|
type: String,
|
|
|
|
|
require: true,
|
|
|
|
|
},
|
|
|
|
|
linkreg: {
|
|
|
|
|
type: String,
|
2019-02-06 18:48:32 +01:00
|
|
|
required: false
|
2018-12-24 20:31:02 +01:00
|
|
|
},
|
|
|
|
|
verified_email: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
},
|
2019-12-31 00:44:53 +01:00
|
|
|
made_gift: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
},
|
2018-12-24 20:31:02 +01:00
|
|
|
tokens: [{
|
|
|
|
|
access: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: true
|
|
|
|
|
},
|
2019-02-14 19:01:41 +01:00
|
|
|
browser: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: true
|
|
|
|
|
},
|
2018-12-24 20:31:02 +01:00
|
|
|
token: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: true
|
2019-02-11 02:59:05 +01:00
|
|
|
},
|
|
|
|
|
date_login: {
|
|
|
|
|
type: Date
|
|
|
|
|
},
|
2018-12-24 20:31:02 +01:00
|
|
|
}],
|
2019-10-13 20:44:05 +02:00
|
|
|
perm: {
|
|
|
|
|
type: Number
|
|
|
|
|
},
|
2019-10-14 20:31:57 +02:00
|
|
|
ipaddr: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2019-10-13 20:44:05 +02:00
|
|
|
date_reg: {
|
|
|
|
|
type: Date,
|
2020-01-20 01:48:25 +01:00
|
|
|
},
|
|
|
|
|
date_temp_reg: {
|
|
|
|
|
type: Date,
|
2019-10-13 20:44:05 +02:00
|
|
|
},
|
2018-12-24 20:31:02 +01:00
|
|
|
date_tokenforgot: {
|
|
|
|
|
type: Date
|
|
|
|
|
},
|
|
|
|
|
tokenforgot: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2019-10-27 00:37:10 +02:00
|
|
|
lasttimeonline: {
|
|
|
|
|
type: Date
|
2019-11-04 20:30:09 +01:00
|
|
|
},
|
2019-11-21 00:18:40 +01:00
|
|
|
news_on: {
|
|
|
|
|
type: Boolean
|
|
|
|
|
},
|
2019-12-29 01:53:51 +01:00
|
|
|
aportador_solidario: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2020-01-13 23:52:51 +01:00
|
|
|
aportador_solidario_nome_completo: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
aportador_solidario_ind_order: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
2020-01-20 01:48:25 +01:00
|
|
|
note: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2019-11-04 20:30:09 +01:00
|
|
|
profile: {
|
|
|
|
|
img: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
2019-12-29 23:30:49 +01:00
|
|
|
nationality: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
|
|
|
|
intcode_cell: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
|
|
|
|
iso2_cell: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
2019-11-04 20:30:09 +01:00
|
|
|
cell: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
2019-12-31 00:44:53 +01:00
|
|
|
country_pay: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
2019-12-29 01:53:51 +01:00
|
|
|
email_paypal: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
2020-01-03 01:52:49 +01:00
|
|
|
paymenttypes: [],
|
2019-12-29 01:53:51 +01:00
|
|
|
username_telegram: {
|
|
|
|
|
type: String
|
|
|
|
|
},
|
2020-01-03 01:52:49 +01:00
|
|
|
teleg_id: {
|
|
|
|
|
type: Number
|
|
|
|
|
},
|
|
|
|
|
teleg_checkcode: {
|
|
|
|
|
type: Number
|
|
|
|
|
},
|
2020-01-03 22:02:18 +01:00
|
|
|
manage_telegram: {
|
|
|
|
|
type: Boolean
|
|
|
|
|
},
|
2019-11-04 20:30:09 +01:00
|
|
|
dateofbirth: {
|
|
|
|
|
type: Date,
|
|
|
|
|
},
|
2020-01-20 01:48:25 +01:00
|
|
|
my_dream: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
saw_zoom_presentation: {
|
|
|
|
|
type: Boolean
|
|
|
|
|
},
|
2019-11-04 20:30:09 +01:00
|
|
|
sex: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
},
|
2018-12-24 20:31:02 +01:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
UserSchema.methods.toJSON = function () {
|
2020-01-13 23:52:51 +01:00
|
|
|
const user = this;
|
|
|
|
|
const userObject = user.toObject();
|
2018-12-24 20:31:02 +01:00
|
|
|
|
2019-10-25 19:08:38 +02:00
|
|
|
return _.pick(userObject, ['_id', ...shared_consts.fieldsUserToChange()]);
|
2018-12-24 20:31:02 +01:00
|
|
|
};
|
|
|
|
|
|
2019-02-09 18:03:14 +01:00
|
|
|
UserSchema.methods.generateAuthToken = function (req) {
|
2019-02-05 18:17:44 +01:00
|
|
|
// console.log("GENERA TOKEN : ");
|
2020-01-13 23:52:51 +01:00
|
|
|
const user = this;
|
2019-02-09 18:03:14 +01:00
|
|
|
|
|
|
|
|
const useragent = req.get('User-Agent');
|
2019-12-31 00:44:53 +01:00
|
|
|
// tools.mylog("GENERATE USER-AGENT = ", useragent);
|
2019-02-09 18:03:14 +01:00
|
|
|
|
2019-10-13 20:44:05 +02:00
|
|
|
const access = 'auth';
|
2019-02-14 19:01:41 +01:00
|
|
|
const browser = useragent;
|
2019-10-13 20:44:05 +02:00
|
|
|
const token = jwt.sign({ _id: user._id.toHexString(), access }, process.env.SIGNCODE).toString();
|
|
|
|
|
const date_login = new Date();
|
2018-12-24 20:31:02 +01:00
|
|
|
|
2019-02-09 18:03:14 +01:00
|
|
|
// CANCELLA IL PRECEDENTE !
|
2019-03-04 19:18:54 +01:00
|
|
|
user.tokens = user.tokens.filter(function (tok) {
|
|
|
|
|
return (tok.access !== access) || ((tok.access === access) && (tok.browser !== browser));
|
|
|
|
|
});
|
2019-02-14 19:01:41 +01:00
|
|
|
user.tokens.push({ access, browser, token, date_login });
|
2018-12-27 18:13:43 +01:00
|
|
|
|
2019-10-27 00:37:10 +02:00
|
|
|
user.lasttimeonline = new Date();
|
|
|
|
|
|
2018-12-27 18:13:43 +01:00
|
|
|
return user.save()
|
|
|
|
|
.then(() => {
|
2019-12-31 00:44:53 +01:00
|
|
|
// console.log("TOKEN CREATO IN LOGIN : " + token);
|
2018-12-27 18:13:43 +01:00
|
|
|
return token;
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
console.log("Error", err.message);
|
|
|
|
|
});
|
2018-12-24 20:31:02 +01:00
|
|
|
};
|
|
|
|
|
|
2019-10-13 20:44:05 +02:00
|
|
|
UserSchema.statics.setPermissionsById = function (id, perm) {
|
|
|
|
|
const user = this;
|
|
|
|
|
|
|
|
|
|
return user.findByIdAndUpdate(id, { $set: { perm } }).then((user) => {
|
|
|
|
|
if (user)
|
|
|
|
|
return res.send({ code: server_constants.RIS_CODE_OK, msg: '' });
|
|
|
|
|
else
|
|
|
|
|
return res.send({ code: server_constants.RIS_CODE_ERR, msg: '' });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-13 23:52:51 +01:00
|
|
|
UserSchema.statics.isAdmin = function (perm) {
|
2019-10-13 20:44:05 +02:00
|
|
|
try {
|
2020-01-03 22:02:18 +01:00
|
|
|
return ((perm & shared_consts.Permissions.Admin) === shared_consts.Permissions.Admin);
|
2019-10-27 00:37:10 +02:00
|
|
|
} catch (e) {
|
2019-10-13 20:44:05 +02:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-03 22:02:18 +01:00
|
|
|
UserSchema.statics.isManager = function (perm) {
|
2019-10-15 20:40:31 +02:00
|
|
|
try {
|
2020-01-03 22:02:18 +01:00
|
|
|
return ((perm & shared_consts.Permissions.Manager) === shared_consts.Permissions.Manager);
|
2019-10-27 00:37:10 +02:00
|
|
|
} catch (e) {
|
2019-10-15 20:40:31 +02:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-09 18:03:14 +01:00
|
|
|
UserSchema.statics.findByToken = function (token, typeaccess) {
|
2019-10-13 20:44:05 +02:00
|
|
|
const User = this;
|
|
|
|
|
let decoded;
|
2018-12-24 20:31:02 +01:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
decoded = jwt.verify(token, process.env.SIGNCODE);
|
|
|
|
|
} catch (e) {
|
2019-02-09 18:03:14 +01:00
|
|
|
return Promise.resolve(null);
|
2018-12-24 20:31:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return User.findOne({
|
|
|
|
|
'_id': decoded._id,
|
|
|
|
|
'tokens.token': token,
|
2019-02-11 02:59:05 +01:00
|
|
|
'tokens.access': typeaccess,
|
2018-12-24 20:31:02 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-13 18:47:58 +01:00
|
|
|
UserSchema.statics.findByTokenAnyAccess = function (token) {
|
2020-01-13 23:52:51 +01:00
|
|
|
const User = this;
|
|
|
|
|
let decoded;
|
2019-02-13 18:47:58 +01:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
decoded = jwt.verify(token, process.env.SIGNCODE);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return Promise.resolve(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return User.findOne({
|
|
|
|
|
'_id': decoded._id,
|
|
|
|
|
'tokens.token': token,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-13 20:44:05 +02:00
|
|
|
UserSchema.statics.findByCredentials = function (idapp, username, password) {
|
2020-01-13 23:52:51 +01:00
|
|
|
const User = this;
|
|
|
|
|
let pwd = "";
|
2018-12-24 20:31:02 +01:00
|
|
|
|
2019-10-13 20:44:05 +02:00
|
|
|
return User.findOne({ idapp, username: username }).then((user) => {
|
2018-12-24 20:31:02 +01:00
|
|
|
if (!user) {
|
2019-03-04 19:18:54 +01:00
|
|
|
// Check if with email:
|
2020-01-27 15:07:53 +01:00
|
|
|
return User.findOne({ idapp, email: username.toLowerCase() })
|
2019-03-04 19:18:54 +01:00
|
|
|
} else {
|
|
|
|
|
return user
|
2018-12-24 20:31:02 +01:00
|
|
|
}
|
2019-03-04 19:18:54 +01:00
|
|
|
}).then(user => {
|
|
|
|
|
if (!user)
|
|
|
|
|
return null;
|
|
|
|
|
|
2018-12-24 20:31:02 +01:00
|
|
|
pwd = user.password;
|
|
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
// Use bcrypt.compare to compare password and user.password
|
2019-02-05 18:17:44 +01:00
|
|
|
// console.log("pwd1 " + password);
|
|
|
|
|
// console.log("pwd2 " + pwd);
|
2018-12-24 20:31:02 +01:00
|
|
|
bcrypt.compare(password, pwd, (err, res) => {
|
|
|
|
|
if (res) {
|
|
|
|
|
resolve(user);
|
|
|
|
|
} else {
|
|
|
|
|
return resolve(null);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2019-10-13 20:44:05 +02:00
|
|
|
UserSchema.statics.findByUsername = function (idapp, username) {
|
|
|
|
|
const User = this;
|
2018-12-24 20:31:02 +01:00
|
|
|
|
|
|
|
|
return User.findOne({
|
2019-10-13 20:44:05 +02:00
|
|
|
'idapp': idapp,
|
2018-12-24 20:31:02 +01:00
|
|
|
'username': username,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-31 00:44:53 +01:00
|
|
|
UserSchema.statics.getUserShortDataByUsername = function (idapp, username) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
return User.findOne({
|
|
|
|
|
'idapp': idapp,
|
|
|
|
|
'username': username,
|
|
|
|
|
}, {
|
|
|
|
|
username: 1,
|
|
|
|
|
name: 1,
|
|
|
|
|
surname: 1,
|
|
|
|
|
verified_email: 1,
|
2020-01-30 01:19:25 +01:00
|
|
|
'profile.teleg_id': 1,
|
|
|
|
|
'profile.saw_zoom_presentation': 1,
|
2019-12-31 00:44:53 +01:00
|
|
|
made_gift: 1,
|
|
|
|
|
email: 1,
|
|
|
|
|
date_reg: 1,
|
|
|
|
|
img: 1
|
|
|
|
|
}).then((ris) => {
|
|
|
|
|
if (!!ris) {
|
|
|
|
|
// console.log('ris', ris);
|
|
|
|
|
if (!!ris._doc)
|
|
|
|
|
return ris._doc;
|
|
|
|
|
else
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserSchema.statics.getDownlineByUsername = function (idapp, username) {
|
|
|
|
|
const User = this;
|
|
|
|
|
return User.find({
|
|
|
|
|
'idapp': idapp,
|
|
|
|
|
'aportador_solidario': username,
|
|
|
|
|
}, {
|
|
|
|
|
username: 1,
|
|
|
|
|
name: 1,
|
|
|
|
|
surname: 1,
|
|
|
|
|
verified_email: 1,
|
2020-01-30 01:19:25 +01:00
|
|
|
'profile.teleg_id': 1,
|
|
|
|
|
'profile.saw_zoom_presentation': 1,
|
2020-01-20 01:48:25 +01:00
|
|
|
made_gift: 1,
|
2019-12-31 00:44:53 +01:00
|
|
|
email: 1,
|
|
|
|
|
date_reg: 1,
|
|
|
|
|
img: 1
|
|
|
|
|
}, (err, arrrec) => {
|
|
|
|
|
return arrrec
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-30 01:19:25 +01:00
|
|
|
|
2020-01-20 01:48:25 +01:00
|
|
|
UserSchema.statics.getnumInvitatiAttivi = function (idapp, username) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
return User.count({
|
|
|
|
|
idapp,
|
|
|
|
|
aportador_solidario: username,
|
2020-01-30 01:19:25 +01:00
|
|
|
'profile.teleg_id': { $gt: 1 },
|
|
|
|
|
'profile.saw_zoom_presentation': true,
|
2020-01-20 01:48:25 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-30 01:19:25 +01:00
|
|
|
UserSchema.statics.getUsersNationalityQuery = function (idapp) {
|
|
|
|
|
const query = [
|
|
|
|
|
{
|
|
|
|
|
$match: { idapp }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$group: { _id: "$profile.nationality", count: { $sum: 1 } }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$sort: { count: -1 }
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
return query
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UserSchema.statics.getindOrderDuplicate = function (idapp) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
return User.aggregate(User.getUsersNationalityQuery(idapp))
|
|
|
|
|
.then(ris => {
|
|
|
|
|
// console.table(ris);
|
|
|
|
|
return JSON.stringify(ris);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-20 01:48:25 +01:00
|
|
|
UserSchema.statics.getnumInvitati = function (idapp, username) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
return User.count({
|
|
|
|
|
idapp,
|
|
|
|
|
aportador_solidario: username,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-24 20:31:02 +01:00
|
|
|
UserSchema.statics.findByLinkreg = function (idapp, linkreg) {
|
2020-01-13 23:52:51 +01:00
|
|
|
const User = this;
|
2018-12-24 20:31:02 +01:00
|
|
|
|
|
|
|
|
return User.findOne({
|
|
|
|
|
'linkreg': linkreg,
|
|
|
|
|
'idapp': idapp,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserSchema.statics.findByLinkTokenforgot = function (idapp, email, tokenforgot) {
|
2020-01-13 23:52:51 +01:00
|
|
|
const User = this;
|
2018-12-24 20:31:02 +01:00
|
|
|
|
|
|
|
|
return User.findOne({
|
|
|
|
|
'email': email,
|
|
|
|
|
'tokenforgot': tokenforgot,
|
2019-11-21 00:18:40 +01:00
|
|
|
'date_tokenforgot': { $gte: tools.IncDateNow(-1000 * 60 * 60 * 4) }, // 4 ore fa!
|
2018-12-24 20:31:02 +01:00
|
|
|
'idapp': idapp,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2019-10-13 20:44:05 +02:00
|
|
|
UserSchema.statics.findByEmail = function (idapp, email) {
|
2020-01-13 23:52:51 +01:00
|
|
|
const User = this;
|
2018-12-24 20:31:02 +01:00
|
|
|
|
|
|
|
|
return User.findOne({
|
2019-10-13 20:44:05 +02:00
|
|
|
'idapp': idapp,
|
2018-12-24 20:31:02 +01:00
|
|
|
'email': email,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-27 15:07:53 +01:00
|
|
|
UserSchema.statics.getLastUser = function (idapp) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
return User.findOne({ idapp }).sort({ ind_order: -1 })
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-24 20:31:02 +01:00
|
|
|
UserSchema.pre('save', function (next) {
|
2020-01-13 23:52:51 +01:00
|
|
|
const user = this;
|
2018-12-24 20:31:02 +01:00
|
|
|
|
2019-10-14 20:31:57 +02:00
|
|
|
|
2018-12-24 20:31:02 +01:00
|
|
|
/*
|
|
|
|
|
if (user.isModified('password')) {
|
|
|
|
|
bcrypt.genSalt(10, (err, salt) => {
|
|
|
|
|
bcrypt.hash(user.password, salt, (err, hash) => {
|
|
|
|
|
user.password = hash;
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
UserSchema.methods.removeToken = function (token) {
|
2019-10-13 20:44:05 +02:00
|
|
|
const user = this;
|
2018-12-24 20:31:02 +01:00
|
|
|
|
2020-01-13 23:52:51 +01:00
|
|
|
return user.updateOne({
|
2018-12-24 20:31:02 +01:00
|
|
|
$pull: {
|
2018-12-27 18:13:43 +01:00
|
|
|
tokens: { token }
|
2018-12-24 20:31:02 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-27 00:37:10 +02:00
|
|
|
UserSchema.statics.getEmailByUsername = async function (idapp, username) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
return await User.findOne({ idapp, username })
|
|
|
|
|
.then((arrrec) => {
|
|
|
|
|
return ((arrrec) ? arrrec.email : '');
|
|
|
|
|
}).catch((e) => {
|
|
|
|
|
console.error('getEmailByUsername', e);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-30 01:19:25 +01:00
|
|
|
UserSchema.statics.getAportadorSolidarioByUsername = async function (idapp, username) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
return await User.findOne({ idapp, username })
|
|
|
|
|
.then((rec) => {
|
|
|
|
|
return ((rec) ? rec.aportador_solidario : '');
|
|
|
|
|
}).catch((e) => {
|
|
|
|
|
console.error('getAportadorSolidarioByUsername', e);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-03 01:52:49 +01:00
|
|
|
UserSchema.statics.UserByIdTelegram = async function (idapp, teleg_id) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
return await User.findOne({ idapp, 'profile.teleg_id': teleg_id })
|
|
|
|
|
.then((rec) => {
|
|
|
|
|
return (!!rec) ? rec._doc : null;
|
|
|
|
|
}).catch((e) => {
|
|
|
|
|
console.error('UserExistByIdTelegram', e);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-03 22:02:18 +01:00
|
|
|
UserSchema.statics.TelegIdByUsername = async function (idapp, username) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
2020-01-20 01:48:25 +01:00
|
|
|
return await User.findOne({ idapp, username }, { 'profile.teleg_id': 1 })
|
2020-01-03 22:02:18 +01:00
|
|
|
.then((rec) => {
|
|
|
|
|
return (!!rec) ? rec.profile.teleg_id : null;
|
|
|
|
|
}).catch((e) => {
|
|
|
|
|
console.error('TelegIdByUsername', e);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-03 01:52:49 +01:00
|
|
|
UserSchema.statics.SetTelegramCheckCode = async function (idapp, username, teleg_checkcode) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
const fields_to_update = {
|
|
|
|
|
'profile.teleg_checkcode': teleg_checkcode
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return await User.findOneAndUpdate({
|
|
|
|
|
idapp,
|
|
|
|
|
username
|
|
|
|
|
}, { $set: fields_to_update }, { new: false }).then((record) => {
|
|
|
|
|
return !!record;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserSchema.statics.SetTelegramIdSuccess = async function (idapp, username, teleg_id) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
const fields_to_update = {
|
|
|
|
|
'profile.teleg_id': teleg_id,
|
|
|
|
|
'profile.teleg_checkcode': 0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return await User.findOneAndUpdate({
|
|
|
|
|
idapp,
|
|
|
|
|
username
|
|
|
|
|
}, { $set: fields_to_update }, { new: false }).then((record) => {
|
|
|
|
|
return record;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-03 22:02:18 +01:00
|
|
|
UserSchema.statics.getNameSurnameByUsername = async function (idapp, username) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
2020-01-20 01:48:25 +01:00
|
|
|
return await User.findOne({ idapp, username }, { name: 1, surname: 1 })
|
2020-01-03 22:02:18 +01:00
|
|
|
.then((rec) => {
|
|
|
|
|
return (!!rec) ? `${rec.name} ${rec.surname}` : '';
|
|
|
|
|
}).catch((e) => {
|
|
|
|
|
console.error('getNameSurnameByUsername', e);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UserSchema.statics.getusersManagers = async function (idapp) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
2020-01-20 01:48:25 +01:00
|
|
|
return await User.find({ idapp, 'profile.manage_telegram': true }, { 'profile.teleg_id': 1 })
|
2020-01-03 22:02:18 +01:00
|
|
|
.then((arrrec) => {
|
|
|
|
|
return (!!arrrec) ? arrrec : null;
|
|
|
|
|
}).catch((e) => {
|
|
|
|
|
console.error('getusersManagers', e);
|
|
|
|
|
});
|
|
|
|
|
};
|
2019-10-27 00:37:10 +02:00
|
|
|
|
2020-01-20 01:48:25 +01:00
|
|
|
UserSchema.statics.getUsersTelegALL = async function (idapp) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
return await User.find({ idapp, 'profile.teleg_id': { $gt: 0 } }, { 'profile.teleg_id': 1 })
|
|
|
|
|
.then((arrrec) => {
|
|
|
|
|
return (!!arrrec) ? arrrec : null;
|
|
|
|
|
}).catch((e) => {
|
|
|
|
|
console.error('getusersManagers', e);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserSchema.statics.isManagerByIdTeleg = async function (idapp, idtelegram) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
2020-01-27 15:07:53 +01:00
|
|
|
return await User.findOne({
|
|
|
|
|
idapp,
|
|
|
|
|
'profile.manage_telegram': true,
|
|
|
|
|
'profile.teleg_id': idtelegram
|
|
|
|
|
}, { 'profile.teleg_id': 1 })
|
|
|
|
|
.then((rec) => {
|
|
|
|
|
return (!!rec && rec.profile.teleg_id === idtelegram);
|
|
|
|
|
}).catch((e) => {
|
|
|
|
|
console.error('getusersManagers', e);
|
|
|
|
|
return false
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserSchema.statics.isAdminByIdTeleg = async function (idapp, idtelegram) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
return await User.findOne({
|
|
|
|
|
idapp,
|
|
|
|
|
'username': 'paoloar77',
|
|
|
|
|
'profile.manage_telegram': true,
|
|
|
|
|
'profile.teleg_id': idtelegram
|
|
|
|
|
}, { 'profile.teleg_id': 1 })
|
2020-01-20 01:48:25 +01:00
|
|
|
.then((rec) => {
|
|
|
|
|
return (!!rec && rec.profile.teleg_id === idtelegram);
|
|
|
|
|
}).catch((e) => {
|
|
|
|
|
console.error('getusersManagers', e);
|
|
|
|
|
return false
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-13 20:44:05 +02:00
|
|
|
UserSchema.statics.getUsersList = function (idapp) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
2019-10-27 00:37:10 +02:00
|
|
|
return User.find({ 'idapp': idapp }, {
|
|
|
|
|
username: 1,
|
|
|
|
|
name: 1,
|
|
|
|
|
surname: 1,
|
|
|
|
|
verified_email: 1,
|
2019-12-31 00:44:53 +01:00
|
|
|
made_gift: 1,
|
2019-10-27 00:37:10 +02:00
|
|
|
perm: 1,
|
|
|
|
|
email: 1,
|
|
|
|
|
date_reg: 1,
|
|
|
|
|
img: 1
|
|
|
|
|
})
|
2019-10-14 20:31:57 +02:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UserSchema.statics.getUsersListByParams = function (params) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
myclParamQuery = new queryclass.CParamsQuery(params);
|
|
|
|
|
|
|
|
|
|
const filterMatchBefore = `${ myclParamQuery.filter }`;
|
|
|
|
|
|
|
|
|
|
return User.find(
|
|
|
|
|
{ $match: filterMatchBefore },
|
|
|
|
|
{ 'idapp': idapp },
|
2019-12-31 00:44:53 +01:00
|
|
|
{
|
|
|
|
|
username: 1,
|
|
|
|
|
name: 1,
|
|
|
|
|
surname: 1,
|
|
|
|
|
verified_email: 1,
|
|
|
|
|
made_gift: 1,
|
|
|
|
|
perm: 1,
|
|
|
|
|
email: 1,
|
|
|
|
|
date_reg: 1,
|
|
|
|
|
img: 1,
|
|
|
|
|
lasttimeonline: 1,
|
|
|
|
|
news_on: 1
|
|
|
|
|
})
|
2019-10-14 20:31:57 +02:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Query blog posts by user -> paginated results and a total count.
|
|
|
|
|
* @returns {Object} Object -> `{ rows, count }`
|
|
|
|
|
*/
|
|
|
|
|
|
2019-10-28 16:01:28 +01:00
|
|
|
UserSchema.statics.getFieldsForSearch = function () {
|
2020-01-27 15:07:53 +01:00
|
|
|
return ['username', 'name', 'surname', 'email', 'profile.cell', 'profile.email_paypal', 'profile.username_telegram', 'aportador_solidario']
|
2019-10-28 16:01:28 +01:00
|
|
|
};
|
|
|
|
|
|
2019-10-20 01:21:54 +02:00
|
|
|
UserSchema.statics.executeQueryTable = function (idapp, params) {
|
2019-10-28 16:01:28 +01:00
|
|
|
params.fieldsearch = this.getFieldsForSearch();
|
2019-10-20 01:21:54 +02:00
|
|
|
return tools.executeQueryTable(this, idapp, params);
|
2019-10-13 20:44:05 +02:00
|
|
|
};
|
|
|
|
|
|
2020-01-13 23:52:51 +01:00
|
|
|
UserSchema.statics.findAllIdApp = function (idapp) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
const myfind = { idapp };
|
|
|
|
|
|
|
|
|
|
return User.find(myfind, (err, arrrec) => {
|
|
|
|
|
return arrrec
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserSchema.statics.DuplicateAllRecords = async function (idapporig, idappdest) {
|
|
|
|
|
|
|
|
|
|
return tools.DuplicateAllRecords(this, idapporig, idappdest);
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-03 01:52:49 +01:00
|
|
|
UserSchema.statics.getDashboard = async function (idapp, aportador_solidario, username) {
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
// DATA: username, name, surname, email, intcode_cell, cell
|
|
|
|
|
const dashboard = {
|
|
|
|
|
aportador: {},
|
|
|
|
|
downline: []
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Data of my Aportador
|
|
|
|
|
dashboard.aportador = await User.getUserShortDataByUsername(idapp, aportador_solidario);
|
|
|
|
|
|
|
|
|
|
// Data of my Downline
|
2020-01-30 01:19:25 +01:00
|
|
|
const arrap = await User.getDownlineByUsername(idapp, aportador_solidario);
|
|
|
|
|
dashboard.numpeople_aportador = arrap.length;
|
|
|
|
|
|
2020-01-03 01:52:49 +01:00
|
|
|
dashboard.downline = await User.getDownlineByUsername(idapp, username);
|
2020-01-30 01:19:25 +01:00
|
|
|
dashboard.downbyuser = {};
|
2020-01-03 01:52:49 +01:00
|
|
|
|
2020-01-30 01:19:25 +01:00
|
|
|
for (const down of dashboard.downline) {
|
|
|
|
|
dashboard.downbyuser[down.username] = await User.getDownlineByUsername(idapp, down.username);
|
|
|
|
|
|
|
|
|
|
for (const down2 of dashboard.downbyuser[down.username]) {
|
|
|
|
|
dashboard.downbyuser[down2.username] = await User.getDownlineByUsername(idapp, down2.username);
|
|
|
|
|
}
|
2020-01-03 01:52:49 +01:00
|
|
|
}
|
|
|
|
|
return dashboard;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-13 23:52:51 +01:00
|
|
|
UserSchema.statics.fixUsername = async function (idapp, aportador_solidario_ind_order, username) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
// Check if somewhere there is my username
|
|
|
|
|
return User.find({ idapp, aportador_solidario_ind_order }, async (err, arrrec) => {
|
|
|
|
|
if (arrrec) {
|
|
|
|
|
for (const myuser of arrrec) {
|
|
|
|
|
if (!myuser.aportador_solidario || myuser.aportador_solidario === tools.APORTADOR_NONE) {
|
|
|
|
|
myuser.aportador_solidario = username;
|
|
|
|
|
await myuser.save()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-30 01:19:25 +01:00
|
|
|
UserSchema.statics.findByCellAndNameSurname = function (idapp, cell, name, surname) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
return User.findOne({
|
|
|
|
|
'idapp': idapp,
|
|
|
|
|
'profile.cell': cell,
|
|
|
|
|
'name': name,
|
|
|
|
|
'surname': surname,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2020-01-20 01:48:25 +01:00
|
|
|
UserSchema.statics.getUsersRegistered = async function (idapp) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
const myfind = { idapp };
|
|
|
|
|
|
|
|
|
|
return await User.count(myfind);
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-27 15:07:53 +01:00
|
|
|
UserSchema.statics.getEmailNotVerified = async function (idapp) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
const myfind = { idapp, verified_email: false };
|
|
|
|
|
|
|
|
|
|
return await User.count(myfind);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserSchema.statics.getUsersTelegramAttivo = async function (idapp) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
const myfind = { idapp, 'profile.teleg_id': { $gt: 0 } };
|
|
|
|
|
|
|
|
|
|
return await User.count(myfind);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserSchema.statics.getUsersTelegramPending = async function (idapp) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
const myfind = { idapp, 'profile.teleg_checkcode': { $gt: 0 } };
|
|
|
|
|
|
|
|
|
|
return await User.count(myfind);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserSchema.statics.getUsersZoom = async function (idapp) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
const myfind = { idapp, 'profile.saw_zoom_presentation': true };
|
|
|
|
|
|
|
|
|
|
return await User.count(myfind);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserSchema.statics.getUsersDreams = async function (idapp) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
const myfind = {
|
|
|
|
|
idapp,
|
|
|
|
|
'profile.my_dream': { $exists: true },
|
|
|
|
|
"$expr": { "$gt": [{ "$strLenCP": "$profile.my_dream" }, 10] }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return await User.count(myfind);
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-20 01:48:25 +01:00
|
|
|
UserSchema.statics.getLastUsers = async function (idapp) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
const lastn = await Settings.getValDbSettings(idapp, 'SHOW_LAST_N_USERS', 5);
|
|
|
|
|
|
2020-01-27 15:07:53 +01:00
|
|
|
return await User.find({ idapp }).sort({ date_temp_reg: -1 }).limit(lastn).then((arr) => {
|
|
|
|
|
//return JSON.stringify(arr)
|
|
|
|
|
return arr
|
|
|
|
|
});
|
2020-01-20 01:48:25 +01:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserSchema.statics.checkUser = async function (idapp, username) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
return await User.findOne({ idapp, username }, {
|
|
|
|
|
verified_email: 1,
|
|
|
|
|
'profile.teleg_id': 1,
|
|
|
|
|
'profile.teleg_checkcode': 1,
|
2020-01-27 15:07:53 +01:00
|
|
|
}).then((rec) => {
|
|
|
|
|
return JSON.stringify(rec)
|
2020-01-20 01:48:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserSchema.statics.calculateStat = async function (idapp, username) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
return calcstat = {
|
|
|
|
|
numinvitati: await User.getnumInvitati(idapp, username),
|
|
|
|
|
numinvitati_attivi: await User.getnumInvitatiAttivi(idapp, username),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-30 01:19:25 +01:00
|
|
|
UserSchema.statics.getDistinctNationalityQuery = function (idapp) {
|
2020-01-27 15:07:53 +01:00
|
|
|
const query = [
|
|
|
|
|
{
|
|
|
|
|
$match: { idapp }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$group: { _id: "$profile.nationality", count: { $sum: 1 } }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$sort: { count: -1 }
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
return query
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UserSchema.statics.findAllDistinctNationality = async function (idapp) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
2020-01-30 01:19:25 +01:00
|
|
|
return User.aggregate(User.getDistinctNationalityQuery(idapp))
|
2020-01-27 15:07:53 +01:00
|
|
|
.then(ris => {
|
|
|
|
|
// console.table(ris);
|
|
|
|
|
return JSON.stringify(ris);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserSchema.statics.getUsersRegDaily = function (idapp, nrec) {
|
|
|
|
|
const query = [
|
|
|
|
|
{
|
|
|
|
|
$match: { idapp }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$group: { _id: { $dateToString: { format: "%Y-%m-%d", date: "$date_temp_reg" } }, count: { $sum: 1 } }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$sort: { _id: 1 }
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$limit: nrec
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
return query
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-20 01:48:25 +01:00
|
|
|
|
2020-01-27 15:07:53 +01:00
|
|
|
UserSchema.statics.calcRegDaily = async function (idapp) {
|
|
|
|
|
const User = this;
|
|
|
|
|
|
|
|
|
|
return User.aggregate(User.getUsersRegDaily(idapp, 30))
|
|
|
|
|
.then(ris => {
|
|
|
|
|
// console.table(ris);
|
|
|
|
|
return JSON.stringify(ris);
|
|
|
|
|
});
|
|
|
|
|
};
|
2020-01-20 01:48:25 +01:00
|
|
|
|
2020-01-03 01:52:49 +01:00
|
|
|
|
2019-10-28 16:01:28 +01:00
|
|
|
if (tools.INITDB_FIRSTIME) {
|
|
|
|
|
console.log(' createIndex User Index...');
|
|
|
|
|
// UserSchema.index({ username: 'text', name: 'text', surname: 'text', email: 'text' });
|
|
|
|
|
// UserSchema.index({ name: 'name' });
|
|
|
|
|
// UserSchema.index({ name: 1 });
|
|
|
|
|
// UserSchema.index({ surname: 1 });
|
|
|
|
|
}
|
2018-12-24 20:31:02 +01:00
|
|
|
|
2020-01-30 01:19:25 +01:00
|
|
|
UserSchema.statics.DbOp = async function (idapp, mydata) {
|
|
|
|
|
const User = this;
|
|
|
|
|
try {
|
|
|
|
|
if (mydata.dbop === 'changeCellInt') {
|
|
|
|
|
arrusers = await User.find({ 'idapp': idapp });
|
|
|
|
|
let num = 0;
|
|
|
|
|
for (const rec of arrusers) {
|
|
|
|
|
let mycell = tools.removespaces(rec.profile.intcode_cell + rec.profile.cell);
|
|
|
|
|
await User.findOneAndUpdate({ _id: rec._id }, { $set: { 'profile.cell': mycell } })
|
|
|
|
|
num++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { num };
|
|
|
|
|
|
|
|
|
|
// return await User.updateMany({ idapp }, { $set: { 'profile.cell': { $concat: ["$profile.intcode_cell", "$profile.cell"] } } })
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-14 20:31:57 +02:00
|
|
|
const User = mongoose.model('User', UserSchema);
|
2018-12-24 20:31:02 +01:00
|
|
|
|
2019-10-28 16:01:28 +01:00
|
|
|
|
2018-12-24 20:31:02 +01:00
|
|
|
class Hero {
|
|
|
|
|
constructor(name, level) {
|
|
|
|
|
this.name = name;
|
|
|
|
|
this.level = level;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Adding a method to the constructor
|
|
|
|
|
greet() {
|
|
|
|
|
return `${this.name} says hello.`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-12-27 18:13:43 +01:00
|
|
|
module.exports = { User, Hero };
|
2018-12-24 20:31:02 +01:00
|
|
|
|
|
|
|
|
|