2020-01-13 23:52:51 +01:00
const bcrypt = require ( 'bcryptjs' ) ;
2022-09-14 11:32:04 +02:00
const mongoose = require ( 'mongoose' ) . set ( 'debug' , false ) ;
2018-12-24 20:31:02 +01:00
const validator = require ( 'validator' ) ;
const jwt = require ( 'jsonwebtoken' ) ;
const _ = require ( 'lodash' ) ;
2023-11-30 01:49:17 +01:00
const printf = require ( 'util' ) . format ;
2019-02-09 18:03:14 +01:00
const tools = require ( '../tools/general' ) ;
2022-12-10 02:01:17 +01:00
const { Settings } = require ( '../models/settings' ) ;
2022-02-03 00:33:15 +01:00
// const {ListaIngresso} = require('../models/listaingresso');
2022-12-10 02:01:17 +01:00
const { Graduatoria } = require ( '../models/graduatoria' ) ;
2022-02-03 00:33:15 +01:00
// const {Nave} = require('../models/nave');
// 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
2023-12-08 14:07:32 +01:00
const server _constants = require ( '../tools/server_constants' ) ;
2023-04-12 15:37:54 +02:00
2023-09-27 18:38:57 +02:00
const { Reaction } = require ( '../models/reaction' ) ;
2023-04-12 15:37:54 +02:00
2022-12-10 02:01:17 +01:00
const { MyGroup } = require ( '../models/mygroup' ) ;
const { Circuit } = require ( '../models/circuit' ) ;
2022-02-03 00:33:15 +01:00
2022-12-10 02:01:17 +01:00
const { Account } = require ( '../models/account' ) ;
const { Movement } = require ( '../models/movement' ) ;
2022-08-26 03:33:13 +02:00
2022-12-10 02:01:17 +01:00
const { ObjectID } = require ( 'mongodb' ) ;
2020-02-19 16:09:16 +01:00
2022-07-28 21:47:23 +02:00
const i18n = require ( 'i18n' ) ;
2019-10-14 20:31:57 +02:00
const shared _consts = require ( '../tools/shared_nodejs' ) ;
2023-12-17 16:20:44 +01:00
2019-02-05 03:40:22 +01:00
mongoose . Promise = global . Promise ;
2021-10-08 00:38:35 +02:00
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 => {
2021-10-08 00:38:35 +02:00
schema . options . usePushEach = true ;
2019-03-04 19:18:54 +01:00
} ) ;
2018-12-24 20:31:02 +01:00
2022-09-14 11:32:04 +02:00
mongoose . set ( 'debug' , false ) ;
2018-12-27 20:09:40 +01:00
2020-01-13 23:52:51 +01:00
const UserSchema = new mongoose . Schema ( {
2022-12-10 02:01:17 +01:00
userId : {
type : String ,
} ,
email : {
type : String ,
2023-10-21 15:27:53 +02:00
// required: true,
2022-12-10 02:01:17 +01:00
trim : true ,
minlength : 1 ,
unique : false ,
/ * v a l i d a t e : {
validator : validator . isEmail ,
message : '{VALUE} is not a valid email'
} * /
} ,
2024-02-08 01:34:30 +01:00
hash : {
type : String ,
} ,
2022-12-10 02:01:17 +01:00
idapp : {
type : String ,
required : true ,
} ,
2024-04-11 11:43:19 +02:00
2022-12-10 02:01:17 +01:00
group : {
type : Number ,
} ,
index : {
type : Number ,
} ,
ind _order : {
type : Number ,
} ,
old _order : {
type : Number ,
} ,
username : {
type : String ,
required : true ,
trim : true ,
2023-01-03 16:51:32 +01:00
minlength : 3 ,
2022-12-10 02:01:17 +01:00
unique : false ,
} ,
name : {
type : String ,
trim : true ,
} ,
surname : {
type : String ,
trim : true ,
} ,
password : {
type : String ,
require : true ,
minlength : 6 ,
} ,
lang : {
type : String ,
require : true ,
} ,
linkreg : {
type : String ,
required : false ,
} ,
verified _email : {
type : Boolean ,
} ,
made _gift : {
type : Boolean ,
} ,
tokens : [
{
access : {
2021-01-18 00:48:17 +01:00
type : String ,
2021-10-08 00:38:35 +02:00
required : true ,
2021-01-18 00:48:17 +01:00
} ,
2022-12-10 02:01:17 +01:00
browser : {
2021-01-18 00:48:17 +01:00
type : String ,
2021-10-08 00:38:35 +02:00
required : true ,
2021-01-18 00:48:17 +01:00
} ,
2022-12-10 02:01:17 +01:00
token : {
2021-10-08 00:38:35 +02:00
type : String ,
required : true ,
2021-01-18 00:48:17 +01:00
} ,
2024-04-09 21:56:50 +02:00
refreshToken : {
type : String ,
default : '' ,
} ,
2022-12-10 02:01:17 +01:00
date _login : {
2021-01-18 00:48:17 +01:00
type : Date ,
} ,
2022-12-10 02:01:17 +01:00
} ] ,
perm : {
type : Number ,
} ,
ipaddr : {
type : String ,
} ,
2024-03-21 21:22:57 +01:00
banIp : {
type : Boolean ,
} ,
2022-12-10 02:01:17 +01:00
date _reg : {
type : Date ,
} ,
date _deleted : {
type : Date ,
} ,
date _tokenforgot : {
type : Date ,
} ,
tokenforgot : {
type : String ,
} ,
2022-12-11 02:57:35 +01:00
tokenforgot _code : {
type : String ,
} ,
2022-12-11 18:04:02 +01:00
retry _pwd : {
type : Number ,
default : 0 ,
} ,
2022-12-10 02:01:17 +01:00
date _tokenreg : {
type : Date ,
} ,
tokenreg : {
type : String ,
} ,
lasttimeonline : {
type : Date ,
} ,
useragent : {
type : String ,
} ,
news _on : {
type : Boolean ,
} ,
2024-02-08 01:34:30 +01:00
email _errata : {
type : Boolean ,
} ,
lastid _newstosent : {
type : String
} ,
2022-12-10 02:01:17 +01:00
aportador _solidario : { // da cancellare
type : String ,
} ,
verified _by _aportador : {
type : Boolean ,
} ,
notask _verif : {
type : Boolean ,
} ,
trust _modified : {
type : Date ,
} ,
aportador _iniziale : {
type : String ,
} ,
aportador _solidario _nome _completo : {
type : String ,
} ,
aportador _solidario _ind _order : {
type : Number ,
} ,
note : {
type : String ,
} ,
deleted : {
type : Boolean ,
default : false ,
} ,
sospeso : {
type : Boolean ,
} ,
blocked : {
type : Boolean ,
} ,
username _who _block : {
type : String ,
} ,
date _blocked : {
type : Date ,
} ,
reported : {
type : Boolean ,
} ,
username _who _report : {
type : String ,
} ,
date _report : {
type : Date ,
} ,
non _voglio _imbarcarmi : {
type : Boolean ,
} ,
navinonpresenti : {
type : Boolean ,
} ,
subaccount : {
type : Boolean ,
} ,
cart : {
type : Object ,
} ,
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 ,
} ,
firstname _telegram : {
type : String ,
} ,
lastname _telegram : {
type : String ,
} ,
website : {
type : String ,
} ,
teleg _id : {
type : Number ,
} ,
teleg _id _old : {
type : Number ,
} ,
teleg _checkcode : {
type : Number ,
} ,
manage _telegram : {
type : Boolean ,
} ,
2023-12-29 15:19:15 +01:00
admin _telegram : {
type : Boolean ,
} ,
2022-12-10 02:01:17 +01:00
resplist : {
type : Boolean ,
} ,
workerslist : {
type : Boolean ,
} ,
dateofbirth : {
type : Date ,
} ,
born _city : {
type : String ,
trim : true ,
} ,
born _city _id : {
type : Number ,
} ,
born _province : {
type : String ,
trim : true ,
} ,
born _country : {
type : String ,
trim : true ,
} ,
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 ,
} ,
biografia : {
type : String ,
} ,
qualifica : {
type : String ,
} ,
motivazioni : {
type : String ,
} ,
competenze _professionalita : {
type : String ,
} ,
cosa _offrire : {
type : String ,
} ,
cosa _ricevere : {
type : String ,
} ,
altre _comunicazioni : {
type : Boolean ,
} ,
come _ci _hai _conosciuto : {
type : Boolean ,
} ,
socio : {
type : Boolean ,
} ,
socioresidente : {
type : Boolean ,
} ,
consiglio : {
type : Boolean ,
} ,
myshares : [
{
description : { type : String } ,
rating : { type : Number } ,
} ] ,
friends : [
{
_id : false ,
username : { type : String } ,
date : { type : Date } ,
} ] , // username
req _friends : [
{
_id : false ,
username : { type : String } ,
date : { type : Date } ,
} ] , // username
2023-01-06 15:51:48 +01:00
handshake : [
{
_id : false ,
username : { type : String } ,
date : { type : Date } ,
} ] , // username
2022-12-10 02:01:17 +01:00
mygroups : [
{
_id : false ,
groupname : { type : String } ,
date : { type : Date } ,
2022-12-23 00:36:35 +01:00
} ] ,
2022-12-10 02:01:17 +01:00
mycircuits : [
{
_id : false ,
circuitname : { type : String } ,
date : { type : Date } ,
2022-12-23 00:36:35 +01:00
} ] ,
2023-03-11 01:01:11 +01:00
last _circuitpath : {
type : String ,
} ,
lastdate _reqRis : {
type : Date ,
} ,
2022-12-10 02:01:17 +01:00
notifs : [
{
_id : false ,
dir : { type : Number } ,
value : { type : Number } ,
2021-01-18 00:48:17 +01:00
} ,
2022-12-10 02:01:17 +01:00
] ,
notif _idCities : [
{
2021-01-18 00:48:17 +01:00
type : Number ,
2022-12-10 02:01:17 +01:00
} ] ,
notif _provinces : [
{
2022-01-07 01:18:01 +01:00
type : String ,
2022-12-10 02:01:17 +01:00
} ] ,
notif _regions : [
{
2022-08-04 17:30:57 +02:00
type : String ,
2022-12-10 02:01:17 +01:00
} ] ,
notif _sectors : [
{
type : Number ,
} ] ,
notif _sector _goods : [
{
type : Number ,
} ] ,
2023-03-04 10:20:43 +01:00
resid _prov _id : {
type : Number ,
} ,
resid _province : {
type : String ,
trim : true ,
} ,
2023-11-26 01:38:02 +01:00
resid _card : {
type : String ,
trim : true ,
} ,
2023-03-11 01:01:11 +01:00
stepTutorial : {
type : Number ,
} ,
noNameSurname : {
type : Boolean ,
2023-03-17 19:07:31 +01:00
} ,
2023-03-22 15:54:45 +01:00
noCircuit : {
type : Boolean ,
} ,
2023-11-19 23:40:38 +01:00
noCircIta : {
type : Boolean ,
} ,
2023-03-17 19:07:31 +01:00
noFoto : {
type : Boolean ,
2023-04-04 15:26:56 +02:00
} ,
2023-04-13 14:27:00 +02:00
seen : [
{
_id : false ,
id : { type : String } ,
tab : { type : Number } ,
} ,
] ,
2023-04-04 15:26:56 +02:00
bookmark : [
{
_id : false ,
id : { type : String } ,
tab : { type : Number } ,
} ,
] ,
favorite : [
{
_id : false ,
id : { type : String } ,
tab : { type : Number } ,
} ,
] ,
2023-04-17 00:11:36 +02:00
attend : [
{
_id : false ,
id : { type : String } ,
tab : { type : Number } ,
num : { type : Number } ,
} ,
] ,
2023-04-12 15:37:54 +02:00
version : { type : Number } ,
2022-12-10 02:01:17 +01:00
} ,
2023-01-03 16:51:32 +01:00
} ) ;
2018-12-24 20:31:02 +01:00
2023-09-27 18:38:57 +02:00
2022-12-10 02:01:17 +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
} ;
2022-12-10 02:01:17 +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 ;
2023-12-07 08:34:24 +01:00
const prova = 'PROVAMSG@1A'
2024-04-09 21:56:50 +02:00
let attiva _scadenza = user . idapp ? tools . getEnableTokenExpiredByIdApp ( user . idapp ) : false ;
let token = null ;
2024-04-11 11:43:19 +02:00
let numsec = process . env . TOKEN _LIFE ;
2024-04-09 21:56:50 +02:00
if ( attiva _scadenza )
token = jwt . sign ( { _id : prova , smart : user . _id . toHexString ( ) , access , un : user . username } ,
2024-04-11 11:43:19 +02:00
process . env . SIGNCODE , { expiresIn : numsec } ) . toString ( ) ;
2024-04-09 21:56:50 +02:00
else
token = jwt . sign ( { _id : prova , smart : user . _id . toHexString ( ) , access , un : user . username } ,
process . env . SIGNCODE ) . toString ( ) ;
const refreshToken = jwt . sign ( { _id : prova , smart : user . _id . toHexString ( ) , access , un : user . username } ,
process . env . SECRK , { expiresIn : process . env . REFRESH _TOKEN _LIFE } ) . toString ( ) ;
2019-10-13 20:44:05 +02:00
const date _login = new Date ( ) ;
2018-12-24 20:31:02 +01:00
2019-02-09 18:03:14 +01:00
// CANCELLA IL PRECEDENTE !
2022-12-10 02:01:17 +01:00
user . tokens = user . tokens . filter ( function ( tok ) {
2021-10-08 00:38:35 +02:00
return ( tok . access !== access ) ||
2022-12-10 02:01:17 +01:00
( ( tok . access === access ) && ( tok . browser !== browser ) ) ;
2019-03-04 19:18:54 +01:00
} ) ;
2024-04-09 21:56:50 +02:00
user . tokens . push ( { access , browser , token , date _login , refreshToken } ) ;
2018-12-27 18:13:43 +01:00
2019-10-27 00:37:10 +02:00
user . lasttimeonline = new Date ( ) ;
2021-10-08 00:38:35 +02:00
return user . save ( ) . then ( ( ) => {
2024-04-23 22:47:20 +02:00
// console.log('Salvato refreshToken su DB', refreshToken);
2021-10-08 00:38:35 +02:00
// console.log("TOKEN CREATO IN LOGIN : " + token);
2024-04-09 21:56:50 +02:00
return { token , refreshToken } ;
2021-10-08 00:38:35 +02:00
} ) . catch ( err => {
console . log ( 'Error' , err . message ) ;
2024-04-09 21:56:50 +02:00
return { token : '' , refreshToken : '' }
2021-10-08 00:38:35 +02:00
} ) ;
2018-12-24 20:31:02 +01:00
} ;
2022-12-12 18:25:13 +01:00
UserSchema . statics . setOnLine = function ( idapp , username ) {
const User = this ;
try {
return User . findOneAndUpdate ( { idapp , username } , { $set : { lasttimeonline : new Date ( ) } } ) ;
} catch ( e ) {
}
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . setPermissionsById = function ( id , perm ) {
2019-10-13 20:44:05 +02:00
const user = this ;
2022-12-10 02:01:17 +01:00
return user . findByIdAndUpdate ( id , { $set : { perm } } ) . then ( ( user ) => {
2019-10-13 20:44:05 +02:00
if ( user )
2022-12-10 02:01:17 +01:00
return res . send ( { code : server _constants . RIS _CODE _OK , msg : '' } ) ;
2019-10-13 20:44:05 +02:00
else
2022-12-10 02:01:17 +01:00
return res . send ( { code : server _constants . RIS _CODE _ERR , msg : '' } ) ;
2019-10-13 20:44:05 +02:00
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . setZoomPresenza = async function ( idapp , id , presenza ) {
2020-09-04 00:06:49 +02:00
const User = this ;
const telegrambot = require ( '../telegram/telegrambot' ) ;
let allData = { } ;
allData . myuser = await User . getUserById ( idapp , id ) ;
if ( ! ! allData . myuser )
2021-10-08 00:38:35 +02:00
allData . precDataUser = await User . getInfoUser ( idapp ,
2022-12-10 02:01:17 +01:00
allData . myuser . username ) ;
2021-10-08 00:38:35 +02:00
2022-09-11 11:45:33 +02:00
return await User . findByIdAndUpdate ( id ,
2022-12-10 02:01:17 +01:00
{ $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 ,
2021-10-08 00:38:35 +02:00
` L \' utente ${ rec . name } ${ rec . surname } ( ${ rec . username } ) è stato confermato per aver visto lo Zoom di Benvenuto ` ) ;
2022-12-10 02:01:17 +01:00
} else {
telegrambot . sendMsgTelegramToTheManagersAndZoomeri ( idapp ,
2021-10-08 00:38:35 +02:00
` L \' utente ${ rec . name } ${ rec . surname } ( ${ rec . username } ) è stato annullata la sua richiesta per aver visto lo Zoom di Benvenuto! (Non ci risulta) ` ) ;
2022-12-10 02:01:17 +01:00
}
2020-09-04 00:06:49 +02:00
2022-12-10 02:01:17 +01:00
return User . findByIdAndUpdate ( id ,
{ $set : { 'profile.ask_zoom_partecipato' : false } } ) . then ( ( user ) => {
2020-09-04 00:06:49 +02:00
2022-12-10 02:01:17 +01:00
User . checkIfSbloccatiRequisiti ( idapp , allData , id ) ;
} ) ;
2020-09-04 00:06:49 +02:00
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . canHavePower = function ( perm ) {
2022-01-28 18:15:56 +01:00
const User = this ;
try {
let consentito = false ;
if ( User . isAdmin ( perm ) || User . isManager ( perm ) ||
2022-12-10 02:01:17 +01:00
User . isEditor ( perm ) || User . isFacilitatore ( perm ) ) {
2022-01-28 18:15:56 +01:00
consentito = true ;
}
return consentito ;
} catch ( e ) {
return false ;
}
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . isAdmin = function ( perm ) {
2019-10-13 20:44:05 +02:00
try {
2021-10-08 00:38:35 +02:00
return ( ( perm & shared _consts . Permissions . Admin ) ===
2022-12-10 02:01:17 +01:00
shared _consts . Permissions . Admin ) ;
2019-10-27 00:37:10 +02:00
} catch ( e ) {
2021-10-08 00:38:35 +02:00
return false ;
2019-10-13 20:44:05 +02:00
}
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . isManager = function ( perm ) {
2019-10-15 20:40:31 +02:00
try {
2021-10-08 00:38:35 +02:00
return ( ( perm & shared _consts . Permissions . Manager ) ===
2022-12-10 02:01:17 +01:00
shared _consts . Permissions . Manager ) ;
2019-10-27 00:37:10 +02:00
} catch ( e ) {
2021-10-08 00:38:35 +02:00
return false ;
2019-10-15 20:40:31 +02:00
}
} ;
2023-12-15 23:36:43 +01:00
UserSchema . statics . isManagerById = async function ( id ) {
try {
const ris = await User . findOne ( { _id : id } , { perm : 1 } ) . lean ( ) ;
return ( ( ris . perm & shared _consts . Permissions . Manager ) ===
shared _consts . Permissions . Manager ) ;
} catch ( e ) {
return false ;
}
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . isEditor = function ( perm ) {
2020-05-04 19:34:41 +02:00
try {
2021-10-08 00:38:35 +02:00
return ( ( perm & shared _consts . Permissions . Editor ) ===
2022-12-10 02:01:17 +01:00
shared _consts . Permissions . Editor ) ;
2020-05-04 19:34:41 +02:00
} catch ( e ) {
2021-10-08 00:38:35 +02:00
return false ;
2020-05-04 19:34:41 +02:00
}
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . isZoomeri = function ( perm ) {
2020-09-04 00:06:49 +02:00
try {
2021-10-08 00:38:35 +02:00
return ( ( perm & shared _consts . Permissions . Zoomeri ) ===
2022-12-10 02:01:17 +01:00
shared _consts . Permissions . Zoomeri ) ;
2020-09-04 00:06:49 +02:00
} catch ( e ) {
2021-10-08 00:38:35 +02:00
return false ;
2020-09-04 00:06:49 +02:00
}
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . isDepartment = function ( perm ) {
2021-02-03 01:33:30 +01:00
try {
2021-10-08 00:38:35 +02:00
return ( ( perm & shared _consts . Permissions . Zoomeri ) ===
2022-12-10 02:01:17 +01:00
shared _consts . Permissions . Department ) ;
2021-02-03 01:33:30 +01:00
} catch ( e ) {
2021-10-08 00:38:35 +02:00
return false ;
2021-02-03 01:33:30 +01:00
}
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . isFacilitatore = function ( perm ) {
2020-04-07 14:34:29 +02:00
try {
2022-07-11 23:20:28 +02:00
return ( ( perm & shared _consts . Permissions . Facilitatore ) ===
2022-12-10 02:01:17 +01:00
shared _consts . Permissions . Facilitatore ) ;
2020-04-07 14:34:29 +02:00
} catch ( e ) {
2021-10-08 00:38:35 +02:00
return false ;
2020-04-07 14:34:29 +02:00
}
} ;
2024-04-09 21:56:50 +02:00
UserSchema . statics . findByToken = async function ( token , typeaccess , con _auth ) {
2019-10-13 20:44:05 +02:00
const User = this ;
let decoded ;
2024-04-09 21:56:50 +02:00
let code = server _constants . RIS _CODE _HTTP _INVALID _TOKEN ;
let user = null ;
2018-12-24 20:31:02 +01:00
try {
2024-04-09 21:56:50 +02:00
if ( token ) {
decoded = jwt . verify ( token , process . env . SIGNCODE ) ;
code = server _constants . RIS _CODE _OK ;
}
2018-12-24 20:31:02 +01:00
} catch ( e ) {
2024-04-09 21:56:50 +02:00
if ( e . expiredAt ) {
2024-04-11 11:43:19 +02:00
code = server _constants . RIS _CODE _HTTP _FORBIDDEN _TOKEN _EXPIRED ;
2024-04-09 21:56:50 +02:00
if ( con _auth ) {
2024-04-11 11:43:19 +02:00
return { user : null , code } ;
2024-04-09 21:56:50 +02:00
}
} else {
console . error ( 'Err findByToken:' , e ) ;
}
2018-12-24 20:31:02 +01:00
}
2024-04-09 21:56:50 +02:00
if ( code === server _constants . RIS _CODE _OK ) {
user = await User . findOne ( {
'_id' : decoded . smart ,
'tokens.token' : token ,
'tokens.access' : typeaccess ,
} ) ;
if ( user ) {
let check _expiry _date = false
// Controlla se il sito ha attivo il controllo del Token Scaduto
if ( tools . getEnableTokenExpiredByIdApp ( user . idapp ) ) {
check _expiry _date = true
}
if ( check _expiry _date && ( decoded . exp < Date . now ( ) / 1000 ) ) {
console . log ( 'Il token è scaduto, generazione del nuovo token...' ) ;
code = server _constants . RIS _CODE _HTTP _FORBIDDEN _TOKEN _EXPIRED ;
} else {
// TOKEN ANCORA VALIDO
code = server _constants . RIS _CODE _OK ;
}
}
}
return { user , code } ;
2018-12-24 20:31:02 +01:00
} ;
2022-12-10 02:01:17 +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 ) {
2024-04-11 11:43:19 +02:00
console . error ( 'Err findByTokenAnyAccess:' , e ) ;
2019-02-13 18:47:58 +01:00
return Promise . resolve ( null ) ;
}
return User . findOne ( {
2023-12-07 08:34:24 +01:00
'_id' : decoded . smart ,
2019-02-13 18:47:58 +01:00
'tokens.token' : token ,
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . findByCredentials = function ( idapp , username , password , pwdcrypted ) {
2020-01-13 23:52:51 +01:00
const User = this ;
2021-10-08 00:38:35 +02:00
let pwd = '' ;
2018-12-24 20:31:02 +01:00
2022-05-04 00:26:12 +02:00
let regexp = new RegExp ( ` ^ ${ username } $ ` , 'i' ) ;
2022-03-10 23:19:56 +01:00
2020-05-04 19:34:41 +02:00
return User . findOne ( {
2022-03-10 23:19:56 +01:00
idapp ,
2022-12-10 02:01:17 +01:00
username : { $regex : regexp } ,
2020-06-08 13:31:05 +02:00
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ,
2020-06-08 13:31:05 +02:00
{
$and : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : true , $eq : true } } ,
{ subaccount : { $exists : true , $eq : true } } ,
2021-10-08 00:38:35 +02:00
] ,
} ,
2020-06-08 13:31:05 +02:00
] ,
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 ( ) ,
2021-10-08 00:38:35 +02:00
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
2022-03-08 01:01:20 +01:00
} ) ;
2019-03-04 19:18:54 +01:00
} else {
2021-10-08 00:38:35 +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 ;
2022-03-04 22:30:50 +01:00
if ( pwdcrypted ) {
2022-03-08 01:01:20 +01:00
if ( pwd === user . password ) {
return user ;
2022-03-04 22:30:50 +01:00
} else {
2022-03-08 01:01:20 +01:00
return false ;
2022-03-04 22:30:50 +01:00
}
}
2018-12-24 20:31:02 +01:00
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 ) ;
}
} ) ;
} ) ;
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . findByUsername = async function ( idapp , username , alsoemail , onlyifVerifiedByAportador ) {
2019-10-13 20:44:05 +02:00
const User = this ;
2018-12-24 20:31:02 +01:00
2021-10-08 00:38:35 +02:00
const myreg = [ '^' , username , '$' ] . join ( '' ) ;
let regexusername = new RegExp ( myreg , 'i' ) ;
2020-02-05 00:39:25 +01:00
2022-02-25 17:24:31 +01:00
//++TODO: Set only the necessary fields to get in memory
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2021-09-22 01:13:41 +02:00
idapp : idapp ,
2022-12-10 02:01:17 +01:00
username : { $regex : regexusername } ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2022-09-11 11:45:33 +02:00
} ) . then ( async ( ris ) => {
2020-02-19 16:09:16 +01:00
if ( ( ! ris ) && ( alsoemail ) ) {
2021-10-08 00:38:35 +02:00
regexemail = new RegExp ( [ '^' , username . toLowerCase ( ) , '$' ] . join ( '' ) , 'i' ) ;
2020-02-19 16:09:16 +01:00
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2020-02-19 16:09:16 +01:00
'idapp' : idapp ,
2022-12-10 02:01:17 +01:00
'email' : { $regex : regexemail } ,
2021-10-08 00:38:35 +02:00
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
2020-02-19 16:09:16 +01:00
} ) ;
}
return ris ;
2022-03-04 22:30:50 +01:00
} ) . then ( ( rec ) => {
2022-05-05 00:38:41 +02:00
if ( rec && onlyifVerifiedByAportador ) {
2022-03-04 22:30:50 +01:00
if ( tools . getAskToVerifyReg ( idapp ) ) {
if ( ! rec . verified _by _aportador )
return null ;
}
}
return rec ;
2018-12-24 20:31:02 +01:00
} ) ;
} ;
2024-04-11 18:55:15 +02:00
/ * *
* Find a user by their Telegram username .
*
* @ param { string } idapp - The application ID
* @ param { string } username - The Telegram username
* @ param { boolean } alsoemail - Flag to indicate if email should also be considered
* @ param { boolean } onlyifVerifiedByAportador - Flag to indicate if only verified users should be returned
* @ return { Promise } A Promise that resolves to the found user or null
* * /
UserSchema . statics . findByUsernameTelegram = async function ( idapp , username , alsoemail , onlyifVerifiedByAportador ) {
const User = this ;
if ( username && username [ 0 ] === '@' ) {
username = username . substring ( 1 ) ;
}
const myreg = [ '^' , username , '$' ] . join ( '' ) ;
let regexusername = new RegExp ( myreg , 'i' ) ;
//++TODO: Set only the necessary fields to get in memory
return await User . findOne ( {
idapp : idapp ,
'profile.username_telegram' : { $regex : regexusername } ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} ) . then ( async ( ris ) => {
if ( ( ! ris ) && ( alsoemail ) ) {
regexemail = new RegExp ( [ '^' , username . toLowerCase ( ) , '$' ] . join ( '' ) , 'i' ) ;
return await User . findOne ( {
'idapp' : idapp ,
'email' : { $regex : regexemail } ,
$or : [
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
} ) ;
}
return ris ;
} ) . then ( ( rec ) => {
if ( rec && onlyifVerifiedByAportador ) {
if ( tools . getAskToVerifyReg ( idapp ) ) {
if ( ! rec . verified _by _aportador )
return null ;
}
}
return rec ;
} ) ;
} ;
2018-12-24 20:31:02 +01:00
2022-12-10 02:01:17 +01:00
UserSchema . statics . getProjectUser = function ( ) {
2022-10-08 18:09:38 +02:00
return {
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 ,
2022-12-10 02:01:17 +01:00
lasttimeonline : 1 ,
2020-04-24 10:29:25 +02:00
deleted : 1 ,
2022-08-04 17:30:57 +02:00
reported : 1 ,
2022-08-09 17:32:06 +02:00
date _report : 1 ,
username _who _report : 1 ,
2020-04-24 10:29:25 +02:00
sospeso : 1 ,
2019-12-31 00:44:53 +01:00
verified _email : 1 ,
2021-12-29 18:26:08 +01:00
verified _by _aportador : 1 ,
2020-01-30 01:19:25 +01:00
'profile.teleg_id' : 1 ,
2022-03-08 01:01:20 +01:00
'profile.username_telegram' : 1 ,
2022-03-11 12:38:01 +01:00
'profile.firstname_telegram' : 1 ,
'profile.lastname_telegram' : 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 ,
2021-10-08 00:38:35 +02:00
img : 1 ,
2022-11-02 22:32:54 +01:00
tokenreg : 1 ,
date _tokenreg : 1 ,
2022-10-08 18:09:38 +02:00
} ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUserShortDataByUsername = async function ( idapp , username ) {
2022-10-08 18:09:38 +02:00
const User = this ;
let regexp = new RegExp ( ` ^ ${ username } $ ` , 'i' ) ;
const myrec = await User . findOne ( {
2022-12-10 02:01:17 +01:00
'idapp' : idapp ,
username : { $regex : regexp } ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} ,
this . getProjectUser ( ) ,
2022-10-08 18:09:38 +02:00
) . lean ( ) ;
if ( myrec ) {
myrec . qualified = await User . isUserQualified7 ( idapp , myrec . username ) ;
myrec . numNaviEntrato = 0 ;
// myrec.numinvitati = await ListaIngresso.getnumInvitati(idapp, myrec.username);
// myrec.numinvitatiattivi = await ListaIngresso.getnumInvitatiAttivi(idapp, myrec.username);
}
return myrec ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUserShortDataByUsernameTelegram = async function ( idapp , username _telegram ) {
2022-10-08 18:09:38 +02:00
const User = this ;
let regexp = new RegExp ( ` ^ ${ username _telegram } $ ` , 'i' ) ;
const myrec = await User . findOne ( {
2022-12-10 02:01:17 +01:00
'idapp' : idapp ,
'profile.username_telegram' : { $regex : regexp } ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} ,
this . getProjectUser ( ) ,
2022-10-08 18:09:38 +02:00
) . lean ( ) ;
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 ) ;
2022-02-03 00:33:15 +01:00
myrec . numNaviEntrato = 0 ;
// myrec.numinvitati = await ListaIngresso.getnumInvitati(idapp, myrec.username);
// myrec.numinvitatiattivi = await ListaIngresso.getnumInvitatiAttivi(idapp, myrec.username);
2020-02-07 22:08:46 +01:00
}
2021-10-08 00:38:35 +02:00
return myrec ;
2019-12-31 00:44:53 +01:00
} ;
2022-12-10 02:01:17 +01: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 ;
2022-02-03 00:33:15 +01:00
let arrrec = [ ] ;
2020-05-10 21:07:51 +02:00
2020-05-13 01:32:27 +02:00
let myq = {
2020-05-10 21:07:51 +02:00
idapp ,
aportador _solidario : username ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2020-05-13 01:32:27 +02:00
} ;
if ( ! includemyself ) {
2022-12-10 02:01:17 +01:00
myq = { ... myq , username : { $ne : username } } ;
2020-05-13 01:32:27 +02: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
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getQueryQualified = function ( ) {
2020-05-10 21:07:51 +02:00
return [
{
2021-10-08 00:38:35 +02:00
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
2020-05-10 21:07:51 +02:00
} ,
{
$or : [
{
2021-10-08 00:38:35 +02:00
'profile.special_req' : true ,
2020-05-10 21:07:51 +02:00
} ,
{
verified _email : true ,
2022-12-10 02:01:17 +01:00
'profile.teleg_id' : { $gt : 1 } ,
2020-05-10 21:07:51 +02:00
'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 : [
2022-12-10 02:01:17 +01:00
{ 'profile.link_payment' : { $exists : true } } ,
{ 'profile.email_paypal' : { $exists : true } } ,
{ 'profile.payeer_id' : { $exists : true } } ,
{ 'profile.advcash_id' : { $exists : true } } ,
{ 'profile.revolut' : { $exists : true } } ,
2020-07-02 22:00:58 +02:00
] ,
// 'profile.paymenttypes': { "$in": ['paypal'] },
2020-05-10 21:07:51 +02:00
// $where: "this.profile.paymenttypes.length >= 1",
2021-10-08 00:38:35 +02:00
} ] ,
} ,
2020-05-10 21:07:51 +02:00
] ;
2021-10-08 00:38:35 +02:00
} ;
2020-02-19 16:09:16 +01:00
2022-12-10 02:01:17 +01:00
UserSchema . statics . isUserQualified7 = async function ( idapp , username ) {
2020-02-19 16:09:16 +01:00
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 ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . isUserResidente = async function ( idapp , username ) {
2021-03-17 02:24:11 +01:00
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
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . isUserConsiglio = async function ( idapp , username ) {
2021-04-30 01:31:12 +02:00
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
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . isUserVisuProjects = async function ( idapp , username ) {
2021-03-17 02:24:11 +01:00
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 ;
}
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . isUserAlreadyQualified = async function ( idapp , username ) {
2020-06-08 13:31:05 +02:00
const User = this ;
if ( username === undefined )
return false ;
const myquery = {
'idapp' : idapp ,
'username' : username ,
2022-12-10 02:01:17 +01:00
'profile.qualified' : { $exists : true , $eq : true } ,
2020-06-08 13:31:05 +02:00
} ;
const myrec = await User . findOne ( myquery ) ;
return ! ! myrec ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . isUserAlreadyQualified _2Invitati = async function (
idapp , username ) {
2020-06-08 13:31:05 +02:00
const User = this ;
if ( username === undefined )
return false ;
const myquery = {
'idapp' : idapp ,
'username' : username ,
2022-12-10 02:01:17 +01:00
'profile.qualified_2invitati' : { $exists : true , $eq : true } ,
2020-06-08 13:31:05 +02:00
} ;
const myrec = await User . findOne ( myquery ) ;
return ! ! myrec ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . setUserQualified = async function ( idapp , username ) {
2020-06-08 13:31:05 +02:00
const User = this ;
if ( username === undefined )
return false ;
const myquery = {
'idapp' : idapp ,
'username' : username ,
} ;
2021-10-08 00:38:35 +02:00
const myrec = await User . findOneAndUpdate ( myquery ,
2022-12-10 02:01:17 +01:00
{ $set : { 'profile.qualified' : true } } , { new : false } ) ;
2020-06-08 13:31:05 +02:00
return ! ! myrec ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . setVerifiedByAportador = async function (
idapp , username , valuebool ) {
2021-12-29 18:26:08 +01:00
const User = this ;
if ( username === undefined )
return false ;
const myquery = {
'idapp' : idapp ,
'username' : username ,
} ;
2022-12-10 02:01:17 +01:00
const userver = await User . findOne ( myquery , { verified _by _aportador : 1 } ) . lean ( ) ;
2022-02-28 17:20:47 +01:00
2022-03-10 23:19:56 +01:00
let scrivi = true ;
if ( userver ) {
scrivi = userver . verified _by _aportador !== valuebool ;
}
if ( scrivi ) {
2022-02-28 17:20:47 +01:00
const myrec = await User . findOneAndUpdate ( myquery ,
2022-12-10 02:01:17 +01:00
{ $set : { 'verified_by_aportador' : valuebool } } , { new : false } ) ;
2022-02-28 17:20:47 +01:00
return ! ! myrec ;
} else {
return false ;
}
2021-12-29 18:26:08 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . setnotask _verif = async function (
idapp , username , valuebool ) {
2022-02-26 17:35:50 +01:00
const User = this ;
if ( username === undefined )
return false ;
const myquery = {
'idapp' : idapp ,
'username' : username ,
} ;
const myrec = await User . findOneAndUpdate ( myquery ,
2022-12-10 02:01:17 +01:00
{ $set : { 'notask_verif' : valuebool } } , { new : false } ) ;
2022-02-26 17:35:50 +01:00
return ! ! myrec ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . setaportador _solidario = async function (
idapp , username , usernameAportador ) {
2022-03-04 22:30:50 +01:00
const User = this ;
if ( username === undefined )
return false ;
const myquery = {
'idapp' : idapp ,
'username' : username ,
} ;
const myrec = await User . findOneAndUpdate ( myquery ,
2022-12-10 02:01:17 +01:00
{ $set : { 'aportador_solidario' : usernameAportador } } , { new : false } ) ;
2022-03-04 22:30:50 +01:00
return ! ! myrec ;
} ;
2024-02-08 01:34:30 +01:00
UserSchema . statics . setNewsletter = async function (
idapp , username , newsletter _on ) {
const User = this ;
if ( username === undefined )
return false ;
const myquery = {
'idapp' : idapp ,
'username' : username ,
} ;
const myrec = await User . findOneAndUpdate ( myquery ,
{ $set : { 'news_on' : newsletter _on } } , { new : false } ) ;
return ! ! myrec ;
} ;
UserSchema . statics . setEmailErrata = async function (
idapp , username , email _errata ) {
const User = this ;
if ( username === undefined )
return false ;
const myquery = {
'idapp' : idapp ,
'username' : username ,
} ;
const myrec = await User . findOneAndUpdate ( myquery ,
{ $set : { email _errata } } , { new : false } ) ;
return ! ! myrec ;
} ;
UserSchema . statics . isEmailErrata = async function ( idapp , username ) {
const User = this ;
return await User . findOne ( {
idapp , username ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} ) . then ( ( rec ) => {
return ( ( rec ) ? rec . email _errata : false ) ;
} ) . catch ( ( e ) => {
return false ;
} ) ;
} ;
UserSchema . statics . isNewsletterOn = async function ( idapp , username ) {
const User = this ;
return await User . findOne ( {
idapp , username ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} ) . then ( ( rec ) => {
return ( ( rec ) ? rec . news _on : false ) ;
} ) . catch ( ( e ) => {
console . error ( 'isNewsletterOn' , e ) ;
return false ;
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . setVerifiedByAportadorToALL = async function ( ) {
2021-12-29 18:26:08 +01:00
2022-12-10 02:01:17 +01:00
return await User . updateMany ( { } , { $set : { 'verified_by_aportador' : true } } ,
{ new : false } ) ;
2021-12-29 18:26:08 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . setUserQualified _2Invitati = async function (
idapp , username ) {
2020-06-08 13:31:05 +02:00
const User = this ;
if ( username === undefined )
return false ;
const myquery = {
'idapp' : idapp ,
'username' : username ,
} ;
2021-10-08 00:38:35 +02:00
const myrec = await User . findOneAndUpdate ( myquery ,
2022-12-10 02:01:17 +01:00
{ $set : { 'profile.qualified_2invitati' : true } } , { new : false } ) ;
2020-06-08 13:31:05 +02:00
return ! ! myrec ;
} ;
2022-02-03 00:33:15 +01:00
/ *
2021-10-08 00:38:35 +02:00
UserSchema . statics . isUserQualified9 = async function ( idapp , username ) {
2020-03-10 21:44:14 +01:00
const User = this ;
if ( username === undefined )
return false ;
qualified = await User . isUserQualified7 ( idapp , username ) ;
2022-02-03 00:33:15 +01:00
// numinvitatiattivi = await ListaIngresso.getnumInvitatiAttivi(idapp, username);
2020-03-10 21:44:14 +01:00
return qualified && ( numinvitatiattivi >= 2 ) ;
} ;
2022-02-03 00:33:15 +01:00
* /
2020-03-10 21:44:14 +01:00
2020-09-04 00:06:49 +02:00
/ *
2020-02-19 16:09:16 +01:00
UserSchema . statics . getnumPaymentOk = function ( idapp ) {
const User = this ;
2022-12-23 00:36:35 +01:00
return await User . countDocuments ( {
2020-02-19 16:09:16 +01:00
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
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsersNationalityQuery = function ( idapp ) {
2020-01-30 01:19:25 +01:00
const query = [
{
2021-10-08 00:38:35 +02:00
$match : {
idapp ,
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
2021-10-08 00:38:35 +02:00
} ,
2020-01-30 01:19:25 +01:00
} ,
{
2022-12-10 02:01:17 +01:00
$group : { _id : '$profile.nationality' , count : { $sum : 1 } } ,
2020-01-30 01:19:25 +01:00
} ,
{
2022-12-10 02:01:17 +01:00
$sort : { count : - 1 } ,
2021-10-08 00:38:35 +02:00
} ,
2020-01-30 01:19:25 +01:00
] ;
2021-10-08 00:38:35 +02:00
return query ;
2020-01-30 01:19:25 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getindOrderDuplicate = function ( idapp ) {
2020-01-30 01:19:25 +01:00
const User = this ;
2021-10-08 00:38:35 +02:00
return User . aggregate ( User . getUsersNationalityQuery ( idapp ) ) . then ( ris => {
// console.table(ris);
2022-03-03 20:32:04 +01:00
return ris ;
2021-10-08 00:38:35 +02:00
} ) ;
2020-01-30 01:19:25 +01:00
} ;
2022-12-10 02:01:17 +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 ,
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . AportadorOrig = function ( idapp , id ) {
2020-03-10 21:44:14 +01:00
const User = this ;
return User . findOne ( {
'_id' : id ,
'idapp' : idapp ,
} ) . then ( ( rec ) => {
if ( rec )
return rec . aportador _iniziale ;
else
return '' ;
} ) ;
} ;
2022-12-10 02:01:17 +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 ,
2022-12-10 02:01:17 +01:00
'date_tokenforgot' : { $gte : tools . IncDateNow ( - 1000 * 60 * 60 * 4 ) } , // 4 ore fa!
2018-12-24 20:31:02 +01:00
'idapp' : idapp ,
} ) ;
} ;
2022-12-11 02:57:35 +01:00
UserSchema . statics . findByLinkTokenforgotCode = function ( idapp , email , tokenforgot _code ) {
const User = this ;
return User . findOne ( {
email ,
tokenforgot _code ,
date _tokenforgot : { $gte : tools . IncDateNow ( - 1000 * 60 * 60 * 4 ) } , // 4 ore fa!
idapp ,
} ) ;
} ;
2022-12-11 18:04:02 +01:00
UserSchema . statics . createNewRequestPwd = function ( idapp , email , code ) {
2022-03-08 01:01:20 +01:00
const User = this ;
const sendemail = require ( '../sendemail' ) ;
2023-01-29 22:22:48 +01:00
console . log ( 'createNewRequestPwd' ) ;
2022-12-11 18:04:02 +01:00
if ( code && code . length === 6 ) {
return User . findByLinkTokenforgotCode ( idapp , email , code )
. then ( ( user ) => {
if ( user )
return { ris : true , link : tools . getlinkRelativeRequestNewPassword ( idapp , email , user . tokenforgot ) } ;
else
return { ris : false } ;
} ) . catch ( ( e ) => {
console . log ( ' Err createNewRequestPwd' , e . message ) ;
res . status ( 400 ) . send ( ) ;
2022-03-08 01:01:20 +01:00
} ) ;
2022-12-11 18:04:02 +01:00
} else {
return User . findByEmail ( idapp , email ) . then ( async ( user ) => {
if ( ! user ) {
return { ris : false } ;
} else {
// Creo il tokenforgot
2023-12-08 14:07:32 +01:00
user . tokenforgot = jwt . sign ( { _id : 'prova123##' , smart : user . _id . toHexString ( ) } , process . env . SIGNCODE ) .
2022-12-11 18:04:02 +01:00
toString ( ) ;
user . date _tokenforgot = new Date ( ) ;
user . tokenforgot _code = 100000 + Math . round ( Math . random ( ) * 899999 ) ;
user . lasttimeonline = new Date ( ) ;
return await user . save ( ) . then ( async ( ) => {
await sendemail . sendEmail _RequestNewPassword ( user . lang , user , user . email , user . idapp , user . tokenforgot , user . tokenforgot _code ) ;
return { ris : true } ;
} ) ;
}
2022-03-08 01:01:20 +01:00
2022-12-11 18:04:02 +01:00
} ) ;
}
2022-03-08 01:01:20 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . createNewRequestPwdByUsernameAndGetLink = async function ( idapp , username ) {
2022-03-08 01:01:20 +01:00
const User = this ;
const user = await User . findOne ( {
idapp ,
username ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2022-03-08 01:01:20 +01:00
} ) ;
if ( user ) {
2023-12-07 08:34:24 +01:00
const additionalData = {
code : user . username ,
} ;
const prova = 'dasdas1231#11' ;
2022-03-08 01:01:20 +01:00
// Creo il tokenforgot
2023-12-08 14:07:32 +01:00
user . tokenforgot = jwt . sign ( { _id : prova , smart : user . _id . toHexString ( ) , ... additionalData } , process . env . SIGNCODE ) .
2022-12-10 02:01:17 +01:00
toString ( ) ;
2022-03-08 01:01:20 +01:00
user . date _tokenforgot = new Date ( ) ;
2022-12-11 02:57:35 +01:00
user . tokenforgot _code = 100000 + Math . round ( Math . random ( ) * 899999 ) ;
2022-03-08 01:01:20 +01:00
user . lasttimeonline = new Date ( ) ;
2022-12-11 02:57:35 +01:00
user . code _pwd _reset = 0 ;
2022-03-08 01:01:20 +01:00
2022-09-11 11:45:33 +02:00
return await user . save ( ) . then ( ( ) => {
2022-12-11 02:57:35 +01:00
return tools . getlinkRequestNewPassword ( idapp , user . email , user . tokenforgot , user . tokenforgot _code ) ;
2022-03-08 01:01:20 +01:00
} ) ;
}
return '' ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getifRegTokenIsValid = async function ( idapp , tokenreg ) {
2022-11-02 22:32:54 +01:00
const User = this ;
let regexp = new RegExp ( ` ^ ${ tokenreg } $ ` , 'i' ) ;
const user = await User . findOne ( {
idapp ,
2022-12-10 02:01:17 +01:00
tokenreg : { $regex : regexp }
2022-11-02 22:32:54 +01:00
} ) ;
if ( user && user . date _tokenreg ) {
return user . date _tokenreg > ( new Date ( ) . getTime ( ) ) ;
}
return false ;
}
2022-12-10 02:01:17 +01:00
UserSchema . statics . createNewReqRegistrationGetLink = async function ( idapp , username ) {
2022-11-02 22:32:54 +01:00
const User = this ;
const user = await User . findOne ( {
idapp ,
username ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2022-11-02 22:32:54 +01:00
} ) ;
if ( user ) {
2022-12-12 18:25:13 +01:00
if ( true ) {
2022-11-02 22:32:54 +01:00
// Se è scaduto, ne crea uno nuovo
// Creo il tokenforgot
2022-12-12 18:25:13 +01:00
2024-04-04 18:43:17 +02:00
/ *
2022-12-12 18:25:13 +01:00
if ( ! user . date _tokenreg || ( ! user . tokenreg ) || ( user . tokenreg && ( user . date _tokenreg < new Date ( ) . getTime ( ) ) ) ) {
let mycodestr = user . _id . toHexString ( ) + new Date ( ) . getTime ( ) . toString ( ) ;
user . tokenreg = jwt . sign ( mycodestr , process . env . SIGNCODE ) .
toString ( ) ;
if ( user . tokenreg ) {
try {
user . tokenreg = user . tokenreg . replaceAll ( '.' , '' ) ;
user . tokenreg = user . tokenreg . replaceAll ( '/' , '' ) ;
user . tokenreg = user . tokenreg . slice ( - 8 ) ;
} catch ( e ) {
console . error ( 'err' , e ) ;
}
2022-11-06 13:39:01 +01:00
}
}
2022-11-02 22:32:54 +01:00
2022-12-12 18:25:13 +01:00
user . date _tokenreg = tools . AddDate ( new Date ( ) , 1 ) ;
2024-04-04 18:43:17 +02:00
2022-11-02 22:32:54 +01:00
return await user . save ( ) . then ( ( ) => {
return user . tokenreg ;
} ) ;
2024-04-04 18:43:17 +02:00
* /
2022-11-02 22:32:54 +01:00
}
}
return '' ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . findByEmail = function ( idapp , email , onlyifVerifiedByAportador ) {
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 ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2022-03-04 22:30:50 +01:00
} ) . then ( ( rec ) => {
2022-12-17 14:11:37 +01:00
/ * i f ( r e c & & o n l y i f V e r i f i e d B y A p o r t a d o r ) {
2022-03-04 22:30:50 +01:00
if ( tools . getAskToVerifyReg ( idapp ) ) {
if ( ! rec . verified _by _aportador )
return null ;
}
2022-12-17 14:11:37 +01:00
} * /
2022-03-04 22:30:50 +01:00
return rec ;
2018-12-24 20:31:02 +01:00
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getLastUser = function ( idapp ) {
2020-01-27 15:07:53 +01:00
const User = this ;
2020-05-04 19:34:41 +02:00
return User . findOne ( {
idapp ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} ) . sort ( { index : - 1 } ) ;
2020-01-27 15:07:53 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . findByIndex = function ( idapp , index ) {
2020-05-11 22:43:14 +02:00
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 ,
2021-10-08 00:38:35 +02:00
$or : [
2022-12-10 02:01:17 +01:00
{ 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
}
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . findByOldOrder = function ( idapp , old _order ) {
2020-05-10 21:07:51 +02:00
const User = this ;
try {
return User . findOne ( {
idapp ,
old _order ,
2021-10-08 00:38:35 +02:00
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
2020-05-10 21:07:51 +02:00
} ) ;
} catch ( e ) {
console . error ( e . message ) ;
}
} ;
2020-02-19 16:09:16 +01:00
2022-12-10 02:01:17 +01: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 {
2022-12-10 02:01:17 +01:00
const myrec = await User . findOne ( { idapp : this . idapp } ) .
limit ( 1 ) .
sort ( { index : - 1 } ) ;
2020-05-10 21:07:51 +02:00
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
}
} ) ;
2022-12-10 02:01:17 +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 : {
2022-12-10 02:01:17 +01:00
tokens : { token } ,
2021-10-08 00:38:35 +02:00
} ,
2018-12-24 20:31:02 +01:00
} ) ;
} ;
2024-04-25 23:26:16 +02:00
// TODO: Cancellare i token che non hanno refreshToken, ad esempio quando l'utente esce dal browser
// TODO: Cancellare i token con data_login meno di 2 giorni fa, per esempio se l'utente si è disconnesso dal browser ma è ancora online
2024-04-11 17:12:21 +02:00
UserSchema . statics . SvuotaTuttiGliAccessiOnlineConToken = async function ( idapp ) {
const User = this ;
return await User . updateMany ( { idapp } ,
2024-04-26 00:49:05 +02:00
{
$pull : {
tokens :
{
$or : [ { refreshToken : { $exists : false } } ,
{ refreshToken : { $exists : true , $eq : '' } }
]
} ,
}
} ) ;
2024-04-25 23:26:16 +02:00
} ;
2024-04-11 17:12:21 +02:00
2022-12-10 02:01:17 +01:00
UserSchema . statics . getEmailByUsername = async function ( idapp , username ) {
2019-10-27 00:37:10 +02:00
const User = this ;
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2020-05-04 19:34:41 +02:00
idapp , username ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2021-10-08 00:38:35 +02:00
} ) . then ( ( arrrec ) => {
return ( ( arrrec ) ? arrrec . email : '' ) ;
} ) . catch ( ( e ) => {
console . error ( 'getEmailByUsername' , e ) ;
} ) ;
2019-10-27 00:37:10 +02:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsernameById = async function ( idapp , id ) {
2020-03-10 21:44:14 +01:00
const User = this ;
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2020-05-04 19:34:41 +02:00
idapp , _id : id ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} , { username : 1 } ) . then ( ( myuser ) => {
2021-10-08 00:38:35 +02:00
return ( ( myuser ) ? myuser . username : '' ) ;
} ) . catch ( ( e ) => {
2020-03-10 21:44:14 +01:00
2021-10-08 00:38:35 +02:00
} ) ;
2020-03-10 21:44:14 +01:00
} ;
2024-02-08 01:34:30 +01:00
UserSchema . statics . getUsernameByEmail = async function ( idapp , email ) {
const User = this ;
return await User . findOne ( {
idapp , email ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} , { username : 1 } ) . then ( ( myuser ) => {
return ( ( myuser ) ? myuser . username : '' ) ;
} ) . catch ( ( e ) => {
} ) ;
} ;
2020-03-10 21:44:14 +01:00
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUserById = function ( idapp , id ) {
2020-03-10 21:44:14 +01:00
const User = this ;
2020-05-04 19:34:41 +02:00
return User . findOne ( {
idapp , _id : id ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2021-10-08 00:38:35 +02:00
} ) ;
2020-03-10 21:44:14 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUserByUsername = function ( idapp , username ) {
2022-02-03 00:33:15 +01:00
const User = this ;
2023-06-01 11:39:53 +02:00
return User . findOne ( {
2022-02-03 00:33:15 +01:00
idapp ,
username ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2022-02-03 00:33:15 +01:00
} ) ;
} ;
2024-04-11 18:55:15 +02:00
UserSchema . statics . getUserByUsernameTelegram = function ( idapp , username _telegram ) {
const User = this ;
2024-04-25 23:26:16 +02:00
if ( username _telegram [ 0 ] === '@' ) {
2024-04-11 18:55:15 +02:00
username _telegram = username _telegram . substring ( 1 ) ;
}
return User . findOne ( {
idapp ,
'profile.username_telegram' : username _telegram ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . isMyFriend = async function ( idapp , username , myusername ) {
2022-01-28 18:15:56 +01:00
const myfriends = await User . getUsernameFriendsByUsername ( idapp , myusername ) ;
if ( myfriends ) {
2022-09-11 11:45:33 +02:00
return await myfriends . includes ( username ) ;
2022-01-28 18:15:56 +01:00
} else {
return false ;
}
} ;
2023-01-08 02:17:01 +01:00
UserSchema . statics . isMyHandShake = async function ( idapp , username , myusername ) {
const myhandshake = await User . getUsernameHandShakeByUsername ( idapp , myusername ) ;
if ( myhandshake ) {
return await myhandshake . includes ( username ) ;
} else {
return false ;
}
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUserProfileByUsername = async function (
idapp , username , myusername , usaSuperPower , myperm = '' ) {
2022-01-28 18:15:56 +01:00
const User = this ;
// If is my Friend, then can show all
const ismyfriend = await User . isMyFriend ( idapp , username , myusername ) ;
let perm = tools . Perm . PERM _NONE ;
if ( ismyfriend ) {
perm = tools . Perm . PERM _FRIEND ;
}
if ( username === myusername ) {
perm = tools . Perm . PERM _ALL ;
} else {
if ( await User . canHavePower ( myperm ) && usaSuperPower ) {
perm = tools . Perm . PERM _ALL ;
}
}
2022-01-03 21:53:50 +01:00
let whatToShow = { } ;
2022-01-28 18:15:56 +01:00
if ( perm === tools . Perm . PERM _NONE ) {
whatToShow = {
lang : 1 ,
index : 1 ,
username : 1 ,
2022-09-16 17:38:49 +02:00
aportador _solidario : 1 ,
name : 1 ,
surname : 1 ,
2022-12-10 02:01:17 +01:00
lasttimeonline : 1 ,
2022-01-28 18:15:56 +01:00
deleted : 1 ,
sospeso : 1 ,
2022-08-04 17:30:57 +02:00
reported : 1 ,
2022-08-09 17:32:06 +02:00
date _report : 1 ,
username _who _report : 1 ,
2022-01-28 18:15:56 +01:00
verified _email : 1 ,
verified _by _aportador : 1 ,
2022-09-16 17:38:49 +02:00
'profile.nationality' : 1 ,
'profile.mygroups' : 1 ,
'profile.mycircuits' : 1 ,
'profile.qualifica' : 1 ,
'profile.biografia' : 1 ,
'profile.teleg_id' : 1 ,
2022-02-24 23:36:44 +01:00
'profile.username_telegram' : 1 ,
2022-09-16 17:38:49 +02:00
'profile.firstname_telegram' : 1 ,
'profile.lastname_telegram' : 1 ,
2024-03-21 22:58:46 +01:00
// 'profile.intcode_cell': 1,
// 'profile.cell': 1,
2022-09-16 17:38:49 +02:00
'profile.website' : 1 ,
2022-01-28 18:15:56 +01:00
'profile.img' : 1 ,
2022-09-16 17:38:49 +02:00
'profile.sex' : 1 ,
'profile.dateofbirth' : 1 ,
'profile.born_city_id' : 1 ,
'profile.born_province' : 1 ,
'profile.born_country' : 1 ,
2023-03-17 19:07:31 +01:00
'profile.resid_province' : 1 ,
2023-11-26 01:38:02 +01:00
'profile.resid_card' : 1 ,
2022-12-03 03:14:26 +01:00
'profile.calc' : 1 ,
2023-01-06 15:51:48 +01:00
'profile.handshake' : 1 ,
'profile.friends' : 1 ,
2022-09-16 17:38:49 +02:00
email : 1 ,
date _reg : 1 ,
'useraport.username' : 1 ,
'useraport.profile.img' : 1 ,
2022-01-28 18:15:56 +01:00
} ;
} else if ( perm === tools . Perm . PERM _FRIEND ) {
whatToShow = {
lang : 1 ,
index : 1 ,
username : 1 ,
aportador _solidario : 1 ,
name : 1 ,
surname : 1 ,
2022-12-10 02:01:17 +01:00
lasttimeonline : 1 ,
2022-01-28 18:15:56 +01:00
deleted : 1 ,
sospeso : 1 ,
2022-08-04 17:30:57 +02:00
reported : 1 ,
2022-08-09 17:32:06 +02:00
date _report : 1 ,
username _who _report : 1 ,
2022-01-28 18:15:56 +01:00
verified _email : 1 ,
verified _by _aportador : 1 ,
'profile.nationality' : 1 ,
2022-07-21 00:21:03 +02:00
'profile.mygroups' : 1 ,
2022-09-03 13:06:58 +02:00
'profile.mycircuits' : 1 ,
2022-01-28 18:15:56 +01:00
'profile.qualifica' : 1 ,
'profile.biografia' : 1 ,
'profile.teleg_id' : 1 ,
'profile.username_telegram' : 1 ,
2022-03-11 12:38:01 +01:00
'profile.firstname_telegram' : 1 ,
'profile.lastname_telegram' : 1 ,
2022-02-24 23:36:44 +01:00
'profile.intcode_cell' : 1 ,
'profile.cell' : 1 ,
2022-01-28 18:15:56 +01:00
'profile.website' : 1 ,
'profile.img' : 1 ,
'profile.sex' : 1 ,
'profile.dateofbirth' : 1 ,
2022-02-21 13:12:27 +01:00
'profile.born_city_id' : 1 ,
2022-01-28 18:15:56 +01:00
'profile.born_province' : 1 ,
'profile.born_country' : 1 ,
2023-03-17 19:07:31 +01:00
'profile.resid_province' : 1 ,
2023-11-26 01:38:02 +01:00
'profile.resid_card' : 1 ,
2022-12-03 03:14:26 +01:00
'profile.calc' : 1 ,
2023-01-06 15:51:48 +01:00
'profile.handshake' : 1 ,
'profile.friends' : 1 ,
2022-01-28 18:15:56 +01:00
email : 1 ,
date _reg : 1 ,
2022-09-16 17:38:49 +02:00
'useraport.username' : 1 ,
'useraport.profile.img' : 1 ,
2022-01-28 18:15:56 +01:00
} ;
} else if ( perm === tools . Perm . PERM _ALL ) {
2022-01-03 21:53:50 +01:00
whatToShow = {
lang : 1 ,
index : 1 ,
username : 1 ,
aportador _solidario : 1 ,
name : 1 ,
surname : 1 ,
2022-12-10 02:01:17 +01:00
lasttimeonline : 1 ,
2022-01-03 21:53:50 +01:00
deleted : 1 ,
sospeso : 1 ,
2022-08-04 17:30:57 +02:00
reported : 1 ,
2022-08-09 17:32:06 +02:00
date _report : 1 ,
username _who _report : 1 ,
2022-01-03 21:53:50 +01:00
verified _email : 1 ,
verified _by _aportador : 1 ,
2022-02-26 17:35:50 +01:00
notask _verif : 1 ,
2022-01-03 21:53:50 +01:00
'profile.nationality' : 1 ,
2022-07-21 00:21:03 +02:00
'profile.mygroups' : 1 ,
2022-09-03 13:06:58 +02:00
'profile.mycircuits' : 1 ,
2022-01-20 00:39:06 +01:00
'profile.qualifica' : 1 ,
2022-01-03 21:53:50 +01:00
'profile.biografia' : 1 ,
'profile.teleg_id' : 1 ,
'profile.username_telegram' : 1 ,
2022-03-11 12:38:01 +01:00
'profile.firstname_telegram' : 1 ,
'profile.lastname_telegram' : 1 ,
2022-02-24 23:36:44 +01:00
'profile.intcode_cell' : 1 ,
'profile.cell' : 1 ,
2022-01-23 23:25:34 +01:00
'profile.website' : 1 ,
2022-01-03 21:53:50 +01:00
'profile.img' : 1 ,
'profile.sex' : 1 ,
'profile.dateofbirth' : 1 ,
2022-02-21 13:12:27 +01:00
'profile.born_city_id' : 1 ,
2022-01-03 21:53:50 +01:00
'profile.born_province' : 1 ,
'profile.born_country' : 1 ,
2023-03-17 19:07:31 +01:00
'profile.resid_province' : 1 ,
2023-11-26 01:38:02 +01:00
'profile.resid_card' : 1 ,
2022-12-03 03:14:26 +01:00
'profile.calc' : 1 ,
2023-01-06 15:51:48 +01:00
'profile.handshake' : 1 ,
'profile.friends' : 1 ,
2022-02-21 13:12:27 +01:00
'mycities' : 1 ,
'comune' : 1 ,
2022-01-03 21:53:50 +01:00
email : 1 ,
date _reg : 1 ,
2022-09-16 17:38:49 +02:00
'useraport.username' : 1 ,
'useraport.profile.img' : 1 ,
2022-01-03 21:53:50 +01:00
} ;
}
2023-01-13 12:29:28 +01:00
2023-01-12 01:03:10 +01:00
let regexpusername = new RegExp ( ` ^ ${ username } $ ` , 'i' ) ;
2022-01-03 21:53:50 +01:00
2022-02-21 13:12:27 +01:00
const myfind = {
2023-01-12 01:03:10 +01:00
idapp , username : { $regex : regexpusername } ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2022-02-21 13:12:27 +01:00
} ;
const query = [
2022-12-10 02:01:17 +01:00
{ $match : myfind } ,
2022-02-21 13:12:27 +01:00
{
$lookup : {
from : 'cities' ,
localField : 'profile.born_city_id' ,
foreignField : '_id' ,
as : 'mycities' ,
} ,
} ,
2022-08-18 05:26:50 +02:00
{
2022-09-16 17:38:49 +02:00
'$lookup' : {
'from' : 'users' ,
2022-08-18 05:26:50 +02:00
let : {
2022-09-16 17:38:49 +02:00
'idapp' : '$idapp' ,
'user_name' : '$aportador_solidario' ,
2022-08-18 05:26:50 +02:00
} ,
pipeline : [
2022-09-16 17:38:49 +02:00
{
'$match' : {
'$expr' : {
2022-08-18 05:26:50 +02:00
$and : [
2022-12-10 02:01:17 +01:00
{ $eq : [ '$username' , '$$user_name' ] } ,
{ $eq : [ '$idapp' , '$$idapp' ] } ,
2022-09-16 17:38:49 +02:00
] ,
} ,
} ,
} ,
2022-08-18 05:26:50 +02:00
] ,
2022-09-16 17:38:49 +02:00
as : 'useraport' ,
} ,
2022-08-18 05:26:50 +02:00
} ,
{
2022-09-16 17:38:49 +02:00
$unwind :
2022-12-10 02:01:17 +01:00
{
path : '$useraport' ,
preserveNullAndEmptyArrays : true ,
} ,
2022-08-18 05:26:50 +02:00
} ,
2022-02-21 13:12:27 +01:00
{
'$replaceRoot' : {
'newRoot' : {
'$mergeObjects' : [
{
'$arrayElemAt' : [
'$mycities' ,
0 ,
] ,
} ,
'$$ROOT' ,
] ,
} ,
} ,
} ,
2022-12-10 02:01:17 +01:00
{ $project : whatToShow } ,
2022-02-21 13:12:27 +01:00
] ;
try {
const ris = await User . aggregate ( query ) ;
2022-12-03 03:14:26 +01:00
if ( ris && ris . length > 0 ) {
ris [ 0 ] . profile . calc = await User . calcOtherByUser ( idapp , ris [ 0 ] . _id ) ;
2022-02-21 13:12:27 +01:00
return ris [ 0 ] ;
2022-12-03 03:14:26 +01:00
}
2022-02-21 13:12:27 +01:00
} catch ( e ) {
return null ;
}
return null ;
/ *
return User . findOne ( {
2022-01-03 21:53:50 +01:00
} , whatToShow ) . then ( ( rec ) => {
return ( rec . _doc ) ;
} ) . catch ( ( e ) => {
return null ;
} ) ;
2022-02-21 13:12:27 +01:00
* /
2022-01-03 21:53:50 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getArrUsernameFromFieldByUsername = async function (
idapp , username , field , subfield ) {
2022-01-07 01:18:01 +01:00
2022-01-15 12:25:57 +01:00
const myobj = { } ;
myobj [ field + '.' + subfield ] = 1 ;
let arrrec = await User . findOne ( {
2022-01-07 01:18:01 +01:00
idapp , 'username' : username ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2022-01-15 12:25:57 +01:00
} , myobj ) . then ( ( ris ) => ris ? ris . _doc [ field ] [ subfield ] : [ ] ) ;
2022-02-08 23:14:08 +01:00
if ( arrrec && arrrec . length > 0 ) {
2022-01-15 12:25:57 +01:00
return arrrec . map ( m => m . username ) ;
}
return [ ] ;
2022-01-07 01:18:01 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsernameReqFriendsByUsername = async function (
idapp , username ) {
2022-01-14 23:54:33 +01:00
2022-09-11 11:45:33 +02:00
return await this . getArrUsernameFromFieldByUsername ( idapp , username , 'profile' ,
2022-12-10 02:01:17 +01:00
'req_friends' ) ;
2022-01-15 12:25:57 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsernameFriendsByUsername = async function (
idapp , username ) {
2022-01-15 12:25:57 +01:00
2022-09-11 11:45:33 +02:00
return await this . getArrUsernameFromFieldByUsername ( idapp , username , 'profile' ,
2022-12-10 02:01:17 +01:00
'friends' ) ;
2022-01-15 12:25:57 +01:00
2023-01-06 15:51:48 +01:00
} ;
UserSchema . statics . getUsernameHandShakeByUsername = async function (
idapp , username ) {
return await this . getArrUsernameFromFieldByUsername ( idapp , username , 'profile' ,
'handshake' ) ;
2022-01-15 12:25:57 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsernameGroupsByUsername = async function (
idapp , username ) {
2022-02-03 00:33:15 +01:00
2022-09-11 11:45:33 +02:00
return await this . getArrUsernameFromFieldByUsername ( idapp , username , 'profile' ,
2022-12-10 02:01:17 +01:00
'mygroups' ) ;
2022-09-02 02:25:38 +02:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsernameCircuitsByUsername = async function (
idapp , username ) {
2022-09-02 02:25:38 +02:00
2022-09-11 11:45:33 +02:00
return await this . getArrUsernameFromFieldByUsername ( idapp , username , 'profile' ,
2022-12-10 02:01:17 +01:00
'mycircuits' ) ;
2022-02-03 00:33:15 +01:00
} ;
2022-01-15 12:25:57 +01:00
// Rimuovo l'Amicizia
2022-12-10 02:01:17 +01:00
UserSchema . statics . removeFriend = async function (
idapp , username , usernameDest ) {
return await User . updateOne ( { idapp , username } ,
{ $pull : { 'profile.friends' : { username : { $in : [ usernameDest ] } } } } ) ;
2022-01-15 12:25:57 +01:00
} ;
2023-01-06 15:51:48 +01:00
// Rimuovo l'Amicizia
UserSchema . statics . removeHandShake = async function (
idapp , username , usernameDest ) {
return await User . updateOne ( { idapp , username } ,
{ $pull : { 'profile.handshake' : { username : { $in : [ usernameDest ] } } } } ) ;
} ;
2022-02-03 00:33:15 +01:00
// Rimuovo il Gruppo
2022-12-10 02:01:17 +01:00
UserSchema . statics . removeFromMyGroups = async function (
idapp , username , groupnameDest ) {
return await User . updateOne ( { idapp , username } ,
{ $pull : { 'profile.mygroups' : { groupname : { $in : [ groupnameDest ] } } } } ) ;
2022-02-03 00:33:15 +01:00
} ;
2023-06-01 11:39:53 +02:00
// Rimuovo il Circuito
2022-12-10 02:01:17 +01:00
UserSchema . statics . removeFromCircuits = async function ( idapp , username , circuitname ) {
2023-06-01 11:39:53 +02:00
2023-08-27 23:55:31 +02:00
// Elimina la richiesta (se esiste):
update = { $pull : { req _users : { username : { $in : [ username ] } } } } ;
await Circuit . updateOne ( { idapp , name : circuitname } , update ) ;
2023-11-19 23:40:38 +01:00
const ris = await User . updateOne ( { idapp , username } ,
2022-12-10 02:01:17 +01:00
{ $pull : { 'profile.mycircuits' : { circuitname : { $in : [ circuitname ] } } } } ) ;
2022-09-02 02:25:38 +02:00
2023-11-19 23:40:38 +01:00
const circuitId = await Circuit . getCircuitIdByName ( idapp , circuitname ) ;
let remove = false ;
2024-04-25 23:26:16 +02:00
const circuit = await Circuit . findOne ( { idapp , name : circuitname } ) . lean ( ) ;
if ( circuit ) {
// Invio la notifica agli amministratori del circuito
const title = ` L \' utente ${ username } è uscito dal ${ circuitname } ` ;
const msg = ` ` ;
await tools . sendNotifToCircuitsAdmin ( idapp , circuit , true , title , msg , '' )
}
2023-11-19 23:40:38 +01:00
// Se il mio account non è stato utilizzato, allora lo cancello anche questo
const myaccount = await Account . getAccountByUsernameAndCircuitId ( idapp , username , circuitId , false , false , '' , '' ) ;
if ( myaccount && myaccount . totTransato === 0 ) {
remove = true ;
} else {
remove = true ;
}
if ( remove ) {
await Account . removeAccount ( myaccount . _id ) ;
}
return ris ;
2022-09-02 02:25:38 +02:00
} ;
2022-02-03 00:33:15 +01:00
2023-06-01 11:39:53 +02:00
// Aggiungo il Circuito
UserSchema . statics . addCircuitToUser = async function ( idapp , usernameOrig , circuitname , confido , groupname , contocom ) {
let ris = null ;
if ( groupname ) {
ris = await MyGroup . addCircuitFromGroup ( idapp , groupname , circuitname ) ;
// Elimina la richiesta:
update = { $pull : { req _groups : { groupname : { $in : [ groupname ] } } } } ;
await Circuit . updateOne ( { idapp , name : circuitname } , update ) ;
// Elimina eventualmente se era bloccato:
update = { $pull : { refused _groups : { groupname : { $in : [ groupname ] } } } } ;
await Circuit . updateOne ( { idapp , name : circuitname } , update ) ;
} else {
2024-02-23 22:12:36 +01:00
// prima di aggiungerlo controlla se esiste già !
2024-03-21 22:58:46 +01:00
2023-06-01 11:39:53 +02:00
let update = {
2024-02-23 22:12:36 +01:00
$addToSet : { // Utilizziamo $addToSet invece di $push per garantire che l'elemento venga aggiunto solo se non esiste già
2023-06-01 11:39:53 +02:00
'profile.mycircuits' : {
2024-02-23 22:12:36 +01:00
$each : [ {
circuitname ,
date : new Date ( ) ,
} ] ,
}
}
2023-06-01 11:39:53 +02:00
} ;
2024-03-21 22:58:46 +01:00
2024-02-23 22:12:36 +01:00
ris = await User . updateOne ( { idapp , username : usernameOrig , 'profile.mycircuits' : { $not : { $elemMatch : { circuitname } } } } , update ) ;
2023-06-01 11:39:53 +02:00
if ( confido ) {
// Elimina la richiesta:
update = { $pull : { req _users : { username : { $in : [ usernameOrig ] } } } } ;
await Circuit . updateOne ( { idapp , name : circuitname } , update ) ;
}
// Elimina eventualmente se era bloccato:
update = { $pull : { refused _users : { username : { $in : [ usernameOrig ] } } } } ;
await Circuit . updateOne ( { idapp , name : circuitname } , update ) ;
}
await Account . createAccount ( idapp , usernameOrig , circuitname , confido , groupname , contocom ) ;
return ris ;
} ;
2022-02-28 17:20:47 +01:00
// Rimuovo il Gruppo per Tutti gli Utenti
2022-12-10 02:01:17 +01:00
UserSchema . statics . removeAllUsersFromMyGroups = async function ( idapp , groupnameDest ) {
return await User . updateMany ( { idapp } ,
{ $pull : { 'profile.mygroups' : { groupname : { $in : [ groupnameDest ] } } } } ) ;
2022-02-28 17:20:47 +01:00
} ;
2022-09-02 02:25:38 +02:00
// Rimuovo il Circuito per Tutti gli Utenti
2022-12-10 02:01:17 +01:00
UserSchema . statics . removeAllUsersFromMyCircuits = async function ( idapp , circuitname ) {
return await User . updateMany ( { idapp } ,
{ $pull : { 'profile.mycircuits' : { circuitname : { $in : [ circuitname ] } } } } ) ;
2022-09-02 02:25:38 +02:00
} ;
2022-01-15 12:25:57 +01:00
// Rimuovo la Richiesta di Amicizia
2022-12-10 02:01:17 +01:00
UserSchema . statics . removeReqFriend = async function (
idapp , username , usernameDest ) {
return await User . updateOne ( { idapp , username : username } ,
{ $pull : { 'profile.req_friends' : { username : { $in : [ usernameDest ] } } } } ) ;
2022-01-14 23:54:33 +01:00
} ;
2023-04-04 15:26:56 +02:00
2023-04-17 00:11:36 +02:00
// Aggiungo il Partecipa
UserSchema . statics . addAttend = async function (
req , idapp , username , id , tab , num ) {
const ris = await User . updateOne ( { idapp , username } ,
{ $push : { 'profile.attend' : { id , tab , num } } } ) ;
const { SendNotif } = require ( '../models/sendnotif' ) ;
const globalTables = require ( '../tools/globalTables' ) ;
// Invia una Notifica al Destinatario
const recObjCreator = await globalTables . getUserCreatorByNumTabAndId ( idapp , id , tab ) ;
if ( recObjCreator ) {
await SendNotif . createNewNotifToSingleUser ( req , null , { usernameDest : recObjCreator . username , recObjCreator , username _action : req . user . username } , false ,
shared _consts . TypeNotifs . TYPEDIR _EVENTS ,
shared _consts . TypeNotifs . ID _EVENTS _ATTEND ) ;
}
return ris ;
} ;
2023-04-04 15:26:56 +02:00
// Rimuovo il Bookmark
UserSchema . statics . removeBookmark = async function (
2023-04-07 02:45:21 +02:00
idapp , username , id , tab ) {
2023-04-04 15:26:56 +02:00
return await User . updateOne ( { idapp , username } ,
{ $pull : { 'profile.bookmark' : { id : { $in : [ id ] } , tab } } } ) ;
} ;
// Aggiungo il Bookmark
UserSchema . statics . addBookmark = async function (
idapp , username , id , tab ) {
return await User . updateOne ( { idapp , username } ,
{ $push : { 'profile.bookmark' : { id , tab } } } ) ;
} ;
2023-04-17 00:11:36 +02:00
// Rimuovo il Partecipa
UserSchema . statics . removeAttend = async function (
idapp , username , id , tab ) {
return await User . updateOne ( { idapp , username } ,
{ $pull : { 'profile.attend' : { id : { $in : [ id ] } , tab } } } ) ;
} ;
2023-04-13 14:27:00 +02:00
// Aggiungo il Bookmark
UserSchema . statics . addSeen = async function (
idapp , username , id , tab ) {
return await User . updateOne ( { idapp , username } ,
{ $push : { 'profile.seen' : { id , tab } } } ) ;
} ;
2023-04-04 15:26:56 +02:00
2022-12-17 22:12:13 +01:00
UserSchema . statics . setFriendsCmd = async function ( req , idapp , usernameOrig , usernameDest , cmd , value , disablenotif ) {
2022-01-07 01:18:01 +01:00
2022-12-10 02:01:17 +01:00
const { SendNotif } = require ( '../models/sendnotif' ) ;
2022-07-26 15:46:39 +02:00
2022-07-28 21:47:23 +02:00
const telegrambot = require ( '../telegram/telegrambot' ) ;
2022-08-04 17:30:57 +02:00
if ( ! req ) {
req = tools . getReqByPar ( idapp , usernameOrig ) ;
}
2023-11-30 01:49:17 +01:00
let userDest = null ;
if ( usernameDest )
userDest = await User . getUserShortDataByUsername ( idapp , usernameDest ) ;
2022-08-04 17:30:57 +02:00
const username _action = req . user . username ;
let username _worked = usernameDest ;
2022-01-07 01:18:01 +01:00
let ris = null ;
2023-11-30 01:49:17 +01:00
let lang = '' ;
let user = null ;
if ( usernameOrig ) {
user = await User . getUserShortDataByUsername ( idapp , usernameOrig ) ;
}
lang = user ? user . lang : '' ;
2022-01-12 00:38:47 +01:00
let update = { } ;
2022-01-15 12:25:57 +01:00
try {
if ( cmd === shared _consts . FRIENDSCMD . SETTRUST ) {
2022-01-07 01:18:01 +01:00
2023-11-30 19:50:12 +01:00
let msgOrig = '' ;
let msgDest = '' ;
// Aggiorna true se lo accetti e false se non lo accetti
2023-11-30 01:49:17 +01:00
const ris = await User . updateOne ( { idapp , username : usernameDest } ,
2022-12-10 02:01:17 +01:00
{ $set : { verified _by _aportador : value , trust _modified : new Date ( ) } } ,
{ new : false } ) ;
2022-01-14 23:54:33 +01:00
2023-11-30 19:50:12 +01:00
if ( value ) {
// Aggiungi alle amicizie
2023-12-06 00:41:50 +01:00
// await this.setFriendsCmd(req, idapp, usernameOrig, usernameDest,
// shared_consts.FRIENDSCMD.SETFRIEND, value);
2023-11-30 19:50:12 +01:00
msgOrig = i18n . _ _ ( { phrase : '✅ Hai Abilitato l\'accesso alla App a %s !' , locale : userDest . lang } , userDest . username ) ;
msgDest = i18n . _ _ ( { phrase : '✅ Sei stato Abilitato correttamente da %s!' , locale : lang } , usernameOrig ) ;
msgAdmin = i18n . _ _ ( { phrase : '✅ %s è stato Abilitato correttamente (da %s)!' , locale : userDest . lang } , userDest . username , usernameOrig ) ;
} else {
msgOrig = i18n . _ _ ( { phrase : '🚫 Hai rifiutato l\'accesso alla App di RISO da parte di %s!' , locale : userDest . lang } , userDest . username ) ;
msgDest = i18n . _ _ ( { phrase : '🚫 Ti è stato rifiutato l\'accesso. Probabilmente l\'username con cui ti sei registrato non ti conosce. (%s) !<br>Contatta l\'Assistenza Tecnica.' , locale : lang } , usernameOrig ) ;
msgAdmin = i18n . _ _ ( { phrase : '🚫 %s ha rifiutato l\'accesso alla App a %s !' , locale : userDest . lang } , usernameOrig , userDest . username ) ;
}
2023-11-30 01:49:17 +01:00
await telegrambot . sendMsgTelegram ( idapp , usernameOrig , msgOrig ) ;
await telegrambot . sendMsgTelegram ( idapp , userDest . username , msgDest ) ;
2023-11-30 19:50:12 +01:00
2023-11-30 01:49:17 +01:00
// Invia questo msg anche all'Admin
2023-11-30 19:50:12 +01:00
await telegrambot . sendMsgTelegramToTheAdmin ( idapp , msgAdmin , true ) ;
2023-11-30 01:49:17 +01:00
// telegrambot.askConfirmationUser(user.idapp, shared_consts.CallFunz.REGISTRATION, user, '', '', '', '');
return ris ;
2022-01-15 12:25:57 +01:00
} else if ( cmd === shared _consts . FRIENDSCMD . SETFRIEND ) {
// Aggiungo l'Amicizia a me
const foundIfAlreadyFriend = await User . findOne ( {
idapp ,
username : usernameOrig ,
'profile.friends' : {
2022-12-10 02:01:17 +01:00
$elemMatch : { username : { $eq : usernameDest } } ,
2022-01-15 12:25:57 +01:00
} ,
2022-12-10 02:01:17 +01:00
} , { _id : 1 } ) . lean ( ) ;
2022-01-07 01:18:01 +01:00
2022-01-15 12:25:57 +01:00
if ( ! foundIfAlreadyFriend ) {
update = {
$push : {
'profile.friends' : {
username : usernameDest ,
date : new Date ( ) ,
} ,
} ,
} ;
2022-12-10 02:01:17 +01:00
ris = await User . updateOne ( { idapp , username : usernameOrig } , update ) ;
2022-01-07 01:18:01 +01:00
2022-12-17 22:12:13 +01:00
if ( ! disablenotif ) {
// Send a notification to the DESTINATION FRIENDSHIP !
let req = tools . getReqByPar ( idapp , usernameOrig ) ;
await SendNotif . createNewNotifToSingleUser ( req , null , { usernameDest } , true , shared _consts . TypeNotifs . TYPEDIR _FRIENDS ,
2023-01-08 02:17:01 +01:00
shared _consts . TypeNotifs . ID _FRIENDS _ACCEPTED _MY _REQUEST ) ;
2022-07-26 15:46:39 +02:00
2022-12-17 22:12:13 +01:00
// Send a notification to the SENDER FRIENDSHIP !
req = tools . getReqByPar ( idapp , usernameDest ) ;
await SendNotif . createNewNotifToSingleUser ( req , null , { usernameDest : usernameOrig } , true , shared _consts . TypeNotifs . TYPEDIR _FRIENDS ,
2023-01-08 02:17:01 +01:00
shared _consts . TypeNotifs . ID _FRIENDS _ACCEPTED ) ;
2022-12-17 22:12:13 +01:00
}
2022-07-28 21:47:23 +02:00
2022-12-10 02:01:17 +01:00
update = { $pull : { 'profile.req_friends' : { username : { $in : [ usernameDest ] } } } } ;
ris = await User . updateOne ( { idapp , username : usernameOrig } , update ) ;
2022-07-28 21:47:23 +02:00
if ( ris ) {
try {
2022-12-17 22:12:13 +01:00
if ( ! disablenotif ) {
const userDest = await User . getRecLangAndIdByUsername ( idapp , usernameDest ) ;
const user = await User . getRecLangAndIdByUsername ( idapp , usernameOrig ) ;
2023-01-08 02:17:01 +01:00
const msgDest = i18n . _ _ ( { phrase : '✅ %s accepted your Friendship request !' , locale : userDest . lang } , usernameDest ) ;
const msgOrig = i18n . _ _ ( { phrase : '✅ You have accepted %s\' Friendship request!' , locale : user . lang } , usernameOrig ) ;
2022-12-17 22:12:13 +01:00
2023-01-08 02:17:01 +01:00
await telegrambot . sendMsgTelegram ( idapp , usernameOrig , msgDest ) ;
await telegrambot . sendMsgTelegram ( idapp , usernameDest , msgOrig ) ;
2022-12-17 22:12:13 +01:00
}
2022-08-04 17:30:57 +02:00
} catch ( e ) {
2022-07-28 21:47:23 +02:00
console . error ( 'Notification : ' , e ) ;
}
}
2022-01-15 12:25:57 +01:00
}
2022-01-14 23:54:33 +01:00
2022-01-15 12:25:57 +01:00
// Controlla se lui aveva già la mia amicizia
const foundIfAlreadyFriend2 = await User . findOne ( {
idapp ,
username : usernameDest ,
'profile.friends' : {
2022-12-10 02:01:17 +01:00
$elemMatch : { username : { $eq : usernameOrig } } ,
2022-01-15 12:25:57 +01:00
} ,
2022-12-10 02:01:17 +01:00
} , { _id : 1 } ) . lean ( ) ;
2022-01-14 23:54:33 +01:00
2022-01-15 12:25:57 +01:00
if ( ! foundIfAlreadyFriend2 ) {
update = {
$push : {
'profile.friends' : {
username : usernameOrig ,
date : new Date ( ) ,
} ,
} ,
} ;
2022-12-10 02:01:17 +01:00
ris = await User . updateOne ( { idapp , username : usernameDest } , update ) ;
2022-01-15 12:25:57 +01:00
this . removeReqFriend ( idapp , usernameDest , usernameOrig ) ; // Rimuovo l'Amicizia da me
2022-03-11 12:38:01 +01:00
this . removeReqFriend ( idapp , usernameOrig , usernameDest ) ; // Rimuovo l'Amicizia da te
2022-01-14 23:54:33 +01:00
}
2022-03-11 12:38:01 +01:00
//if (ris) {
ris = await User . getInfoFriendByUsername ( idapp , usernameDest ) ;
//}
2023-01-06 15:51:48 +01:00
} else if ( cmd === shared _consts . FRIENDSCMD . SETHANDSHAKE ) {
2023-01-08 19:20:02 +01:00
// Aggiungo la Stretta di mano a lui
2023-06-07 10:14:57 +02:00
const foundIfAlreadyHandshake = await User . findOne ( {
2023-01-06 15:51:48 +01:00
idapp ,
2023-01-08 19:20:02 +01:00
username : usernameDest ,
2023-01-06 15:51:48 +01:00
'profile.handshake' : {
2023-01-08 19:20:02 +01:00
$elemMatch : { username : { $eq : usernameOrig } } ,
2023-01-06 15:51:48 +01:00
} ,
} , { _id : 1 } ) . lean ( ) ;
2023-01-09 04:05:11 +01:00
let rec = null ;
2023-06-07 10:14:57 +02:00
if ( ! foundIfAlreadyHandshake ) {
2023-01-06 15:51:48 +01:00
update = {
$push : {
'profile.handshake' : {
2023-01-08 19:20:02 +01:00
username : usernameOrig ,
2023-01-06 15:51:48 +01:00
date : new Date ( ) ,
} ,
} ,
} ;
2024-04-25 23:26:16 +02:00
const already _stretta = await User . isMyHandShake ( idapp , usernameDest , usernameOrig ) ;
const already _stretta _orig = await User . isMyHandShake ( idapp , usernameOrig , usernameDest ) ;
2023-01-09 04:05:11 +01:00
rec = await User . updateOne ( { idapp , username : usernameDest } , update ) ;
2023-01-06 15:51:48 +01:00
2023-06-07 10:14:57 +02:00
tools . sendNotificationByUsername ( idapp , usernameDest , cmd , true , usernameOrig ) ;
let req = tools . getReqByPar ( idapp , usernameOrig ) ;
if ( rec ) {
try {
if ( ! disablenotif ) {
const userDest = await User . getRecLangAndIdByUsername ( idapp , usernameDest ) ;
const user = await User . getRecLangAndIdByUsername ( idapp , usernameOrig ) ;
2024-04-26 00:49:05 +02:00
2024-04-25 23:26:16 +02:00
let msgDest = i18n . _ _ ( { phrase : 'HANDSHAKE_SENT_FROM_YOU' , locale : userDest . lang } , usernameDest ) ;
if ( already _stretta )
msgDest = i18n . _ _ ( { phrase : 'HANDSHAKE_CONFIRMED' , locale : userDest . lang } , usernameDest ) ;
2023-11-03 12:49:10 +01:00
let phrase = 'HANDSHAKE_SET' ;
if ( already _stretta ) {
phrase = 'HANDSHAKE_ACCEPTED' ;
}
const msgOrig = i18n . _ _ ( { phrase , locale : user . lang } , usernameOrig ) ;
2023-06-07 10:14:57 +02:00
await telegrambot . sendMsgTelegram ( idapp , usernameOrig , msgDest ) ;
await telegrambot . sendMsgTelegram ( idapp , usernameDest , msgOrig ) ;
}
} catch ( e ) {
console . error ( 'Notification : ' , e ) ;
}
2023-01-06 15:51:48 +01:00
}
2023-06-07 10:14:57 +02:00
// Send a notification to the DESTINATION HANDSHAKE !
// await SendNotif.createNewNotifToSingleUser(req, null, { usernameDest }, false, shared_consts.TypeNotifs.TYPEDIR_HANDSHAKE,
// shared_consts.TypeNotifs.ID_HANDSHAKE_ACCEPTED);
2023-01-06 15:51:48 +01:00
}
2023-01-09 04:05:11 +01:00
const userprofile = await User . getInfoFriendByUsername ( idapp , usernameDest ) ;
const myuser = await User . getInfoFriendByUsername ( idapp , usernameOrig ) ;
2023-01-13 12:29:28 +01:00
ris = { rec , userprofile , myuser } ;
2022-01-15 12:25:57 +01:00
} else if ( cmd === shared _consts . FRIENDSCMD . REQFRIEND ) {
2022-01-28 18:15:56 +01:00
// Aggiungo la richiesta di Amicizia a me
2022-01-15 12:25:57 +01:00
const foundIfAlreadyAskFriend = await User . findOne ( {
idapp ,
username : usernameDest ,
'profile.req_friends' : {
2022-12-10 02:01:17 +01:00
$elemMatch : { username : { $eq : usernameOrig } } ,
2022-01-15 12:25:57 +01:00
} ,
2022-12-10 02:01:17 +01:00
} , { _id : 1 } ) . lean ( ) ;
2022-01-15 12:25:57 +01:00
if ( value ) {
if ( ! foundIfAlreadyAskFriend ) {
update = {
$push : {
'profile.req_friends' : {
username : usernameOrig ,
date : new Date ( ) ,
} ,
} ,
} ;
2022-12-10 02:01:17 +01:00
ris = await User . updateOne ( { idapp , username : usernameDest } , update ) ;
2022-01-15 12:25:57 +01:00
}
if ( ris ) {
// Invia una notifica alla persona
2022-03-11 12:38:01 +01:00
tools . sendNotificationByUsername ( idapp , usernameDest , cmd , true , usernameOrig ) ;
2022-01-15 12:25:57 +01:00
}
2023-01-09 15:55:12 +01:00
if ( foundIfAlreadyAskFriend ) {
ris = await User . getInfoFriendByUsername ( idapp , usernameDest ) ;
}
2023-01-06 15:51:48 +01:00
}
2022-01-15 12:25:57 +01:00
} else if ( cmd === shared _consts . FRIENDSCMD . REMOVE _FROM _MYFRIENDS ) {
2022-03-11 12:38:01 +01:00
// Rimuovi anche le eventuali richieste di Amicizia !
await this . removeReqFriend ( idapp , usernameDest , usernameOrig ) ; // Rimuovo la Richiesta di Amicizia da lui
await this . removeReqFriend ( idapp , usernameOrig , usernameDest ) ; // Rimuovo la Richiesta di Amicizia da me
2022-01-15 12:25:57 +01:00
await this . removeFriend ( idapp , usernameDest , usernameOrig ) ; // Rimuovo l'Amicizia da lui
ris = await this . removeFriend ( idapp , usernameOrig , usernameDest ) ; // Rimuovo l'Amicizia da me
2023-01-06 15:51:48 +01:00
} else if ( cmd === shared _consts . FRIENDSCMD . REMOVE _FROM _MYHANDSHAKE ) {
2023-01-09 04:05:11 +01:00
ris = await this . removeHandShake ( idapp , usernameDest , usernameOrig ) ; // Rimuovo l'Amicizia da lui
2023-01-06 15:51:48 +01:00
2022-01-15 12:25:57 +01:00
} else if ( cmd === shared _consts . FRIENDSCMD . CANCEL _REQ _FRIEND ) {
2022-07-27 20:56:01 +02:00
// CREATE NOTIFICATION IN TABLE SENDNOTIF
const req = tools . getReqByPar ( idapp , usernameOrig ) ;
2022-12-10 02:01:17 +01:00
await SendNotif . createNewNotifToSingleUser ( req , null , { usernameDest } , true , shared _consts . TypeNotifs . TYPEDIR _FRIENDS ,
shared _consts . TypeNotifs . ID _FRIENDS _REFUSED ) ;
2022-07-27 20:56:01 +02:00
2023-01-09 15:55:12 +01:00
ris = true ;
2022-01-15 12:25:57 +01:00
} else if ( cmd === shared _consts . FRIENDSCMD . BLOCK _USER ) {
2022-03-11 12:38:01 +01:00
await this . removeReqFriend ( idapp , usernameDest , usernameOrig ) ; // Rimuovo la Richiesta di Amicizia da lui
await this . removeReqFriend ( idapp , usernameOrig , usernameDest ) ; // Rimuovo la Richiesta di Amicizia da me
2022-01-15 12:25:57 +01:00
await this . removeFriend ( idapp , usernameDest , usernameOrig ) ; // Rimuovo l'Amicizia da lui
await this . removeFriend ( idapp , usernameOrig , usernameDest ) ; // Rimuovo l'Amicizia da me
// Blocco la persona
2022-12-10 02:01:17 +01:00
ris = await User . updateOne ( { idapp , username : usernameDest } , {
2022-01-15 12:25:57 +01:00
$set : {
blocked : true ,
sospeso : true ,
username _who _block : usernameOrig ,
date _blocked : new Date ( ) ,
} ,
} ) ;
2022-08-04 17:30:57 +02:00
} else if ( cmd === shared _consts . FRIENDSCMD . REPORT _USER ) {
username _worked = usernameDest ;
// Segnalo la persona
2022-12-10 02:01:17 +01:00
ris = await User . updateOne ( { idapp , username : username _worked } , {
2022-08-04 17:30:57 +02:00
$set : {
reported : true ,
username _who _report : usernameOrig ,
date _report : new Date ( ) ,
} ,
} ) ;
if ( ris ) {
// Send a notification to the DESTINATION!
// Sei stato segnalato da %s per comportamenti non idonei. Contatta %s per chiarimenti
2022-12-10 02:01:17 +01:00
await SendNotif . createNewNotifToSingleUser ( req , null , { username _worked , usernameDest , username _action } , false ,
shared _consts . TypeNotifs . TYPEDIR _FRIENDS ,
shared _consts . TypeNotifs . ID _FRIENDS _REPORTED ) ;
2022-08-04 17:30:57 +02:00
// Send a notification to the SENDER !
// Hai segnalato %s da %s per comportamenti non idonei.
2022-12-10 02:01:17 +01:00
await SendNotif . createNewNotifToSingleUser ( req , null , { username _worked , usernameDest : username _action , username _action } , false ,
shared _consts . TypeNotifs . TYPEDIR _FRIENDS ,
shared _consts . TypeNotifs . ID _FRIENDS _REPORTED ) ;
2022-08-04 17:30:57 +02:00
if ( usernameOrig !== telegrambot . ADMIN _USER _SERVER ) {
// Send a notification to the Admin
2022-09-16 17:38:49 +02:00
await SendNotif . createNewNotifToSingleUser ( req , null ,
2022-12-10 02:01:17 +01:00
{ username _worked , usernameDest : telegrambot . ADMIN _USER _SERVER , username _action , isAdmin : true } , false ,
shared _consts . TypeNotifs . TYPEDIR _FRIENDS ,
shared _consts . TypeNotifs . ID _FRIENDS _REPORTED ) ;
2022-08-04 17:30:57 +02:00
}
2022-08-08 16:35:32 +02:00
}
} else if ( cmd === shared _consts . FRIENDSCMD . UNBLOCK _USER ) {
username _worked = usernameDest ;
// Sblocco la persona
2022-12-10 02:01:17 +01:00
ris = await User . updateOne ( { idapp , username : username _worked } , {
2022-08-08 16:35:32 +02:00
$set : {
reported : false ,
} ,
} ) ;
if ( ris ) {
// Send a notification to the DESTINATION!
2022-12-10 02:01:17 +01:00
await SendNotif . createNewNotifToSingleUser ( req , null , { username _worked , usernameDest , username _action } , false ,
shared _consts . TypeNotifs . TYPEDIR _FRIENDS ,
shared _consts . TypeNotifs . ID _FRIENDS _UNBLOCKED ) ;
2022-08-08 16:35:32 +02:00
// Send a notification to the SENDER !
// Hai segnalato %s da %s per comportamenti non idonei.
2022-12-10 02:01:17 +01:00
await SendNotif . createNewNotifToSingleUser ( req , null , { username _worked , usernameDest : username _action , username _action } , false ,
shared _consts . TypeNotifs . TYPEDIR _FRIENDS ,
shared _consts . TypeNotifs . ID _FRIENDS _UNBLOCKED ) ;
2022-08-08 16:35:32 +02:00
if ( usernameOrig !== telegrambot . ADMIN _USER _SERVER ) {
// Send a notification to the Admin
2022-09-16 17:38:49 +02:00
await SendNotif . createNewNotifToSingleUser ( req , null ,
2022-12-10 02:01:17 +01:00
{ username _worked , usernameDest : telegrambot . ADMIN _USER _SERVER , username _action , isAdmin : true } , false ,
shared _consts . TypeNotifs . TYPEDIR _FRIENDS ,
shared _consts . TypeNotifs . ID _FRIENDS _UNBLOCKED ) ;
2022-08-08 16:35:32 +02:00
}
2022-08-04 17:30:57 +02:00
}
2022-01-15 12:25:57 +01:00
}
} catch ( e ) {
console . error ( 'Error: ' , e ) ;
2022-01-07 01:18:01 +01:00
}
return ris ;
} ;
2023-11-19 23:40:38 +01:00
UserSchema . statics . sendCmd = async function ( req , idapp , usernameOrig , usernameDest , cmd , value , disablenotif ) {
const { SendNotif } = require ( '../models/sendnotif' ) ;
const telegrambot = require ( '../telegram/telegrambot' ) ;
const cl = telegrambot . getclTelegByidapp ( idapp ) ;
if ( ! req ) {
req = tools . getReqByPar ( idapp , usernameOrig ) ;
}
const myuser = await User . getUserByUsername ( idapp , usernameOrig ) ;
const recuserDest = await User . getUserByUsername ( idapp , usernameDest ) ;
const langdest = recuserDest . lang ;
const telegid = recuserDest . profile . teleg _id ;
let userId = recuserDest . _id ;
let title = tools . getNomeAppByIdApp ( idapp ) ;
let keyb = null ;
let descr = '' ;
2023-11-30 01:49:17 +01:00
2023-11-19 23:40:38 +01:00
let send _notif = false ;
let send _msgTelegramBot = false ;
let actions = [ ] ;
let popupOnApp = '' ;
let ris = null ;
try {
if ( cmd === shared _consts . CallFunz . ENTRA _RIS _ITALIA ) {
mycircuitOrig = await Circuit . getCircuitMyProvince ( idapp , usernameOrig ) ;
descr = i18n . _ _ ( { phrase : 'SENDMSG_ENTRA_IN_RISO_ITALIA' , locale : langdest } , usernameDest , usernameOrig , mycircuitOrig ) ;
msgtelegram = descr ;
openUrl = '/circuit/ris_italia' ;
bottone = i18n . _ _ ( { phrase : 'CIRCUIT_OPEN_RISITALIA' , locale : langdest } ) ;
tag = 'risitalia' ;
2023-11-30 01:49:17 +01:00
2023-11-19 23:40:38 +01:00
send _notif = true ;
send _msgTelegramBot = true ;
keyb = cl . getInlineKeyboard ( langdest , [
{
text : bottone ,
url : tools . getHostByIdApp ( idapp ) + openUrl ,
// callback_data: InlineConferma.RISPOSTA_SI + myfunc + tools.SEP + myuser.username + tools.SEP + '' + tools.SEP + '' + tools.SEP +
// groupid,
} ,
] ) ;
popupOnApp = 'Messaggio inviato al destinatario' ;
}
if ( send _notif ) {
// SEND PUSH NOTIFICATION
await tools . sendNotificationToUser ( userId , title , descr , openUrl , '' , tag , actions ) ;
}
// Invia Msg
2023-11-30 01:49:17 +01:00
if ( send _msgTelegramBot && msgtelegram ) {
2023-11-19 23:40:38 +01:00
await telegrambot . local _sendMsgTelegramByIdTelegram ( idapp , telegid , msgtelegram , undefined , undefined , true , keyb ) ;
}
const userprofile = await User . getInfoFriendByUsername ( idapp , usernameDest ) ;
const myuser = await User . getInfoFriendByUsername ( idapp , usernameOrig ) ;
2023-11-30 01:49:17 +01:00
2023-11-19 23:40:38 +01:00
ris = { userprofile , myuser , popupOnApp , result : true } ;
2023-11-30 01:49:17 +01:00
2023-11-19 23:40:38 +01:00
} catch ( e ) {
popupOnApp = e ;
ris = { popupOnApp } ;
console . error ( 'Error sendCmd: ' , e ) ;
}
return ris ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . ifAlreadyInGroup = async function ( idapp , usernameOrig , groupnameDest ) {
2022-03-06 00:48:33 +01:00
// Controllo se è stato già inserito
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2022-03-06 00:48:33 +01:00
idapp ,
username : usernameOrig ,
'profile.mygroups' : {
2022-12-10 02:01:17 +01:00
$elemMatch : { groupname : { $eq : groupnameDest } } ,
2022-03-06 00:48:33 +01:00
} ,
} ) . lean ( ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . ifAlreadyInCircuit = async function ( idapp , usernameOrig , circuitname ) {
2022-09-02 02:25:38 +02:00
// Controllo se è stato già inserito
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2022-09-02 02:25:38 +02:00
idapp ,
username : usernameOrig ,
'profile.mycircuits' : {
2022-12-10 02:01:17 +01:00
$elemMatch : { circuitname : { $eq : circuitname } } ,
2022-09-02 02:25:38 +02:00
} ,
} ) . lean ( ) ;
} ;
2024-04-23 22:47:20 +02:00
//** Get true if in 'profile.mycircuits' exist at least one circuit */
UserSchema . statics . ExistAtLeastOneCircuit = async function ( idapp , username ) {
// Controllo se è stato più inserito
return await User . countDocuments ( {
idapp ,
username ,
'profile.mycircuits' : {
$ne : [ ] ,
} ,
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . countUsersInGroup = async function ( idapp , groupnameDest ) {
2022-02-03 00:33:15 +01:00
2022-08-04 17:30:57 +02:00
// Controllo se è stato già inserito
2022-09-11 11:45:33 +02:00
return await User . countDocuments ( {
2022-08-04 17:30:57 +02:00
idapp ,
'profile.mygroups' : {
2022-12-10 02:01:17 +01:00
$elemMatch : { groupname : { $eq : groupnameDest } } ,
2022-08-04 17:30:57 +02:00
} ,
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . countUsersInCircuit = async function ( idapp , circuitname ) {
2022-09-02 02:25:38 +02:00
// Controllo se è stato già inserito
2022-09-11 11:45:33 +02:00
return await User . countDocuments ( {
2022-09-02 02:25:38 +02:00
idapp ,
'profile.mycircuits' : {
2022-12-10 02:01:17 +01:00
$elemMatch : { circuitname : { $eq : circuitname } } ,
2022-09-02 02:25:38 +02:00
} ,
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . setGroupsCmd = async function ( idapp , usernameOrig , groupnameDest , cmd , value , username _action ) {
2022-07-28 21:47:23 +02:00
2022-02-03 00:33:15 +01:00
let ris = null ;
let update = { } ;
try {
if ( cmd === shared _consts . GROUPSCMD . SETGROUP ) {
2022-03-06 00:48:33 +01:00
const foundIfAlreadyGroup = await this . ifAlreadyInGroup ( idapp , usernameOrig , groupnameDest ) ;
2022-02-03 00:33:15 +01:00
if ( ! foundIfAlreadyGroup ) {
update = {
$push : {
2022-02-05 23:28:15 +01:00
'profile.mygroups' : {
2022-02-03 00:33:15 +01:00
groupname : groupnameDest ,
date : new Date ( ) ,
} ,
} ,
} ;
2022-12-10 02:01:17 +01:00
ris = await User . updateOne ( { idapp , username : usernameOrig } , update ) ;
2022-02-03 00:33:15 +01:00
2022-02-05 23:28:15 +01:00
// Elimina la richiesta:
2022-12-10 02:01:17 +01:00
update = { $pull : { req _users : { username : { $in : [ usernameOrig ] } } } } ;
await MyGroup . updateOne ( { idapp , groupname : groupnameDest } , update ) ;
2022-08-08 16:35:32 +02:00
// Elimina eventualmente se era bloccato:
2022-12-10 02:01:17 +01:00
update = { $pull : { refused _users : { username : { $in : [ usernameOrig ] } } } } ;
await MyGroup . updateOne ( { idapp , groupname : groupnameDest } , update ) ;
2022-02-05 23:28:15 +01:00
} else {
ris = false ;
2022-02-03 00:33:15 +01:00
}
if ( ris ) {
2022-03-06 00:48:33 +01:00
// Invia una notifica alla persona e agli Admin
2022-08-04 17:30:57 +02:00
tools . sendNotificationByGroupname ( idapp , usernameOrig , groupnameDest , cmd , value , true , username _action ) ;
2022-02-03 00:33:15 +01:00
ris = await MyGroup . getInfoGroupByGroupname ( idapp , groupnameDest ) ;
}
} else if ( cmd === shared _consts . GROUPSCMD . REQGROUP ) {
2022-03-06 00:48:33 +01:00
// Aggiungo la richiesta di Gruppo a me
2022-02-03 00:33:15 +01:00
const foundIfAlreadyAskGroup = await MyGroup . findOne ( {
idapp ,
groupname : groupnameDest ,
2022-02-05 23:28:15 +01:00
'req_users' : {
2022-12-10 02:01:17 +01:00
$elemMatch : { username : { $eq : usernameOrig } } ,
2022-02-03 00:33:15 +01:00
} ,
} ) ;
if ( value ) {
if ( ! foundIfAlreadyAskGroup ) {
update = {
$push : {
2022-02-05 23:28:15 +01:00
'req_users' : {
2022-02-03 00:33:15 +01:00
username : usernameOrig ,
date : new Date ( ) ,
} ,
} ,
} ;
2022-12-10 02:01:17 +01:00
ris = await MyGroup . updateOne ( { idapp , groupname : groupnameDest } ,
update ) ;
2022-02-03 00:33:15 +01:00
}
if ( ris ) {
// Invia una notifica alla persona
2022-08-04 17:30:57 +02:00
await tools . sendNotificationByGroupname ( idapp , usernameOrig , groupnameDest , cmd , true , true , username _action ) ;
2022-02-03 00:33:15 +01:00
}
} else {
if ( foundIfAlreadyAskGroup ) {
2022-08-04 17:30:57 +02:00
ris = await this . removeFromMyGroups ( idapp , usernameOrig , groupnameDest ) ; // Rimuovo il Gruppo da me
// Invia una notifica alla persona
await tools . sendNotificationByGroupname ( idapp , usernameOrig , groupnameDest , cmd , false , true , username _action ) ;
2022-02-03 00:33:15 +01:00
}
}
if ( ris ) {
2022-02-05 23:28:15 +01:00
ris = await MyGroup . getInfoGroupByGroupname ( idapp , groupnameDest ) ;
2022-02-03 00:33:15 +01:00
}
2022-02-05 23:28:15 +01:00
} else if ( cmd === shared _consts . GROUPSCMD . REMOVE _FROM _MYGROUP ) {
2022-02-03 00:33:15 +01:00
2022-08-10 17:07:02 +02:00
// Remove if is also an Admin
await MyGroup . removeAdminOfMyGroup ( idapp , usernameOrig , groupnameDest ) ;
2022-02-05 23:28:15 +01:00
ris = await User . removeFromMyGroups ( idapp , usernameOrig , groupnameDest ) ; // Rimuovo l'Amicizia da me
console . log ( 'ris' , ris ) ;
2022-02-03 00:33:15 +01:00
2022-08-04 17:30:57 +02:00
// Invia una notifica alla persona
await tools . sendNotificationByGroupname ( idapp , usernameOrig , groupnameDest , cmd , false , true , username _action ) ;
2022-06-17 12:30:44 +02:00
} else if ( cmd === shared _consts . GROUPSCMD . DELETE _GROUP ) {
ris = await User . removeFromMyGroups ( idapp , usernameOrig , groupnameDest ) ; // Rimuovo l'Amicizia da me
if ( ris ) {
// Invia una notifica alla persona e agli Admin
2022-08-04 17:30:57 +02:00
await tools . sendNotificationByGroupname ( idapp , usernameOrig , groupnameDest , cmd , false , true , username _action ) ;
2022-06-17 12:30:44 +02:00
}
ris = await MyGroup . deleteGroup ( idapp , usernameOrig , groupnameDest ) ; // Rimuovo l'Amicizia da me
console . log ( 'ris' , ris ) ;
2022-02-03 00:33:15 +01:00
} else if ( cmd === shared _consts . GROUPSCMD . CANCEL _REQ _GROUP ) {
ris = await MyGroup . removeReqGroup ( idapp , usernameOrig , groupnameDest ) ; // Rimuovo l'Amicizia da me
2022-09-02 02:25:38 +02:00
} else if ( cmd === shared _consts . GROUPSCMD . REFUSE _REQ _GROUP ) {
ris = await MyGroup . removeReqGroup ( idapp , usernameOrig , groupnameDest ) ; // Rimuovo l'Amicizia da me
ris = await MyGroup . refuseReqGroup ( idapp , usernameOrig , groupnameDest ) ; // Rimuovo l'Amicizia da me
2022-02-03 00:33:15 +01:00
} else if ( cmd === shared _consts . GROUPSCMD . BLOCK _GROUP ) {
2022-02-05 23:28:15 +01:00
await User . removeFromMyGroups ( idapp , usernameOrig , groupnameDest ) ; // Rimuovo l'Amicizia da me
2022-02-03 00:33:15 +01:00
// Blocco il Gruppo
2022-12-10 02:01:17 +01:00
ris = await MyGroup . updateOne ( { idapp , groupname : groupnameDest } , {
2022-02-03 00:33:15 +01:00
$set : {
blocked : true ,
username _who _block : usernameOrig ,
date _blocked : new Date ( ) ,
} ,
} ) ;
//++Todo: Send Notification to Admin and Group's manager
2022-08-04 17:30:57 +02:00
tools . sendNotificationByGroupname ( idapp , usernameOrig , groupnameDest , cmd , false , true , username _action ) ;
2022-02-03 00:33:15 +01:00
2022-08-09 17:32:06 +02:00
} else if ( cmd === shared _consts . GROUPSCMD . ADDADMIN _OFMYGROUP ) {
ris = await MyGroup . addToAdminOfMyGroup ( idapp , usernameOrig , groupnameDest ) ; // Rimuovo la richiesta di entrare nel gruppo
// Invia una notifica alla persona
await tools . sendNotificationByGroupname ( idapp , usernameOrig , groupnameDest , cmd , false , true , username _action ) ;
} else if ( cmd === shared _consts . GROUPSCMD . REMOVEADMIN _OFMYGROUP ) {
ris = await MyGroup . removeAdminOfMyGroup ( idapp , usernameOrig , groupnameDest ) ; // Rimuovo la richiesta di entrare nel gruppo
// Invia una notifica alla persona
await tools . sendNotificationByGroupname ( idapp , usernameOrig , groupnameDest , cmd , false , true , username _action ) ;
2022-02-03 00:33:15 +01:00
}
} catch ( e ) {
console . error ( 'Error: ' , e ) ;
}
return ris ;
} ;
2023-03-21 18:11:56 +01:00
UserSchema . statics . updateMyData = async function ( outres , idapp , username ) {
try {
//++Todo: Ottimizzare ! Non occorre inviare tutti questi dati !!! Solo per il Circuito ?!
const userprofile = await User . getExtraInfoByUsername ( idapp , username ) ;
if ( userprofile ) {
outres . userprofile = userprofile ;
}
outres . listcircuits = await Circuit . findAllIdApp ( idapp ) ;
outres . mygroups = await MyGroup . findAllGroups ( idapp ) ;
} catch ( e ) {
console . error ( 'ERR' , e ) ;
}
return outres ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . setCircuitCmd = async function ( idapp , usernameOrig , circuitname , cmd , value , username _action , extrarec ) {
2022-08-30 17:00:48 +02:00
2022-10-13 21:10:07 +02:00
// console.log('setCircuitCmd', cmd);
2022-12-10 02:01:17 +01:00
const { SendNotif } = require ( '../models/sendnotif' ) ;
2022-09-13 12:28:49 +02:00
2022-08-30 17:00:48 +02:00
let ris = null ;
2022-09-14 17:37:29 +02:00
let outres = {
result : false ,
} ;
2022-08-30 17:00:48 +02:00
let update = { } ;
2023-01-03 16:51:32 +01:00
let groupname = extrarec && extrarec . groupname ? extrarec . groupname : '' ;
2022-08-30 17:00:48 +02:00
try {
2023-02-01 01:19:50 +01:00
if ( cmd === shared _consts . CIRCUITCMD . CREATE ) {
const mycirc = await Circuit . findOne ( { idapp , name : circuitname } ) ;
if ( mycirc ) {
// Il Conto Comunitario prende il nome del circuito !
2023-06-01 11:39:53 +02:00
await Account . createAccount ( idapp , '' , circuitname , true , '' , mycirc . path ) ;
2023-02-01 01:19:50 +01:00
}
} else if ( cmd === shared _consts . CIRCUITCMD . SET ) {
2023-01-03 16:51:32 +01:00
if ( groupname ) {
const foundIfCircuitInGroup = await MyGroup . ifCircuitAlreadyInGroup ( idapp , groupname , circuitname ) ;
2022-08-30 17:00:48 +02:00
2023-01-03 16:51:32 +01:00
if ( ! foundIfCircuitInGroup ) {
2023-06-01 11:39:53 +02:00
ris = await this . addCircuitToUser ( idapp , usernameOrig , circuitname , true , groupname ) ;
2022-10-22 15:38:15 +02:00
2023-01-03 16:51:32 +01:00
} else {
ris = false ;
}
2022-10-22 15:38:15 +02:00
2023-01-03 16:51:32 +01:00
await Circuit . updateData ( idapp , circuitname )
if ( ris ) {
// Invia una notifica alla persona e agli Admin
tools . sendNotificationByCircuit ( idapp , usernameOrig , circuitname , cmd , value , true , username _action , extrarec ) ;
outres . result = await Circuit . getInfoCircuitByName ( idapp , circuitname ) ;
}
} else {
const foundIfAlreadyCircuit = await this . ifAlreadyInCircuit ( idapp , usernameOrig , circuitname ) ;
if ( ! foundIfAlreadyCircuit ) {
2022-08-30 17:00:48 +02:00
update = {
$push : {
2023-01-03 16:51:32 +01:00
'profile.mycircuits' : {
circuitname ,
2022-08-30 17:00:48 +02:00
date : new Date ( ) ,
} ,
} ,
} ;
2023-01-03 16:51:32 +01:00
ris = await User . updateOne ( { idapp , username : usernameOrig } , update ) ;
// Elimina eventualmente se era bloccato:
update = { $pull : { refused _users : { username : { $in : [ usernameOrig ] } } } } ;
await Circuit . updateOne ( { idapp , name : circuitname } , update ) ;
await Account . createAccount ( idapp , usernameOrig , circuitname ) ;
2022-09-12 18:37:08 +02:00
2023-01-03 16:51:32 +01:00
} else {
ris = false ;
2022-08-30 17:00:48 +02:00
}
2023-01-03 16:51:32 +01:00
await Circuit . updateData ( idapp , circuitname )
2022-08-30 17:00:48 +02:00
if ( ris ) {
2023-01-03 16:51:32 +01:00
// Invia una notifica alla persona e agli Admin
tools . sendNotificationByCircuit ( idapp , usernameOrig , circuitname , cmd , value , true , username _action , extrarec ) ;
outres . result = await Circuit . getInfoCircuitByName ( idapp , circuitname ) ;
}
}
2023-06-01 11:39:53 +02:00
} else if ( cmd === shared _consts . CIRCUITCMD . SETFIDO ) {
ris = await Circuit . setFido ( idapp , usernameOrig , circuitname , groupname ) ;
2023-12-12 00:56:40 +01:00
if ( ris && ris . fidoConcesso && ris . changed ) {
2023-06-01 11:39:53 +02:00
2023-12-11 20:48:52 +01:00
if ( extrarec ) {
extrarec . fidoConcesso = ris . fidoConcesso ;
extrarec . qta _maxConcessa = ris . qta _maxConcessa ;
}
2023-06-01 11:39:53 +02:00
2023-11-15 17:50:14 +01:00
// Elimina la richiesta:
update = { $pull : { req _users : { username : { $in : [ usernameOrig ] } } } } ;
await Circuit . updateOne ( { idapp , name : circuitname } , update ) ;
await Circuit . updateData ( idapp , circuitname )
if ( ris ) {
// Invia una notifica alla persona e agli Admin
tools . sendNotificationByCircuit ( idapp , usernameOrig , circuitname , cmd , value , true , username _action , extrarec ) ;
outres . result = await Circuit . getInfoCircuitByName ( idapp , circuitname ) ;
}
} else {
// errore !?
2023-06-01 11:39:53 +02:00
}
2023-01-03 16:51:32 +01:00
} else if ( cmd === shared _consts . CIRCUITCMD . REQ ) {
if ( groupname ) {
// Aggiungo la richiesta di Gruppo a me
const foundIfAlreadyAskCircuit = await Circuit . findOne ( {
idapp ,
name : circuitname ,
'req_groups' : {
$elemMatch : { groupname : { $eq : groupname } } ,
} ,
} ) ;
if ( value ) {
2023-08-27 23:55:31 +02:00
ris = await this . addCircuitToUser ( idapp , usernameOrig , circuitname , false , groupname , '' ) ;
2023-01-03 16:51:32 +01:00
if ( ! foundIfAlreadyAskCircuit ) {
update = {
$push : {
'req_groups' : {
groupname ,
date : new Date ( ) ,
} ,
} ,
} ;
ris = await Circuit . updateOne ( { idapp , name : circuitname } , update ) ;
}
if ( ris ) {
// Invia una notifica alla persona
await tools . sendNotificationByCircuit ( idapp , usernameOrig , circuitname , cmd , true , true , username _action , extrarec ) ;
}
} else {
if ( foundIfAlreadyAskCircuit ) {
outres . result = await MyGroup . removeCircuitFromGroup ( idapp , groupname , circuitname ) ; // Rimuovo il Circuito dal gruppo
// Invia una notifica alla persona
await tools . sendNotificationByCircuit ( idapp , usernameOrig , circuitname , cmd , false , true , username _action , extrarec ) ;
}
2022-08-30 17:00:48 +02:00
}
2023-01-03 16:51:32 +01:00
2022-08-30 17:00:48 +02:00
} else {
2023-06-01 11:39:53 +02:00
// Aggiungo la richiesta di Circuito a me
2023-01-03 16:51:32 +01:00
const foundIfAlreadyAskCircuit = await Circuit . findOne ( {
idapp ,
name : circuitname ,
'req_users' : {
$elemMatch : { username : { $eq : usernameOrig } } ,
} ,
} ) ;
2022-08-30 17:00:48 +02:00
2023-01-03 16:51:32 +01:00
if ( value ) {
2023-06-01 11:39:53 +02:00
// Aggiungi intanto l'utente al Circuito (senza fido)
ris = await this . addCircuitToUser ( idapp , usernameOrig , circuitname , false ) ;
2023-01-03 16:51:32 +01:00
if ( ! foundIfAlreadyAskCircuit ) {
update = {
$push : {
'req_users' : {
username : usernameOrig ,
date : new Date ( ) ,
} ,
} ,
} ;
2023-06-01 11:39:53 +02:00
// Aggiungi la richiesta per ottenere il fido
2023-01-03 16:51:32 +01:00
ris = await Circuit . updateOne ( { idapp , name : circuitname } , update ) ;
}
if ( ris ) {
// Invia una notifica alla persona
await tools . sendNotificationByCircuit ( idapp , usernameOrig , circuitname , cmd , true , true , username _action , extrarec ) ;
}
} else {
if ( foundIfAlreadyAskCircuit ) {
outres . result = await this . removeFromCircuits ( idapp , usernameOrig , circuitname ) ; // Rimuovo il Gruppo da me
// Invia una notifica alla persona
await tools . sendNotificationByCircuit ( idapp , usernameOrig , circuitname , cmd , false , true , username _action , extrarec ) ;
}
2022-08-30 17:00:48 +02:00
}
2023-01-03 16:51:32 +01:00
2022-08-30 17:00:48 +02:00
}
2023-01-03 16:51:32 +01:00
2022-09-02 02:25:38 +02:00
} else if ( cmd === shared _consts . CIRCUITCMD . REMOVE _FROM _MYLIST ) {
2022-08-30 17:00:48 +02:00
2023-02-02 13:53:02 +01:00
if ( groupname ) {
2023-08-27 23:55:31 +02:00
// Elimina la richiesta:
update = { $pull : { req _groups : { groupname : { $in : [ groupname ] } } } } ;
await Circuit . updateOne ( { idapp , name : circuitname } , update ) ;
2023-02-02 13:53:02 +01:00
outres . result = await MyGroup . removeCircuitFromGroup ( idapp , groupname , circuitname ) ; // Rimuovo l'Amicizia da me
} else {
2022-08-30 17:00:48 +02:00
2023-02-02 13:53:02 +01:00
// Remove if is also an Admin
await Circuit . removeAdminOfMyCircuit ( idapp , usernameOrig , circuitname ) ;
outres . result = await this . removeFromCircuits ( idapp , usernameOrig , circuitname ) ; // Rimuovo l'Amicizia da me
console . log ( 'ris' , ris ) ;
2022-08-30 17:00:48 +02:00
2023-02-02 13:53:02 +01:00
await Circuit . updateData ( idapp , circuitname ) ;
}
2022-10-22 15:38:15 +02:00
2022-08-30 17:00:48 +02:00
// Invia una notifica alla persona
2022-09-03 13:06:58 +02:00
await tools . sendNotificationByCircuit ( idapp , usernameOrig , circuitname , cmd , false , true , username _action , extrarec ) ;
2022-08-30 17:00:48 +02:00
2022-09-02 02:25:38 +02:00
} else if ( cmd === shared _consts . CIRCUITCMD . DELETE ) {
2022-08-30 17:00:48 +02:00
2022-09-02 02:25:38 +02:00
ris = await this . removeFromCircuits ( idapp , usernameOrig , circuitname ) ; // Rimuovo l'Amicizia da me
2022-08-30 17:00:48 +02:00
if ( ris ) {
// Invia una notifica alla persona e agli Admin
2022-09-03 13:06:58 +02:00
await tools . sendNotificationByCircuit ( idapp , usernameOrig , circuitname , cmd , false , true , username _action , extrarec ) ;
2022-08-30 17:00:48 +02:00
}
2022-09-14 17:37:29 +02:00
outres . result = await Circuit . deleteCircuit ( idapp , usernameOrig , circuitname ) ; // Rimuovo l'Amicizia da me
2022-08-30 17:00:48 +02:00
2022-09-02 02:25:38 +02:00
} else if ( cmd === shared _consts . CIRCUITCMD . CANCEL _REQ ) {
2022-08-30 17:00:48 +02:00
2023-01-03 16:51:32 +01:00
if ( groupname )
outres . result = await Circuit . removeReqGroupCircuit ( idapp , groupname , circuitname ) ; // Rimuovo l'Amicizia da me
else
outres . result = await Circuit . removeReqCircuit ( idapp , usernameOrig , circuitname ) ; // Rimuovo l'Amicizia da me
2022-10-22 15:38:15 +02:00
await Circuit . updateData ( idapp , circuitname )
2022-08-30 17:00:48 +02:00
2022-09-02 02:25:38 +02:00
} else if ( cmd === shared _consts . CIRCUITCMD . REFUSE _REQ ) {
2022-08-30 17:00:48 +02:00
2023-01-03 16:51:32 +01:00
if ( groupname ) {
outres . result = await MyGroup . removeCircuitFromGroup ( idapp , groupname , circuitname ) ; // Rimuovo l'Amicizia da me
outres . result = await Circuit . removeReqGroupCircuit ( idapp , groupname , circuitname ) ; // Rimuovo l'Amicizia da me
outres . result = await Circuit . refuseReqGroupCircuit ( idapp , groupname , circuitname ) ; // Rimuovo l'Amicizia da me
} else {
outres . result = await this . removeFromCircuits ( idapp , usernameOrig , circuitname ) ; // Rimuovo l'Amicizia da me
outres . result = await Circuit . removeReqCircuit ( idapp , usernameOrig , circuitname ) ; // Rimuovo l'Amicizia da me
outres . result = await Circuit . refuseReqCircuit ( idapp , usernameOrig , circuitname ) ; // Rimuovo l'Amicizia da me
}
2022-08-30 17:00:48 +02:00
2022-09-12 18:37:08 +02:00
// Invia una notifica alla persona
await tools . sendNotificationByCircuit ( idapp , usernameOrig , circuitname , cmd , false , true , username _action , extrarec ) ;
2022-09-02 02:25:38 +02:00
} else if ( cmd === shared _consts . CIRCUITCMD . ADDADMIN ) {
2022-09-20 11:14:23 +02:00
outres . result = await Circuit . addToAdminOfMyCircuit ( idapp , usernameOrig , circuitname ) ; // Rimuovo la richiesta di entrare nel gruppo
2022-08-30 17:00:48 +02:00
// Invia una notifica alla persona
2022-09-03 13:06:58 +02:00
await tools . sendNotificationByCircuit ( idapp , usernameOrig , circuitname , cmd , false , true , username _action , extrarec ) ;
2022-08-30 17:00:48 +02:00
2022-09-02 02:25:38 +02:00
} else if ( cmd === shared _consts . CIRCUITCMD . REMOVEADMIN ) {
2022-09-20 11:14:23 +02:00
outres . result = await Circuit . removeAdminOfMyCircuit ( idapp , usernameOrig , circuitname ) ; // Rimuovo la richiesta di entrare nel gruppo
2022-09-13 12:28:49 +02:00
2022-08-30 17:00:48 +02:00
// Invia una notifica alla persona
2022-09-03 13:06:58 +02:00
await tools . sendNotificationByCircuit ( idapp , usernameOrig , circuitname , cmd , false , true , username _action , extrarec ) ;
} else if ( cmd === shared _consts . CIRCUITCMD . SENDCOINS _REQ ) {
2022-09-12 18:37:08 +02:00
2022-09-14 17:37:29 +02:00
outres = await Circuit . sendCoins ( true , idapp , usernameOrig , extrarec ) ;
if ( outres . cansend ) {
2022-09-12 18:37:08 +02:00
// Invia una notifica di moneta alla persona
2022-10-13 21:10:07 +02:00
const out = await tools . sendNotificationByCircuit ( idapp , usernameOrig , circuitname , cmd , false , true , username _action ,
2022-12-10 02:01:17 +01:00
extrarec ) ;
2022-10-13 21:10:07 +02:00
if ( out )
outres . result = out . ris ;
2022-09-12 18:37:08 +02:00
} else {
2022-09-14 17:37:29 +02:00
outres . cansend = false ;
2022-09-12 18:37:08 +02:00
}
2022-09-03 13:06:58 +02:00
2022-09-18 01:21:04 +02:00
ris = true ;
2022-09-14 11:32:04 +02:00
} else if ( ( cmd === shared _consts . CIRCUITCMD . SENDCOINS _ACCEPT ) || ( cmd === shared _consts . CIRCUITCMD . SENDCOINS _REFUSE ) ) {
2022-09-13 12:28:49 +02:00
// Before to accept, I see if it's already set !
2023-11-15 17:50:14 +01:00
2022-09-14 17:37:29 +02:00
outres = {
2022-09-13 12:28:49 +02:00
cansend : false ,
errormsg : '' ,
} ;
2022-10-13 21:10:07 +02:00
let outcheck = outres ;
2022-09-13 12:28:49 +02:00
let risStatus = '' ;
const status = await SendNotif . getStatus ( extrarec . notifId ) ;
if ( status === shared _consts . CircuitsNotif . STATUS _ACCEPTED ) {
risStatus = i18n . _ _ ( 'STATUS_SENT' ) ;
} else if ( status === shared _consts . CircuitsNotif . STATUS _REFUSED ) {
risStatus = i18n . _ _ ( 'STATUS_REFUSED' ) ;
2022-09-12 18:37:08 +02:00
}
2022-10-13 21:10:07 +02:00
// if (!await SendNotif.checkIfCoinsAlreadySent(extrarec.notifId)) {
if ( ! await Movement . checkIfCoinsAlreadySent ( extrarec . notifId ) ) {
2022-09-14 11:32:04 +02:00
if ( cmd === shared _consts . CIRCUITCMD . SENDCOINS _ACCEPT ) {
2022-10-13 21:10:07 +02:00
outcheck = await Circuit . sendCoins ( true , idapp , usernameOrig , extrarec ) ;
2022-09-14 11:32:04 +02:00
} else {
2022-10-13 21:10:07 +02:00
outcheck . cansend = true ;
2022-09-14 17:37:29 +02:00
outres . cansend = true ;
2022-09-14 11:32:04 +02:00
}
2022-08-30 17:00:48 +02:00
2022-10-13 21:10:07 +02:00
if ( cmd === shared _consts . CIRCUITCMD . SENDCOINS _ACCEPT && outcheck . cansend ) {
if ( ! await Movement . checkIfCoinsAlreadySent ( extrarec . notifId ) ) {
outres = await Circuit . sendCoins ( false , idapp , usernameOrig , extrarec ) ;
2023-01-03 16:51:32 +01:00
2022-10-13 21:10:07 +02:00
} else {
outcheck . cansend = false ; //GIA INVIATO
}
}
if ( outcheck . cansend ) {
// Invia una notifica di moneta (accettata o rifiutata) alla persona
const out = await tools . sendNotificationByCircuit ( idapp , usernameOrig , circuitname , cmd , false , true , username _action , extrarec ) ;
2023-01-03 16:51:32 +01:00
if ( outres && extrarec . groupname ) {
// Setta agli altri admin,
}
2022-10-13 21:10:07 +02:00
}
2022-09-13 12:28:49 +02:00
2022-09-14 17:37:29 +02:00
outres . recnotif = await SendNotif . getRecNotif ( extrarec . notifId ) ;
2024-05-09 23:36:46 +02:00
outres . arrrecnotif = await SendNotif . findAllNotifByUsernameIdAndIdApp ( username _action , extrarec . lastdr , idapp , shared _consts . LIMIT _NOTIF _FOR _USER , shared _consts . TypeNotifs . TYPEDIR _OTHERS ) ;
outres . arrrecnotifcoins = await SendNotif . findAllNotifByUsernameIdAndIdApp ( username _action , extrarec . lastdr , idapp , shared _consts . LIMIT _NOTIF _FOR _USER , shared _consts . TypeNotifs . TYPEDIR _CIRCUITS ) ;
2022-09-14 11:32:04 +02:00
2022-09-13 12:28:49 +02:00
} else {
2022-09-14 17:37:29 +02:00
outres . cansend = false ;
2022-09-13 12:28:49 +02:00
2022-09-14 17:37:29 +02:00
outres . errormsg = i18n . _ _ ( 'CIRCUIT_COINS_ALREADY_PROCESSED' , risStatus ) ;
2022-09-13 12:28:49 +02:00
}
2022-09-14 17:37:29 +02:00
ris = true ;
2022-08-30 17:00:48 +02:00
}
2022-09-14 17:37:29 +02:00
if ( ris && username _action ) {
2023-03-21 18:11:56 +01:00
outres = await this . updateMyData ( outres , idapp , username _action ) ;
2022-09-14 17:37:29 +02:00
}
if ( circuitname )
outres . circuit = await Circuit . getInfoCircuitByName ( idapp , circuitname ) ;
return outres ;
2022-08-30 17:00:48 +02:00
} catch ( e ) {
2022-09-02 02:25:38 +02:00
console . error ( 'Error setCircuitCmd: ' , e ) ;
2022-08-30 17:00:48 +02:00
}
return ris ;
} ;
2022-01-12 00:38:47 +01:00
function getWhatToShow ( idapp , username ) {
return {
2022-01-07 01:18:01 +01:00
username : 1 ,
aportador _solidario : 1 ,
name : 1 ,
surname : 1 ,
2022-12-10 02:01:17 +01:00
lasttimeonline : 1 ,
2022-01-07 01:18:01 +01:00
deleted : 1 ,
sospeso : 1 ,
2022-08-04 17:30:57 +02:00
reported : 1 ,
2022-08-09 17:32:06 +02:00
date _report : 1 ,
username _who _report : 1 ,
2022-01-07 01:18:01 +01:00
verified _email : 1 ,
verified _by _aportador : 1 ,
2022-02-26 17:35:50 +01:00
notask _verif : 1 ,
2022-01-07 01:18:01 +01:00
'profile.nationality' : 1 ,
2022-07-21 00:21:03 +02:00
'profile.mygroups' : 1 ,
2022-09-03 13:06:58 +02:00
'profile.mycircuits' : 1 ,
2022-01-20 00:39:06 +01:00
'profile.qualifica' : 1 ,
2022-01-07 01:18:01 +01:00
'profile.biografia' : 1 ,
'profile.username_telegram' : 1 ,
2022-03-11 12:38:01 +01:00
'profile.firstname_telegram' : 1 ,
'profile.lastname_telegram' : 1 ,
2022-02-24 23:36:44 +01:00
'profile.intcode_cell' : 1 ,
'profile.cell' : 1 ,
2022-01-23 23:25:34 +01:00
'profile.website' : 1 ,
2022-01-07 01:18:01 +01:00
'profile.img' : 1 ,
'profile.sex' : 1 ,
'profile.dateofbirth' : 1 ,
2022-02-21 13:12:27 +01:00
'profile.born_city_id' : 1 ,
2022-01-07 01:18:01 +01:00
'profile.born_province' : 1 ,
'profile.born_country' : 1 ,
2023-03-17 19:07:31 +01:00
'profile.resid_province' : 1 ,
2023-11-26 01:38:02 +01:00
'profile.resid_card' : 1 ,
2022-12-03 03:14:26 +01:00
'profile.calc' : 1 ,
2022-01-07 01:18:01 +01:00
email : 1 ,
date _reg : 1 ,
2023-01-06 15:51:48 +01:00
'profile.friends' : 1 ,
'profile.handshake' : 1 ,
2023-04-12 15:37:54 +02:00
} ;
2022-01-07 01:18:01 +01:00
2022-01-12 00:38:47 +01:00
}
2022-01-14 23:54:33 +01:00
function getWhatToShow _Unknown ( idapp , username ) {
return {
username : 1 ,
aportador _solidario : 1 ,
name : 1 ,
// deleted: 1,
// sospeso: 1,
verified _email : 1 ,
verified _by _aportador : 1 ,
2022-02-24 23:36:44 +01:00
'profile.username_telegram' : 1 ,
2022-01-14 23:54:33 +01:00
'profile.img' : 1 ,
'profile.sex' : 1 ,
'profile.born_province' : 1 ,
'profile.born_country' : 1 ,
2023-03-17 19:07:31 +01:00
'profile.resid_province' : 1 ,
2023-11-26 01:38:02 +01:00
'profile.resid_card' : 1 ,
2022-12-03 03:14:26 +01:00
'profile.calc' : 1 ,
2022-01-14 23:54:33 +01:00
date _reg : 1 ,
2023-01-06 15:51:48 +01:00
'profile.handshake' : 1 ,
'profile.friends' : 1 ,
2023-04-07 02:45:21 +02:00
'profile.favorite' : 1 ,
'profile.bookmark' : 1 ,
2023-04-17 00:11:36 +02:00
'profile.attend' : 1 ,
2023-04-13 14:27:00 +02:00
'profile.seen' : 1 ,
2023-04-12 15:37:54 +02:00
}
2022-01-14 23:54:33 +01:00
}
2022-12-10 02:01:17 +01:00
UserSchema . statics . getWhatToShow _IfFriends = async function ( idapp , username ) {
2022-02-05 23:28:15 +01:00
return {
username : 1 ,
aportador _solidario : 1 ,
name : 1 ,
// deleted: 1,
// sospeso: 1,
verified _email : 1 ,
2022-02-24 23:36:44 +01:00
'profile.username_telegram' : 1 ,
2022-02-05 23:28:15 +01:00
verified _by _aportador : 1 ,
'profile.img' : 1 ,
'profile.sex' : 1 ,
'profile.born_province' : 1 ,
'profile.born_country' : 1 ,
2023-03-17 19:07:31 +01:00
'profile.resid_province' : 1 ,
2023-11-26 01:38:02 +01:00
'profile.resid_card' : 1 ,
2022-12-03 03:14:26 +01:00
'profile.calc' : 1 ,
2022-08-10 17:07:02 +02:00
reported : 1 ,
date _report : 1 ,
username _who _report : 1 ,
2022-02-05 23:28:15 +01:00
date _reg : 1 ,
groups : 1 ,
2023-01-06 15:51:48 +01:00
'profile.handshake' : 1 ,
'profile.friends' : 1 ,
2023-04-07 02:45:21 +02:00
'profile.favorite' : 1 ,
'profile.bookmark' : 1 ,
2023-04-17 00:11:36 +02:00
'profile.attend' : 1 ,
2023-04-13 14:27:00 +02:00
'profile.seen' : 1 ,
2023-04-12 15:37:54 +02:00
} ;
2022-02-05 23:28:15 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getInfoFriendByUsername = async function ( idapp , username ) {
2022-01-12 00:38:47 +01:00
const whatToShow = getWhatToShow ( idapp , username ) ;
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2022-01-12 00:38:47 +01:00
idapp ,
username ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2022-03-06 00:48:33 +01:00
} , whatToShow ) . lean ( ) . then ( ( rec ) => ! ! rec ? rec : null ) ;
2022-01-12 00:38:47 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getInfoAskFriendByUsername = async function (
idapp , username ) {
2022-01-14 23:54:33 +01:00
const whatToShow = getWhatToShow _Unknown ( idapp , username ) ;
2022-01-07 01:18:01 +01:00
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2022-01-07 01:18:01 +01:00
idapp ,
2022-01-14 23:54:33 +01:00
username ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2022-03-06 00:48:33 +01:00
} , whatToShow ) . lean ( ) . then ( ( rec ) => ! ! rec ? rec : null ) ;
2022-01-07 01:18:01 +01:00
2022-01-14 23:54:33 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getAskedFriendsByUsername = async function ( idapp , username ) {
2022-01-14 23:54:33 +01:00
const whatToShow _Unknown = getWhatToShow _Unknown ( idapp , username ) ;
2022-09-11 11:45:33 +02:00
return await User . find ( {
2022-01-14 23:54:33 +01:00
idapp ,
'profile.req_friends' : {
2022-12-10 02:01:17 +01:00
$elemMatch : { username : { $eq : username } } ,
2022-01-14 23:54:33 +01:00
} ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2022-01-14 23:54:33 +01:00
} , whatToShow _Unknown ) . then ( ( rec ) => {
//return rec.map(m => m.username);
} ) ;
} ;
2023-01-06 15:51:48 +01:00
2023-11-30 01:49:17 +01:00
UserSchema . statics . getUsersToVerify = async function ( idapp , username ) {
const whatToShow _Unknown = getWhatToShow ( idapp , username ) ;
let usersToVerify = await User . find ( {
idapp , aportador _solidario : username ,
verified _by _aportador : { $exists : false } ,
$or : [
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
} , whatToShow _Unknown ) . lean ( ) ;
return usersToVerify ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getFriendsByUsername = async function ( idapp , username ) {
2022-01-14 23:54:33 +01:00
2023-01-04 02:09:42 +01:00
if ( ! username ) {
return {
listFriends : [ ] ,
listRequestFriends : [ ] ,
listTrusted : [ ] ,
listSentRequestFriends : [ ] ,
}
}
2022-01-14 23:54:33 +01:00
try {
const whatToShow = getWhatToShow ( idapp , username ) ;
const whatToShow _Unknown = getWhatToShow _Unknown ( idapp , username ) ;
const arrUsernameFriends = await User . getUsernameFriendsByUsername ( idapp ,
2022-12-10 02:01:17 +01:00
username ) ;
2022-01-14 23:54:33 +01:00
const arrUsernameReqFriends = await User . getUsernameReqFriendsByUsername (
2022-12-10 02:01:17 +01:00
idapp , username ) ;
2023-01-06 15:51:48 +01:00
const arrUsernameHandShake = await User . getUsernameHandShakeByUsername ( idapp ,
username ) ;
2022-01-14 23:54:33 +01:00
let listFriends = await User . find ( {
idapp ,
2022-12-10 02:01:17 +01:00
username : { $in : arrUsernameFriends } ,
2022-01-14 23:54:33 +01:00
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
2022-01-14 23:54:33 +01:00
} , whatToShow ) ;
2023-01-06 15:51:48 +01:00
let listHandShake = await User . find ( {
idapp ,
username : { $in : arrUsernameHandShake } ,
$or : [
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
} , whatToShow ) ;
2022-01-14 23:54:33 +01:00
let listRequestFriends = await User . find ( {
idapp ,
2022-12-10 02:01:17 +01:00
username : { $in : arrUsernameReqFriends } ,
2022-01-14 23:54:33 +01:00
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
2022-01-14 23:54:33 +01:00
} , whatToShow _Unknown ) ;
let listSentRequestFriends = await User . find ( {
idapp ,
'profile.req_friends' : {
2022-12-10 02:01:17 +01:00
$elemMatch : { username : { $eq : username } } ,
2022-01-14 23:54:33 +01:00
} ,
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
2022-01-14 23:54:33 +01:00
} , whatToShow _Unknown ) ;
2022-01-07 01:18:01 +01:00
2022-01-14 23:54:33 +01:00
let listTrusted = await User . find ( {
idapp , aportador _solidario : username ,
2022-12-10 02:01:17 +01:00
'profile.teleg_id' : { $gt : 0 } ,
2022-01-14 23:54:33 +01:00
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
2022-01-14 23:54:33 +01:00
} , whatToShow ) ;
return {
listFriends ,
2023-01-06 15:51:48 +01:00
listHandShake ,
2022-01-14 23:54:33 +01:00
listRequestFriends ,
listTrusted ,
listSentRequestFriends ,
} ;
2022-01-07 01:18:01 +01:00
2022-01-14 23:54:33 +01:00
} catch ( e ) {
console . log ( 'Error' , e ) ;
}
return {
listFriends : [ ] ,
2023-01-06 15:51:48 +01:00
listHandShake : [ ] ,
2022-01-14 23:54:33 +01:00
listRequestFriends : [ ] ,
listTrusted : [ ] ,
listSentRequestFriends : [ ] ,
2022-01-15 12:25:57 +01:00
} ;
2022-01-07 01:18:01 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getAportadorSolidarioByUsername = async function (
idapp , username ) {
2020-01-30 01:19:25 +01:00
const User = this ;
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2020-05-04 19:34:41 +02:00
idapp , username ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2021-10-08 00:38:35 +02:00
} ) . then ( ( rec ) => {
return ( ( rec ) ? rec . aportador _solidario : '' ) ;
} ) . catch ( ( e ) => {
console . error ( 'getAportadorSolidarioByUsername' , e ) ;
} ) ;
2020-01-30 01:19:25 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . UserByIdTelegram = async function ( idapp , teleg _id ) {
2020-01-03 01:52:49 +01:00
const User = this ;
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2023-10-01 01:24:47 +02:00
idapp ,
'profile.teleg_id' : teleg _id ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2023-10-01 01:24:47 +02:00
} ) . lean ( ) . then ( async ( rec ) => {
if ( ! rec ) {
// Cerca se esiste in quello salvato in precedenza:
const recold = await User . findOne ( {
idapp ,
'profile.teleg_id_old' : teleg _id
} ) . lean ( ) ;
if ( recold && recold . profile . teleg _id _old === teleg _id ) {
// Riaggiorna l'ID perché è ritornato sulla chat!
await User . SetTelegramIdSuccess ( idapp , recold . _id , recold . profile . teleg _id _old ) ;
2020-01-03 01:52:49 +01:00
2023-10-01 01:24:47 +02:00
rec = await User . findOne ( {
idapp ,
'profile.teleg_id' : teleg _id ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} ) . lean ( ) ;
}
2020-03-10 21:44:14 +01:00
2023-10-01 01:24:47 +02:00
}
2022-03-06 00:48:33 +01:00
return ( ! ! rec ) ? rec : null ;
2021-10-08 00:38:35 +02:00
} ) . catch ( ( e ) => {
console . error ( 'UserExistByIdTelegram' , e ) ;
} ) ;
2020-03-10 21:44:14 +01:00
} ;
2023-10-01 01:24:47 +02:00
2022-12-10 02:01:17 +01:00
UserSchema . statics . setPicProfile = async function ( idapp , username , imgpic ) {
2022-03-01 23:50:58 +01:00
const User = this ;
const fields _to _update = {
'profile.img' : imgpic ,
} ;
2022-09-11 11:45:33 +02:00
return await User . findOneAndUpdate ( {
2022-03-01 23:50:58 +01:00
idapp , username ,
2022-12-10 02:01:17 +01:00
} , { $set : fields _to _update } , { new : false } ) . lean ( ) . then ( ( record ) => {
2022-03-01 23:50:58 +01:00
return ! ! record ;
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . TelegIdByUsername = async function ( idapp , username ) {
2020-01-03 22:02:18 +01:00
const User = this ;
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2020-05-04 19:34:41 +02:00
idapp , username ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} , { 'profile.teleg_id' : 1 } ) . lean ( ) . then ( ( rec ) => {
2021-10-08 00:38:35 +02:00
return ( ! ! rec ) ? rec . profile . teleg _id : null ;
} ) . catch ( ( e ) => {
console . error ( 'TelegIdByUsername' , e ) ;
} ) ;
2020-01-03 22:02:18 +01:00
} ;
2022-03-06 00:48:33 +01:00
2022-12-10 02:01:17 +01:00
UserSchema . statics . TelegIdById = async function ( idapp , id ) {
2022-03-06 00:48:33 +01:00
const User = this ;
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2022-03-06 00:48:33 +01:00
idapp ,
_id : id ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} , { 'profile.teleg_id' : 1 } ) . lean ( ) . then ( ( rec ) => {
2022-03-06 00:48:33 +01:00
return ( ! ! rec ) ? rec . profile . teleg _id : null ;
} ) . catch ( ( e ) => {
console . error ( 'TelegIdByUsername' , e ) ;
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . notAsk _VerifByUsername = async function ( idapp , username ) {
2022-02-26 17:35:50 +01:00
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2022-02-26 17:35:50 +01:00
idapp , username ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} , { 'notask_verif' : 1 } ) . lean ( ) . then ( ( rec ) => {
2022-03-08 01:01:20 +01:00
return ( ! ! rec && rec . notask _verif ) ? true : false ;
2022-02-26 17:35:50 +01:00
} ) . catch ( ( e ) => {
console . error ( 'notAsk_VerifByUsername' , e ) ;
return false ;
} ) ;
} ;
2020-01-03 22:02:18 +01:00
2022-12-10 02:01:17 +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 = {
2021-10-08 00:38:35 +02:00
'profile.teleg_checkcode' : teleg _checkcode ,
2020-01-03 01:52:49 +01:00
} ;
2022-09-11 11:45:33 +02:00
return await User . findOneAndUpdate ( {
2021-10-08 00:38:35 +02:00
_id : id ,
2022-12-10 02:01:17 +01:00
} , { $set : fields _to _update } , { new : false } ) . lean ( ) . then ( ( record ) => {
2020-01-03 01:52:49 +01:00
return ! ! record ;
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . NonVoglioImbarcarmi = async function ( idapp , username ) {
2020-06-08 13:31:05 +02:00
const User = this ;
const fields _to _update = {
2021-10-08 00:38:35 +02:00
non _voglio _imbarcarmi : true ,
2020-06-08 13:31:05 +02:00
} ;
2022-09-11 11:45:33 +02:00
return await User . findOneAndUpdate ( {
2020-06-08 13:31:05 +02:00
idapp ,
username ,
2022-12-10 02:01:17 +01:00
} , { $set : fields _to _update } , { new : false } ) . then ( ( record ) => {
2020-06-08 13:31:05 +02:00
return ! ! record ;
} ) ;
} ;
2022-12-10 02:01:17 +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 ,
2021-10-08 00:38:35 +02:00
'profile.teleg_checkcode' : 0 ,
2020-01-03 01:52:49 +01:00
} ;
2022-09-11 11:45:33 +02:00
return await User . findOneAndUpdate ( {
2020-01-03 01:52:49 +01:00
idapp ,
2021-10-08 00:38:35 +02:00
_id : id ,
2022-12-10 02:01:17 +01:00
} , { $set : fields _to _update } , { new : false } ) . lean ( ) . then ( ( record ) => {
2022-03-09 14:53:09 +01:00
if ( record ) {
return User . findOne ( {
idapp ,
2022-03-10 23:19:56 +01:00
_id : id ,
2022-03-09 14:53:09 +01:00
} ) . lean ( ) ;
}
2020-02-19 16:09:16 +01:00
} ) ;
2022-03-09 14:53:09 +01:00
return null ;
2020-02-19 16:09:16 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsernameTelegram = async function ( idapp , username ) {
2022-10-08 18:09:38 +02:00
const User = this ;
2022-12-10 02:01:17 +01:00
return await User . findOne ( { idapp , username } , { 'profile.username_telegram' : 1 } ) . lean ( ) . then ( ( ris ) => {
2022-10-08 18:09:38 +02:00
if ( ris )
return ris . profile . username _telegram ;
else
return '' ;
} ) ;
2022-09-18 20:17:24 +02:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . setUsernameTelegram = async function (
idapp , id , username _telegram , firstname _telegram , lastname _telegram ) {
2022-01-26 01:31:22 +01:00
const User = this ;
const fields _to _update = {
'profile.username_telegram' : username _telegram ,
'profile.firstname_telegram' : firstname _telegram ,
'profile.lastname_telegram' : lastname _telegram ,
} ;
2022-09-11 11:45:33 +02:00
return await User . findOneAndUpdate ( {
2022-01-26 01:31:22 +01:00
idapp ,
_id : id ,
2022-12-10 02:01:17 +01:00
} , { $set : fields _to _update } , { new : false } ) . then ( ( record ) => {
2022-01-26 01:31:22 +01:00
return record ;
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . SetLang = async function ( idapp , id , lang ) {
2020-03-31 20:34:24 +02:00
const User = this ;
const fields _to _update = {
lang ,
} ;
2022-09-11 11:45:33 +02:00
return await User . findOneAndUpdate ( {
2021-10-08 00:38:35 +02:00
_id : id ,
2022-12-10 02:01:17 +01:00
} , { $set : fields _to _update } , { new : false } ) . then ( ( record ) => {
2020-03-31 20:34:24 +02:00
return record ;
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . SetTelegramWasBlocked = async function ( idapp , teleg _id ) {
2020-02-19 16:09:16 +01:00
const User = this ;
const fields _to _update = {
'profile.teleg_id_old' : teleg _id ,
'profile.teleg_id' : 0 ,
} ;
2023-10-01 01:24:47 +02:00
// if (tools.sulServer()) {
if ( true ) {
2020-05-19 00:18:13 +02:00
const ris = await User . findOneAndUpdate ( {
idapp ,
2021-10-08 00:38:35 +02:00
'profile.teleg_id' : teleg _id ,
2022-12-10 02:01:17 +01:00
} , { $set : fields _to _update } , { new : false } ) . then ( ( record ) => {
2020-05-19 00:18:13 +02:00
return record ;
} ) ;
}
2020-01-03 01:52:49 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getNameSurnameByUsername = async function (
idapp , username , reale = false ) {
2020-01-03 22:02:18 +01:00
const User = this ;
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2020-05-04 19:34:41 +02:00
idapp , username ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} , { username : 1 , name : 1 , surname : 1 } ) . then ( ( rec ) => {
2022-02-08 23:14:08 +01:00
let ris = rec . username ;
2022-02-09 19:48:39 +01:00
if ( ! ! rec ) {
if ( reale ) {
if ( ! rec . name )
2022-02-11 01:08:03 +01:00
return '' ;
2022-02-09 19:48:39 +01:00
2022-02-11 01:08:03 +01:00
ris = ` ${ rec . name } ${ rec . surname } ` ;
2022-02-09 19:48:39 +01:00
} else {
if ( rec . name ) {
2022-02-11 01:08:03 +01:00
ris = ` ${ rec . name } ${ rec . surname } ` ;
2022-02-09 19:48:39 +01:00
}
}
2022-02-08 23:14:08 +01:00
}
2022-02-09 19:48:39 +01:00
return ( ! ! rec ) ? ris : '' ;
} ) . catch ( ( e ) => {
console . error ( 'getNameSurnameByUsername' , e ) ;
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getIdByUsername = async function ( idapp , username ) {
2022-02-09 19:48:39 +01:00
const User = this ;
2022-05-04 00:26:12 +02:00
let regexp = new RegExp ( ` ^ ${ username } $ ` , 'i' ) ;
2022-03-10 00:28:23 +01:00
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2022-03-10 00:28:23 +01:00
idapp ,
2022-12-10 02:01:17 +01:00
username : { $regex : regexp } ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} , { username : 1 , _id : 1 } ) . then ( ( rec ) => {
2022-02-09 19:48:39 +01:00
return ( ! ! rec ) ? rec . _id . toString ( ) : '' ;
2021-10-08 00:38:35 +02:00
} ) . catch ( ( e ) => {
2022-03-10 00:28:23 +01:00
console . error ( 'getIdByUsername' , e ) ;
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getRealUsernameByUsername = async function ( idapp , username ) {
2022-03-10 00:28:23 +01:00
const User = this ;
2022-05-04 00:26:12 +02:00
let regexp = new RegExp ( ` ^ ${ username } $ ` , 'i' ) ;
2022-03-10 00:28:23 +01:00
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2022-03-10 00:28:23 +01:00
idapp ,
2022-03-10 23:19:56 +01:00
username :
2022-12-10 02:01:17 +01:00
{ $regex : regexp } ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} , { username : 1 , _id : 1 } ) . then ( ( rec ) => {
2022-03-10 00:28:23 +01:00
return ( ! ! rec ) ? rec . username : '' ;
} ) . catch ( ( e ) => {
console . error ( 'getRealUsernameByUsername' , e ) ;
2021-10-08 00:38:35 +02:00
} ) ;
2020-01-03 22:02:18 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getRecLangAndIdByUsername = async function ( idapp , username ) {
2022-03-06 00:48:33 +01:00
const User = this ;
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2022-03-06 00:48:33 +01:00
idapp , username ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} , { lang : 1 , _id : 1 } ) . then ( ( rec ) => {
2022-03-06 00:48:33 +01:00
return ( ! ! rec ) ? rec : null ;
} ) . catch ( ( e ) => {
console . error ( 'getRecLangAndIdByUsername' , e ) ;
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getNameSurnameById = async function ( idapp , userId ) {
2021-04-30 01:31:12 +02:00
const User = this ;
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2021-04-30 01:31:12 +02:00
idapp ,
_id : userId ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} , { name : 1 , surname : 1 } ) . then ( ( rec ) => {
2021-10-08 00:38:35 +02:00
return ( ! ! rec ) ? ` ${ rec . name } ${ rec . surname } ` : '' ;
} ) . catch ( ( e ) => {
console . error ( 'getNameSurnameById' , e ) ;
} ) ;
2021-04-30 01:31:12 +02:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getusersManagers = async function ( idapp ) {
2020-01-03 22:02:18 +01:00
const User = this ;
2023-06-07 10:14:57 +02:00
// Int32 mongodb 6.0
2023-08-27 23:55:31 +02:00
return await User . find ( { idapp , 'profile.manage_telegram' : true , perm : { $bitsAnySet : 0b010 } } ,
2022-12-10 02:01:17 +01:00
{ username : 1 , 'profile.teleg_id' : 1 , perm : 1 } ) . then ( ( arrrec ) => {
return ( ! ! arrrec ) ? arrrec : null ;
} ) . catch ( ( e ) => {
console . error ( 'getusersManagers' , e ) ;
} ) ;
2020-01-03 22:02:18 +01:00
} ;
2019-10-27 00:37:10 +02:00
2023-06-07 10:14:57 +02:00
UserSchema . statics . getusersAdmin = async function ( idapp ) {
const User = this ;
// Int32 mongodb 6.0
2023-12-29 15:19:15 +01:00
return await User . find ( { idapp , 'profile.admin_telegram' : true , perm : { $bitsAnySet : 0b001 } } ,
2023-06-07 10:14:57 +02:00
{ username : 1 , 'profile.teleg_id' : 1 , perm : 1 } ) . then ( ( arrrec ) => {
return ( ! ! arrrec ) ? arrrec : null ;
} ) . catch ( ( e ) => {
console . error ( 'getusersAdmin' , e ) ;
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getusersRespList = async function ( idapp ) {
2021-02-11 02:20:35 +01:00
const User = this ;
2022-12-10 02:01:17 +01:00
return await User . find ( { idapp , 'profile.resplist' : true } ,
{ _id : 1 , username : 1 , name : 1 , surname : 1 } ) . then ( ( arrrec ) => {
return ( ! ! arrrec ) ? arrrec : null ;
} ) . catch ( ( e ) => {
console . error ( 'getusersRespList' , e ) ;
} ) ;
2021-02-11 02:20:35 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getusersWorkersList = async function ( idapp ) {
2021-02-11 02:20:35 +01:00
const User = this ;
2022-12-10 02:01:17 +01:00
return await User . find ( { idapp , 'profile.workerslist' : true } ,
{ _id : 1 , username : 1 , name : 1 , surname : 1 } ) . then ( ( arrrec ) => {
return ( ! ! arrrec ) ? arrrec : null ;
} ) . catch ( ( e ) => {
console . error ( 'getusersWorkersList' , e ) ;
} ) ;
2021-02-11 02:20:35 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getusersManagersAndZoomeri = async function ( idapp ) {
2020-09-04 00:06:49 +02:00
const User = this ;
2022-09-11 11:45:33 +02:00
return await User . find (
2022-12-10 02:01:17 +01:00
{
idapp ,
or : [
{ 'profile.manage_telegram' : true } ,
{
perm :
2021-10-08 00:38:35 +02:00
{
2022-12-10 02:01:17 +01:00
$bit :
2020-09-04 00:06:49 +02:00
2022-12-10 02:01:17 +01:00
Number ( shared _consts . Permissions . Zoomeri ) ,
2020-09-04 00:06:49 +02:00
2021-10-08 00:38:35 +02:00
} ,
2022-12-10 02:01:17 +01:00
} ,
] ,
} ,
{ 'profile.teleg_id' : 1 } ) . then ( ( arrrec ) => {
return ( ! ! arrrec ) ? arrrec : null ;
} ) . catch ( ( e ) => {
console . error ( 'getusersManagers' , e ) ;
} ) ;
2020-09-04 00:06:49 +02:00
} ;
2022-12-10 02:01:17 +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 ) {
2022-12-10 02:01:17 +01:00
return await User . find ( { idapp , username , 'profile.teleg_id' : { $gt : 0 } } ) . lean ( ) ;
2022-03-08 01:01:20 +01:00
then ( ( arrrec ) => {
return ( ! ! arrrec ) ? arrrec : null ;
} ) .
2022-12-10 02:01:17 +01:00
catch ( ( e ) => {
console . error ( 'getUsersTelegALL' , e ) ;
} ) ;
2020-03-10 21:44:14 +01:00
} else {
2022-12-10 02:01:17 +01:00
return await User . find ( { idapp , 'profile.teleg_id' : { $gt : 0 } } ) .
lean ( ) .
then ( ( arrrec ) => {
return ( ! ! arrrec ) ? arrrec : null ;
} ) .
catch ( ( e ) => {
console . error ( 'getUsersTelegALL' , e ) ;
} ) ;
2020-03-10 21:44:14 +01:00
}
2020-01-20 01:48:25 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . isManagerByIdTeleg = async function ( idapp , idtelegram ) {
2020-01-20 01:48:25 +01:00
const User = this ;
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2020-01-27 15:07:53 +01:00
idapp ,
'profile.manage_telegram' : true ,
2021-10-08 00:38:35 +02:00
'profile.teleg_id' : idtelegram ,
2022-12-10 02:01:17 +01:00
} , { 'profile.teleg_id' : 1 } ) . then ( ( rec ) => {
2021-10-08 00:38:35 +02:00
return ( ! ! rec && rec . profile . teleg _id === idtelegram ) ;
} ) . catch ( ( e ) => {
2023-06-07 10:14:57 +02:00
console . error ( 'isManagerByIdTeleg' , e ) ;
2021-10-08 00:38:35 +02:00
return false ;
} ) ;
2020-01-27 15:07:53 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . isAdminByIdTeleg = async function ( idapp , idtelegram ) {
2020-01-27 15:07:53 +01:00
const User = this ;
2022-09-11 11:45:33 +02:00
return await User . findOne ( {
2020-01-27 15:07:53 +01:00
idapp ,
2021-09-22 01:13:41 +02:00
username : 'paoloar77' ,
2023-12-29 15:19:15 +01:00
'profile.admin_telegram' : true ,
2021-10-08 00:38:35 +02:00
'profile.teleg_id' : idtelegram ,
2022-12-10 02:01:17 +01:00
} , { 'profile.teleg_id' : 1 } ) . then ( ( rec ) => {
2021-10-08 00:38:35 +02:00
return ( ! ! rec && rec . profile . teleg _id === idtelegram ) ;
} ) . catch ( ( e ) => {
console . error ( 'getusersManagers' , e ) ;
return false ;
} ) ;
2020-01-20 01:48:25 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsersList = function ( idapp ) {
2019-10-13 20:44:05 +02:00
const User = this ;
2020-04-24 10:29:25 +02:00
return User . find ( {
2022-12-10 02:01:17 +01:00
'idapp' : idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} ,
{
username : 1 ,
name : 1 ,
surname : 1 ,
lasttimeonline : 1 ,
verified _email : 1 ,
verified _by _aportador : 1 ,
made _gift : 1 ,
perm : 1 ,
email : 1 ,
date _reg : 1 ,
img : 1 ,
} ,
2021-10-08 00:38:35 +02:00
) ;
2019-10-14 20:31:57 +02:00
} ;
/ * *
* Query blog posts by user - > paginated results and a total count .
* @ returns { Object } Object - > ` { rows, count } `
* /
2022-12-10 02:01:17 +01:00
UserSchema . statics . getFieldsForSearch = function ( ) {
2020-05-10 21:07:51 +02:00
return [
2022-12-10 02:01:17 +01:00
{ field : 'username' , type : tools . FieldType . string } ,
{ field : 'name' , type : tools . FieldType . string } ,
{ field : 'index' , type : tools . FieldType . number } ,
{ field : 'ind_order' , type : tools . FieldType . number } ,
2023-03-23 00:09:52 +01:00
// { field: 'old_order', type: tools.FieldType.number },
2022-12-10 02:01:17 +01: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 } ,
2023-03-23 00:09:52 +01:00
// { field: 'profile.payeer_id', type: tools.FieldType.string },
// { field: 'profile.advcash_id', type: tools.FieldType.string },
// { field: 'profile.revolut', type: tools.FieldType.string },
2022-12-10 02:01:17 +01:00
{ field : 'profile.link_payment' , type : tools . FieldType . string } ,
{ field : 'profile.teleg_id' , type : tools . FieldType . number } ,
{ field : 'profile.username_telegram' , type : tools . FieldType . string } ,
{ field : 'ipaddr' , type : tools . FieldType . string } ,
2022-03-08 01:01:20 +01:00
] ;
2022-02-28 17:20:47 +01:00
//{field: 'aportador_solidario', type: tools.FieldType.string}
2021-10-08 00:38:35 +02:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getFieldsForSearchUserFriend = function ( ) {
2022-12-15 21:57:38 +01:00
return [
{ field : 'username' , type : tools . FieldType . exact } ,
{ field : 'profile.username_telegram' , type : tools . FieldType . string } ,
{ field : 'name' , type : tools . FieldType . string } ,
{ field : 'surname' , type : tools . FieldType . string } ,
] ;
2022-01-12 00:38:47 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getFieldsForSearchUserFriend _AllWords = function ( ) {
2022-12-15 21:57:38 +01:00
return [
{ field : 'username' , type : tools . FieldType . string } ,
{ field : 'profile.username_telegram' , type : tools . FieldType . string } ,
{ field : 'name' , type : tools . FieldType . string } ,
{ field : 'surname' , type : tools . FieldType . string } ,
] ;
2022-02-21 13:12:27 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . executeQueryTable = function ( idapp , params ) {
2019-10-28 16:01:28 +01:00
params . fieldsearch = this . getFieldsForSearch ( ) ;
2022-01-12 00:38:47 +01:00
if ( params . options ) {
2022-01-14 23:54:33 +01:00
if ( tools . isBitActive ( params . options ,
2022-12-10 02:01:17 +01:00
shared _consts . OPTIONS _SEARCH _USER _ONLY _FULL _WORDS ) ) {
2022-01-12 00:38:47 +01:00
params . fieldsearch = this . getFieldsForSearchUserFriend ( ) ;
2022-02-21 13:12:27 +01:00
} else if ( tools . isBitActive ( params . options ,
2022-12-10 02:01:17 +01:00
shared _consts . OPTIONS _SEARCH _USER _ALL _WORDS ) ) {
2022-02-21 13:12:27 +01:00
params . fieldsearch = this . getFieldsForSearchUserFriend _AllWords ( ) ;
2022-01-12 00:38:47 +01:00
}
}
2019-10-20 01:21:54 +02:00
return tools . executeQueryTable ( this , idapp , params ) ;
2019-10-13 20:44:05 +02:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . findAllIdApp = async function ( idapp ) {
2020-01-13 23:52:51 +01:00
const User = this ;
2020-05-04 19:34:41 +02:00
const myfind = {
idapp ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2020-05-04 19:34:41 +02:00
} ;
2020-01-13 23:52:51 +01:00
2022-09-11 11:45:33 +02:00
return await User . find ( myfind , ( err , arrrec ) => {
2021-10-08 00:38:35 +02:00
return arrrec ;
2020-01-13 23:52:51 +01:00
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . DuplicateAllRecords = async function ( idapporig , idappdest ) {
2020-01-13 23:52:51 +01:00
2022-09-11 11:45:33 +02:00
return await tools . DuplicateAllRecords ( this , idapporig , idappdest ) ;
2020-01-13 23:52:51 +01:00
} ;
2022-12-10 02:01:17 +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
2021-10-08 00:38:35 +02:00
dashboard . aportador = await User . getUserShortDataByUsername ( idapp ,
2022-12-10 02:01:17 +01:00
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
2021-10-08 00:38:35 +02:00
const arrap = await User . getDownlineByUsername ( idapp , username , false ,
2022-12-10 02:01:17 +01:00
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
// 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 ;
}
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getDownline = async function (
idapp , aportador _solidario , username ) {
2020-05-14 17:23:17 +02:00
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 ) {
2021-10-08 00:38:35 +02:00
downline . downbyuser [ down . username ] = await User . getDownlineByUsername (
2022-12-10 02:01:17 +01:00
idapp , down . username , false ) ;
2020-05-14 17:23:17 +02:00
for ( const down2 of downline . downbyuser [ down . username ] ) {
2021-10-08 00:38:35 +02:00
downline . downbyuser [ down2 . username ] = await User . getDownlineByUsername (
2022-12-10 02:01:17 +01:00
idapp , down2 . username , false ) ;
2020-05-14 17:23:17 +02:00
}
}
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
2022-12-10 02:01:17 +01:00
UserSchema . statics . findByCellAndNameSurname = function (
idapp , cell , name , surname ) {
2020-01-30 01:19:25 +01:00
const User = this ;
return User . findOne ( {
'idapp' : idapp ,
'profile.cell' : cell ,
'name' : name ,
'surname' : surname ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2020-01-30 01:19:25 +01:00
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsersRegistered = async function ( idapp ) {
const User = this ;
const myfind = {
idapp ,
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
} ;
2023-01-03 16:51:32 +01:00
return await User . countDocuments ( myfind ) ;
2022-12-10 02:01:17 +01:00
} ;
UserSchema . statics . getUsersRegisteredToday = async function ( idapp ) {
2020-01-20 01:48:25 +01:00
const User = this ;
2022-12-10 02:01:17 +01:00
let starttoday = new Date ( ) ;
starttoday . setHours ( 0 , 0 , 0 , 0 ) ;
2020-04-24 10:29:25 +02:00
const myfind = {
idapp ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
date _reg : { $gt : starttoday } ,
2020-04-24 10:29:25 +02:00
} ;
2020-01-20 01:48:25 +01:00
2023-01-03 16:51:32 +01:00
return await User . countDocuments ( myfind ) ;
2020-01-20 01:48:25 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsersOnLineToday = async function ( idapp ) {
2022-03-10 23:19:56 +01:00
const User = this ;
let starttoday = new Date ( ) ;
2022-12-10 02:01:17 +01:00
starttoday . setHours ( 0 , 0 , 0 , 0 ) ;
2022-03-10 23:19:56 +01:00
const myfind = {
idapp ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
lasttimeonline : { $gt : starttoday } ,
2022-03-10 23:19:56 +01:00
} ;
2023-01-03 16:51:32 +01:00
return await User . countDocuments ( myfind ) ;
2022-03-10 23:19:56 +01:00
} ;
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-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
2022-12-10 02:01:17 +01:00
UserSchema . statics . getEmailNotVerified = async function ( idapp ) {
2020-01-27 15:07:53 +01:00
const User = this ;
2020-04-24 10:29:25 +02:00
const myfind = {
idapp ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2021-10-08 00:38:35 +02:00
verified _email : false ,
2020-04-24 10:29:25 +02:00
} ;
2020-01-27 15:07:53 +01:00
2023-01-03 16:51:32 +01:00
return await User . countDocuments ( myfind ) ;
2020-01-27 15:07:53 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsersTelegramAttivo = async function ( idapp ) {
2020-01-27 15:07:53 +01:00
const User = this ;
2020-04-24 10:29:25 +02:00
const myfind = {
idapp ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
'profile.teleg_id' : { $gt : 0 } ,
2020-04-24 10:29:25 +02:00
} ;
2020-01-27 15:07:53 +01:00
2023-01-03 16:51:32 +01:00
return await User . countDocuments ( myfind ) ;
2020-01-27 15:07:53 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsersAutorizzati = async function ( idapp ) {
2022-03-08 01:01:20 +01:00
const User = this ;
const myfind = {
idapp ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
'profile.teleg_id' : { $gt : 0 } ,
2022-03-08 01:01:20 +01:00
verified _by _aportador : true ,
} ;
2023-01-03 16:51:32 +01:00
return await User . countDocuments ( myfind ) ;
2022-03-08 01:01:20 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsersAutorizzare = async function ( idapp ) {
2022-03-08 01:01:20 +01:00
const User = this ;
const myfind = {
idapp ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
'profile.teleg_id' : { $gt : 0 } ,
verified _by _aportador : { $exists : false } ,
2022-03-08 01:01:20 +01:00
} ;
2023-01-03 16:51:32 +01:00
return await User . countDocuments ( myfind ) ;
2022-03-08 01:01:20 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsersTelegramPending = async function ( idapp ) {
2020-01-27 15:07:53 +01:00
const User = this ;
2020-04-24 10:29:25 +02:00
const myfind = {
idapp ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
'profile.teleg_checkcode' : { $gt : 0 } ,
2020-04-24 10:29:25 +02:00
} ;
2020-01-27 15:07:53 +01:00
2023-01-03 16:51:32 +01:00
return await User . countDocuments ( myfind ) ;
2022-02-09 19:48:39 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getNumUsers = async function ( idapp ) {
2022-02-09 19:48:39 +01:00
const User = this ;
const myfind = {
idapp ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2022-02-09 19:48:39 +01:00
} ;
2023-01-03 16:51:32 +01:00
return await User . countDocuments ( myfind ) ;
2020-01-27 15:07:53 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsersZoom = async function ( idapp ) {
2020-01-27 15:07:53 +01:00
const User = this ;
2020-04-24 10:29:25 +02:00
const myfind = {
idapp ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2021-10-08 00:38:35 +02:00
'profile.saw_zoom_presentation' : true ,
2020-04-24 10:29:25 +02:00
} ;
2020-01-27 15:07:53 +01:00
2023-01-03 16:51:32 +01:00
return await User . countDocuments ( myfind ) ;
2020-01-27 15:07:53 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsersResidenti = async function ( idapp ) {
2021-02-24 04:48:31 +01:00
const User = this ;
const myfind = {
idapp ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
'profile.socioresidente' : { $exists : true , $eq : true } ,
2021-02-24 04:48:31 +01:00
} ;
2022-12-10 02:01:17 +01:00
return await User . find ( myfind , { username : 1 , name : 1 , surname : 1 } ) ;
2021-02-24 04:48:31 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getSaw _and _Accepted = async function ( idapp ) {
2020-02-07 22:08:46 +01:00
const User = this ;
2020-04-24 10:29:25 +02:00
const myfind = {
idapp ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
2021-10-08 00:38:35 +02:00
'profile.saw_and_accepted' : shared _consts . ALL _SAW _AND _ACCEPTED ,
2020-04-24 10:29:25 +02:00
} ;
2020-02-07 22:08:46 +01:00
2023-01-03 16:51:32 +01:00
return await User . countDocuments ( myfind ) ;
2020-02-07 22:08:46 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsersDreams = async function ( idapp ) {
2020-01-27 15:07:53 +01:00
const User = this ;
const myfind = {
idapp ,
2022-12-10 02:01:17 +01:00
$or : [ { deleted : { $exists : false } } , { deleted : { $exists : true , $eq : false } } ] ,
'profile.my_dream' : { $exists : true } ,
'$expr' : { '$gt' : [ { '$strLenCP' : '$profile.my_dream' } , 10 ] } ,
2020-01-27 15:07:53 +01:00
} ;
2023-01-03 16:51:32 +01:00
return await User . countDocuments ( myfind ) ;
2020-01-27 15:07:53 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getLastUsers = async function ( idapp ) {
2020-01-20 01:48:25 +01:00
const User = this ;
2023-11-30 01:49:17 +01:00
const lastn = await Settings . getValDbSettings ( idapp , 'SHOW_LAST_N_USERS' , 20 ) ;
2020-01-20 01:48:25 +01:00
2022-09-11 11:45:33 +02:00
return await User . find (
2022-12-10 02:01:17 +01:00
{
idapp ,
$or : [
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
} ,
{
username : 1 ,
name : 1 ,
surname : 1 ,
lasttimeonline : 1 ,
2023-11-30 01:49:17 +01:00
verified _by _aportador : 1 ,
2022-12-10 02:01:17 +01:00
'profile.img' : 1 ,
date _reg : 1 ,
index : 1 ,
'profile.nationality' : 1 ,
} ) . sort ( { date _reg : - 1 } ) . limit ( lastn ) . then ( ( arr ) => {
//return JSON.stringify(arr)
return arr ;
} ) ;
2020-01-20 01:48:25 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getLastOnlineUsers = async function ( idapp ) {
2020-01-20 01:48:25 +01:00
const User = this ;
2023-11-03 12:49:10 +01:00
const lastn = await Settings . getValDbSettings ( idapp , 'SHOW_LAST_ONLINE_USERS' , 20 ) ;
2022-12-10 02:01:17 +01:00
return await User . find (
{
idapp ,
$or : [
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
} ,
{
username : 1 ,
name : 1 ,
surname : 1 ,
lasttimeonline : 1 ,
2023-03-11 01:01:11 +01:00
date _reg : 1 ,
2024-02-06 20:12:54 +01:00
verified _by _aportador : 1 ,
2022-12-10 02:01:17 +01:00
'profile.img' : 1 ,
index : 1 ,
} ) . sort ( { lasttimeonline : - 1 } ) . limit ( lastn ) . then ( ( arr ) => {
//return JSON.stringify(arr)
return arr ;
} ) ;
} ;
2022-12-12 18:25:13 +01:00
UserSchema . statics . getLastSharedLink = async function ( idapp ) {
const User = this ;
const lastn = 10 ;
return await User . find (
{
idapp ,
$or : [
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
} ,
{
username : 1 ,
name : 1 ,
surname : 1 ,
lasttimeonline : 1 ,
2024-02-06 20:12:54 +01:00
verified _by _aportador : 1 ,
2023-03-11 01:01:11 +01:00
date _reg : 1 ,
2022-12-12 18:25:13 +01:00
'profile.img' : 1 ,
index : 1 ,
} ) . sort ( { date _tokenreg : - 1 } ) . limit ( lastn ) . then ( ( arr ) => {
return arr ;
} ) ;
} ;
2022-12-11 02:57:35 +01:00
UserSchema . statics . getDiffusoriUsers = async function ( idapp ) {
const User = this ;
const lastn = 10 ;
return await User . aggregate ( User . getQueryUsersDiffusori ( idapp ) ) . then ( ris => {
// console.table(ris);
return ris ;
} ) ;
} ;
2023-01-08 19:20:02 +01:00
UserSchema . statics . getBestStretteDiManoUsers = async function ( idapp ) {
const User = this ;
const lastn = 10 ;
return await User . aggregate ( User . getQueryUsersStretteDiMano ( idapp ) ) . then ( ris => {
// console.table(ris);
return ris ;
} ) ;
} ;
2023-03-11 01:01:11 +01:00
UserSchema . statics . getReceiveRISUsers = async function ( idapp ) {
const User = this ;
return await User . aggregate ( User . getQueryReceiveRISUsers ( idapp , 8 ) ) . then ( ris => {
// console.table(ris);
return ris ;
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . checkUser = async function ( idapp , username ) {
const User = this ;
return await User . findOne ( { idapp , username } , {
2020-01-20 01:48:25 +01:00
verified _email : 1 ,
2021-12-29 18:26:08 +01:00
verified _by _aportador : 1 ,
2022-02-26 17:35:50 +01:00
notask _verif : 1 ,
2020-01-20 01:48:25 +01:00
'profile.teleg_id' : 1 ,
'profile.teleg_checkcode' : 1 ,
2022-03-03 20:32:04 +01:00
} ) . lean ( ) . then ( ( rec ) => {
return rec ;
2020-01-20 01:48:25 +01:00
} ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . calculateStat = async function ( idapp , username ) {
2020-01-20 01:48:25 +01:00
const User = this ;
2022-02-21 13:12:27 +01:00
try {
2022-12-10 02:01:17 +01:00
const { MySkill } = require ( '../models/myskill' ) ;
const { MyGood } = require ( '../models/mygood' ) ;
const { MyBacheca } = require ( '../models/mybacheca' ) ;
const { MyGroup } = require ( '../models/mygroup' ) ;
2023-04-12 15:37:54 +02:00
const globalTables = require ( '../tools/globalTables' ) ;
2020-01-20 01:48:25 +01:00
2022-02-21 13:12:27 +01:00
const numUsersReg = await User . countDocuments (
2022-12-10 02:01:17 +01:00
{
idapp ,
$or : [
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
} ) ;
2022-02-11 01:15:40 +01:00
2022-03-08 01:01:20 +01:00
let numByTab = { } ;
2022-03-03 20:32:04 +01:00
for ( let table of shared _consts . TABLES _VISU _STAT _IN _HOME ) {
let mytable = globalTables . getTableByTableName ( table ) ;
if ( mytable ) {
2022-12-10 02:01:17 +01:00
numByTab [ table ] = await mytable . countDocuments ( { idapp } ) ;
2022-03-03 20:32:04 +01:00
}
}
2022-12-10 02:01:17 +01:00
return { numByTab , numUsersReg } ;
2022-02-21 13:12:27 +01:00
} catch ( e ) {
console . error ( e . message ) ;
}
2022-02-03 00:33:15 +01:00
2020-01-20 01:48:25 +01:00
} ;
2022-12-10 02:01:17 +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 ,
2021-10-08 00:38:35 +02:00
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
2021-10-08 00:38:35 +02:00
} ,
2020-01-27 15:07:53 +01:00
} ,
{
2022-12-10 02:01:17 +01:00
$group : { _id : '$profile.nationality' , count : { $sum : 1 } } ,
2020-01-27 15:07:53 +01:00
} ,
{
2022-12-10 02:01:17 +01:00
$sort : { count : - 1 } ,
2021-10-08 00:38:35 +02:00
} ,
2020-01-27 15:07:53 +01:00
] ;
2021-10-08 00:38:35 +02:00
return query ;
2020-01-27 15:07:53 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . findAllDistinctNationality = async function ( idapp ) {
2020-01-27 15:07:53 +01:00
const User = this ;
2022-09-11 11:45:33 +02:00
return await User . aggregate ( User . getDistinctNationalityQuery ( idapp ) ) . then ( ris => {
2021-10-08 00:38:35 +02:00
// console.table(ris);
2022-03-03 20:32:04 +01:00
return ris ;
2021-10-08 00:38:35 +02:00
} ) ;
2020-01-27 15:07:53 +01:00
} ;
2022-12-11 02:57:35 +01:00
2022-12-10 02:01:17 +01:00
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 : {
2021-10-08 00:38:35 +02:00
idapp ,
2022-12-10 02:01:17 +01:00
date _reg : { $gte : tools . IncDateNow ( - ( 1000 * 60 * 60 * 24 * nrec ) ) } ,
2021-10-08 00:38:35 +02:00
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
2021-10-08 00:38:35 +02:00
} ,
2020-01-27 15:07:53 +01:00
} ,
{
2021-04-30 01:31:12 +02:00
$group : {
2021-10-08 00:38:35 +02:00
_id : {
$dateToString : {
format : '%Y-%m-%d' ,
date : '$date_reg' ,
timezone : 'Europe/Rome' ,
} ,
} ,
2022-12-10 02:01:17 +01:00
count : { $sum : 1 } ,
2021-10-08 00:38:35 +02:00
} ,
2020-01-27 15:07:53 +01:00
} ,
{
2022-12-10 02:01:17 +01:00
$sort : { _id : 1 } ,
2021-10-08 00:38:35 +02:00
} ,
2020-03-10 21:44:14 +01:00
] ;
2021-10-08 00:38:35 +02:00
return query ;
2020-03-10 21:44:14 +01:00
} ;
2024-02-06 20:12:54 +01:00
UserSchema . statics . getUsersRegDailyAverage = 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 } }
] ,
} ,
} ,
{
$group : {
_id : {
$dateToString : {
format : '%Y-%m-%d' ,
date : '$date_reg' ,
timezone : 'Europe/Rome' ,
} ,
} ,
count : { $sum : 1 } ,
} ,
} ,
{
$group : {
_id : null ,
total : { $sum : '$count' } ,
days : { $sum : 1 } ,
} ,
} ,
{
$project : {
_id : 0 ,
dailyAverage : { $divide : [ '$total' , '$days' ] } ,
} ,
} ,
] ;
return query ;
} ;
2022-12-11 02:57:35 +01:00
UserSchema . statics . getQueryUsersDiffusori = function ( idapp ) {
const query = [
{
$match : {
idapp ,
$or : [
{
deleted : {
$exists : false ,
} ,
} ,
{
deleted : {
$exists : true ,
$eq : false ,
} ,
} ,
] ,
} ,
} ,
{
$group : {
_id : "$aportador_solidario" ,
count : {
$sum : 1 ,
} ,
} ,
} ,
{
2022-12-11 18:04:02 +01:00
$match : { "count" : { $gte : 2 } }
2022-12-11 02:57:35 +01:00
} ,
{
$sort : {
count : - 1 ,
} ,
} ,
2022-12-11 18:04:02 +01:00
{ $limit : 20 } ,
2022-12-11 02:57:35 +01:00
{
$lookup : {
from : "users" ,
let : {
username : "$_id" ,
idapp ,
} ,
pipeline : [
{
$match : {
$expr : {
$and : [
{
$eq : [
"$$username" ,
"$username" ,
] ,
} ,
{
$eq : [
"$$idapp" ,
"$idapp" ,
] ,
} ,
] ,
} ,
} ,
} ,
] ,
as : "user" ,
} ,
} ,
{ $unwind : "$user" } ,
{
$replaceRoot : {
newRoot : {
2022-12-11 18:04:02 +01:00
$mergeObjects : [ "$user" , "$$ROOT" ] ,
2022-12-11 02:57:35 +01:00
} ,
} ,
} ,
2022-12-11 18:04:02 +01:00
{
2022-12-11 02:57:35 +01:00
$project : {
_id : 0 ,
count : 1 ,
aportador _solidario : 1 ,
username : 1 ,
name : 1 ,
surname : 1 ,
lasttimeonline : 1 ,
2023-03-11 01:01:11 +01:00
date _reg : 1 ,
2022-12-11 02:57:35 +01:00
idapp : 1 ,
"profile.img" : 1 ,
2023-03-11 01:01:11 +01:00
'profile.mycircuits' : 1 ,
2024-02-06 20:12:54 +01:00
'profile.handshake' : 1 ,
2022-12-11 02:57:35 +01:00
} ,
} ,
] ;
return query ;
} ;
2023-01-08 19:20:02 +01:00
UserSchema . statics . getQueryUsersStretteDiMano = function ( idapp ) {
const query = [
{
$match : {
2023-01-09 02:37:35 +01:00
idapp : "13" ,
'profile.handshake' : { $exists : true } ,
2023-01-08 19:20:02 +01:00
$or : [
{
deleted : {
$exists : false ,
} ,
} ,
{
deleted : {
$exists : true ,
$eq : false ,
} ,
} ,
] ,
} ,
} ,
2023-01-09 02:37:35 +01:00
2023-01-08 19:20:02 +01:00
{
2023-01-09 02:37:35 +01:00
$group :
{
_id : "$username" ,
count :
{ $sum : { $size : "$profile.handshake" } }
}
2023-01-08 19:20:02 +01:00
} ,
2023-01-09 02:37:35 +01:00
{ $sort : { count : - 1 } } ,
2023-01-08 19:20:02 +01:00
{ $limit : 20 } ,
{
$lookup : {
from : "users" ,
let : {
username : "$_id" ,
idapp ,
} ,
pipeline : [
{
$match : {
$expr : {
$and : [
{
$eq : [
"$$username" ,
"$username" ,
] ,
} ,
{
$eq : [
"$$idapp" ,
"$idapp" ,
] ,
} ,
] ,
} ,
} ,
} ,
] ,
as : "user" ,
} ,
} ,
{ $unwind : "$user" } ,
{
$replaceRoot : {
newRoot : {
$mergeObjects : [ "$user" , "$$ROOT" ] ,
} ,
} ,
} ,
{
$project : {
_id : 0 ,
count : 1 ,
aportador _solidario : 1 ,
username : 1 ,
name : 1 ,
surname : 1 ,
lasttimeonline : 1 ,
2023-03-11 01:01:11 +01:00
date _reg : 1 ,
idapp : 1 ,
"profile.img" : 1 ,
'profile.handshake' : 1 ,
'profile.mycircuits' : 1 ,
} ,
} ,
] ;
return query ;
} ;
UserSchema . statics . getQueryReceiveRISUsers = function ( idapp , hours ) {
const query = [
{
$match : {
idapp ,
'profile.lastdate_reqRis' : { $gte : tools . IncDateNow ( - ( 1000 * 60 * 60 * hours ) ) } ,
$or : [
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
} ,
} ,
{
$group :
{
_id : "$username" ,
count : {
$sum : 1 ,
} ,
}
} ,
{ $sort : { 'profile.lastdate_reqRis' : - 1 } } ,
{ $limit : 30 } ,
{
$lookup : {
from : "users" ,
let : {
username : "$_id" ,
idapp ,
} ,
pipeline : [
{
$match : {
$expr : {
$and : [
{
$eq : [
"$$username" ,
"$username" ,
] ,
} ,
{
$eq : [
"$$idapp" ,
"$idapp" ,
] ,
} ,
] ,
} ,
} ,
} ,
] ,
as : "user" ,
} ,
} ,
{ $unwind : "$user" } ,
{
$replaceRoot : {
newRoot : {
$mergeObjects : [ "$user" , "$$ROOT" ] ,
} ,
} ,
} ,
{
$project : {
_id : 0 ,
count : 1 ,
aportador _solidario : 1 ,
username : 1 ,
name : 1 ,
surname : 1 ,
lasttimeonline : 1 ,
'profile.lastdate_reqRis' : 1 ,
'profile.mycircuits' : 1 ,
date _reg : 1 ,
2023-01-08 19:20:02 +01:00
idapp : 1 ,
"profile.img" : 1 ,
'profile.handshake' : 1 ,
} ,
} ,
] ;
return query ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsersRegWeekly = function ( idapp , nrec ) {
2020-05-04 19:34:41 +02:00
const query = [
{
$match : {
2021-10-08 00:38:35 +02:00
idapp ,
2022-12-10 02:01:17 +01:00
date _reg : { $gte : tools . IncDateNow ( - ( 1000 * 60 * 60 * 24 * nrec ) ) } ,
2021-10-08 00:38:35 +02:00
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
2021-10-08 00:38:35 +02:00
} ,
2020-05-04 19:34:41 +02:00
} ,
{
2021-04-30 01:31:12 +02:00
$group : {
2021-10-08 00:38:35 +02:00
_id : {
$dateToString : {
format : '%Y-%U' ,
date : '$date_reg' ,
timezone : 'Europe/Rome' ,
} ,
} ,
2022-12-10 02:01:17 +01:00
count : { $sum : 1 } ,
2021-10-08 00:38:35 +02:00
} ,
2020-05-04 19:34:41 +02:00
} ,
2024-02-06 20:12:54 +01:00
{
$group : {
_id : null , // Raggruppa tutti i risultati
total : { $sum : '$count' } , // Calcola il numero totale di iscritti
days : { $sum : 1 } , // Calcola il numero totale di giorni
} ,
} ,
{
$project : {
_id : 0 , // Escludi il campo _id dal risultato finale
dailyAverage : { $divide : [ '$total' , '$days' ] } , // Calcola la media giornaliera
} ,
} ,
2020-05-04 19:34:41 +02:00
{
2022-12-10 02:01:17 +01:00
$sort : { _id : 1 } ,
2021-10-08 00:38:35 +02:00
} ,
2020-05-04 19:34:41 +02:00
] ;
2021-10-08 00:38:35 +02:00
return query ;
2020-05-04 19:34:41 +02:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getnumRegNDays = function ( idapp , nrec ) {
2020-03-10 21:44:14 +01:00
const query = [
{
2020-05-04 19:34:41 +02:00
$match : {
idapp ,
2022-12-10 02:01:17 +01:00
date _reg : { $lt : tools . IncDateNow ( - ( 1000 * 60 * 60 * 24 * nrec ) ) } ,
2021-10-08 00:38:35 +02:00
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
2021-10-08 00:38:35 +02:00
} ,
2020-03-10 21:44:14 +01:00
} ,
{
2021-04-30 01:31:12 +02:00
$group : {
2021-10-08 00:38:35 +02:00
_id : {
$dateToString : {
format : '%Y-%m-%d' ,
date : '$date_reg' ,
timezone : 'Europe/Rome' ,
} ,
} ,
2022-12-10 02:01:17 +01:00
count : { $sum : 1 } ,
2021-10-08 00:38:35 +02:00
} ,
2020-01-27 15:07:53 +01:00
} ,
{
2022-12-10 02:01:17 +01:00
$sort : { _id : 1 } ,
2021-10-08 00:38:35 +02:00
} ,
2020-01-27 15:07:53 +01:00
] ;
2021-10-08 00:38:35 +02:00
return query ;
2020-01-27 15:07:53 +01:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . calcnumRegUntilDay = async function ( idapp ) {
2020-03-10 21:44:14 +01:00
const User = this ;
2023-03-11 01:01:11 +01:00
return await User . aggregate ( User . getnumRegNDays ( idapp , 60 ) ) . then ( ( arr ) => {
2021-10-08 00:38:35 +02:00
return arr . reduce ( ( sum , rec ) => sum + rec . count , 0 ) ;
} ) ;
2020-03-10 21:44:14 +01:00
} ;
2020-01-20 01:48:25 +01:00
2024-02-06 20:12:54 +01:00
function calculate30DayAverage ( data ) {
const averages = [ ] ;
for ( let i = 0 ; i < data . length ; i ++ ) {
2024-03-21 22:58:46 +01:00
const startDate = new Date ( data [ i ] . _id ) ;
let sum = data [ i ] . count ;
let count = 1 ;
for ( let j = i + 1 ; j < data . length ; j ++ ) {
const currentDate = new Date ( data [ j ] . _id ) ;
const diffInTime = Math . abs ( startDate . getTime ( ) - currentDate . getTime ( ) ) ;
const diffInDays = Math . ceil ( diffInTime / ( 1000 * 60 * 60 * 24 ) ) ;
if ( diffInDays <= 30 ) {
sum += data [ j ] . count ;
count ++ ;
} else {
break ;
2024-02-06 20:12:54 +01:00
}
2024-03-21 22:58:46 +01:00
}
2024-02-06 20:12:54 +01:00
2024-03-21 22:58:46 +01:00
averages . push ( {
_id : data [ i ] . _id ,
dailyAverage : sum / count
} ) ;
2024-02-06 20:12:54 +01:00
}
return averages ;
}
2022-12-10 02:01:17 +01:00
UserSchema . statics . calcRegDaily = async function ( idapp ) {
2020-01-27 15:07:53 +01:00
const User = this ;
2023-12-02 15:23:35 +01:00
return await User . aggregate ( User . getUsersRegDaily ( idapp , 120 ) ) . then ( ris => {
2021-10-08 00:38:35 +02:00
// console.table(ris);
2022-03-03 20:32:04 +01:00
return ris ;
2021-10-08 00:38:35 +02:00
} ) ;
2020-01-27 15:07:53 +01:00
} ;
2020-01-20 01:48:25 +01:00
2024-02-06 20:12:54 +01:00
2022-12-10 02:01:17 +01:00
UserSchema . statics . calcRegWeekly = async function ( idapp ) {
2020-05-04 19:34:41 +02:00
const User = this ;
2024-02-06 20:12:54 +01:00
return await User . aggregate ( User . getUsersRegDaily ( idapp , 120 ) ) . then ( ris => {
2021-10-08 00:38:35 +02:00
// console.table(ris);
2024-02-06 20:12:54 +01:00
return calculate30DayAverage ( ris ) ;
2021-10-08 00:38:35 +02:00
} ) ;
2020-05-04 19:34:41 +02:00
} ;
2019-10-28 16:01:28 +01:00
if ( tools . INITDB _FIRSTIME ) {
console . log ( ' createIndex User Index...' ) ;
2023-09-27 18:38:57 +02:00
UserSchema . index ( { userId : 1 } ) ;
2023-12-09 11:55:58 +01:00
UserSchema . index ( { idapp : 1 } ) ;
2023-09-27 18:38:57 +02:00
2019-10-28 16:01:28 +01:00
// UserSchema.index({ username: 'text', name: 'text', surname: 'text', email: 'text' });
// UserSchema.index({ name: 'name' });
// UserSchema.index({ name: 1 });
// UserSchema.index({ surname: 1 });
2022-12-20 12:36:41 +01:00
// ter
2023-09-27 18:38:57 +02:00
tools . INITDB _FIRSTIME = false ;
2019-10-28 16:01:28 +01:00
}
2018-12-24 20:31:02 +01:00
2022-12-10 02:01:17 +01:00
UserSchema . statics . getUsernameByIndex = async function ( idapp , index ) {
2020-05-10 21:07:51 +02:00
const myrec = await User . findOne ( {
idapp ,
index ,
2022-12-10 02:01:17 +01:00
} , { username : 1 } ) ;
2020-05-10 21:07:51 +02:00
2021-10-08 00:38:35 +02:00
return ( ! ! myrec ) ? myrec . username : '' ;
2020-05-10 21:07:51 +02:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . ricalcolaIndex = async function ( idapp ) {
2020-05-10 21:07:51 +02:00
const User = this ;
2020-05-14 17:23:17 +02:00
const arrusers = await User . find ( {
idapp ,
2022-12-10 02:01:17 +01:00
$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 = {
2021-10-08 00:38:35 +02:00
index ,
2020-05-10 21:07:51 +02:00
} ;
index ++ ;
2022-12-10 02:01:17 +01:00
const ris = await User . findOneAndUpdate ( { _id : user . _id } , { $set : field } ,
{ new : false } ) ;
2020-05-10 21:07:51 +02:00
}
2020-05-11 22:43:14 +02:00
} catch ( e ) {
2020-05-10 21:07:51 +02:00
console . error ( e . message ) ;
}
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getInfoUser = async function ( idapp , username ) {
2020-09-04 00:06:49 +02:00
return {
username ,
is7req : await User . isUserQualified7 ( idapp , username ) ,
2022-02-03 00:33:15 +01:00
// is9req: await User.isUserQualified9(idapp, username),
2021-10-08 00:38:35 +02:00
} ;
2020-09-04 00:06:49 +02:00
2021-10-08 00:38:35 +02:00
} ;
2020-09-04 00:06:49 +02:00
2022-12-10 02:01:17 +01:00
UserSchema . statics . checkIfSbloccatiRequisiti = async function (
idapp , allData , id ) {
2020-09-04 00:06:49 +02:00
const User = this ;
const telegrambot = require ( '../telegram/telegrambot' ) ;
if ( ! allData . myuser )
return false ;
2022-02-03 00:33:15 +01:00
//if (await Nave.checkIfNaveExist(idapp, allData.myuser.username)) {
// // Se già sei dentro la Nave, allora sei OK
// return true; //TOGLEREE
//}
2020-09-04 00:06:49 +02:00
// Controlla se Sblocca i 7 requisiti
const is7req = await User . isUserQualified7 ( idapp , allData . myuser . username ) ;
2022-02-03 00:33:15 +01:00
const is9req = false ;
// const is9req = await User.isUserQualified9(idapp, allData.myuser.username);
2020-09-04 00:06:49 +02:00
if ( ! ! allData . precDataUser ) {
2021-10-08 00:38:35 +02:00
if ( ( ! allData . precDataUser . is9req && is9req ) &&
2022-12-10 02:01:17 +01:00
! await User . isUserAlreadyQualified _2Invitati ( idapp ,
allData . myuser . username ) ) {
2020-09-04 00:06:49 +02:00
await User . setUserQualified _2Invitati ( idapp , allData . myuser . username ) ;
2021-12-23 14:13:40 +01:00
2020-09-04 00:06:49 +02:00
// ORA HAI I 9 REQUISITI !
2021-12-23 14:13:40 +01:00
// 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
2020-09-04 00:06:49 +02:00
}
}
// 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
}
* /
}
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . mettiSognoePaypal = async function ( idapp , modifica ) {
2020-09-04 00:06:49 +02:00
const User = this ;
let num = 0 ;
arrusers = await User . find ( {
'idapp' : idapp ,
2021-10-08 00:38:35 +02:00
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ,
{ subaccount : { $exists : false } } ,
{ subaccount : { $exists : true , $eq : false } } ] ,
2020-09-04 00:06:49 +02:00
} ) ;
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 )
2021-10-08 00:38:35 +02:00
allData . precDataUser = await User . getInfoUser ( idapp ,
2022-12-10 02:01:17 +01:00
allData . myuser . username ) ;
2020-09-04 00:06:49 +02:00
else
allData . precDataUser = null ;
const risupd = await User . findByIdAndUpdate ( rec . _id , {
'profile.email_paypal' : rec . profile . email _paypal ,
2021-10-08 00:38:35 +02:00
'profile.my_dream' : rec . profile . my _dream ,
2022-12-10 02:01:17 +01:00
} , { new : false } ) ;
2020-09-04 00:06:49 +02:00
if ( risupd ) {
await User . checkIfSbloccatiRequisiti ( idapp , allData , rec . id ) ;
}
}
}
}
2022-12-10 02:01:17 +01:00
return { num } ;
2020-09-04 00:06:49 +02:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . convSubAccount = async function ( idapp ) {
2020-06-08 13:31:05 +02:00
const User = this ;
// Solo i Cancellati !
2022-12-10 02:01:17 +01:00
arrusers = await User . find ( { 'idapp' : idapp , deleted : true } ) ;
2020-06-08 13:31:05 +02:00
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 ,
2021-10-08 00:38:35 +02:00
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
2020-06-08 13:31:05 +02:00
} ) ;
if ( trovato ) {
num ++ ;
2022-12-10 02:01:17 +01:00
await User . findByIdAndUpdate ( rec . _id , { subaccount : true } , { new : false } ) ;
2020-06-08 13:31:05 +02:00
}
}
2022-12-10 02:01:17 +01:00
return { num } ;
2020-06-08 13:31:05 +02:00
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getLastRec = async function ( idapp ) {
2020-07-13 23:35:05 +02:00
const User = this ;
2022-12-10 02:01:17 +01:00
lastrec = await User . find ( { idapp } ) . sort ( { date _reg : - 1 } ) . limit ( 1 ) . lean ( ) ;
2020-07-13 23:35:05 +02:00
if ( ! ! lastrec ) {
return lastrec [ 0 ] ;
} else {
return null ;
}
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . DbOp = async function ( idapp , mydata ) {
2020-01-30 01:19:25 +01:00
const User = this ;
try {
if ( mydata . dbop === 'changeCellInt' ) {
2022-12-10 02:01:17 +01:00
arrusers = await User . find ( { 'idapp' : idapp } ) ;
2020-01-30 01:19:25 +01:00
let num = 0 ;
for ( const rec of arrusers ) {
2020-02-19 16:09:16 +01:00
// DISATTIVATO: ORA NON MI SERVE PIU
if ( false ) {
2021-10-08 00:38:35 +02:00
let mycell = tools . removespaces (
2022-12-10 02:01:17 +01:00
rec . profile . intcode _cell + rec . profile . cell ) ;
await User . findOneAndUpdate ( { _id : rec . _id } ,
{ $set : { 'profile.cell' : mycell } } ) ;
2020-02-19 16:09:16 +01:00
num ++ ;
}
2020-01-30 01:19:25 +01:00
}
2022-12-10 02:01:17 +01:00
return { num } ;
2020-01-30 01:19:25 +01:00
2021-12-23 14:13:40 +01:00
// return 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' ) {
2022-12-10 02:01:17 +01:00
arrusers = await User . find ( { 'idapp' : idapp } ) ;
2020-02-19 16:09:16 +01:00
let num = 0 ;
for ( const rec of arrusers ) {
let myemail = rec . email . toLowerCase ( ) ;
if ( myemail !== rec . email ) {
2022-12-10 02:01:17 +01:00
await User . findOneAndUpdate ( { _id : rec . _id } ,
{ $set : { 'email' : myemail } } ) ;
2020-02-19 16:09:16 +01:00
num ++ ;
}
}
2022-12-10 02:01:17 +01:00
return { num } ;
2020-02-19 16:09:16 +01:00
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
}
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . getExtraInfoByUsername = async function ( idapp , username ) {
2022-09-14 11:32:04 +02:00
const User = this ;
2023-01-08 02:17:01 +01:00
let myuser = await User . findOne ( { idapp , username } ) . lean ( ) ;
2022-09-14 11:32:04 +02:00
if ( myuser ) {
2023-01-09 02:37:35 +01:00
myuserextra = await User . addExtraInfo ( idapp , myuser , null ) ;
2024-01-17 21:49:59 +01:00
return myuser . profile ;
2022-09-14 11:32:04 +02:00
}
2024-01-17 21:49:59 +01:00
return null ;
2022-09-14 11:32:04 +02:00
} ;
2024-03-21 22:58:46 +01:00
UserSchema . statics . getProfilePerActivitiesByUsername = async function ( idapp , username ) {
const User = this ;
try {
let myuser = await User . findOne ( { idapp , username } ) . lean ( ) ;
if ( myuser ) {
2024-04-09 21:56:50 +02:00
return {
mygroups : myuser . profile . mygroups ,
mycircuits : myuser . profile . mycircuits
} ;
2024-03-21 22:58:46 +01:00
}
} catch ( e ) {
console . error ( 'e' , e ) ;
}
return null ;
} ;
2023-04-12 15:37:54 +02:00
UserSchema . statics . addExtraInfo = async function ( idapp , recUser , recUserSave , version ) {
2022-01-20 00:39:06 +01:00
try {
2023-11-19 23:40:38 +01:00
// tools.startTimeLog('addExtraInfo')
2022-09-14 17:37:29 +02:00
2023-04-13 13:46:48 +02:00
if ( version ) {
let versattualeuser = 0 ;
2023-04-17 00:11:36 +02:00
if ( ! recUser . profile . version ) {
2023-04-13 13:46:48 +02:00
recUser . version = 0 ;
versattualeuser = 0 ;
} else {
versattualeuser = recUser . profile . version ;
}
2023-09-27 18:38:57 +02:00
// versattualeuser = 0; // TOGLIERE!
2023-04-17 00:11:36 +02:00
2023-04-13 13:46:48 +02:00
if ( versattualeuser < version ) {
// Aggiornamento versione
recUser = await User . updateVersion ( versattualeuser , recUser ) ;
2023-04-17 00:11:36 +02:00
await User . findOneAndUpdate ( { _id : recUser . _id } , { $set : { 'profile.version' : version } } ) ;
2022-09-16 17:38:49 +02:00
}
2022-09-14 17:37:29 +02:00
}
2022-01-20 00:39:06 +01:00
const listSentMyRequestFriends = await User . find ( {
idapp ,
'profile.req_friends' : {
2022-12-10 02:01:17 +01:00
$elemMatch : { username : { $eq : recUser . username } } ,
2022-01-20 00:39:06 +01:00
} ,
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
} , { username : 1 } ) . lean ( ) ;
2022-01-20 00:39:06 +01:00
2023-01-08 02:17:01 +01:00
recUser . profile . asked _friends = listSentMyRequestFriends
2022-12-10 02:01:17 +01:00
? listSentMyRequestFriends
: [ ] ;
2022-02-03 00:33:15 +01:00
2022-02-05 23:28:15 +01:00
const listSentMyRequestGroups = await MyGroup . find ( {
2022-02-03 00:33:15 +01:00
idapp ,
2022-02-05 23:28:15 +01:00
'req_users' : {
2022-12-10 02:01:17 +01:00
$elemMatch : { username : { $eq : recUser . username } } ,
2022-02-03 00:33:15 +01:00
} ,
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
2022-03-06 00:48:33 +01:00
} , MyGroup . getWhatToShow _Unknown ( ) ) . lean ( ) ;
2022-02-03 00:33:15 +01:00
2023-01-08 02:17:01 +01:00
recUser . profile . asked _groups = listSentMyRequestGroups
2022-12-10 02:01:17 +01:00
? listSentMyRequestGroups
: [ ] ;
2022-02-05 23:28:15 +01:00
2022-08-08 16:35:32 +02:00
const listRefusedGroups = await MyGroup . find ( {
idapp ,
'refused_users' : {
2022-12-10 02:01:17 +01:00
$elemMatch : { username : { $eq : recUser . username } } ,
2022-08-08 16:35:32 +02:00
} ,
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
2022-08-08 16:35:32 +02:00
} , MyGroup . getWhatToShow _Unknown ( ) ) . lean ( ) ;
2023-01-08 02:17:01 +01:00
recUser . profile . refused _groups = listRefusedGroups
2022-12-10 02:01:17 +01:00
? listRefusedGroups
: [ ] ;
2022-08-08 16:35:32 +02:00
2022-02-05 23:28:15 +01:00
const listManageGroups = await MyGroup . find ( {
idapp ,
'admins' : {
2022-12-10 02:01:17 +01:00
$elemMatch : { username : { $eq : recUser . username } } ,
2022-02-05 23:28:15 +01:00
} ,
$or : [
2022-12-10 02:01:17 +01:00
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
2022-03-06 00:48:33 +01:00
} ) . lean ( ) ;
2022-02-05 23:28:15 +01:00
2023-10-01 01:24:47 +02:00
recUser . profile . reaction = await Reaction . find ( { idapp , username : recUser . username } ) . lean ( ) ;
2023-09-27 18:38:57 +02:00
2023-11-30 01:49:17 +01:00
recUser . profile . userstoverify = await User . getUsersToVerify ( idapp , recUser . username ) ;
2023-01-08 02:17:01 +01:00
recUser . profile . manage _mygroups = listManageGroups
2022-12-10 02:01:17 +01:00
? listManageGroups
: [ ] ;
2022-02-05 23:28:15 +01:00
2022-09-03 13:06:58 +02:00
// Circuit>
2022-08-26 03:33:13 +02:00
2022-09-14 11:32:04 +02:00
const circuitobj = await Circuit . getCircuitsByUsername ( idapp , recUser . username , recUser ) ;
2022-08-26 03:33:13 +02:00
2022-09-03 13:06:58 +02:00
const useraccounts = await Account . getUserAccounts ( idapp , recUser . username ) ;
2023-01-13 12:29:28 +01:00
2023-01-12 01:03:10 +01:00
for ( const group of listManageGroups ) {
const myaccounts = await Account . getGroupAccounts ( idapp , group . groupname ) ;
if ( myaccounts && myaccounts . length > 0 )
2023-01-13 12:29:28 +01:00
group . account = myaccounts [ 0 ] ;
else
group . account = null ;
2023-01-12 01:03:10 +01:00
}
2022-09-03 13:06:58 +02:00
2023-01-08 02:17:01 +01:00
recUser . profile = { ... recUser . profile , ... circuitobj , useraccounts } ;
2022-08-26 03:33:13 +02:00
2023-01-08 02:17:01 +01:00
recUser . calcstat = await User . calculateStat ( idapp , recUser . username ) ;
2022-09-16 17:38:49 +02:00
2023-01-08 02:17:01 +01:00
recUser . profile . calc = await User . calcOtherByUser ( idapp , recUser . _id ) ;
2022-12-10 02:01:17 +01:00
2023-11-19 23:40:38 +01:00
// tools.endTimeLog('addExtraInfo')
2023-10-21 15:27:53 +02:00
2023-01-08 02:17:01 +01:00
return recUser ;
2022-03-06 00:48:33 +01:00
2022-02-11 01:08:03 +01:00
} catch ( e ) {
2022-09-16 17:38:49 +02:00
console . error ( 'Err addExtraInfo' , e ) ;
2022-01-20 00:39:06 +01:00
}
return recUser ;
} ;
2023-09-27 18:38:57 +02:00
addRecordReaction = async function ( arr , fields , fieldtoset ) {
let newrecord = { } ;
try {
if ( tools . isArray ( arr ) ) {
for ( let myrec of arr ) {
if ( myrec . id ) {
let myfields = { ... { idrec : myrec . id } , ... fields } ;
newrecord = await Reaction . findOne ( myfields )
if ( ! newrecord ) {
newrecord = new Reaction ( myfields ) ;
// console.log('Nuovo Record: ', newrecord._doc);
}
newrecord [ fieldtoset ] = true ;
await newrecord . save ( ) ;
let newrecfound = await Reaction . findOne ( myfields )
}
// if (newrecfound)
// console.log('trovato')
}
}
} catch ( e ) {
console . error ( e . message ) ;
}
} ;
UserSchema . statics . moverecordsFavorite = async function ( tab ) {
const { MySkill } = require ( '../models/myskill' ) ;
const { MyGood } = require ( '../models/mygood' ) ;
console . log ( 'INIZIO - moverecordsFavorite tab' , tab ) ;
const arrusers = await User . find ( { idapp : '13' } ) . lean ( ) ;
let index = 0 ;
try {
let index = 0 ;
let totali = 0 ;
for ( let user of arrusers ) {
if ( user . profile ) {
let arrfav = user . profile . favorite ;
let arrseen = user . profile . seen ;
let arrbookmark = user . profile . bookmark ;
let arrattend = user . profile . attend ;
totali ++ ;
if ( arrfav || arrseen ) {
console . log ( 'utente n.' , index , 'su' , totali , user . username ) ;
index ++ ;
let fields = {
idapp : user . idapp ,
userId : user . _id ,
username : user . username ,
tab
} ;
await addRecordReaction ( arrfav , fields , 'fav' ) ;
await addRecordReaction ( arrseen , fields , 'seen' ) ;
await addRecordReaction ( arrbookmark , fields , 'book:' ) ;
await addRecordReaction ( arrattend , fields , 'attend' ) ;
}
}
}
2023-10-01 01:24:47 +02:00
} catch ( e ) {
console . error ( e . message ) ;
}
console . log ( 'fine moverecordsFavorite' ) ;
} ;
UserSchema . statics . removerecordsFavorite = async function ( ) {
2023-10-03 23:16:52 +02:00
// Rimuovi i record del vecchio schema
const attivacanc = true ;
2023-10-01 01:24:47 +02:00
2023-10-03 23:16:52 +02:00
if ( attivacanc ) {
const queryfind = { idapp : '13' } ;
await User . findOneAndUpdate ( queryfind , {
$set :
{
'profile.favorite' : [ ] ,
'profile.bookmark' : [ ] ,
'profile.attend' : [ ] ,
'profile.seen' : [ ]
} ,
} ) ;
2023-09-27 18:38:57 +02:00
2023-10-03 23:16:52 +02:00
}
2023-09-27 18:38:57 +02:00
} ;
2023-04-12 15:37:54 +02:00
UserSchema . statics . updateVersion = async function ( userversion , recUser ) {
2023-04-17 00:11:36 +02:00
2023-04-13 13:46:48 +02:00
const { MySkill } = require ( '../models/myskill' ) ;
const { MyGood } = require ( '../models/mygood' ) ;
const { City } = require ( '../models/city' ) ;
if ( userversion < 604 ) {
if ( ! recUser . profile . notifs || recUser . profile . notifs . length === 0 ) {
recUser . profile . notifs = shared _consts . DEFAULT _NOTIFS _USER ;
}
2023-04-12 15:37:54 +02:00
2023-04-17 00:11:36 +02:00
const recfavoriteNotif = recUser . profile . notifs . find ( ( rec ) => rec . dir === shared _consts . TypeNotifs . TYPEDIR _FAVORITE ) ;
if ( ! recfavoriteNotif ) {
recUser . profile . notifs . push ( { dir : shared _consts . TypeNotifs . TYPEDIR _FAVORITE , value : shared _consts . TypeNotifs . ID _FAVORITE _ADDED } )
}
const recAttendNotif = recUser . profile . notifs . find ( ( rec ) => rec . dir === shared _consts . TypeNotifs . TYPEDIR _EVENTS ) ;
if ( recAttendNotif ) {
if ( ! tools . isBitActive ( recAttendNotif . value , shared _consts . TypeNotifs . ID _EVENTS _ATTEND ) ) {
recAttendNotif . value = tools . SetBit ( recAttendNotif . value , shared _consts . TypeNotifs . ID _EVENTS _ATTEND ) ;
}
for ( let i = 0 ; i < recUser . profile . notifs . length ; i ++ ) {
if ( recUser . profile . notifs [ i ] . dir === shared _consts . TypeNotifs . TYPEDIR _EVENTS )
recUser . profile . notifs [ i ] . value = recAttendNotif . value ;
}
2023-04-13 13:46:48 +02:00
}
// Imposta la provincia, se non l'ho messa, in base al bene o servizio che hai messo.
if ( ! recUser . profile . resid _province ) {
let resid _province = '' ;
2023-04-17 00:11:36 +02:00
const firstrec = await MySkill . findOne ( { userId : recUser . _id } ) . lean ( ) ;
2023-04-13 13:46:48 +02:00
if ( firstrec && firstrec . idCity && firstrec . idCity . length > 0 ) {
2023-04-17 00:11:36 +02:00
resid _province = await City . getProvinceByIdCity ( firstrec . idCity [ 0 ] ) ;
2023-04-13 13:46:48 +02:00
} else {
2023-04-17 00:11:36 +02:00
const firstrec = await MyGood . findOne ( { userId : recUser . _id } ) . lean ( ) ;
2023-04-13 13:46:48 +02:00
if ( firstrec && firstrec . idCity && firstrec . idCity . length === 1 ) {
resid _province = await City . getProvinceByIdCity ( firstrec . idCity [ 0 ] ) ;
}
}
2023-04-17 00:11:36 +02:00
2023-04-13 13:46:48 +02:00
if ( resid _province ) {
2023-04-17 00:11:36 +02:00
await User . findOneAndUpdate ( { _id : recUser . _id } , { $set : { 'profile.resid_province' : resid _province } } ) ;
2023-04-13 13:46:48 +02:00
}
}
let recmybach = recUser . profile . notifs . find ( ( rec ) => rec . dir === shared _consts . TypeNotifs . TYPEDIR _BACHECA ) ;
if ( ! tools . isBitActive ( recmybach . value , shared _consts . UsersNotif . NEW _ADV _YOUR _PROVINCE ) ) {
recmybach . value = tools . SetBit ( recmybach . value , shared _consts . UsersNotif . NEW _ADV _YOUR _PROVINCE ) ;
for ( let i = 0 ; i < recUser . profile . notifs . length ; i ++ ) {
if ( recUser . profile . notifs [ i ] . dir === shared _consts . TypeNotifs . TYPEDIR _BACHECA )
recUser . profile . notifs [ i ] . value = recmybach . value ;
}
}
2023-04-12 15:37:54 +02:00
2023-04-17 00:11:36 +02:00
await User . findOneAndUpdate ( { _id : recUser . _id } , { $set : { 'profile.notifs' : recUser . profile . notifs } } ) ;
2023-04-12 15:37:54 +02:00
}
2023-09-27 18:38:57 +02:00
if ( userversion < 1016 ) {
}
2023-04-12 15:37:54 +02:00
return recUser ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . calcOtherByUser = async function ( idapp , userId ) {
2022-12-03 03:14:26 +01:00
2022-12-10 02:01:17 +01:00
const { MySkill } = require ( '../models/myskill' ) ;
const { MyGood } = require ( '../models/mygood' ) ;
const { MyHosp } = require ( '../models/myhosp' ) ;
2022-12-03 03:14:26 +01:00
let calc = { } ;
2022-12-10 02:01:17 +01:00
let numgoods = await MyGood . countDocuments ( { idapp , userId } ) ;
let numskills = await MySkill . countDocuments ( { idapp , userId } ) ;
let numhosps = await MyHosp . countDocuments ( { idapp , userId } ) ;
2022-12-07 18:16:00 +01:00
calc . numGoodsAndServices = numgoods + numskills + numhosps ;
2022-12-03 03:14:26 +01:00
return calc ;
} ;
2022-12-11 18:04:02 +01:00
UserSchema . statics . tooManyReqPassword = async function ( idapp , email , set ) {
const User = this ;
const maxnum = 30 ;
const user = await User . findByEmail ( idapp , email ) ;
if ( user ) {
if ( ! user . retry _pwd )
user . retry _pwd = 0
if ( set && user . retry _pwd <= maxnum ) {
user . retry _pwd ++ ;
await User . findOneAndUpdate ( { _id : user . _id } , { $set : { retry _pwd : user . retry _pwd } } ) ;
}
2022-12-12 18:25:13 +01:00
return user . retry _pwd > maxnum ;
2022-12-11 18:04:02 +01:00
}
} ;
2024-05-04 14:49:02 +02:00
UserSchema . statics . tooManyLoginWrong = async function ( idapp , username , set ) {
const User = this ;
const maxnum = 30 ;
const user = await User . findByUsername ( idapp , username , true , false ) ;
if ( user ) {
if ( ! user . retry _pwd )
user . retry _pwd = 0
if ( set && user . retry _pwd <= maxnum ) {
user . retry _pwd ++ ;
await User . findOneAndUpdate ( { _id : user . _id } , { $set : { retry _pwd : user . retry _pwd } } ) ;
}
return { troppilogin : user . retry _pwd > maxnum , retry _pwd : user . retry _pwd } ;
}
return { troppilogin : false , retry _pwd : 0 } ;
} ;
2022-12-11 18:04:02 +01:00
2023-03-11 01:01:11 +01:00
UserSchema . statics . setLastCircuitOpened = async function ( idapp , username , circuitpath ) {
try {
return await User . findOneAndUpdate ( { idapp , username } , { $set : { 'profile.last_circuitpath' : circuitpath } } ) ;
} catch ( e ) {
return false ;
}
} ;
UserSchema . statics . setReceiveRis = async function ( idapp , username ) {
const User = this ;
return await User . findOneAndUpdate ( {
idapp , username ,
} ,
{ $set : { 'profile.lastdate_reqRis' : new Date ( ) } } , { new : false } ) . lean ( ) . then ( ( record ) => {
return ! ! record ;
} ) ;
} ;
2022-12-23 00:36:35 +01:00
2023-12-08 14:07:32 +01:00
UserSchema . statics . addNewSite = async function ( idappPass , body ) {
const User = this ;
const Site = require ( '../models/site' ) ;
// Inserisci il nuovo Sito (se non esiste)
// Site.
// Inserisci il Nuovo Utente (se non esiste)
if ( ! body . code || body . code . toString ( ) !== process . env . AUTH _NEW _SITES ) {
return { code : server _constants . RIS _CODE _ERR _UNAUTHORIZED , idapp : - 1 } ;
}
try {
// cerca un IdApp Libero
2023-12-09 00:19:40 +01:00
let idapp = await Site . generateNewSite _IdApp ( idappPass , body , true ) ;
2023-12-08 14:07:32 +01:00
2024-03-21 22:58:46 +01:00
2023-12-08 14:07:32 +01:00
if ( idapp ) {
let arrSite = await Site . find ( { idapp } ) . lean ( ) ;
let numutenti = 0 ;
if ( idapp ) {
numutenti = await User . countDocuments ( { idapp } ) ;
} ;
2023-12-16 18:40:17 +01:00
if ( arrSite && arrSite . length === 1 && numutenti < 2 ) {
2023-12-17 16:20:44 +01:00
const MyTelegramBot = require ( '../telegram/telegrambot' ) ;
2024-03-21 22:58:46 +01:00
2023-12-08 14:07:32 +01:00
// Nessun Sito Installato e Nessun Utente installato !
let myuser = new User ( ) ;
myuser . _id = new ObjectID ( ) ;
myuser . idapp = idapp ;
myuser . email = body . email ;
myuser . username = body . username ;
myuser . password = body . password ;
myuser . name = body . username ;
myuser . index = 1 ;
myuser . surname = '' ;
myuser . lang = 'it' ;
myuser . verified _email = true ;
myuser . verified _by _aportador = true ;
myuser . perm = '3' ;
myuser . profile . special _req = true ;
myuser . profile . nationality = 'IT' ;
2023-12-17 16:20:44 +01:00
myuser . profile . manage _telegram = true ;
2023-12-29 15:19:15 +01:00
myuser . profile . admin _telegram = true ;
2023-12-17 16:20:44 +01:00
myuser . profile . teleg _id = MyTelegramBot . ADMIN _IDTELEGRAM _SERVER ;
myuser . profile . username _telegram = MyTelegramBot . ADMIN _USERNAME _TELEGRAM ;
2023-12-08 14:07:32 +01:00
myuser . lasttimeonline = new Date ( ) ;
myuser . date _reg = new Date ( ) ;
await myuser . save ( ) ;
2023-12-17 16:20:44 +01:00
const { MyBot } = require ( '../models/bot' ) ;
// Genera il Menu del BOT:
await MyBot . generateBotMenuRecords ( idapp ) ;
2023-12-08 14:07:32 +01:00
return { code : server _constants . RIS _CODE _OK , idapp } ;
}
}
return { code : server _constants . RIS _CODE _ERR , idapp : - 1 } ;
} catch ( e ) {
console . error ( 'Error in addNewSite:' , e . message ) ;
}
} ;
2024-04-25 23:26:16 +02:00
UserSchema . statics . renameCircuitName = async function ( idapp , oldcircuitname , newcircuitname ) {
const User = this ;
return await User . updateMany ( { idapp , 'profile.mycircuits.circuitname' : oldcircuitname } , { $set : { 'profile.mycircuits.$.circuitname' : newcircuitname } } ) ;
} ;
2022-12-10 02:01:17 +01:00
UserSchema . statics . createNewSubRecord = async function ( idapp , req ) {
2021-10-08 00:38:35 +02:00
const User = this ;
// console.log("GENERA TOKEN : ");
let userId = req . body . data . userId ;
let fieldkey = req . body . data . field ;
let data = req . body . data . data ;
2020-02-19 16:09:16 +01:00
2022-12-10 02:01:17 +01:00
const filtro = { _id : userId } ;
2021-10-08 00:38:35 +02:00
2022-12-10 02:01:17 +01:00
let fieldsvalue = { ... data } ;
2021-10-08 00:38:35 +02:00
fieldsvalue . date _created = new Date ( ) ;
fieldsvalue . date _updated = new Date ( ) ;
var set = {
profile : { } ,
} ;
set . profile [ fieldkey ] = fieldsvalue ;
2022-12-10 02:01:17 +01:00
const rec = await User . findOneAndUpdate ( filtro , { $set : set } , { new : false } ) ;
2021-10-08 00:38:35 +02:00
return rec ;
} ;
2018-12-24 20:31:02 +01:00
2023-10-21 15:27:53 +02:00
2021-10-08 00:38:35 +02:00
const User = mongoose . model ( 'User' , UserSchema ) ;
2019-10-28 16:01:28 +01:00
2023-12-09 11:55:58 +01:00
User . createIndexes ( ( err ) => {
if ( err ) throw err ;
} ) ;
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. ` ;
}
}
2023-10-21 15:27:53 +02:00
const FuncUsers = {
createRegistration _withTelegram ( userdata ) {
const telegrambot = require ( '../telegram/telegrambot' ) ;
try {
// Cerca se esiste già
const user = new User ( userdata ) ;
user . verified _email = false ;
user . lasttimeonline = new Date ( ) ;
user . date _reg = new Date ( ) ;
return user . save ( ) . then ( async ( ) => {
return User . findByUsername ( user . idapp , user . username , false ) .
then ( async ( usertrovato ) => {
2023-11-15 17:50:14 +01:00
2023-10-21 15:27:53 +02:00
const numutenti = await User . getNumUsers ( user . idapp ) ;
let msg = '++ Nuovo Entrato: [' + numutenti + '] ' + user . username + ' ' + user . name + ' ' + user . surname ;
await telegrambot . sendMsgTelegramToTheManagers ( user . idapp , msg ) ;
return telegrambot . askConfirmationUser ( user . idapp , shared _consts . CallFunz . REGISTRATION , user , '' , '' , '' , '' ) ;
} ) ;
} ) . catch ( ( e ) => {
2023-11-15 17:50:14 +01:00
console . error ( e . message ) ;
2023-10-21 15:27:53 +02:00
} ) ;
} catch ( e ) {
console . error ( e . message ) ;
}
}
} ;
module . exports = {
User , Hero , FuncUsers
} ;
2018-12-24 20:31:02 +01:00