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-06-08 13:31:05 +02:00
const { Flotta } = require ( '../models/flotta' ) ;
2020-01-20 01:48:25 +01:00
const { Settings } = require ( '../models/settings' ) ;
2020-02-19 16:09:16 +01:00
const { ListaIngresso } = require ( '../models/listaingresso' ) ;
2020-05-19 00:18:13 +02:00
const { Graduatoria } = require ( '../models/graduatoria' ) ;
2020-03-10 21:44:14 +01:00
const { Nave } = require ( '../models/nave' ) ;
2020-03-31 20:34:24 +02:00
const { NavePersistente } = require ( '../models/navepersistente' ) ;
2020-05-10 21:07:51 +02:00
// const { ExtraList } = require('../models/extralist');
2020-01-20 01:48:25 +01:00
2020-02-19 16:09:16 +01:00
const { ObjectID } = require ( 'mongodb' ) ;
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 ( {
2021-01-18 00:48:17 +01:00
userId : {
2018-12-24 20:31:02 +01:00
type : String ,
} ,
2021-01-18 00:48:17 +01:00
email : {
2019-02-14 19:01:41 +01:00
type : String ,
2021-01-18 00:48:17 +01:00
required : true ,
trim : true ,
minlength : 1 ,
unique : false ,
/ * v a l i d a t e : {
validator : validator . isEmail ,
message : '{VALUE} is not a valid email'
} * /
2019-02-14 19:01:41 +01:00
} ,
2021-01-18 00:48:17 +01:00
idapp : {
2018-12-24 20:31:02 +01:00
type : String ,
2021-01-18 00:48:17 +01:00
required : true ,
2019-02-11 02:59:05 +01:00
} ,
2021-01-18 00:48:17 +01:00
index : {
type : Number
2019-02-11 02:59:05 +01:00
} ,
2021-01-18 00:48:17 +01:00
ind _order : {
type : Number
2019-11-04 20:30:09 +01:00
} ,
2021-01-18 00:48:17 +01:00
old _order : {
type : Number
2019-12-29 23:30:49 +01:00
} ,
2021-01-18 00:48:17 +01:00
username : {
type : String ,
required : true ,
trim : true ,
minlength : 6 ,
unique : false ,
2019-12-29 23:30:49 +01:00
} ,
2021-01-18 00:48:17 +01:00
name : {
type : String ,
trim : true ,
2019-12-29 23:30:49 +01:00
} ,
2021-01-18 00:48:17 +01:00
surname : {
type : String ,
trim : true ,
2019-11-04 20:30:09 +01:00
} ,
2021-01-18 00:48:17 +01:00
password : {
type : String ,
require : true ,
minlength : 6 ,
2019-12-31 00:44:53 +01:00
} ,
2021-01-18 00:48:17 +01:00
lang : {
type : String ,
require : true ,
2019-12-29 01:53:51 +01:00
} ,
2021-01-18 00:48:17 +01:00
linkreg : {
type : String ,
required : false
} ,
verified _email : {
type : Boolean ,
2020-09-04 00:06:49 +02:00
} ,
2021-01-18 00:48:17 +01:00
made _gift : {
type : Boolean ,
2020-09-04 00:06:49 +02:00
} ,
2021-01-18 00:48:17 +01:00
tokens : [ {
access : {
type : String ,
required : true
} ,
browser : {
type : String ,
required : true
} ,
token : {
type : String ,
required : true
} ,
date _login : {
type : Date
} ,
} ] ,
perm : {
type : Number
2020-07-02 22:00:58 +02:00
} ,
2021-01-18 00:48:17 +01:00
ipaddr : {
type : String ,
2020-06-08 13:31:05 +02:00
} ,
2021-01-18 00:48:17 +01:00
date _reg : {
type : Date ,
2020-06-08 13:31:05 +02:00
} ,
2021-01-18 00:48:17 +01:00
date _deleted : {
type : Date ,
2019-12-29 01:53:51 +01:00
} ,
2021-01-18 00:48:17 +01:00
date _tokenforgot : {
type : Date
2020-01-03 01:52:49 +01:00
} ,
2021-01-18 00:48:17 +01:00
tokenforgot : {
type : String ,
2020-02-19 16:09:16 +01:00
} ,
2021-01-18 00:48:17 +01:00
lasttimeonline : {
type : Date
2020-01-03 01:52:49 +01:00
} ,
2021-01-18 00:48:17 +01:00
news _on : {
2020-01-03 22:02:18 +01:00
type : Boolean
} ,
2021-01-18 00:48:17 +01:00
aportador _solidario : { // da cancellare
type : String ,
} ,
aportador _iniziale : {
type : String ,
2019-11-04 20:30:09 +01:00
} ,
2021-01-18 00:48:17 +01:00
aportador _solidario _nome _completo : {
2020-01-20 01:48:25 +01:00
type : String ,
} ,
2021-01-18 00:48:17 +01:00
aportador _solidario _ind _order : {
2020-02-07 22:08:46 +01:00
type : Number ,
} ,
2021-01-18 00:48:17 +01:00
note : {
type : String ,
2020-01-20 01:48:25 +01:00
} ,
2021-01-18 00:48:17 +01:00
deleted : {
type : Boolean ,
default : false
2020-09-04 00:06:49 +02:00
} ,
2021-01-18 00:48:17 +01:00
sospeso : {
2020-02-19 16:09:16 +01:00
type : Boolean
} ,
2021-01-18 00:48:17 +01:00
non _voglio _imbarcarmi : {
2020-06-08 13:31:05 +02:00
type : Boolean
} ,
2021-01-18 00:48:17 +01:00
navinonpresenti : {
2020-03-10 21:44:14 +01:00
type : Boolean
} ,
2021-01-18 00:48:17 +01:00
subaccount : {
type : Boolean
2020-04-24 10:29:25 +02:00
} ,
2021-01-18 00:48:17 +01:00
cart : {
type : Object
2020-04-24 10:29:25 +02:00
} ,
2021-01-18 00:48:17 +01:00
profile : {
img : {
type : String
} ,
nationality : {
type : String
} ,
intcode _cell : {
type : String
} ,
iso2 _cell : {
type : String
} ,
cell : {
type : String
} ,
country _pay : {
type : String
} ,
email _paypal : {
type : String
} ,
payeer _id : {
type : String
} ,
advcash _id : {
type : String
} ,
revolut : {
type : String
} ,
link _payment : {
type : String
} ,
note _payment : {
type : String
} ,
paymenttypes : [ ] ,
username _telegram : {
type : String
} ,
teleg _id : {
type : Number
} ,
teleg _id _old : {
type : Number
} ,
teleg _checkcode : {
type : Number
} ,
manage _telegram : {
type : Boolean
} ,
2021-02-11 02:20:35 +01:00
resplist : {
type : Boolean
} ,
workerslist : {
type : Boolean
} ,
2021-01-18 00:48:17 +01:00
dateofbirth : {
type : Date ,
} ,
my _dream : {
type : String ,
} ,
saw _and _accepted : {
type : Number ,
} ,
saw _zoom _presentation : {
type : Boolean
} ,
ask _zoom _partecipato : {
type : Boolean
} ,
qualified : {
type : Boolean
} ,
qualified _2invitati : {
type : Boolean
} ,
special _req : {
type : Boolean
} ,
sex : {
type : Number ,
} ,
2021-04-30 01:31:12 +02:00
motivazioni : {
type : String
} ,
competenze _professionalita : {
2021-01-18 00:48:17 +01:00
type : String
} ,
2021-04-30 01:31:12 +02:00
cosa _offrire : {
2021-01-18 00:48:17 +01:00
type : String
} ,
2021-04-30 01:31:12 +02:00
cosa _ricevere : {
2021-01-18 00:48:17 +01:00
type : String
} ,
2021-04-30 01:31:12 +02:00
altre _comunicazioni : {
type : Boolean ,
} ,
come _ci _hai _conosciuto : {
2021-01-18 00:48:17 +01:00
type : Boolean ,
} ,
2021-05-10 01:50:40 +02:00
socio : {
type : Boolean ,
} ,
2021-01-18 00:48:17 +01:00
socioresidente : {
type : Boolean ,
} ,
2021-04-30 01:31:12 +02:00
consiglio : {
type : Boolean ,
} ,
2021-01-18 00:48:17 +01:00
myshares : [ {
description : { type : String } ,
rating : { type : Number } ,
} ]
2020-04-24 10:29:25 +02:00
} ,
2018-12-24 20:31:02 +01:00
2021-01-18 00:48:17 +01:00
} )
;
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-09-04 00:06:49 +02:00
UserSchema . statics . setZoomPresenza = async function ( idapp , id , presenza ) {
const User = this ;
const telegrambot = require ( '../telegram/telegrambot' ) ;
let allData = { } ;
allData . myuser = await User . getUserById ( idapp , id ) ;
if ( ! ! allData . myuser )
allData . precDataUser = await User . getInfoUser ( idapp , allData . myuser . username ) ;
return await User . findByIdAndUpdate ( id , { $set : { 'profile.saw_zoom_presentation' : presenza } } )
. then ( ( rec ) => {
if ( presenza ) {
const messaggio = tools . get _ _ ( 'ZOOM_CONFERMATO' ) ;
telegrambot . sendMsgTelegram ( rec . idapp , rec . username , messaggio ) ;
telegrambot . sendMsgTelegramToTheManagersAndZoomeri ( idapp , ` L \' utente ${ rec . name } ${ rec . surname } ( ${ rec . username } ) è stato confermato per aver visto lo Zoom di Benvenuto ` ) ;
} else {
telegrambot . sendMsgTelegramToTheManagersAndZoomeri ( idapp , ` L \' utente ${ rec . name } ${ rec . surname } ( ${ rec . username } ) è stato annullata la sua richiesta per aver visto lo Zoom di Benvenuto! (Non ci risulta) ` ) ;
}
return User . findByIdAndUpdate ( id , { $set : { 'profile.ask_zoom_partecipato' : false } } )
. then ( ( user ) => {
User . checkIfSbloccatiRequisiti ( idapp , allData , id ) ;
} ) ;
} ) ;
} ;
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
}
} ;
2021-04-30 01:31:12 +02:00
UserSchema . statics . isEditor = function ( perm ) {
2020-05-04 19:34:41 +02:00
try {
2021-04-30 01:31:12 +02:00
return ( ( perm & shared _consts . Permissions . Editor ) === shared _consts . Permissions . Editor ) ;
2020-05-04 19:34:41 +02:00
} catch ( e ) {
return false
}
} ;
2020-09-04 00:06:49 +02:00
UserSchema . statics . isZoomeri = function ( perm ) {
try {
return ( ( perm & shared _consts . Permissions . Zoomeri ) === shared _consts . Permissions . Zoomeri ) ;
} catch ( e ) {
return false
}
} ;
2021-02-03 01:33:30 +01:00
UserSchema . statics . isDepartment = function ( perm ) {
try {
return ( ( perm & shared _consts . Permissions . Zoomeri ) === shared _consts . Permissions . Department ) ;
} catch ( e ) {
return false
}
} ;
2020-04-07 14:34:29 +02:00
UserSchema . statics . isTutor = function ( perm ) {
try {
return ( ( perm & shared _consts . Permissions . Tutor ) === shared _consts . Permissions . Tutor ) ;
} catch ( e ) {
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
2020-05-04 19:34:41 +02:00
return User . findOne ( {
idapp , username : username ,
2020-06-08 13:31:05 +02:00
$or : [
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ,
{
$and : [
{ deleted : { $exists : true , $eq : true } } ,
{ subaccount : { $exists : true , $eq : true } }
]
}
] ,
2020-05-04 19:34:41 +02:00
} ) . 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-05-04 19:34:41 +02:00
return User . findOne ( {
idapp , email : username . toLowerCase ( ) ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
} )
2019-03-04 19:18:54 +01:00
} else {
2020-06-08 13:31:05 +02:00
return ! user . deleted || ( user . deleted && user . subaccount ) ? user : null
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 ) ;
}
} ) ;
} ) ;
} ) ;
} ;
2020-02-19 16:09:16 +01:00
UserSchema . statics . findByUsername = async function ( idapp , username , alsoemail ) {
2019-10-13 20:44:05 +02:00
const User = this ;
2018-12-24 20:31:02 +01:00
2020-02-19 16:09:16 +01:00
const regexusername = new RegExp ( [ "^" , username , "$" ] . join ( "" ) , "i" ) ;
2020-02-05 00:39:25 +01:00
2020-02-19 16:09:16 +01:00
return await User . findOne ( {
2019-10-13 20:44:05 +02:00
'idapp' : idapp ,
2020-02-19 16:09:16 +01:00
'username' : regexusername ,
2020-05-04 19:34:41 +02:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2020-02-19 16:09:16 +01:00
} ) . then ( async ( ris ) => {
if ( ( ! ris ) && ( alsoemail ) ) {
regexemail = new RegExp ( [ "^" , username . toLowerCase ( ) , "$" ] . join ( "" ) , "i" ) ;
2020-03-10 21:44:14 +01:00
return User . findOne ( {
2020-02-19 16:09:16 +01:00
'idapp' : idapp ,
'email' : regexemail ,
2020-05-04 19:34:41 +02:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2020-02-19 16:09:16 +01:00
} ) ;
}
return ris ;
2018-12-24 20:31:02 +01:00
} ) ;
} ;
2020-02-07 22:08:46 +01:00
UserSchema . statics . getUserShortDataByUsername = async function ( idapp , username ) {
2019-12-31 00:44:53 +01:00
const User = this ;
2020-02-07 22:08:46 +01:00
const myrec = await User . findOne ( {
2019-12-31 00:44:53 +01:00
'idapp' : idapp ,
'username' : username ,
2020-05-04 19:34:41 +02:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2019-12-31 00:44:53 +01:00
} , {
2020-06-08 13:31:05 +02:00
idapp : 1 ,
2020-03-31 20:34:24 +02:00
lang : 1 ,
2020-05-11 22:43:14 +02:00
index : 1 ,
2020-02-02 04:06:32 +01:00
ind _order : 1 ,
2019-12-31 00:44:53 +01:00
username : 1 ,
2020-05-10 21:07:51 +02:00
aportador _solidario : 1 ,
2019-12-31 00:44:53 +01:00
name : 1 ,
surname : 1 ,
2020-04-24 10:29:25 +02:00
deleted : 1 ,
sospeso : 1 ,
2019-12-31 00:44:53 +01:00
verified _email : 1 ,
2020-01-30 01:19:25 +01:00
'profile.teleg_id' : 1 ,
2020-09-15 11:49:20 +02:00
// 'profile.saw_zoom_presentation': 1,
2020-09-04 00:06:49 +02:00
'profile.ask_zoom_partecipato' : 1 ,
2020-06-08 13:31:05 +02:00
'profile.qualified' : 1 ,
'profile.qualified_2invitati' : 1 ,
2020-02-07 22:08:46 +01:00
'profile.saw_and_accepted' : 1 ,
2020-02-05 00:39:25 +01:00
'profile.email_paypal' : 1 ,
2020-09-04 00:06:49 +02:00
'profile.payeer_id' : 1 ,
'profile.advcash_id' : 1 ,
2020-07-02 22:00:58 +02:00
'profile.revolut' : 1 ,
2020-06-08 13:31:05 +02:00
'profile.link_payment' : 1 ,
'profile.note_payment' : 1 ,
2020-09-15 11:49:20 +02:00
// 'profile.my_dream': 1,
2020-02-05 00:39:25 +01:00
'profile.paymenttypes' : 1 ,
2020-03-10 21:44:14 +01:00
'profile.cell' : 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 ;
}
} ) ;
2020-02-07 22:08:46 +01:00
if ( myrec ) {
2020-02-19 16:09:16 +01:00
myrec . qualified = await User . isUserQualified7 ( idapp , myrec . username ) ;
2020-05-10 21:07:51 +02:00
myrec . numNaviEntrato = await Nave . getnumNaviByUsername ( idapp , myrec . username ) ;
myrec . numinvitati = await ListaIngresso . getnumInvitati ( idapp , myrec . username ) ;
myrec . numinvitatiattivi = await ListaIngresso . getnumInvitatiAttivi ( idapp , myrec . username ) ;
2020-02-07 22:08:46 +01:00
}
return myrec
2019-12-31 00:44:53 +01:00
} ;
2020-05-14 17:23:17 +02:00
UserSchema . statics . getDownlineByUsername = async function ( idapp , username , includemyself , onlynumber ) {
2020-05-10 21:07:51 +02:00
2020-03-10 21:44:14 +01:00
if ( username === undefined )
return null ;
2020-05-14 17:23:17 +02:00
let arrrec = await ListaIngresso . getInvitati ( idapp , username , includemyself ,
2020-05-10 21:07:51 +02:00
{
index : 1 ,
lang : 1 ,
invitante _username : 1 ,
ind _order : 1 ,
username : 1 ,
name : 1 ,
surname : 1 ,
verified _email : 1 ,
'profile.teleg_id' : 1 ,
2020-09-15 11:49:20 +02:00
// 'profile.saw_zoom_presentation': 1,
2020-09-04 00:06:49 +02:00
'profile.ask_zoom_partecipato' : 1 ,
2020-06-08 13:31:05 +02:00
'profile.qualified' : 1 ,
'profile.qualified_2invitati' : 1 ,
2020-05-10 21:07:51 +02:00
'profile.saw_and_accepted' : 1 ,
'profile.email_paypal' : 1 ,
2020-09-04 00:06:49 +02:00
'profile.payeer_id' : 1 ,
'profile.advcash_id' : 1 ,
2020-07-02 22:00:58 +02:00
'profile.revolut' : 1 ,
2020-06-08 13:31:05 +02:00
'profile.link_payment' : 1 ,
'profile.note_payment' : 1 ,
2020-09-15 11:49:20 +02:00
// 'profile.my_dream': 1,
2020-05-10 21:07:51 +02:00
'profile.paymenttypes' : 1 ,
'profile.cell' : 1 ,
made _gift : 1 ,
email : 1 ,
date _reg : 1 ,
img : 1
}
) ;
2020-05-13 01:32:27 +02:00
let myq = {
2020-05-10 21:07:51 +02:00
idapp ,
aportador _solidario : username ,
2020-05-04 19:34:41 +02:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
2020-05-13 01:32:27 +02:00
} ;
if ( ! includemyself ) {
2020-05-14 17:23:17 +02:00
myq = { ... myq , username : { $ne : username } }
2020-05-13 01:32:27 +02:00
}
// Ottieni gli invitati che ancora non hanno un'imbarco
const arrinv = await User . find ( myq ) ;
2020-02-05 00:39:25 +01:00
2020-05-10 21:07:51 +02:00
for ( const inv of arrinv ) {
if ( ! arrrec . find ( ( rec ) => rec . username === inv . username ) )
2020-05-14 17:23:17 +02:00
arrrec . push ( inv . _doc ) ;
2020-05-10 21:07:51 +02:00
}
2020-05-14 17:23:17 +02:00
const arrusername = [ ] ;
for ( const inv of arrrec ) {
2020-05-10 21:07:51 +02:00
2020-05-14 17:23:17 +02:00
const rectrovato = arrusername . find ( ( rec ) => inv . username === rec . username ) ;
if ( ! ! rectrovato ) {
rectrovato . quanti ++ ;
} else {
const myrec = { ... inv } ;
myrec . quanti = 1 ;
arrusername . push ( myrec )
}
}
arrrec = arrusername ;
if ( ! onlynumber ) {
if ( ! ! arrrec ) {
for ( const rec of arrrec ) {
rec . qualified = await User . isUserQualified7 ( idapp , rec . username ) ;
rec . numNaviEntrato = await Nave . getnumNaviByUsername ( idapp , rec . username ) ;
rec . numinvitati = await ListaIngresso . getnumInvitati ( idapp , rec . username ) ;
rec . numinvitatiattivi = await ListaIngresso . getnumInvitatiAttivi ( idapp , rec . username ) ;
}
2020-02-05 00:39:25 +01:00
}
2020-02-07 22:08:46 +01:00
}
2020-05-10 21:07:51 +02:00
return arrrec ;
2020-02-05 00:39:25 +01:00
2019-12-31 00:44:53 +01:00
} ;
2020-05-10 21:07:51 +02:00
UserSchema . statics . getQueryQualified = function ( ) {
return [
{
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} ,
{
$or : [
{
'profile.special_req' : true
} ,
{
verified _email : true ,
'profile.teleg_id' : { $gt : 1 } ,
'profile.saw_and_accepted' : shared _consts . ALL _SAW _AND _ACCEPTED ,
2020-09-15 11:49:20 +02:00
// 'profile.saw_zoom_presentation': true,
2020-07-02 22:00:58 +02:00
$or : [
2020-09-04 00:06:49 +02:00
{ 'profile.link_payment' : { $exists : true } } ,
2020-07-02 22:00:58 +02:00
{ 'profile.email_paypal' : { $exists : true } } ,
2020-09-04 00:06:49 +02:00
{ 'profile.payeer_id' : { $exists : true } } ,
{ 'profile.advcash_id' : { $exists : true } } ,
2020-07-02 22:00:58 +02:00
{ 'profile.revolut' : { $exists : true } } ,
] ,
// 'profile.paymenttypes': { "$in": ['paypal'] },
2020-05-10 21:07:51 +02:00
// $where: "this.profile.paymenttypes.length >= 1",
} ]
}
] ;
}
2020-01-30 01:19:25 +01:00
2020-02-19 16:09:16 +01:00
UserSchema . statics . isUserQualified7 = async function ( idapp , username ) {
const User = this ;
2020-03-10 21:44:14 +01:00
if ( username === undefined )
return false ;
const myquery = {
2020-02-19 16:09:16 +01:00
'idapp' : idapp ,
'username' : username ,
2020-05-10 21:07:51 +02:00
$and : User . getQueryQualified ( ) ,
2020-03-10 21:44:14 +01:00
} ;
const myrec = await User . findOne ( myquery ) ;
2020-02-19 16:09:16 +01:00
return ! ! myrec ;
} ;
2021-03-17 02:24:11 +01:00
UserSchema . statics . isUserResidente = async function ( idapp , username ) {
const User = this ;
if ( username === undefined )
return false ;
const myquery = {
'idapp' : idapp ,
'username' : username ,
} ;
const myrec = await User . findOne ( myquery ) ;
if ( ! ! myrec ) {
return myrec . profile . socioresidente ;
} else {
return false ;
}
2021-04-30 01:31:12 +02:00
} ;
UserSchema . statics . isUserConsiglio = async function ( idapp , username ) {
const User = this ;
if ( username === undefined )
return false ;
const myquery = {
'idapp' : idapp ,
'username' : username ,
} ;
const myrec = await User . findOne ( myquery ) ;
if ( ! ! myrec ) {
return myrec . profile . consiglio ;
} else {
return false ;
}
2021-03-17 02:24:11 +01:00
} ;
UserSchema . statics . isUserVisuProjects = async function ( idapp , username ) {
const User = this ;
if ( username === undefined )
return false ;
const myquery = {
'idapp' : idapp ,
'username' : username ,
} ;
const myrec = await User . findOne ( myquery ) ;
if ( ! ! myrec ) {
return myrec . profile . socioresidente ;
} else {
return false ;
}
} ;
2020-06-08 13:31:05 +02:00
UserSchema . statics . isUserAlreadyQualified = async function ( idapp , username ) {
const User = this ;
if ( username === undefined )
return false ;
const myquery = {
'idapp' : idapp ,
'username' : username ,
'profile.qualified' : { $exists : true , $eq : true } ,
} ;
const myrec = await User . findOne ( myquery ) ;
return ! ! myrec ;
} ;
UserSchema . statics . isUserAlreadyQualified _2Invitati = async function ( idapp , username ) {
const User = this ;
if ( username === undefined )
return false ;
const myquery = {
'idapp' : idapp ,
'username' : username ,
'profile.qualified_2invitati' : { $exists : true , $eq : true } ,
} ;
const myrec = await User . findOne ( myquery ) ;
return ! ! myrec ;
} ;
UserSchema . statics . setUserQualified = async function ( idapp , username ) {
const User = this ;
if ( username === undefined )
return false ;
const myquery = {
'idapp' : idapp ,
'username' : username ,
} ;
2020-07-02 22:00:58 +02:00
const myrec = await User . findOneAndUpdate ( myquery , { $set : { 'profile.qualified' : true } } , { new : false } ) ;
2020-06-08 13:31:05 +02:00
return ! ! myrec ;
} ;
UserSchema . statics . setUserQualified _2Invitati = async function ( idapp , username ) {
const User = this ;
if ( username === undefined )
return false ;
const myquery = {
'idapp' : idapp ,
'username' : username ,
} ;
2020-07-02 22:00:58 +02:00
const myrec = await User . findOneAndUpdate ( myquery , { $set : { 'profile.qualified_2invitati' : true } } , { new : false } ) ;
2020-06-08 13:31:05 +02:00
return ! ! myrec ;
} ;
2020-03-10 21:44:14 +01:00
UserSchema . statics . isUserQualified9 = async function ( idapp , username ) {
const User = this ;
if ( username === undefined )
return false ;
qualified = await User . isUserQualified7 ( idapp , username ) ;
2020-05-10 21:07:51 +02:00
numinvitatiattivi = await ListaIngresso . getnumInvitatiAttivi ( idapp , username ) ;
2020-03-10 21:44:14 +01:00
return qualified && ( numinvitatiattivi >= 2 ) ;
} ;
2020-09-04 00:06:49 +02:00
/ *
2020-02-19 16:09:16 +01:00
UserSchema . statics . getnumPaymentOk = function ( idapp ) {
const User = this ;
return User . count ( {
idapp ,
2020-07-02 22:00:58 +02:00
$and : [
{
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
'profile.paymenttypes' : { "$in" : [ 'paypal' ] } ,
} ,
{
$or : [
{ 'profile.email_paypal' : { $exists : true } } ,
{ 'profile.revolut' : { $exists : true } } ,
]
}
]
2020-01-20 01:48:25 +01:00
} ) ;
} ;
2020-09-04 00:06:49 +02:00
* /
2020-01-20 01:48:25 +01:00
2020-01-30 01:19:25 +01:00
UserSchema . statics . getUsersNationalityQuery = function ( idapp ) {
const query = [
{
2020-05-04 19:34:41 +02:00
$match : { idapp , $or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] , }
2020-01-30 01:19:25 +01:00
} ,
{
$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 ) ;
} ) ;
} ;
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 ,
} ) ;
} ;
2020-03-10 21:44:14 +01:00
UserSchema . statics . AportadorOrig = function ( idapp , id ) {
const User = this ;
return User . findOne ( {
'_id' : id ,
'idapp' : idapp ,
} ) . then ( ( rec ) => {
if ( rec )
return rec . aportador _iniziale ;
else
return '' ;
} ) ;
} ;
2018-12-24 20:31:02 +01:00
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-05-04 19:34:41 +02:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2018-12-24 20:31:02 +01:00
} ) ;
} ;
2020-01-27 15:07:53 +01:00
UserSchema . statics . getLastUser = function ( idapp ) {
const User = this ;
2020-05-04 19:34:41 +02:00
return User . findOne ( {
idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2020-05-11 22:43:14 +02:00
} ) . sort ( { index : - 1 } )
2020-01-27 15:07:53 +01:00
} ;
2020-02-19 16:09:16 +01:00
UserSchema . statics . findByIndOrder = function ( idapp , ind _order ) {
const User = this ;
2020-05-11 22:43:14 +02:00
return User . getRecByIndOrder ( idapp , ind _order ) ;
} ;
UserSchema . statics . findByIndex = function ( idapp , index ) {
const User = this ;
2020-02-19 16:09:16 +01:00
try {
2020-03-10 21:44:14 +01:00
// ++Todo: non mettere tutti i campi !!
2020-02-19 16:09:16 +01:00
return User . findOne ( {
2020-03-10 21:44:14 +01:00
idapp ,
2020-05-11 22:43:14 +02:00
index ,
2020-05-04 19:34:41 +02:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2020-02-19 16:09:16 +01:00
} ) ;
} catch ( e ) {
2020-05-04 19:34:41 +02:00
console . error ( e . message ) ;
2020-02-19 16:09:16 +01:00
}
} ;
2020-05-10 21:07:51 +02:00
UserSchema . statics . findByOldOrder = function ( idapp , old _order ) {
const User = this ;
try {
return User . findOne ( {
idapp ,
old _order ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} ) ;
} catch ( e ) {
console . error ( e . message ) ;
}
} ;
2020-02-19 16:09:16 +01:00
2018-12-24 20:31:02 +01:00
2020-05-10 21:07:51 +02:00
UserSchema . pre ( 'save' , async function ( next ) {
2019-10-14 20:31:57 +02:00
2020-05-10 21:07:51 +02:00
try {
if ( this . isNew ) {
try {
const myrec = await User . findOne ( { idapp : this . idapp } ) . limit ( 1 ) . sort ( { index : - 1 } ) ;
if ( ! ! myrec ) {
this . index = myrec . _doc . index + 1 ;
} else {
this . index = 1 ;
}
} catch ( e ) {
this . index = 2 ;
}
}
/ *
if ( user . isModified ( 'password' ) ) {
bcrypt . genSalt ( 10 , ( err , salt ) => {
bcrypt . hash ( user . password , salt , ( err , hash ) => {
user . password = hash ;
next ( ) ;
} ) ;
2018-12-24 20:31:02 +01:00
} ) ;
2020-05-10 21:07:51 +02:00
} else {
next ( ) ;
}
* /
2018-12-24 20:31:02 +01:00
next ( ) ;
2020-05-11 22:43:14 +02:00
} catch ( e ) {
2020-05-10 21:07:51 +02:00
console . error ( e . message ) ;
2018-12-24 20:31:02 +01:00
}
} ) ;
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 ;
2020-05-04 19:34:41 +02:00
return await User . findOne ( {
idapp , username ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
} )
2019-10-27 00:37:10 +02:00
. then ( ( arrrec ) => {
return ( ( arrrec ) ? arrrec . email : '' ) ;
} ) . catch ( ( e ) => {
console . error ( 'getEmailByUsername' , e ) ;
} ) ;
} ;
2020-03-10 21:44:14 +01:00
UserSchema . statics . getUsernameById = async function ( idapp , id ) {
const User = this ;
2020-05-04 19:34:41 +02:00
return await User . findOne ( {
idapp , _id : id ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
} , { username : 1 } )
2020-03-10 21:44:14 +01:00
. then ( ( myuser ) => {
return ( ( myuser ) ? myuser . username : '' ) ;
} ) . catch ( ( e ) => {
} ) ;
} ;
UserSchema . statics . getUserById = function ( idapp , id ) {
const User = this ;
2020-05-04 19:34:41 +02:00
return User . findOne ( {
idapp , _id : id ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
} )
2020-03-10 21:44:14 +01:00
} ;
2020-01-30 01:19:25 +01:00
UserSchema . statics . getAportadorSolidarioByUsername = async function ( idapp , username ) {
const User = this ;
2020-05-04 19:34:41 +02:00
return await User . findOne ( {
idapp , username ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
} )
2020-01-30 01:19:25 +01:00
. 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 ;
2020-05-04 19:34:41 +02:00
return await User . findOne ( {
idapp , 'profile.teleg_id' : teleg _id ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} )
2020-01-03 01:52:49 +01:00
. then ( ( rec ) => {
return ( ! ! rec ) ? rec . _doc : null ;
} ) . catch ( ( e ) => {
console . error ( 'UserExistByIdTelegram' , e ) ;
} ) ;
} ;
2020-03-10 21:44:14 +01:00
UserSchema . statics . UsersByIdTelegram = async function ( idapp , teleg _id ) {
const User = this ;
2020-05-04 19:34:41 +02:00
return await User . find ( {
idapp , 'profile.teleg_id' : teleg _id ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
} )
2020-03-10 21:44:14 +01:00
. 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-05-04 19:34:41 +02:00
return await User . findOne ( {
idapp , username ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
} , { '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-02-19 16:09:16 +01:00
UserSchema . statics . SetTelegramCheckCode = async function ( idapp , id , teleg _checkcode ) {
2020-01-03 01:52:49 +01:00
const User = this ;
const fields _to _update = {
'profile.teleg_checkcode' : teleg _checkcode
} ;
return await User . findOneAndUpdate ( {
2020-02-19 16:09:16 +01:00
_id : id
2020-01-03 01:52:49 +01:00
} , { $set : fields _to _update } , { new : false } ) . then ( ( record ) => {
return ! ! record ;
} ) ;
} ;
2020-06-08 13:31:05 +02:00
UserSchema . statics . NonVoglioImbarcarmi = async function ( idapp , username ) {
const User = this ;
const fields _to _update = {
non _voglio _imbarcarmi : true
} ;
return await User . findOneAndUpdate ( {
idapp ,
username ,
} , { $set : fields _to _update } , { new : false } ) . then ( ( record ) => {
return ! ! record ;
} ) ;
} ;
2020-02-19 16:09:16 +01:00
UserSchema . statics . SetTelegramIdSuccess = async function ( idapp , id , teleg _id ) {
2020-01-03 01:52:49 +01:00
const User = this ;
const fields _to _update = {
'profile.teleg_id' : teleg _id ,
2020-02-19 16:09:16 +01:00
'profile.teleg_id_old' : 0 ,
2020-01-03 01:52:49 +01:00
'profile.teleg_checkcode' : 0
} ;
return await User . findOneAndUpdate ( {
idapp ,
2020-02-19 16:09:16 +01:00
_id : id
} , { $set : fields _to _update } , { new : false } ) . then ( ( record ) => {
return record ;
} ) ;
} ;
2020-03-31 20:34:24 +02:00
UserSchema . statics . getLangByIndOrder = async function ( idapp , ind _order ) {
const User = this ;
2020-05-11 22:43:14 +02:00
const rec = await User . getSmallRecByIndOrder ( idapp , ind _order ) ;
return ( ! ! rec ) ? rec . lang : '' ;
2020-03-31 20:34:24 +02:00
} ;
UserSchema . statics . SetLang = async function ( idapp , id , lang ) {
const User = this ;
const fields _to _update = {
lang ,
} ;
return await User . findOneAndUpdate ( {
_id : id
} , { $set : fields _to _update } , { new : false } ) . then ( ( record ) => {
return record ;
} ) ;
} ;
2020-02-19 16:09:16 +01:00
UserSchema . statics . SetTelegramWasBlocked = async function ( idapp , teleg _id ) {
const User = this ;
const fields _to _update = {
'profile.teleg_id_old' : teleg _id ,
'profile.teleg_id' : 0 ,
} ;
2020-06-08 13:31:05 +02:00
if ( process . env . PROD === "1" ) {
2020-05-19 00:18:13 +02:00
const ris = await User . findOneAndUpdate ( {
idapp ,
'profile.teleg_id' : teleg _id
} , { $set : fields _to _update } , { new : false } ) . then ( ( record ) => {
return record ;
} ) ;
}
2020-01-03 01:52:49 +01:00
} ;
2020-01-03 22:02:18 +01:00
UserSchema . statics . getNameSurnameByUsername = async function ( idapp , username ) {
const User = this ;
2020-05-04 19:34:41 +02:00
return await User . findOne ( {
idapp , username ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
} , { 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 ) ;
} ) ;
} ;
2021-04-30 01:31:12 +02:00
UserSchema . statics . getNameSurnameById = async function ( idapp , userId ) {
const User = this ;
return await User . findOne ( {
idapp ,
_id : userId ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
} , { name : 1 , surname : 1 } )
. then ( ( rec ) => {
return ( ! ! rec ) ? ` ${ rec . name } ${ rec . surname } ` : '' ;
} ) . catch ( ( e ) => {
console . error ( 'getNameSurnameById' , e ) ;
} ) ;
} ;
2020-03-10 21:44:14 +01:00
UserSchema . statics . getSmallRecByIndOrder = async function ( idapp , ind _order ) {
2020-05-10 21:07:51 +02:00
try {
const rec = await ListaIngresso . getarray ( idapp ,
{
idapp ,
ind _order ,
2020-05-14 17:23:17 +02:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
2020-05-10 21:07:51 +02:00
} ,
{
idapp : 1 ,
2020-05-11 22:43:14 +02:00
index : 1 ,
2020-05-10 21:07:51 +02:00
ind _order : 1 ,
2020-05-11 22:43:14 +02:00
old _order : 1 ,
2020-05-10 21:07:51 +02:00
username : 1 ,
name : 1 ,
2020-05-11 22:43:14 +02:00
lang : 1 ,
surname : 1 ,
'profile.teleg_id' : 1 ,
2020-05-10 21:07:51 +02:00
} ) ;
if ( ! ! rec )
return rec [ 0 ] ;
return null ;
} catch ( e ) {
console . error ( 'getSmallRecByIndOrder' , e ) ;
}
2020-03-10 21:44:14 +01:00
} ;
2020-05-14 17:23:17 +02:00
UserSchema . statics . NuovoSistema = function ( idapp ) {
const User = this ;
try {
return User . findOne ( { idapp , old _order : { $exists : true } } )
. then ( ( ris ) => {
return ! ! ris ;
} ) ;
} catch ( e ) {
console . error ( 'NuovoSistema' , e . message ) ;
}
} ;
2020-05-11 22:43:14 +02:00
UserSchema . statics . getRecByIndOrder = async function ( idapp , ind _order ) {
2020-05-14 17:23:17 +02:00
const User = this ;
2020-05-11 22:43:14 +02:00
try {
2020-05-14 17:23:17 +02:00
let filters = {
idapp : 1 ,
index : 1 ,
ind _order : 1 ,
old _order : 1 ,
username : 1 ,
2020-06-08 13:31:05 +02:00
email : 1 ,
2020-05-14 17:23:17 +02:00
name : 1 ,
lang : 1 ,
surname : 1 ,
'profile.teleg_id' : 1 ,
2020-06-08 13:31:05 +02:00
'profile.email_paypal' : 1 ,
2020-09-04 00:06:49 +02:00
'profile.payeer_id' : 1 ,
'profile.advcash_id' : 1 ,
2020-07-02 22:00:58 +02:00
'profile.revolut' : 1 ,
2020-06-08 13:31:05 +02:00
'profile.link_payment' : 1 ,
'profile.note_payment' : 1 ,
'profile.paymenttypes' : 1 ,
2020-05-14 17:23:17 +02:00
} ;
2020-05-11 22:43:14 +02:00
const rec = await ListaIngresso . getarray ( idapp ,
{
idapp ,
ind _order ,
2020-05-14 17:23:17 +02:00
} ,
filters ) ;
2020-05-11 22:43:14 +02:00
if ( ! ! rec )
return rec [ 0 ] ;
return null ;
} catch ( e ) {
console . error ( 'getRecByIndOrder' , e ) ;
}
} ;
2020-01-03 22:02:18 +01:00
UserSchema . statics . getusersManagers = async function ( idapp ) {
const User = this ;
2020-02-19 16:09:16 +01:00
return await User . find ( { idapp , 'profile.manage_telegram' : true } , { 'profile.teleg_id' : 1 , perm : 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
2021-02-11 02:20:35 +01:00
UserSchema . statics . getusersRespList = async function ( idapp ) {
const User = this ;
2021-02-24 04:48:31 +01:00
return await User . find ( { idapp , 'profile.resplist' : true } , { _id : 1 , username : 1 , name : 1 , surname : 1 } )
2021-02-11 02:20:35 +01:00
. then ( ( arrrec ) => {
return ( ! ! arrrec ) ? arrrec : null ;
} ) . catch ( ( e ) => {
console . error ( 'getusersRespList' , e ) ;
} ) ;
} ;
UserSchema . statics . getusersWorkersList = async function ( idapp ) {
const User = this ;
2021-02-24 04:48:31 +01:00
return await User . find ( { idapp , 'profile.workerslist' : true } , { _id : 1 , username : 1 , name : 1 , surname : 1 } )
2021-02-11 02:20:35 +01:00
. then ( ( arrrec ) => {
return ( ! ! arrrec ) ? arrrec : null ;
} ) . catch ( ( e ) => {
console . error ( 'getusersWorkersList' , e ) ;
} ) ;
} ;
2020-09-04 00:06:49 +02:00
UserSchema . statics . getusersManagersAndZoomeri = async function ( idapp ) {
const User = this ;
return await User . find (
{
idapp ,
or : [
{ 'profile.manage_telegram' : true } ,
{
perm :
{
$bit :
Number ( shared _consts . Permissions . Zoomeri )
}
}
] ,
} ,
{ 'profile.teleg_id' : 1 } )
. then ( ( arrrec ) => {
return ( ! ! arrrec ) ? arrrec : null ;
} ) . catch ( ( e ) => {
console . error ( 'getusersManagers' , e ) ;
} ) ;
} ;
2020-03-10 21:44:14 +01:00
UserSchema . statics . getUsersTelegALL = async function ( idapp , username ) {
2020-01-20 01:48:25 +01:00
const User = this ;
2020-03-10 21:44:14 +01:00
if ( ! ! username ) {
return await User . find ( { idapp , username , 'profile.teleg_id' : { $gt : 0 } } )
. then ( ( arrrec ) => {
return ( ! ! arrrec ) ? arrrec : null ;
} ) . catch ( ( e ) => {
console . error ( 'getUsersTelegALL' , e ) ;
} ) ;
} else {
return await User . find ( { idapp , 'profile.teleg_id' : { $gt : 0 } } )
. then ( ( arrrec ) => {
return ( ! ! arrrec ) ? arrrec : null ;
} ) . catch ( ( e ) => {
console . error ( 'getUsersTelegALL' , e ) ;
} ) ;
}
2020-01-20 01:48:25 +01:00
} ;
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 ,
2021-05-10 01:50:40 +02:00
username : 'paoloarcnm' ,
2020-01-27 15:07:53 +01:00
'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 ;
2020-04-24 10:29:25 +02:00
return User . find ( {
'idapp' : idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
} ,
{
username : 1 ,
name : 1 ,
surname : 1 ,
verified _email : 1 ,
made _gift : 1 ,
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 ) ;
2021-01-18 00:48:17 +01:00
const filterMatchBefore = ` ${ myclParamQuery . filter } ` ;
2019-10-14 20:31:57 +02:00
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-05-10 21:07:51 +02:00
return [
{ field : 'username' , type : tools . FieldType . string } ,
2020-04-10 13:02:33 +02:00
{ field : 'name' , type : tools . FieldType . string } ,
2020-05-10 21:07:51 +02:00
{ field : 'index' , type : tools . FieldType . number } ,
2020-04-10 13:02:33 +02:00
{ field : 'ind_order' , type : tools . FieldType . number } ,
2020-05-10 21:07:51 +02:00
{ field : 'old_order' , type : tools . FieldType . number } ,
2020-04-10 13:02:33 +02:00
{ field : 'surname' , type : tools . FieldType . string } ,
{ field : 'email' , type : tools . FieldType . string } ,
{ field : 'profile.cell' , type : tools . FieldType . string } ,
{ field : 'profile.email_paypal' , type : tools . FieldType . string } ,
2020-09-04 00:06:49 +02:00
{ field : 'profile.payeer_id' , type : tools . FieldType . string } ,
{ field : 'profile.advcash_id' , type : tools . FieldType . string } ,
2020-07-02 22:00:58 +02:00
{ field : 'profile.revolut' , type : tools . FieldType . string } ,
2020-06-08 13:31:05 +02:00
{ field : 'profile.link_payment' , type : tools . FieldType . string } ,
2020-05-10 21:07:51 +02:00
{ field : 'profile.teleg_id' , type : tools . FieldType . number } ,
2020-04-10 13:02:33 +02:00
{ field : 'profile.username_telegram' , type : tools . FieldType . string } ,
{ field : 'aportador_solidario' , type : tools . FieldType . string } ]
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 ;
2020-05-04 19:34:41 +02:00
const myfind = {
idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
} ;
2020-01-13 23:52:51 +01:00
return User . find ( myfind , ( err , arrrec ) => {
return arrrec
} ) ;
} ;
UserSchema . statics . DuplicateAllRecords = async function ( idapporig , idappdest ) {
return tools . DuplicateAllRecords ( this , idapporig , idappdest ) ;
} ;
2020-02-02 04:06:32 +01:00
UserSchema . statics . getDashboard = async function ( idapp , aportador _solidario , username , aportador _solidario _nome _completo ) {
2020-01-03 01:52:49 +01:00
try {
// DATA: username, name, surname, email, intcode_cell, cell
const dashboard = {
aportador : { } ,
2020-05-10 21:07:51 +02:00
arrposizioni : [ ] ,
arrimbarchi : [ ] ,
arrusers : { } ,
2020-01-03 01:52:49 +01:00
} ;
2020-02-02 04:06:32 +01:00
dashboard . myself = await User . getUserShortDataByUsername ( idapp , username ) ;
2020-01-03 01:52:49 +01:00
// Data of my Aportador
dashboard . aportador = await User . getUserShortDataByUsername ( idapp , aportador _solidario ) ;
2020-05-10 21:07:51 +02:00
// if (dashboard.aportador === undefined) {
// dashboard.aportador = await ExtraList.getUserNotRegisteredByNameSurname(idapp, aportador_solidario_nome_completo);
// }
2020-02-02 04:06:32 +01:00
2020-05-14 17:23:17 +02:00
const arrap = await User . getDownlineByUsername ( idapp , username , false , true ) ;
2020-03-10 21:44:14 +01:00
if ( ! ! arrap )
dashboard . numpeople _aportador = arrap . length ;
2020-01-30 01:19:25 +01:00
2020-05-10 21:07:51 +02:00
dashboard . arrimbarchi = await ListaIngresso . findAllByUsername ( idapp , username ) ;
2020-03-10 21:44:14 +01:00
2020-05-10 21:07:51 +02:00
// dashboard.arrprofili = await Nave.getArrProfiliByIndOrder(idapp, dashboard.myself.ind_order);
dashboard . arrposizioni = await Nave . getArrPosizioniByUsername ( idapp , username ) ;
2020-05-19 00:18:13 +02:00
/ * l e t a r r r e c = [ ] ;
2020-05-11 22:43:14 +02:00
if ( dashboard . arrimbarchi . length > 0 ) {
arrrec = await ListaIngresso . getProssimiInLista ( idapp , true ) ;
2020-05-19 00:18:13 +02:00
} * /
2020-05-04 19:34:41 +02:00
for ( let myimbarco of dashboard . arrimbarchi ) {
2020-05-10 21:07:51 +02:00
if ( ! ! myimbarco . invitante _username )
dashboard . arrusers [ myimbarco . invitante _username ] = await User . getUserShortDataByUsername ( idapp , myimbarco . invitante _username ) ;
2020-05-19 00:18:13 +02:00
myimbarco . _doc . posiz = await Graduatoria . getPosizioneInGraduatoria ( idapp , myimbarco . ind _order , myimbarco . username , myimbarco . num _tess ) ;
2020-05-04 19:34:41 +02:00
}
2020-03-31 20:34:24 +02:00
dashboard . navi _partenza = await NavePersistente . getListaNavi ( idapp ) ;
2020-05-04 19:34:41 +02:00
dashboard . lastnave = await NavePersistente . getLastNave ( idapp ) ;
2020-04-07 14:34:29 +02:00
for ( let mypos of dashboard . arrposizioni ) {
2020-05-14 17:23:17 +02:00
mypos . rec = await Nave . getNaveByRigaCol ( idapp , mypos . riga , mypos . col ) ;
mypos . nave _partenza = await NavePersistente . findByRigaColByDonatore ( idapp , mypos . riga , mypos . col , 0 ) ;
2020-06-08 13:31:05 +02:00
if ( ! ! mypos . nave _partenza )
mypos . flotta = await Flotta . getFlottaByNavePersistente ( idapp , mypos . nave _partenza ) ;
2020-03-10 21:44:14 +01:00
}
2020-05-11 22:43:14 +02:00
2020-05-10 21:07:51 +02:00
//for (let indriga = 0; indriga < 10; indriga++) {
// dashboard.navi_partenza.push(await Nave.getPrimaNaveByRiga(idapp, indriga));
//}
2020-05-04 19:34:41 +02:00
const arrnew = [ ] ;
2020-05-14 17:23:17 +02:00
try {
for ( let mypos of dashboard . arrposizioni ) {
// Controlla se è presente la Nave con il num_tess pari
2020-05-04 19:34:41 +02:00
2020-05-14 17:23:17 +02:00
let trovato = false ;
2020-05-04 19:34:41 +02:00
2020-05-14 17:23:17 +02:00
if ( mypos . num _tess % 2 !== 0 ) {
for ( let myrec of dashboard . arrposizioni ) {
if ( myrec . num _tess === mypos . num _tess + 1 && ( myrec . ind _order === mypos . ind _order ) ) {
// La Nave di Ritorno (numtess = 2) esiste nella lista !
trovato = true ;
break ;
}
2020-05-04 19:34:41 +02:00
}
2020-05-14 17:23:17 +02:00
} else {
trovato = true ;
2020-05-04 19:34:41 +02:00
}
2020-05-14 17:23:17 +02:00
if ( ! trovato ) {
let myr = null ;
if ( ! ! mypos . _doc )
myr = mypos . _doc ;
else
myr = mypos ;
if ( ! ! myr && ! ! myr . rec . mediatore ) {
const mymediatore = myr . rec . mediatore . arrdonatori [ 7 ] ;
if ( ! ! mymediatore ) {
const myrec = {
riga : mymediatore . riga ,
col : mymediatore . col ,
name : myr . rec . mediatore . recmediatore . name ,
surname : myr . rec . mediatore . recmediatore . surname ,
username : myr . rec . mediatore . recmediatore . username ,
num _tess : myr . rec . mediatore . recmediatore . num _tess + 1 ,
rec : await Nave . getNaveByRigaCol ( idapp , mymediatore . riga , mymediatore . col ) ,
nave _partenza : { } ,
} ;
myrec . nave _partenza = await NavePersistente . findByRigaColByDonatore ( idapp , myrec . riga , myrec . col , 0 ) ;
arrnew . push ( myrec ) ;
}
}
2020-05-04 19:34:41 +02:00
}
}
2020-05-14 17:23:17 +02:00
} catch ( e ) {
console . error ( e . message ) ;
2020-05-04 19:34:41 +02:00
}
dashboard . arrposizioni = [ ... dashboard . arrposizioni , ... arrnew ] ;
2020-05-10 21:07:51 +02:00
// console.table(dashboard.arrnavi);
2020-03-21 10:28:26 +01:00
2020-01-03 01:52:49 +01:00
return dashboard ;
} catch ( e ) {
2020-05-04 19:34:41 +02:00
console . error ( e . message ) ;
2020-01-03 01:52:49 +01:00
return false ;
}
} ;
2020-05-14 17:23:17 +02:00
UserSchema . statics . getDownline = async function ( idapp , aportador _solidario , username ) {
try {
// DATA: username, name, surname, email, intcode_cell, cell
let downline = { } ;
downline . downline = [ ] ;
// Data of my Downline
const arrap = await User . getDownlineByUsername ( idapp , username ) ;
if ( ! ! arrap )
downline . numpeople _aportador = arrap . length ;
downline . downline = await User . getDownlineByUsername ( idapp , username , true ) ;
downline . downbyuser = { } ;
for ( const down of downline . downline ) {
downline . downbyuser [ down . username ] = await User . getDownlineByUsername ( idapp , down . username , false ) ;
for ( const down2 of downline . downbyuser [ down . username ] ) {
downline . downbyuser [ down2 . username ] = await User . getDownlineByUsername ( idapp , down2 . username , false ) ;
}
}
return downline ;
} catch ( e ) {
console . error ( e . message ) ;
return false ;
}
} ;
2020-05-11 22:43:14 +02:00
/ *
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-05-11 22:43:14 +02:00
* /
2020-01-13 23:52:51 +01:00
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-05-04 19:34:41 +02:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
2020-01-30 01:19:25 +01:00
} ) ;
} ;
2020-01-20 01:48:25 +01:00
UserSchema . statics . getUsersRegistered = async function ( idapp ) {
const User = this ;
2020-04-24 10:29:25 +02:00
const myfind = {
idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
} ;
2020-01-20 01:48:25 +01:00
return await User . count ( myfind ) ;
} ;
2020-05-10 21:07:51 +02:00
/ *
2020-02-19 16:09:16 +01:00
UserSchema . statics . getUsersQualified = async function ( idapp , numinvitati ) {
const User = this ;
const arrusers = await User . find ( {
idapp ,
2020-05-04 19:34:41 +02:00
$and : [
{ $or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] } ,
2020-02-19 16:09:16 +01:00
{
2020-05-04 19:34:41 +02:00
$or : [
{
'profile.special_req' : true
} ,
{
verified _email : true ,
'profile.teleg_id' : { $gt : 0 } ,
'profile.paymenttypes' : { "$in" : [ 'paypal' ] } ,
'profile.saw_and_accepted' : shared _consts . ALL _SAW _AND _ACCEPTED ,
'profile.saw_zoom_presentation' : true ,
'profile.my_dream' : { $exists : true } ,
$and : [
{ "$expr" : { "$gt" : [ { "$strLenCP" : "$profile.my_dream" } , 10 ] } } ,
{ "$expr" : { "$gt" : [ { "$strLenCP" : "$profile.email_paypal" } , 6 ] } }
] ,
} ]
2020-02-19 16:09:16 +01:00
} ]
} , {
'username' : 1 ,
} ) ;
if ( numinvitati === 0 )
return arrusers ; // PRENDI TUTTI
let arrris = [ ] ;
for ( const rec of arrusers ) {
2020-05-10 21:07:51 +02:00
rec . numinvitatiattivi = await ListaIngresso . getnumInvitatiAttivi ( idapp , rec . username ) ;
2020-02-19 16:09:16 +01:00
if ( rec . numinvitatiattivi >= numinvitati ) {
arrris . push ( rec ) ;
}
}
return arrris
} ;
2020-05-10 21:07:51 +02:00
* /
2020-02-19 16:09:16 +01:00
2020-04-10 13:02:33 +02:00
UserSchema . statics . visuUtentiNonInNavi = async function ( idapp ) {
const User = this ;
const arrusers = await User . find ( {
idapp ,
2020-05-10 21:07:51 +02:00
$and : User . getQueryQualified ( )
2020-04-10 13:02:33 +02:00
} , {
name : 1 ,
surname : 1 ,
username : 1 ,
ind _order : 1 ,
2020-04-24 10:29:25 +02:00
deleted : 1 ,
sospeso : 1 ,
2020-04-10 13:02:33 +02:00
} ) ;
let num = 0 ;
let innave = 0 ;
let noninnave = 0 ;
let mystr = 'visuUtentiNonInNavi: ' + tools . ACAPO ;
let reg = 0 ;
let num0inv = 0 ;
let num1inv = 0 ;
let num2inv = 0 ;
let numnoinlista = 0 ;
2020-04-24 10:29:25 +02:00
let numeliminati = 0 ;
let numsospesi = 0 ;
2020-05-04 19:34:41 +02:00
let strnavidoppie = '' ;
let esiste = false ;
2020-04-10 13:02:33 +02:00
for ( const user of arrusers ) {
2020-05-04 19:34:41 +02:00
esiste = true ;
if ( ! ! user . deleted ) {
if ( user . deleted ) {
numeliminati ++ ;
esiste = false ;
}
}
if ( esiste ) {
let visualizza = false ;
// Controlla se ho un doppione nelle Navi !
let mienavi = await Nave . find ( { idapp , ind _order : user . ind _order } , { num _tess : 1 } ) ;
let strnavidoppie = [ ] ;
if ( ! ! mienavi ) {
strnavidoppie = mienavi . reduce ( ( acc , currentValue , index , array ) => {
if ( array . indexOf ( currentValue . num _tess ) > - 1 && ! acc . includes ( currentValue . num _tess ) )
acc . push ( currentValue . num _tess ) ;
return acc ;
} , [ ] ) ;
}
if ( strnavidoppie . length > 1 ) {
visualizza = true ;
}
2020-05-10 21:07:51 +02:00
user . numinvitati = await ListaIngresso . getnumInvitati ( idapp , user . username ) ;
2020-04-24 10:29:25 +02:00
reg ++ ;
let mianave = await Nave . findOne ( { idapp , ind _order : user . ind _order } ) ;
let mialistaingresso = await ListaIngresso . findOne ( { idapp , ind _order : user . ind _order } ) ;
let trovato = false ;
2020-05-04 19:34:41 +02:00
if ( ! mianave )
visualizza = true ;
if ( visualizza ) {
2020-05-11 22:43:14 +02:00
mystr += user . username + ' ' + user . name + ' ' + user . surname + ' [' + user . index + '] [inv=' + user . numinvitati + ']'
2020-04-24 10:29:25 +02:00
trovato = true ;
}
2020-04-10 13:02:33 +02:00
2020-05-04 19:34:41 +02:00
if ( strnavidoppie . length > 1 ) {
mystr += ' NAVI DUPLICATE! ' + strnavidoppie . join ( ',' ) ;
}
if ( ! mianave )
noninnave ++ ;
else
innave ++ ;
2020-04-24 10:29:25 +02:00
if ( user . sospeso ) {
numsospesi ++ ;
}
2020-04-10 13:02:33 +02:00
2020-04-24 10:29:25 +02:00
if ( ! mialistaingresso ) {
mystr += ' NO IN LISTA INGRESSO!' ;
trovato = true ;
numnoinlista ++ ;
}
2020-04-10 13:02:33 +02:00
2020-04-24 10:29:25 +02:00
if ( trovato )
mystr += tools . ACAPO ;
if ( user . numinvitati === 0 ) {
num0inv ++ ;
}
if ( user . numinvitati === 1 ) {
num1inv ++ ;
}
if ( user . numinvitati >= 2 ) {
num2inv ++ ;
}
2020-04-10 13:02:33 +02:00
}
}
mystrstart = 'Registrati: ' + reg + tools . ACAPO ;
mystrstart += '0 Invitati: ' + num0inv + tools . ACAPO ;
mystrstart += '1 Invitato: ' + num1inv + tools . ACAPO ;
mystrstart += '2 o più Invitati: ' + num2inv + tools . ACAPO ;
mystrstart += 'Presente in Nave: ' + innave + tools . ACAPO ;
mystrstart += 'Non in Nave: ' + noninnave + tools . ACAPO ;
mystrstart += 'Non in Lista Imbarco: ' + numnoinlista + tools . ACAPO ;
2020-04-24 10:29:25 +02:00
mystrstart += 'Usciti (Nascosti): ' + numeliminati + tools . ACAPO ;
mystrstart += 'Sospesi: ' + numsospesi + tools . ACAPO ;
2020-04-10 13:02:33 +02:00
mystrstart += tools . ACAPO ;
mystr = mystrstart + mystr ;
return { num , mystr } ;
} ;
2020-05-10 21:07:51 +02:00
// UserSchema.statics.getNumUsersQualified = async function (idapp, numinvitati) {
//
// arrrec = await this.getUsersQualified(idapp, numinvitati);
//
// return arrrec.length
//
// };
2020-02-19 16:09:16 +01:00
2020-01-27 15:07:53 +01:00
UserSchema . statics . getEmailNotVerified = async function ( idapp ) {
const User = this ;
2020-04-24 10:29:25 +02:00
const myfind = {
idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
verified _email : false
} ;
2020-01-27 15:07:53 +01:00
return await User . count ( myfind ) ;
} ;
UserSchema . statics . getUsersTelegramAttivo = async function ( idapp ) {
const User = this ;
2020-04-24 10:29:25 +02:00
const myfind = {
idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
'profile.teleg_id' : { $gt : 0 }
} ;
2020-01-27 15:07:53 +01:00
return await User . count ( myfind ) ;
} ;
UserSchema . statics . getUsersTelegramPending = async function ( idapp ) {
const User = this ;
2020-04-24 10:29:25 +02:00
const myfind = {
idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
'profile.teleg_checkcode' : { $gt : 0 }
} ;
2020-01-27 15:07:53 +01:00
return await User . count ( myfind ) ;
} ;
UserSchema . statics . getUsersZoom = async function ( idapp ) {
const User = this ;
2020-04-24 10:29:25 +02:00
const myfind = {
idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
'profile.saw_zoom_presentation' : true
} ;
2020-01-27 15:07:53 +01:00
return await User . count ( myfind ) ;
} ;
2021-02-24 04:48:31 +01:00
UserSchema . statics . getUsersResidenti = async function ( idapp ) {
const User = this ;
const myfind = {
idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
'profile.socioresidente' : { $exists : true , $eq : true }
} ;
2021-04-30 01:31:12 +02:00
return await User . find ( myfind , { username : 1 , name : 1 , surname : 1 } ) ;
2021-02-24 04:48:31 +01:00
} ;
2020-02-07 22:08:46 +01:00
UserSchema . statics . getSaw _and _Accepted = async function ( idapp ) {
const User = this ;
2020-04-24 10:29:25 +02:00
const myfind = {
idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
'profile.saw_and_accepted' : shared _consts . ALL _SAW _AND _ACCEPTED
} ;
2020-02-07 22:08:46 +01:00
return await User . count ( myfind ) ;
} ;
2020-01-27 15:07:53 +01:00
UserSchema . statics . getUsersDreams = async function ( idapp ) {
const User = this ;
const myfind = {
idapp ,
2020-04-24 10:29:25 +02:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2020-01-27 15:07:53 +01:00
'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-04-24 10:29:25 +02:00
return await User . find (
{
idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
} ,
{
username : 1 ,
name : 1 ,
surname : 1 ,
date _reg : 1 ,
2020-05-11 22:43:14 +02:00
index : 1 ,
2020-04-24 10:29:25 +02:00
'profile.nationality' : 1 ,
} ) . sort ( { date _reg : - 1 } ) . limit ( lastn ) . then ( ( arr ) => {
2020-01-27 15:07:53 +01:00
//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 = {
2020-05-10 21:07:51 +02:00
numinvitati : await ListaIngresso . getnumInvitati ( idapp , username ) ,
numinvitati _attivi : await ListaIngresso . getnumInvitatiAttivi ( idapp , username ) ,
2020-01-20 01:48:25 +01:00
} ;
} ;
2020-01-30 01:19:25 +01:00
UserSchema . statics . getDistinctNationalityQuery = function ( idapp ) {
2020-01-27 15:07:53 +01:00
const query = [
{
2020-05-19 00:18:13 +02:00
$match : {
idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
}
2020-01-27 15:07:53 +01:00
} ,
{
$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 ) {
2020-03-10 21:44:14 +01:00
2020-01-27 15:07:53 +01:00
const query = [
{
2020-05-04 19:34:41 +02:00
$match : {
idapp , date _reg : { $gte : tools . IncDateNow ( - ( 1000 * 60 * 60 * 24 * nrec ) ) } ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
}
2020-01-27 15:07:53 +01:00
} ,
{
2021-04-30 01:31:12 +02:00
$group : {
_id : { $dateToString : { format : "%Y-%m-%d" , date : "$date_reg" , timezone : 'Europe/Rome' } } ,
count : { $sum : 1 }
}
2020-01-27 15:07:53 +01:00
} ,
{
$sort : { _id : 1 }
2020-03-10 21:44:14 +01:00
}
] ;
return query
} ;
2020-05-04 19:34:41 +02:00
UserSchema . statics . getUsersRegWeekly = function ( idapp , nrec ) {
const query = [
{
$match : {
idapp , date _reg : { $gte : tools . IncDateNow ( - ( 1000 * 60 * 60 * 24 * nrec ) ) } ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
}
} ,
{
2021-04-30 01:31:12 +02:00
$group : {
_id : { $dateToString : { format : "%Y-%U" , date : "$date_reg" , timezone : 'Europe/Rome' } } ,
count : { $sum : 1 }
}
2020-05-04 19:34:41 +02:00
} ,
{
$sort : { _id : 1 }
}
] ;
return query
} ;
2020-03-10 21:44:14 +01:00
UserSchema . statics . getnumRegNDays = function ( idapp , nrec ) {
const query = [
{
2020-05-04 19:34:41 +02:00
$match : {
idapp ,
date _reg : { $lt : tools . IncDateNow ( - ( 1000 * 60 * 60 * 24 * nrec ) ) } ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
}
2020-03-10 21:44:14 +01:00
} ,
{
2021-04-30 01:31:12 +02:00
$group : {
_id : { $dateToString : { format : "%Y-%m-%d" , date : "$date_reg" , timezone : 'Europe/Rome' } } ,
count : { $sum : 1 }
}
2020-01-27 15:07:53 +01:00
} ,
{
2020-03-10 21:44:14 +01:00
$sort : { _id : 1 }
2020-01-27 15:07:53 +01:00
}
] ;
return query
} ;
2020-03-10 21:44:14 +01:00
UserSchema . statics . calcnumRegUntilDay = async function ( idapp ) {
const User = this ;
return await User . aggregate ( User . getnumRegNDays ( idapp , 30 ) )
. then ( ( arr ) => {
return arr . reduce ( ( sum , rec ) => sum + rec . count , 0 ) ;
} ) ;
} ;
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-05-04 19:34:41 +02:00
UserSchema . statics . calcRegWeekly = async function ( idapp ) {
const User = this ;
return User . aggregate ( User . getUsersRegWeekly ( idapp , 20 * 7 ) )
. then ( ris => {
// console.table(ris);
return JSON . stringify ( ris . slice ( 0 , - 1 ) ) ;
} ) ;
} ;
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-03-10 21:44:14 +01:00
async function addUtentiInLista ( idapp , mode , arrusers ) {
2020-05-10 21:07:51 +02:00
2020-02-19 16:09:16 +01:00
let num = 0 ;
for ( const rec of arrusers ) {
let ok = false ;
let qualified = await User . isUserQualified7 ( idapp , rec . username ) ;
2020-05-10 21:07:51 +02:00
let numinvitatiattivi = await ListaIngresso . getnumInvitatiAttivi ( idapp , rec . username ) ;
let numinvitati = await ListaIngresso . getnumInvitati ( idapp , rec . username ) ;
2020-02-19 16:09:16 +01:00
2020-03-10 21:44:14 +01:00
if ( rec . profile . special _req ) {
numinvitatiattivi = 2 ;
}
2020-02-19 16:09:16 +01:00
if ( mode === 1 ) {
// 9 punti qualificati
ok = qualified && ( numinvitatiattivi >= 2 ) ;
} else if ( mode === 2 ) {
2020-03-10 21:44:14 +01:00
// 8 punti qualificati ( 1 Invitato)
ok = qualified && ( numinvitati === 2 ) ;
} else if ( mode === 3 ) {
ok = qualified && ( numinvitatiattivi === 1 ) ;
} else if ( mode === 4 ) {
ok = qualified && ( numinvitati === 1 ) ;
} else if ( mode === 5 ) {
2020-02-19 16:09:16 +01:00
ok = qualified ;
}
if ( ok ) {
2020-05-10 21:07:51 +02:00
ris = await ListaIngresso . addUserInListaIngresso ( idapp , rec . username , rec . aportador _solidario , rec . lang , false , true ) ;
2020-03-10 21:44:14 +01:00
if ( ! ! ris )
2020-02-19 16:09:16 +01:00
num ++ ;
}
}
return num ;
2020-05-10 21:07:51 +02:00
2020-02-19 16:09:16 +01:00
}
2020-03-10 21:44:14 +01:00
UserSchema . statics . getUsernameByIndOrder = async function ( idapp , ind _order ) {
2020-05-11 22:43:14 +02:00
const myrec = await User . getSmallRecByIndOrder ( idapp , ind _order ) ;
return ( ! ! myrec ) ? myrec . username : '' ;
2020-03-10 21:44:14 +01:00
} ;
2020-05-10 21:07:51 +02:00
UserSchema . statics . getUsernameByIndex = async function ( idapp , index ) {
const myrec = await User . findOne ( {
idapp ,
index ,
} , { username : 1 } ) ;
return ( ! ! myrec ) ? myrec . username : ''
} ;
UserSchema . statics . ricalcolaIndex = async function ( idapp ) {
const User = this ;
2020-05-14 17:23:17 +02:00
const arrusers = await User . find ( {
idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
} ) . sort ( { old _order : 1 } ) ;
2020-05-11 22:43:14 +02:00
let index = 0 ;
2020-05-10 21:07:51 +02:00
try {
for ( const user of arrusers ) {
let field = {
index
} ;
index ++ ;
const ris = await User . findOneAndUpdate ( { _id : user . _id } , { $set : field } , { new : false } ) ;
}
2020-05-11 22:43:14 +02:00
} catch ( e ) {
2020-05-10 21:07:51 +02:00
console . error ( e . message ) ;
}
} ;
UserSchema . statics . changeInvitante = async function ( idapp , username , invitante _username , ind _order _ingr ) {
const User = this ;
const rec _ind _order _prima = await ListaIngresso . findOne ( { idapp , username } ) ;
let modif _aportador = false ;
// cambia aportador_solidario solo se è la prima nave!
// Oppure se ancora non sono in Lista!
if ( ! rec _ind _order _prima ) {
modif _aportador = true ;
} else {
if ( rec _ind _order _prima . ind _order === ind _order _ingr ) {
modif _aportador = true ;
}
}
if ( modif _aportador ) {
await User . findOneAndUpdate ( { idapp , username } , { $set : { aportador _solidario : invitante _username } } ) ;
}
// **
// ** Cambia invitante_username e ind_order di LISTAINGRESSO
// **
const rec _ingr = await ListaIngresso . findOne ( { idapp , username : username } ) ;
if ( ! ! rec _ingr ) {
2020-05-11 22:43:14 +02:00
// await ListaIngresso.findByIdAndUpdate(rec_ingr._id, { $set: { invitante_username, ind_order: ind_order_ingr } });
await ListaIngresso . findByIdAndUpdate ( rec _ingr . _id , { $set : { invitante _username } } ) ;
2020-05-10 21:07:51 +02:00
}
} ;
2020-06-08 13:31:05 +02:00
UserSchema . statics . NessunaNavePresenteByUsername = async function ( idapp , username ) {
const User = this ;
const rec = await User . findOne ( { idapp , username } , { username : 1 , ind _order : 1 } ) ;
if ( ! ! rec ) {
// Controlla se è qualificato!
const qualified = await User . isUserQualified7 ( idapp , rec . username ) ;
if ( qualified ) {
// Ha un'imbarco almeno?
const arrimbarchi = await ListaIngresso . findOne ( {
idapp , username : rec . username ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
} ) ;
const arrnavi = await Nave . findOne ( {
idapp , ind _order : rec . old _order ,
} ) ;
if ( ! arrimbarchi && ! arrnavi ) {
// NEANCHE 1 !
const recout = await User . findOneAndUpdate ( {
idapp ,
username
} , { $set : { navinonpresenti : true } } , { new : false } ) ;
return ( ! ! recout ) ;
}
}
}
return false ;
} ;
2020-09-04 00:06:49 +02:00
UserSchema . statics . getInfoUser = async function ( idapp , username ) {
return {
username ,
is7req : await User . isUserQualified7 ( idapp , username ) ,
is9req : await User . isUserQualified9 ( idapp , username ) ,
}
}
UserSchema . statics . checkIfSbloccatiRequisiti = async function ( idapp , allData , id ) {
const User = this ;
const telegrambot = require ( '../telegram/telegrambot' ) ;
if ( ! allData . myuser )
return false ;
if ( await Nave . checkIfNaveExist ( idapp , allData . myuser . username ) ) {
// Se già sei dentro la Nave, allora sei OK
return true ; //TOGLEREE
}
// Controlla se Sblocca i 7 requisiti
const is7req = await User . isUserQualified7 ( idapp , allData . myuser . username ) ;
const is9req = await User . isUserQualified9 ( idapp , allData . myuser . username ) ;
const userlista = await ListaIngresso . getListaTessByUsername ( idapp , allData . myuser . username ) ;
//if (userlista.length > 0) { //TOGLIERE
if ( userlista . length === 0 ) {
// Se non sono ancora dentro alla lista, allora controllo
if ( ! ! allData . precDataUser ) {
if ( ( ! allData . precDataUser . is7req && is7req ) && ! await User . isUserAlreadyQualified ( idapp , allData . myuser . username ) ) {
await User . setUserQualified ( idapp , allData . myuser . username ) ;
// ORA HAI I 7 REQUISITI !
// const msgtext = telegrambot.getCiao(idapp, allData.myuser.username, allData.myuser.lang) + tools.gettranslate('HAI_I_7_REQUISITI', allData.myuser.lang);
// telegrambot.sendMsgTelegram(idapp, allData.myuser.username, msgtext, true); // Anche a STAFF
// Aggiungilo alla ListaIngresso
risingr = await ListaIngresso . addUserInListaIngresso ( idapp , allData . myuser . username , allData . myuser . aportador _iniziale , allData . myuser . lang , true , false ) ;
}
}
}
if ( ! ! allData . precDataUser ) {
if ( ( ! allData . precDataUser . is9req && is9req ) && ! await User . isUserAlreadyQualified _2Invitati ( idapp , allData . myuser . username ) ) {
await User . setUserQualified _2Invitati ( idapp , allData . myuser . username ) ;
// ORA HAI I 9 REQUISITI !
const msgtext = telegrambot . getCiao ( idapp , allData . myuser . username , allData . myuser . lang ) + tools . gettranslate ( 'HAI_I_9_REQUISITI' , allData . myuser . lang ) ;
telegrambot . sendMsgTelegram ( idapp , allData . myuser . username , msgtext , false ) ; // Anche a STAFF
}
}
// CHECK APORTADOR SOLIDARIO:
if ( ! ! allData . useraportador ) {
/ *
const is9reqAportador = await User . isUserQualified9 ( idapp , allData . myuser . aportador _solidario ) ;
if ( ! allData . precDataAportador . is9req && is9reqAportador ) {
// ORA HAI I 9 REQUISITI !
const msgtext = telegrambot . getCiao ( idapp , allData . myuser . aportador _solidario , allData . useraportador . lang ) + tools . gettranslate ( 'HAI_I_9_REQUISITI' , allData . useraportador . lang ) ;
telegrambot . sendMsgTelegram ( idapp , allData . myuser . aportador _solidario , msgtext , true ) ; // Anche a STAFF
}
* /
}
} ;
UserSchema . statics . mettiSognoePaypal = async function ( idapp , modifica ) {
const User = this ;
let num = 0 ;
arrusers = await User . find ( {
'idapp' : idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ,
{ subaccount : { $exists : false } } , { subaccount : { $exists : true , $eq : false } } ]
} ) ;
for ( const rec of arrusers ) {
if ( rec . profile . saw _zoom _presentation ) {
aggiornato = false ;
my _dream _ok = ! ! rec . profile . my _dream ;
if ( my _dream _ok )
my _dream _ok = rec . profile . my _dream . length >= 8 ;
if ( ! my _dream _ok ) {
if ( modifica ) {
rec . profile . my _dream = '............' ;
aggiornato = true ;
}
num ++ ;
}
revolut _ok = ! ! rec . profile . revolut ;
paypal _ok = ! ! rec . profile . email _paypal ;
if ( paypal _ok )
paypal _ok = rec . profile . email _paypal . length > 8 ;
if ( ( ! revolut _ok ) && ( ! paypal _ok ) ) {
if ( modifica ) {
rec . profile . email _paypal = rec . email ;
aggiornato = true ;
}
num ++ ;
}
if ( aggiornato && modifica ) {
let allData = { } ;
allData . myuser = await User . getUserById ( idapp , rec . id ) ;
if ( ! ! allData . myuser )
allData . precDataUser = await User . getInfoUser ( idapp , allData . myuser . username ) ;
else
allData . precDataUser = null ;
const risupd = await User . findByIdAndUpdate ( rec . _id , {
'profile.email_paypal' : rec . profile . email _paypal ,
'profile.my_dream' : rec . profile . my _dream
} , { new : false } ) ;
if ( risupd ) {
await User . checkIfSbloccatiRequisiti ( idapp , allData , rec . id ) ;
}
}
}
}
return { num } ;
} ;
2020-06-08 13:31:05 +02:00
UserSchema . statics . flagUtentiNaviNonPresenti = async function ( idapp ) {
const User = this ;
let num = 0 ;
await User . updateMany ( { idapp } , { $set : { navinonpresenti : false } } ) ;
arrusers = await User . find ( {
'idapp' : idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ,
{ subaccount : { $exists : false } } , { subaccount : { $exists : true , $eq : false } } ]
} ) ;
for ( const rec of arrusers ) {
const nessunanave = await User . NessunaNavePresenteByUsername ( idapp , rec . username ) ;
if ( nessunanave )
num ++ ;
}
return { num } ;
} ;
UserSchema . statics . addNavePerUtentiNaviNonPresenti = async function ( idapp ) {
const User = this ;
let num = 0 ;
arrusers = await User . find ( {
'idapp' : idapp ,
navinonpresenti : true
} ) ;
for ( const user of arrusers ) {
// Controlla se è qualificato!
mydata = tools . AddDate ( user . date _reg , 7 ) ;
const newrecingr = await ListaIngresso . addUserInListaIngresso ( idapp , user . username , user . aportador _solidario , user . lang , true , true , mydata ) ;
await tools . snooze ( 1000 ) ;
num ++ ;
}
return { num } ;
} ;
UserSchema . statics . convSubAccount = async function ( idapp ) {
const User = this ;
// Solo i Cancellati !
arrusers = await User . find ( { 'idapp' : idapp , deleted : true } ) ;
let num = 0 ;
for ( const rec of arrusers ) {
// Cerca il suo Principale!
const trovato = await User . findOne ( {
idapp ,
'profile.teleg_id' : rec . profile . teleg _id ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
} ) ;
if ( trovato ) {
num ++ ;
await User . findByIdAndUpdate ( rec . _id , { subaccount : true } , { new : false } ) ;
}
}
return { num } ;
} ;
2020-07-13 23:35:05 +02:00
UserSchema . statics . getLastRec = async function ( idapp ) {
const User = this ;
lastrec = await User . find ( { idapp } ) . sort ( { date _reg : - 1 } ) . limit ( 1 ) ;
if ( ! ! lastrec ) {
return lastrec [ 0 ] ;
} else {
return null ;
}
} ;
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 ) {
2020-02-19 16:09:16 +01:00
// DISATTIVATO: ORA NON MI SERVE PIU
if ( false ) {
let mycell = tools . removespaces ( rec . profile . intcode _cell + rec . profile . cell ) ;
await User . findOneAndUpdate ( { _id : rec . _id } , { $set : { 'profile.cell' : mycell } } ) ;
num ++ ;
}
2020-01-30 01:19:25 +01:00
}
return { num } ;
// return await User.updateMany({ idapp }, { $set: { 'profile.cell': { $concat: ["$profile.intcode_cell", "$profile.cell"] } } })
2020-02-19 16:09:16 +01:00
} else if ( mydata . dbop === 'changeEmailLowerCase' ) {
arrusers = await User . find ( { 'idapp' : idapp } ) ;
let num = 0 ;
for ( const rec of arrusers ) {
let myemail = rec . email . toLowerCase ( ) ;
if ( myemail !== rec . email ) {
await User . findOneAndUpdate ( { _id : rec . _id } , { $set : { 'email' : myemail } } ) ;
num ++ ;
}
}
return { num } ;
2020-07-02 22:00:58 +02:00
/ * } e l s e i f ( m y d a t a . d b o p = = = ' c r e a L i s t a ' ) {
2020-02-19 16:09:16 +01:00
2020-07-02 22:00:58 +02:00
await ListaIngresso . deleteMany ( { idapp , added : false } ) ;
2020-02-19 16:09:16 +01:00
2020-07-02 22:00:58 +02:00
arrusers = await User . find ( {
'idapp' : idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ]
} ) . sort ( { date _added : 1 } ) ;
let num = 0 ;
2020-02-19 16:09:16 +01:00
2020-07-02 22:00:58 +02:00
num += await addUtentiInLista ( idapp , 1 , arrusers ) ;
num += await addUtentiInLista ( idapp , 2 , arrusers ) ;
num += await addUtentiInLista ( idapp , 3 , arrusers ) ;
num += await addUtentiInLista ( idapp , 4 , arrusers ) ;
num += await addUtentiInLista ( idapp , 5 , arrusers ) ;
// num += await addUtentiInLista(idapp, 3);
// num += await addUtentiInLista(idapp, 4);
2020-02-19 16:09:16 +01:00
2020-07-02 22:00:58 +02:00
return { num } ;
* /
2020-01-30 01:19:25 +01:00
}
} catch ( e ) {
2020-05-04 19:34:41 +02:00
console . error ( e . message ) ;
2020-01-30 01:19:25 +01:00
}
} ;
2020-02-19 16:09:16 +01:00
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