2019-02-05 03:40:22 +01:00
const express = require ( 'express' ) ;
2019-12-07 00:20:06 +01:00
const router = express . Router ( ) ,
fs = require ( 'fs' ) ,
path = require ( 'path' ) ;
2020-01-20 01:48:25 +01:00
const jwt = require ( 'jsonwebtoken' ) ;
2020-01-30 01:19:25 +01:00
const telegrambot = require ( '../telegram/telegrambot' ) ;
2020-01-20 01:48:25 +01:00
const sendemail = require ( '../sendemail' ) ;
2019-02-05 03:40:22 +01:00
2019-10-23 23:47:21 +02:00
const { authenticate , authenticate _noerror } = require ( '../middleware/authenticate' ) ;
2019-02-22 10:23:39 +01:00
const { ObjectID } = require ( 'mongodb' ) ;
2020-03-10 21:44:14 +01:00
const { ListaIngresso } = require ( '../models/listaingresso' ) ;
2020-05-19 00:18:13 +02:00
const { Graduatoria } = require ( '../models/graduatoria' ) ;
2019-02-22 10:23:39 +01:00
2019-10-20 01:21:54 +02:00
const mongoose = require ( 'mongoose' ) ;
2019-02-22 10:23:39 +01:00
const cfgserver = mongoose . model ( 'cfgserver' ) ;
2019-12-07 00:20:06 +01:00
const ftp = require ( '../ftp/FTPClient' ) ,
formidable = require ( 'formidable' ) ,
2019-12-27 12:41:39 +01:00
folder = path . join ( _ _dirname , 'upload' ) ;
2019-12-07 00:20:06 +01:00
if ( ! fs . existsSync ( folder ) ) {
fs . mkdirSync ( folder )
}
2019-02-06 18:48:32 +01:00
const _ = require ( 'lodash' ) ;
2019-10-20 01:21:54 +02:00
const { User } = require ( '../models/user' ) ;
2020-03-10 21:44:14 +01:00
const { Nave } = require ( '../models/nave' ) ;
2020-03-25 18:26:51 +01:00
const { NavePersistente } = require ( '../models/navepersistente' ) ;
2020-05-10 21:07:51 +02:00
// const { ExtraList } = require('../models/extralist');
2019-10-20 01:21:54 +02:00
const { Booking } = require ( '../models/booking' ) ;
const { Operator } = require ( '../models/operator' ) ;
2019-10-21 20:38:10 +02:00
const { Where } = require ( '../models/where' ) ;
2019-10-20 01:21:54 +02:00
const { MyEvent } = require ( '../models/myevent' ) ;
2019-10-23 23:47:21 +02:00
const { Contribtype } = require ( '../models/contribtype' ) ;
2020-01-03 01:52:49 +01:00
const { PaymentType } = require ( '../models/paymenttype' ) ;
2019-11-12 21:34:03 +01:00
const { Discipline } = require ( '../models/discipline' ) ;
2019-11-21 00:18:40 +01:00
const { Newstosent } = require ( '../models/newstosent' ) ;
2019-12-07 00:20:06 +01:00
const { MyPage } = require ( '../models/mypage' ) ;
2020-01-21 01:37:15 +01:00
const { CalZoom } = require ( '../models/calzoom' ) ;
2019-12-27 12:41:39 +01:00
const { Gallery } = require ( '../models/gallery' ) ;
2019-12-04 02:03:44 +01:00
const { TemplEmail } = require ( '../models/templemail' ) ;
const { OpzEmail } = require ( '../models/opzemail' ) ;
2019-11-21 00:18:40 +01:00
const { MailingList } = require ( '../models/mailinglist' ) ;
2019-11-04 20:30:09 +01:00
const { Settings } = require ( '../models/settings' ) ;
2019-10-24 23:30:45 +02:00
const { SendMsg } = require ( '../models/sendmsg' ) ;
2019-10-28 16:01:28 +01:00
const { Permission } = require ( '../models/permission' ) ;
2019-02-22 10:23:39 +01:00
2019-02-06 18:48:32 +01:00
2019-10-14 20:31:57 +02:00
const tools = require ( '../tools/general' ) ;
2019-10-20 01:21:54 +02:00
const server _constants = require ( '../tools/server_constants' ) ;
const actions = require ( './api/actions' ) ;
2019-02-06 18:48:32 +01:00
2020-05-04 19:34:41 +02:00
const shared _consts = require ( '../tools/shared_nodejs' ) ;
2020-03-10 21:44:14 +01:00
UserCost = {
FIELDS _REQUISITI : [ 'verified_email' ,
'profile.teleg_id' ,
'profile.saw_zoom_presentation' ,
'profile.saw_and_accepted' ,
'profile.email_paypal' ,
'profile.my_dream' ,
'profile.paymenttypes' ]
} ;
2019-02-06 18:48:32 +01:00
2019-02-05 03:40:22 +01:00
router . post ( process . env . LINKVERIF _REG , ( req , res ) => {
2019-10-20 01:21:54 +02:00
const body = _ . pick ( req . body , [ 'idapp' , 'idlink' ] ) ;
const idapp = body . idapp ;
const idlink = body . idlink ;
2020-01-20 01:48:25 +01:00
// console.log("LINKVERIF_REG POST " + process.env.LINKVERIF_REG + " idapp= " + idapp + " idlink = " + idlink);
2019-02-05 03:40:22 +01:00
// Cerco l'idlink se è ancora da Verificare
User . findByLinkreg ( idapp , idlink ) . then ( ( user ) => {
if ( ! user ) {
//console.log("NON TROVATO!");
return res . status ( 404 ) . send ( ) ;
} else {
2020-01-20 01:48:25 +01:00
console . log ( 'user' , user ) ;
2019-02-05 03:40:22 +01:00
if ( user . verified _email ) {
res . send ( {
code : server _constants . RIS _CODE _EMAIL _ALREADY _VERIFIED ,
2020-01-20 01:48:25 +01:00
msg : tools . getres _ _ ( "L'Email è già stata Verificata" , res )
2019-02-05 03:40:22 +01:00
} ) ;
} else {
user . verified _email = true ;
2019-10-27 00:37:10 +02:00
user . lasttimeonline = new Date ( ) ;
2019-02-05 03:40:22 +01:00
user . save ( ) . then ( ( ) => {
//console.log("TROVATOOOOOO!");
2020-03-10 21:44:14 +01:00
res . send ( {
code : server _constants . RIS _CODE _EMAIL _VERIFIED ,
msg : tools . getres _ _ ( 'EMAIL' , res ) + ' ' + tools . getres _ _ ( 'VERIF' , res )
} ) ;
2019-02-05 03:40:22 +01:00
} ) ;
}
}
} ) . catch ( ( e ) => {
2020-05-04 19:34:41 +02:00
console . log ( e . message ) ;
2019-02-05 03:40:22 +01:00
res . status ( 400 ) . send ( ) ;
} ) ;
} ) ;
// Faccio richiesta di una Nuova Password
router . post ( process . env . LINK _REQUEST _NEWPASSWORD , ( req , res ) => {
2019-10-23 23:47:21 +02:00
const body = _ . pick ( req . body , [ 'idapp' , 'email' ] ) ;
const idapp = body . idapp ;
2020-03-10 21:44:14 +01:00
const email = body . email . toLowerCase ( ) . trim ( ) ;
2019-02-05 03:40:22 +01:00
console . log ( "POST " + process . env . LINK _REQUEST _NEWPASSWORD + " idapp= " + idapp + " email = " + email ) ;
2020-01-30 01:19:25 +01:00
User . findByEmail ( idapp , email ) . then ( async ( user ) => {
2019-02-05 03:40:22 +01:00
if ( ! user ) {
2020-01-30 01:19:25 +01:00
await tools . snooze ( 5000 ) ;
2020-01-20 01:48:25 +01:00
return res . status ( 200 ) . send ( { code : server _constants . RIS _CODE _EMAIL _NOT _EXIST , msg : '' } ) ;
2019-02-05 03:40:22 +01:00
} else {
// Creo il tokenforgot
user . tokenforgot = jwt . sign ( user . _id . toHexString ( ) , process . env . SIGNCODE ) . toString ( ) ;
user . date _tokenforgot = new Date ( ) ;
2019-10-27 00:37:10 +02:00
user . lasttimeonline = new Date ( ) ;
2019-12-07 00:20:06 +01:00
user . save ( ) . then ( async ( ) => {
2020-01-20 01:48:25 +01:00
await sendemail . sendEmail _RequestNewPassword ( res . locale , user , user . email , user . idapp , user . tokenforgot ) ;
2019-02-05 03:40:22 +01:00
res . send ( { code : server _constants . RIS _CODE _OK , msg : '' } ) ;
} ) ;
}
} ) . catch ( ( e ) => {
2020-05-04 19:34:41 +02:00
console . log ( e . message ) ;
2019-02-05 03:40:22 +01:00
res . status ( 400 ) . send ( ) ;
res . send ( { code : server _constants . RIS _CODE _ERR , msg : e } ) ;
} ) ;
} ) ;
2019-10-23 23:47:21 +02:00
2019-02-05 03:40:22 +01:00
// Invio la Nuova Password richiesta dal reset!
// Ritorna il token per poter effettuare le chiamate...
2020-01-30 01:19:25 +01:00
router . post ( process . env . LINK _UPDATE _PWD , ( req , res ) => {
const body = _ . pick ( req . body , [ 'idapp' , 'email' , 'tokenforgot' , 'password' ] ) ;
const idapp = body . idapp ;
2020-03-10 21:44:14 +01:00
const email = body . email . toLowerCase ( ) . trim ( ) ;
2020-01-30 01:19:25 +01:00
const tokenforgot = body . tokenforgot ;
const password = body . password ;
const msg = "Richiesta Nuova Password: idapp= " + idapp + " email = " + email ;
console . log ( msg ) ;
2020-02-19 16:09:16 +01:00
// telegrambot.sendMsgTelegramToTheManagers(body.idapp, msg);
2019-02-05 03:40:22 +01:00
User . findByLinkTokenforgot ( idapp , email , tokenforgot ) . then ( ( user ) => {
if ( ! user ) {
2020-02-05 00:39:25 +01:00
return res . send ( { code : server _constants . RIS _CODE _TOKEN _RESETPASSWORD _NOT _FOUND } ) ;
2019-02-05 03:40:22 +01:00
} else {
// aggiorna la nuova password
user . password = password ;
2019-10-27 00:37:10 +02:00
user . lasttimeonline = new Date ( ) ;
2019-02-05 03:40:22 +01:00
// Crea token
2019-02-09 18:03:14 +01:00
user . generateAuthToken ( req ) . then ( token => {
2019-02-05 03:40:22 +01:00
user . tokenforgot = '' ; // Svuota il tokenforgot perché non ti servirà più...
// Salva lo User
user . save ( ) . then ( ( ) => {
2020-02-05 00:39:25 +01:00
res . header ( 'x-auth' , token ) . send ( { code : server _constants . RIS _CODE _OK } ) ; // Ritorna il token di ritorno
2019-02-05 03:40:22 +01:00
} ) ;
} )
}
} ) . catch ( ( e ) => {
2020-05-04 19:34:41 +02:00
console . log ( e . message ) ;
2019-02-05 03:40:22 +01:00
res . status ( 400 ) . send ( ) ;
} ) ;
} ) ;
2019-10-15 20:40:31 +02:00
function getTableByTableName ( tablename ) {
2019-10-14 20:31:57 +02:00
2019-10-20 01:21:54 +02:00
mytable = '' ;
2019-10-15 20:40:31 +02:00
if ( tablename === 'users' )
2019-10-14 20:31:57 +02:00
mytable = User ;
2020-05-10 21:07:51 +02:00
else if ( tablename === 'tessitura' )
mytable = Tessitura ;
// else if (tablename === 'extralist')
// mytable = ExtraList;
2019-10-20 01:21:54 +02:00
else if ( tablename === 'bookings' )
2019-10-14 20:31:57 +02:00
mytable = Booking ;
2019-10-20 01:21:54 +02:00
else if ( tablename === 'operators' )
mytable = Operator ;
2019-10-24 23:30:45 +02:00
else if ( tablename === 'sendmsgs' )
mytable = SendMsg ;
2019-10-21 20:38:10 +02:00
else if ( tablename === 'wheres' )
mytable = Where ;
else if ( tablename === 'myevents' )
2019-10-20 01:21:54 +02:00
mytable = MyEvent ;
2019-10-23 23:47:21 +02:00
else if ( tablename === 'contribtype' )
mytable = Contribtype ;
2020-01-03 01:52:49 +01:00
else if ( tablename === 'paymenttypes' )
mytable = PaymentType ;
2019-11-12 21:34:03 +01:00
else if ( tablename === 'disciplines' )
mytable = Discipline ;
2019-11-21 00:18:40 +01:00
else if ( tablename === 'newstosent' )
mytable = Newstosent ;
2019-12-27 12:41:39 +01:00
else if ( tablename === 'gallery' )
mytable = Gallery ;
2019-12-07 00:20:06 +01:00
else if ( tablename === 'mypage' )
mytable = MyPage ;
2020-01-21 01:37:15 +01:00
else if ( tablename === 'calzoom' )
mytable = CalZoom ;
2019-12-04 02:03:44 +01:00
else if ( tablename === 'templemail' )
mytable = TemplEmail ;
else if ( tablename === 'opzemail' )
mytable = OpzEmail ;
2019-11-04 20:30:09 +01:00
else if ( tablename === 'settings' )
mytable = Settings ;
2019-10-28 16:01:28 +01:00
else if ( tablename === 'permissions' )
mytable = Permission ;
2019-11-21 00:18:40 +01:00
else if ( tablename === 'mailinglist' )
mytable = MailingList ;
2020-03-10 21:44:14 +01:00
else if ( tablename === 'navi' )
mytable = Nave ;
2020-03-25 18:26:51 +01:00
else if ( tablename === 'navepersistente' )
mytable = NavePersistente ;
2020-03-10 21:44:14 +01:00
else if ( tablename === 'listaingressos' )
mytable = ListaIngresso ;
2020-05-19 00:18:13 +02:00
else if ( tablename === 'graduatorias' )
mytable = Graduatoria ;
2019-10-14 20:31:57 +02:00
2019-10-15 20:40:31 +02:00
return mytable
}
2019-10-20 01:21:54 +02:00
router . post ( '/settable' , authenticate , ( req , res ) => {
const params = req . body ;
const mytable = getTableByTableName ( params . table ) ;
const mydata = req . body . data ;
mydata . idapp = req . user . idapp ;
2019-10-28 16:01:28 +01:00
if ( params . table === 'permissions' ) {
if ( mydata [ "_id" ] === undefined ) {
mydata . _id = 1 ;
}
} else {
if ( mydata [ "_id" ] === undefined ) {
mydata . _id = new ObjectID ( )
}
}
2019-10-20 01:21:54 +02:00
mytablerec = new mytable ( mydata ) ;
2019-10-28 16:01:28 +01:00
console . log ( 'mytablerec' , mytablerec ) ;
2019-10-20 01:21:54 +02:00
return mytablerec . save ( )
. then ( rec => {
// tools.mylog('rec', rec);
return res . send ( rec ) ;
} ) . catch ( ( e ) => {
2020-05-04 19:34:41 +02:00
console . log ( e . message ) ;
2019-10-20 01:21:54 +02:00
res . status ( 400 ) . send ( e ) ;
} ) ;
} ) ;
2019-10-15 20:40:31 +02:00
router . post ( '/gettable' , authenticate , ( req , res ) => {
const params = req . body ;
const mytable = getTableByTableName ( params . table ) ;
2019-10-20 01:21:54 +02:00
// console.log('mytable', mytable);
if ( ! mytable ) {
2019-12-04 02:03:44 +01:00
console . log ( ` Table ${ params . table } not found ` ) ;
return res . status ( 400 ) . send ( { } ) ;
2019-10-20 01:21:54 +02:00
}
2019-10-15 20:40:31 +02:00
2019-10-20 01:21:54 +02:00
return mytable . executeQueryTable ( req . user . idapp , params ) . then ( ris => {
2019-10-14 20:31:57 +02:00
return res . send ( ris ) ;
2019-10-20 01:21:54 +02:00
2019-10-14 20:31:57 +02:00
} ) . catch ( ( e ) => {
2020-05-04 19:34:41 +02:00
console . log ( e . message ) ;
2019-10-14 20:31:57 +02:00
res . status ( 400 ) . send ( e ) ;
} ) ;
} ) ;
2020-03-10 21:44:14 +01:00
async function checkIfSbloccatiRequisiti ( idapp , allData , id ) {
2020-05-10 21:07:51 +02:00
if ( await Nave . checkIfNaveExist ( idapp , allData . myuser . username ) ) {
2020-03-10 21:44:14 +01:00
// Se già sei dentro la Nave, allora sei OK
return true ;
}
// Controlla se Sblocca i 7 requisiti
const is7req = await User . isUserQualified7 ( idapp , allData . myuser . username ) ;
const is9req = await User . isUserQualified9 ( idapp , allData . myuser . username ) ;
2020-05-10 21:07:51 +02:00
const userlista = await ListaIngresso . getListaTessByUsername ( idapp , allData . myuser . username ) ;
if ( userlista . length === 0 ) {
2020-03-10 21:44:14 +01:00
// Se non sono ancora dentro alla lista, allora controllo
2020-02-05 00:39:25 +01:00
2020-03-10 21:44:14 +01:00
if ( ! allData . precDataUser . is7req && is7req ) {
// ORA HAI I 7 REQUISITI !
2020-05-10 21:07:51 +02:00
// const msgtext = telegrambot.getCiao(idapp, allData.myuser.username, allData.myuser.lang) + tools.gettranslate('HAI_I_7_REQUISITI', allData.myuser.lang);
// telegrambot.sendMsgTelegram(idapp, allData.myuser.username, msgtext, true); // Anche a STAFF
2020-03-10 21:44:14 +01:00
if ( tools . isAbilitaNave ( idapp ) ) {
// Aggiungilo alla ListaIngresso
2020-05-10 21:07:51 +02:00
risingr = await ListaIngresso . addUserInListaIngresso ( idapp , allData . myuser . username , allData . myuser . aportador _iniziale , allData . myuser . lang , true , false ) ;
2020-03-10 21:44:14 +01:00
}
}
}
if ( ! allData . precDataUser . is9req && is9req ) {
// ORA HAI I 9 REQUISITI !
const msgtext = telegrambot . getCiao ( idapp , allData . myuser . username , allData . myuser . lang ) + tools . gettranslate ( 'HAI_I_9_REQUISITI' , allData . myuser . lang ) ;
2020-05-10 21:07:51 +02:00
telegrambot . sendMsgTelegram ( idapp , allData . myuser . username , msgtext , false ) ; // Anche a STAFF
2020-03-10 21:44:14 +01:00
}
// CHECK APORTADOR SOLIDARIO:
if ( ! ! allData . useraportador ) {
2020-05-11 22:43:14 +02:00
/ *
const is9reqAportador = await User . isUserQualified9 ( idapp , allData . myuser . aportador _solidario ) ;
2020-03-10 21:44:14 +01:00
2020-05-11 22:43:14 +02:00
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
}
* /
2020-03-10 21:44:14 +01:00
}
}
async function getInfoUser ( idapp , username ) {
return {
username ,
is7req : await User . isUserQualified7 ( idapp , username ) ,
is9req : await User . isUserQualified9 ( idapp , username ) ,
}
}
2020-03-31 20:34:24 +02:00
router . patch ( '/setlang' , authenticate , async ( req , res ) => {
const username = req . body . data . username ;
const idapp = req . body . idapp ;
const mydata = req . body . data ;
const lang = mydata . lang ;
const fieldsvalue = {
lang
} ;
if ( ! ! lang ) {
const rec = await User . findByUsername ( idapp , username , false ) ;
let ris = null ;
if ( ! ! rec )
ris = await User . findByIdAndUpdate ( rec . id , { $set : fieldsvalue } ) ;
if ( ! ! ris ) {
return res . send ( { code : server _constants . RIS _CODE _OK , msg : '' } ) ;
}
res . status ( 400 ) . send ( ) ;
}
} ) ;
2020-03-10 21:44:14 +01:00
router . patch ( '/chval' , authenticate , async ( req , res ) => {
2019-10-15 20:40:31 +02:00
// const idapp = req.body.idapp;
const id = req . body . data . id ;
2020-02-02 04:06:32 +01:00
const idapp = req . body . idapp ;
2019-10-15 20:40:31 +02:00
const mydata = req . body . data ;
const mytable = getTableByTableName ( mydata . table ) ;
const fieldsvalue = mydata . fieldsvalue ;
2020-03-10 21:44:14 +01:00
// tools.mylogshow('PATCH CHVAL: ', id, fieldsvalue);
2019-10-15 20:40:31 +02:00
2019-12-29 01:53:51 +01:00
// If I change my record...
2020-05-04 19:34:41 +02:00
if ( ( ! User . isAdmin ( req . user . perm ) && ! User . isManager ( req . user . perm ) && ! User . isTraduttrici ( req . user . perm ) && ! User . isTutor ( req . user . perm ) ) && ! ( req . user . _id . toString ( ) === id ) && ! tools . ModificheConsentite ( mydata . table , fieldsvalue ) ) {
2019-10-15 20:40:31 +02:00
// If without permissions, exit
return res . status ( 404 ) . send ( { code : server _constants . RIS _CODE _ERR _UNAUTHORIZED , msg : '' } ) ;
}
2020-03-10 21:44:14 +01:00
const camporequisiti = UserCost . FIELDS _REQUISITI . includes ( Object . keys ( fieldsvalue ) [ 0 ] ) ;
let allData = { } ;
if ( mydata . table === 'users' ) {
if ( camporequisiti ) {
allData = { } ;
allData . myuser = await User . getUserById ( idapp , id ) ;
allData . precDataUser = await getInfoUser ( idapp , allData . myuser . username ) ;
2020-05-10 21:07:51 +02:00
// allData.useraportador = await ListaIngresso.getUserByInvitante_Username(idapp, allData.myuser.aportador_solidario);
// allData.precDataAportador = await getInfoUser(idapp, allData.myuser.aportador_solidario);
2020-03-10 21:44:14 +01:00
}
}
2020-04-24 10:29:25 +02:00
let index = 0 ;
2020-03-10 21:44:14 +01:00
await mytable . findByIdAndUpdate ( id , { $set : fieldsvalue } ) . then ( async ( rec ) => {
2019-12-31 00:44:53 +01:00
// tools.mylogshow(' REC TO MODIFY: ', rec);
2019-10-15 20:40:31 +02:00
if ( ! rec ) {
return res . status ( 404 ) . send ( ) ;
} else {
2020-02-02 04:06:32 +01:00
if ( mydata . notifBot ) {
// Send Notification to the BOT
2020-04-07 14:34:29 +02:00
await telegrambot . sendMsgTelegram ( idapp , mydata . notifBot . un , mydata . notifBot . txt ) ;
2020-02-05 00:39:25 +01:00
addtext = '[Msg Inviato a ' + mydata . notifBot . un + ']:' + '\n' + mydata . notifBot . txt ;
2020-05-11 22:43:14 +02:00
telegrambot . sendMsgTelegramToTheManagers ( idapp , addtext , true ) ;
2020-02-02 04:06:32 +01:00
}
2020-02-05 00:39:25 +01:00
2020-03-10 21:44:14 +01:00
if ( mydata . table === 'users' ) {
if ( camporequisiti ) {
await checkIfSbloccatiRequisiti ( idapp , allData , id ) ;
}
}
2020-05-04 19:34:41 +02:00
if ( mydata . table === 'users' ) {
if ( 'aportador_solidario' in fieldsvalue ) {
2020-05-10 21:07:51 +02:00
ind _order _ingr = mydata . ind _order _ingr ;
// SERVE SE CI METTO LE MINUSCOLE/MAIUSCOLE SBAGLIATE in invitante_username!
2020-05-04 19:34:41 +02:00
const myuserfound = await User . findByUsername ( idapp , fieldsvalue . aportador _solidario , false ) ;
if ( ! ! myuserfound ) {
if ( ! ! myuserfound . _id && ! myuserfound . deleted ) {
2020-05-10 21:07:51 +02:00
const aportador = await User . getUsernameById ( idapp , myuserfound . _id ) ;
fieldsvalue . aportador _solidario = aportador ;
2020-05-04 19:34:41 +02:00
//Aggiorna record !
await mytable . findByIdAndUpdate ( id , { $set : fieldsvalue } ) ;
2020-05-10 21:07:51 +02:00
const myfirstrec = await ListaIngresso . findOne ( { username : rec . username , ind _order : ind _order _ingr } ) ;
if ( ! ! myfirstrec ) {
if ( ! ! myfirstrec . _id && ! myfirstrec . deleted ) {
let fieldsv2 = {
invitante _username : aportador
} ;
//Aggiorna record !
const ris = await ListaIngresso . findByIdAndUpdate ( myfirstrec . _id . toString ( ) , { $set : fieldsv2 } ) ;
}
}
2020-05-04 19:34:41 +02:00
}
} else {
res . send ( { code : server _constants . RIS _CODE _ERR , msg : 'Non aggiornato' } ) ;
res . status ( 400 ) . send ( ) ;
return false ;
}
2020-05-19 00:18:13 +02:00
} else if ( 'deleted' in fieldsvalue ) {
await telegrambot . sendMsgTelegramToTheManagers ( idapp , ` L \' utente ${ rec . name } ${ rec . surname } ( ${ rec . username } ) è stato cancellato (nascosto) da ${ req . user . name } ${ req . user . surname } ` ) ;
2020-05-04 19:34:41 +02:00
}
}
2020-05-10 21:07:51 +02:00
if ( mydata . table === 'listaingressos' ) {
if ( 'invitante_username' in fieldsvalue ) {
// SERVE SE CI METTO LE MINUSCOLE/MAIUSCOLE SBAGLIATE in invitante_username!
const myuserfound = await User . findByUsername ( idapp , fieldsvalue . invitante _username , false ) ;
if ( ! ! myuserfound ) {
if ( ! ! myuserfound . _id && ! myuserfound . deleted ) {
fieldsvalue . invitante _username = await User . getUsernameById ( idapp , myuserfound . _id ) ;
//Aggiorna record !
const ris = await mytable . findByIdAndUpdate ( id , { $set : fieldsvalue } ) ;
if ( ! ! ris ) {
/ * l e t f i e l d s v 2 = {
aportador _solidario : fieldsvalue . invitante _username
} ;
//Aggiorna record !
await User . findByIdAndUpdate ( myuserfound . _id , { $set : fieldsv2 } ) ;
* /
}
}
} else {
// res.send({ code: server_constants.RIS_CODE_ERR, msg: 'Non aggiornato' });
// res.status(400).send();
// return false;
}
}
}
2020-02-05 00:39:25 +01:00
if ( tools . ModificheConsentite ( mydata . table , fieldsvalue ) ) {
let msg = '' ;
if ( mydata . table === 'users' ) {
if ( 'aportador_solidario' in fieldsvalue ) {
const nomecognomenuovo = await User . getNameSurnameByUsername ( idapp , fieldsvalue . aportador _solidario ) ;
const nomecognomeas = await User . getNameSurnameByUsername ( idapp , rec . aportador _solidario ) ;
msg = ` Variato l'invitante di ` + rec . name + ' ' + rec . surname + '\nmodificato da ' + req . user . name + ' ' + req . user . surname + ' \n' +
'Prima: ' + nomecognomeas + ' (' + rec . aportador _solidario + ')\n' +
'Dopo: ' + nomecognomenuovo + ' (' + fieldsvalue . aportador _solidario + ') ]' ;
2020-03-10 21:44:14 +01:00
// Metti l'iniziale
if ( ! await User . AportadorOrig ( id ) ) {
await mytable . findByIdAndUpdate ( id , { $set : { aportador _iniziale : fieldsvalue . aportador _solidario } } , { new : false } ) ;
}
2020-02-05 00:39:25 +01:00
}
2020-03-10 21:44:14 +01:00
2020-04-24 10:29:25 +02:00
} else if ( mydata . table === 'navi' ) {
if ( 'made_gift' in fieldsvalue ) {
if ( ! ! fieldsvalue . riga ) {
await Nave . ricalcolaNave ( idapp , null , fieldsvalue . riga , fieldsvalue . col , true , index )
}
}
2020-02-05 00:39:25 +01:00
}
if ( msg !== '' )
telegrambot . sendMsgTelegramToTheManagers ( idapp , msg ) ;
}
2019-10-15 20:40:31 +02:00
res . send ( { code : server _constants . RIS _CODE _OK , msg : '' } ) ;
2020-02-02 04:06:32 +01:00
2019-10-15 20:40:31 +02:00
}
} ) . catch ( ( e ) => {
2020-04-24 10:29:25 +02:00
tools . mylogserr ( 'Error patch USER: ' , e . message ) ;
2019-10-15 20:40:31 +02:00
res . status ( 400 ) . send ( ) ;
} )
2020-05-10 21:07:51 +02:00
2019-10-15 20:40:31 +02:00
} ) ;
2020-05-19 00:18:13 +02:00
router . patch ( '/askfunz' , authenticate , async ( req , res ) => {
// const idapp = req.body.idapp;
const id = req . body . data . id ;
const ind _order = req . body . data . ind _order ;
const username = req . body . data . username ;
const idapp = req . body . idapp ;
const mydata = req . body . data ;
let entra = false ;
if ( ! entra ) {
// If I change my record...
if ( ( ! User . isAdmin ( req . user . perm ) && ! User . isManager ( req . user . perm ) && ! User . isTutor ( req . user . perm ) ) && ! ( req . user . _id . toString ( ) === id ) ) {
// If without permissions, exit
return res . status ( 404 ) . send ( { code : server _constants . RIS _CODE _ERR _UNAUTHORIZED , msg : '' } ) ;
}
}
if ( mydata . myfunc === shared _consts . CallFunz . DAMMI _PRIMO _UTENTE _LIBERO ) {
const userfree = await Graduatoria . getFirstUserGradFree ( idapp ) ;
if ( ! ! userfree )
return res . send ( { code : server _constants . RIS _CODE _OK , out : userfree } ) ;
}
} ) ;
2020-04-24 10:29:25 +02:00
router . patch ( '/callfunz' , authenticate , async ( req , res ) => {
// const idapp = req.body.idapp;
const id = req . body . data . id ;
const ind _order = req . body . data . ind _order ;
2020-05-10 21:07:51 +02:00
const username = req . body . data . username ;
2020-04-24 10:29:25 +02:00
const idapp = req . body . idapp ;
const mydata = req . body . data ;
try {
2020-05-04 19:34:41 +02:00
let entra = false ;
if ( mydata . myfunc === shared _consts . CallFunz . AGGIUNGI _NUOVO _IMBARCO ||
mydata . myfunc === shared _consts . CallFunz . CANCELLA _IMBARCO ) {
entra = true
}
if ( ! entra ) {
// If I change my record...
2020-05-19 00:18:13 +02:00
if ( ( ! User . isAdmin ( req . user . perm ) && ! User . isManager ( req . user . perm ) && ! User . isTutor ( req . user . perm ) ) && ! ( req . user . _id . toString ( ) === id ) ) {
2020-05-04 19:34:41 +02:00
// If without permissions, exit
return res . status ( 404 ) . send ( { code : server _constants . RIS _CODE _ERR _UNAUTHORIZED , msg : '' } ) ;
}
2020-04-24 10:29:25 +02:00
}
2020-05-11 22:43:14 +02:00
let myuser = await User . findOne ( { idapp , username } ) ;
2020-04-24 10:29:25 +02:00
let rimosso = 0 ;
2020-05-04 19:34:41 +02:00
if ( mydata . myfunc === shared _consts . CallFunz . SOSTITUISCI ) { // SOSTITUISCI
2020-05-11 22:43:14 +02:00
username _da _sostituire = mydata . data . username _da _sostituire ;
2020-05-13 01:32:27 +02:00
let myuservecchio = await User . findOne ( { idapp , username : username _da _sostituire } ) ;
2020-04-24 10:29:25 +02:00
mianavedasost = await Nave . findOne ( { idapp , riga : mydata . data . riga , col : mydata . data . col } ) ;
2020-05-11 22:43:14 +02:00
if ( ! ! mianavedasost ) {
2020-04-24 10:29:25 +02:00
2020-05-11 22:43:14 +02:00
// Sostituisci l'Utente
myusernuovo = await User . getUserShortDataByUsername ( idapp , mydata . data . username ) ;
let navepersistente = await NavePersistente . findByRigaColByDonatore ( idapp , mydata . data . riga , mydata . data . col , 0 ) ;
2020-04-24 10:29:25 +02:00
2020-05-11 22:43:14 +02:00
if ( ! ! myusernuovo ) {
if ( ! mydata . data . AddImbarco && ! ! mianavedasost && mianavedasost . ind _order > 0 ) {
2020-05-19 00:18:13 +02:00
// Controlla prima se è in una Nave Temporanea, allora lo elimina dalla PRIMA Nave Temporanea
2020-05-11 22:43:14 +02:00
miaarrnavi = await Nave . getArrPosizioniByUsername ( idapp , username ) ;
if ( miaarrnavi ) {
for ( const mianave of miaarrnavi ) {
let persistente = await NavePersistente . findByRigaColByDonatore ( idapp , mianave . riga , mianave . col , 0 ) ;
if ( persistente . provvisoria ) {
fieldsvalue = {
ind _order : - 1
} ;
2020-05-19 00:18:13 +02:00
let ris = await Nave . findByIdAndUpdate ( mianave . _id , { $set : fieldsvalue } ) ;
2020-05-11 22:43:14 +02:00
if ( ! ! ris ) {
rimosso ++ ;
break ; // Rimuovilo solo 1 !
}
}
2020-04-24 10:29:25 +02:00
}
}
}
2020-05-13 01:32:27 +02:00
if ( ! ! myusernuovo ) {
2020-05-11 22:43:14 +02:00
if ( ! ! mianavedasost && mianavedasost . ind _order >= 0 ) {
2020-05-13 01:32:27 +02:00
2020-05-11 22:43:14 +02:00
// Metti campo 'delete': true su ListaIngresso
2020-05-19 00:18:13 +02:00
olduseringresso = await ListaIngresso . findById ( mianavedasost . idListaIngresso ) ;
2020-05-11 22:43:14 +02:00
if ( ! ! olduseringresso ) {
let fieldsvalue = {
2020-05-14 17:23:17 +02:00
date _deleted : new Date ( ) ,
2020-05-11 22:43:14 +02:00
deleted : true
} ;
2020-05-19 00:18:13 +02:00
const risul = await ListaIngresso . findByIdAndUpdate ( mianavedasost . idListaIngresso , { $set : fieldsvalue } , { new : false } ) ;
if ( ! ! risul ) {
const myrecuser = await ListaIngresso . getIngrEUserByFilter ( idapp , {
idapp ,
username : username _da _sostituire ,
_id : ObjectID ( mianavedasost . idListaIngresso )
} ) ;
if ( ! ! myrecuser ) {
const risdel = await ListaIngresso . deleteOne ( { _id : olduseringresso . id } ) ;
if ( ! ! risdel ) {
await actions . doOtherThingsAfterDeleted ( 'listaingressos' , myrecuser , false )
}
}
}
2020-05-11 22:43:14 +02:00
}
}
}
2020-05-13 01:32:27 +02:00
if ( ! ! myuservecchio ) {
2020-05-14 17:23:17 +02:00
// Se ha gia delle altre navi, non cancellarlo!
2020-05-19 00:18:13 +02:00
if ( ! await Nave . checkIfMadeGift ( idapp , myuservecchio . username ) ) {
if ( mydata . data . deleteUser && ! ! mianavedasost && mianavedasost . ind _order > 0 ) {
// Metti Deleted allo User
fieldsvalue = {
deleted : true ,
date _deleted : new Date ( ) ,
} ;
await User . findByIdAndUpdate ( myuservecchio . id , { $set : fieldsvalue } ) ;
await telegrambot . sendMsgTelegramToTheManagers ( idapp , ` L \' utente ${ myuservecchio . name } ${ myuservecchio . surname } ( ${ myuservecchio . username } ) è stato cancellato (nascosto) perchè sostituito (da ${ req . user . name } ${ req . user . surname } ) ` ) ;
}
2020-05-11 22:43:14 +02:00
}
}
let ind _order = - 1 ;
let myingr = null ;
// Estrai un ind_order dalla Lista, se era ancora in attesa
if ( ! mydata . data . AddImbarco )
myingr = await ListaIngresso . findOne ( { idapp , added : false , username : myusernuovo . username } ) ;
if ( ! ! myingr ) {
ind _order = myingr . ind _order ;
myingr . added = true ;
await myingr . save ( ) ;
} else {
// Crea un nuovo Ingresso
myingr = await ListaIngresso . addUserInListaIngresso ( idapp , myuser . username , myuser . username , myuser . lang , false , true , null , null , true ) ;
ind _order = myingr . ind _order ;
await myingr . save ( ) ;
2020-05-19 00:18:13 +02:00
2020-04-24 10:29:25 +02:00
}
2020-05-19 00:18:13 +02:00
// Togliolo dalla Graduatoria!
const mygrad = await Graduatoria . findOneAndUpdate ( {
idapp ,
idListaIngresso : myingr . _id
} , { $set : { ind _order : - 1 } } , { new : false } ) ;
2020-05-11 22:43:14 +02:00
// Aggiorna la Nave con il Nuovo
2020-04-24 10:29:25 +02:00
fieldsvalue = {
2020-05-11 22:43:14 +02:00
ind _order
2020-04-24 10:29:25 +02:00
} ;
2020-05-11 22:43:14 +02:00
const dachi = req . user . name + ' ' + req . user . surname ;
2020-04-24 10:29:25 +02:00
2020-05-11 22:43:14 +02:00
return await Nave . findByIdAndUpdate ( mianavedasost . id , { $set : fieldsvalue } )
. then ( async ( rec ) => {
// tools.mylogshow(' REC TO MODIFY: ', rec);
if ( ! rec ) {
return res . status ( 404 ) . send ( ) ;
} else {
2020-05-13 01:32:27 +02:00
// Send Notification to the BOT
let messaggio = tools . get _ _ ( 'SPOSTATO' , req . user . lang ) ;
2020-04-24 10:29:25 +02:00
2020-05-13 01:32:27 +02:00
if ( ! ! navepersistente . date _start ) {
messaggio += tools . ACAPO + tools . get _ _ ( 'DATA_PART_NAVE' , req . user . lang ) + tools . getstrDateLong ( navepersistente . date _gift _chat _open ) + tools . ACAPO ;
}
if ( ! ! navepersistente . link _chat ) {
messaggio += tools . ACAPO + '👉🏻👉🏻 <strong><a href="' + navepersistente . link _chat + '">' + tools . get _ _ ( 'ENTRA_GIFT_CHAT' , req . user . lang ) + '</a></strong> ' + tools . ACAPO ;
}
const myplacca = await Nave . getNavePos ( idapp , navepersistente . riga , navepersistente . col ) ;
messaggio += tools . ACAPO + myplacca ;
2020-04-24 10:29:25 +02:00
2020-05-13 01:32:27 +02:00
const mymsg = mydata . notifBot . txt + ' ' + myusernuovo . name + ' ' + myusernuovo . surname + ' [da ' + dachi + ']' + tools . ACAPO + 'Inviato messaggio: ' + messaggio ;
if ( mydata . data . notifBot && ! ! navepersistente . link _chat ) {
2020-04-24 10:29:25 +02:00
2020-05-11 22:43:14 +02:00
await telegrambot . sendMsgTelegram ( idapp , myusernuovo . username , messaggio ) ;
2020-05-13 01:32:27 +02:00
await telegrambot . sendMsgTelegramToTheManagers ( idapp , mymsg ) ;
2020-05-11 22:43:14 +02:00
await telegrambot . sendMsgTelegram ( idapp , req . user . username , mydata . notifBot . txt ) ;
await telegrambot . sendMsgTelegram ( idapp , req . user . username , myplacca ) ;
2020-04-24 10:29:25 +02:00
}
2020-05-13 01:32:27 +02:00
tools . writeManagersLog ( mymsg ) ;
2020-04-24 10:29:25 +02:00
2020-05-11 22:43:14 +02:00
// const nomecognomeprima = myuser.name + ' ' + myuser.surname + '(' + myuser.username + ')';
// const nomecognomenuovo = await User.getNameSurnameByUsername(idapp,);
2020-04-24 10:29:25 +02:00
2020-05-11 22:43:14 +02:00
res . send ( { code : server _constants . RIS _CODE _OK , msg : '' } ) ;
2020-04-24 10:29:25 +02:00
}
2020-05-11 22:43:14 +02:00
} ) . catch ( ( e ) => {
tools . mylogserr ( 'Error patch USER: ' , e ) ;
res . status ( 400 ) . send ( ) ;
} )
}
2020-04-24 10:29:25 +02:00
}
2020-05-04 19:34:41 +02:00
} else if ( mydata . myfunc === shared _consts . CallFunz . AGGIUNGI _NUOVO _IMBARCO ) {
// Ottieni il prossimo Numero di Tessitura
2020-05-10 21:07:51 +02:00
//let num_tess = await Nave.getNextNumTess(idapp, ind_order);
const num _tess = 1 ;
2020-05-04 19:34:41 +02:00
2020-05-10 21:07:51 +02:00
/ *
2020-05-04 19:34:41 +02:00
let listaingr = await ListaIngresso . find ( { idapp , ind _order } ) . sort ( { num _tess : 1 } ) ;
const trovato = listaingr . find ( ( rec ) => rec . num _tess === num _tess ) ;
if ( trovato ) {
num _tess = listaingr . slice ( - 1 ) [ 0 ] . num _tess + 2 ;
}
2020-05-10 21:07:51 +02:00
* /
2020-05-04 19:34:41 +02:00
2020-05-10 21:07:51 +02:00
// metti l'invitante inizialmente a Te stesso !
const invitante _username = req . body . data . invitante _username ;
2020-05-04 19:34:41 +02:00
2020-05-10 21:07:51 +02:00
ris = await ListaIngresso . addUserInListaIngresso ( idapp , username , invitante _username , myuser . lang , true , true ) ;
arrimbarchi = await ListaIngresso . findAllByUsername ( idapp , username ) ;
2020-05-04 19:34:41 +02:00
return res . send ( { code : server _constants . RIS _CODE _OK , arrimbarchi } ) ;
2020-05-10 21:07:51 +02:00
2020-05-04 19:34:41 +02:00
} else if ( mydata . myfunc === shared _consts . CallFunz . CANCELLA _IMBARCO ) {
2020-05-19 00:18:13 +02:00
const myrec = await ListaIngresso . getIngrEUserByFilter ( idapp , { idapp , _id : ObjectID ( mydata . data . id ) } ) ;
if ( ! ! myrec ) {
const risdel = await ListaIngresso . deleteOne ( { _id : mydata . data . id } ) ;
2020-05-04 19:34:41 +02:00
2020-05-19 00:18:13 +02:00
if ( ! ! risdel ) {
return await actions . doOtherThingsAfterDeleted ( 'listaingressos' , myrec , false )
. then ( ( ris ) => {
if ( ! ! ris )
// tools.mylog('DELETED Others things ...');
return res . send ( { code : server _constants . RIS _CODE _OK , msg : '' } ) ;
} ) ;
}
}
2020-05-04 19:34:41 +02:00
2020-04-24 10:29:25 +02:00
}
2020-05-19 00:18:13 +02:00
return res . send ( { code : server _constants . RIS _CODE _ERR } ) ;
2020-04-24 10:29:25 +02:00
} catch ( e ) {
2020-05-04 19:34:41 +02:00
console . log ( e . message ) ;
2020-04-24 10:29:25 +02:00
res . status ( 400 ) . send ( ) ;
}
} ) ;
2020-01-13 23:52:51 +01:00
router . get ( '/copyfromapptoapp/:idapporig/:idappdest' , async ( req , res ) => {
2020-02-05 00:39:25 +01:00
// const idapporig = req.params.idapporig;
// const idappdest = req.params.idappdest;
// if (!idapporig || !idappdest)
// res.status(400).send();
//
// const mytablesstr = ['settings', 'users', 'templemail', 'contribtype'];
//
// try {
// let numrectot = 0;
// for (const table of mytablesstr) {
// const mytable = getTableByTableName(table);
//
// tools.mylogshow('copyfromapptoapp: ', table, mytable);
//
// await mytable.DuplicateAllRecords(idapporig, idappdest).then((numrec) => {
// // tools.mylogshow(' REC TO MODIFY: ', rec);
// numrectot += numrec
// });
// }
//
// res.send({ code: server_constants.RIS_CODE_OK, msg: '', numrectot });
//
// } catch (e) {
// tools.mylogserr('Error copyfromapptoapp: ', e);
// res.status(400).send();
// }
2020-01-13 23:52:51 +01:00
} ) ;
2020-04-24 10:29:25 +02:00
router . delete ( '/delrec/:table/:id' , authenticate , async ( req , res ) => {
2019-10-15 20:40:31 +02:00
const id = req . params . id ;
2020-02-05 00:39:25 +01:00
const idapp = req . user . idapp ;
2019-10-15 20:40:31 +02:00
const tablename = req . params . table ;
2020-02-05 00:39:25 +01:00
let notifBot = false ;
2019-10-15 20:40:31 +02:00
// const idapp = req.body.idapp;
2019-10-20 01:21:54 +02:00
console . log ( 'id' , id , 'table' , tablename ) ;
2019-10-15 20:40:31 +02:00
const mytable = getTableByTableName ( tablename ) ;
2020-03-10 21:44:14 +01:00
const fields = { 'ALL' : 1 } ;
if ( ( ! User . isAdmin ( req . user . perm ) && ! User . isManager ( req . user . perm ) ) && ( tablename !== 'extralist' ) && ! tools . ModificheConsentite ( tablename , fields , id , req . user ) ) {
2019-10-15 20:40:31 +02:00
// If without permissions, exit
return res . status ( 404 ) . send ( { code : server _constants . RIS _CODE _ERR _UNAUTHORIZED , msg : '' } ) ;
}
2020-04-24 10:29:25 +02:00
let cancellato = false ;
2020-02-05 00:39:25 +01:00
notifBot = tools . NotifyIfDelRecord ( tablename ) ;
2020-04-24 10:29:25 +02:00
let myrec = null ;
2020-02-05 00:39:25 +01:00
2020-04-24 10:29:25 +02:00
if ( ! User . isAdmin ( req . user . perm ) || ! User . isManager ( req . user . perm ) ) {
if ( tablename === 'users' ) {
let fieldsvalue = {
2020-05-14 17:23:17 +02:00
deleted : true ,
date _deleted : new Date ( ) ,
2020-04-24 10:29:25 +02:00
} ;
const rec = await mytable . findByIdAndUpdate ( id , { $set : fieldsvalue } ) ;
myrec = rec ;
cancellato = true ;
2019-10-15 20:40:31 +02:00
}
2020-04-24 10:29:25 +02:00
}
let ris = null ;
if ( ! cancellato ) {
ris = await mytable . findByIdAndRemove ( id ) . then ( ( rec ) => {
if ( ! rec ) {
return res . status ( 404 ) . send ( ) ;
}
myrec = rec ;
cancellato = true ;
2019-10-15 20:40:31 +02:00
2020-04-24 10:29:25 +02:00
tools . mylog ( 'DELETED ' , rec . _id ) ;
} ) . catch ( ( e ) => {
2020-05-04 19:34:41 +02:00
console . log ( e . message ) ;
2020-04-24 10:29:25 +02:00
res . status ( 400 ) . send ( ) ;
} ) ;
}
2019-10-15 20:40:31 +02:00
2020-05-19 00:18:13 +02:00
2020-04-24 10:29:25 +02:00
if ( cancellato ) {
2019-10-20 01:21:54 +02:00
// Do extra things after deleted
2020-05-19 00:18:13 +02:00
return actions . doOtherThingsAfterDeleted ( tablename , myrec , notifBot ) ;
2020-04-24 10:29:25 +02:00
}
2019-10-20 01:21:54 +02:00
2020-04-24 10:29:25 +02:00
res . send ( { code : server _constants . RIS _CODE _ERR , msg : '' } ) ;
return ris ;
2019-10-20 22:45:09 +02:00
2019-10-15 20:40:31 +02:00
} ) ;
2019-10-20 22:45:09 +02:00
router . post ( '/duprec/:table/:id' , authenticate , ( req , res ) => {
const id = req . params . id ;
const tablename = req . params . table ;
// const idapp = req.body.idapp;
console . log ( 'id' , id , 'table' , tablename ) ;
const mytable = getTableByTableName ( tablename ) ;
2020-01-03 22:02:18 +01:00
if ( ! req . user ) {
return res . status ( 404 ) . send ( { code : server _constants . RIS _CODE _ERR _UNAUTHORIZED , msg : '' } ) ;
}
if ( ! User . isAdmin ( req . user . perm ) && ! User . isManager ( req . user . perm ) ) {
2019-10-20 22:45:09 +02:00
// If without permissions, exit
return res . status ( 404 ) . send ( { code : server _constants . RIS _CODE _ERR _UNAUTHORIZED , msg : '' } ) ;
}
2019-10-21 20:38:10 +02:00
return mytable . findById ( id ) . then ( ( mydata ) => {
2019-10-20 22:45:09 +02:00
2019-10-21 20:38:10 +02:00
const datadup = tools . CloneRecordToNew ( mydata ) ;
const mynewrec = new mytable ( datadup ) ;
2019-10-20 22:45:09 +02:00
2019-10-21 20:38:10 +02:00
return mynewrec . save ( )
. then ( ( rec ) => {
if ( ! rec ) {
return res . status ( 404 ) . send ( ) ;
}
2019-10-20 22:45:09 +02:00
2019-10-21 20:38:10 +02:00
tools . mylog ( 'DUPLICATED ' , rec ) ;
2019-10-20 22:45:09 +02:00
2019-10-21 20:38:10 +02:00
// Do extra things after deleted
return actions . doOtherThingsAfterDuplicated ( tablename , rec ) . then ( ( { myrec } ) => {
// ...
mytable . findById ( myrec . _id ) . then ( ( record ) => {
return res . send ( { code : server _constants . RIS _CODE _OK , record , msg : '' } ) ;
} ) ;
} ) ;
} ) . catch ( ( e ) => {
2020-05-04 19:34:41 +02:00
console . error ( e . message ) ;
2019-10-21 20:38:10 +02:00
res . status ( 400 ) . send ( ) ;
} ) ;
2019-10-20 22:45:09 +02:00
} )
} ) ;
2019-10-23 23:47:21 +02:00
router . get ( '/loadsite/:userId/:idapp/:sall' , authenticate _noerror , ( req , res ) => {
const userId = req . params . userId ;
const idapp = req . params . idapp ;
const sall = req . params . sall ;
// var category = req.params.category;
2019-10-24 23:30:45 +02:00
// tools.mylog('loadsite : ', req.params);
2019-10-23 23:47:21 +02:00
let bookedevent = [ ] ;
2019-10-24 23:30:45 +02:00
let msgs = [ ] ;
2019-10-23 23:47:21 +02:00
if ( userId !== '0' ) {
// LOGGED WITH USERID
bookedevent = Booking . findAllByUserIdAndIdApp ( userId , idapp , sall ) ;
}
// Extract all the todos of the userId only
const eventlist = MyEvent . findAllIdApp ( idapp ) ;
const operators = Operator . findAllIdApp ( idapp ) ;
const wheres = Where . findAllIdApp ( idapp ) ;
const contribtype = Contribtype . findAllIdApp ( idapp ) ;
2020-01-03 01:52:49 +01:00
const paymenttype = PaymentType . findAllIdApp ( idapp ) ;
2019-11-12 21:34:03 +01:00
const disciplines = Discipline . findAllIdApp ( idapp ) ;
2019-12-04 02:03:44 +01:00
const settings = Settings . findAllIdApp ( idapp , false ) ;
2019-10-28 16:01:28 +01:00
const permissions = Permission . findAllIdApp ( ) ;
2019-10-23 23:47:21 +02:00
2019-11-21 00:18:40 +01:00
let newstosent = Promise . resolve ( [ ] ) ;
let mailinglist = Promise . resolve ( [ ] ) ;
2019-12-07 00:20:06 +01:00
let mypage = MyPage . findAllIdApp ( idapp ) ;
2020-01-21 01:37:15 +01:00
let calzoom = CalZoom . findAllIdApp ( idapp ) ;
2019-12-27 12:41:39 +01:00
let gallery = Gallery . findAllIdApp ( idapp ) ;
2019-12-07 00:20:06 +01:00
if ( sall ) {
2019-11-21 00:18:40 +01:00
newstosent = Newstosent . findAllIdApp ( idapp ) ;
}
2019-10-27 00:37:10 +02:00
2020-01-20 01:48:25 +01:00
let calcstat = null ;
if ( req . user )
calcstat = User . calculateStat ( idapp , req . user . username ) ;
2020-01-21 01:37:15 +01:00
return Promise . all ( [ bookedevent , eventlist , operators , wheres , contribtype , settings , permissions , disciplines , newstosent , mailinglist , mypage , gallery , paymenttype , calcstat , calzoom ] )
2019-10-23 23:47:21 +02:00
. then ( ( arrdata ) => {
2019-10-24 23:30:45 +02:00
// console.table(arrdata);
2019-12-29 01:53:51 +01:00
const myuser = req . user ;
if ( myuser ) {
myuser . password = '' ;
2020-01-20 01:48:25 +01:00
myuser . _doc . calcstat = arrdata [ 13 ] ;
2019-12-29 01:53:51 +01:00
}
2019-10-24 23:30:45 +02:00
res . send ( {
bookedevent : arrdata [ 0 ] ,
eventlist : arrdata [ 1 ] ,
operators : arrdata [ 2 ] ,
wheres : arrdata [ 3 ] ,
contribtype : arrdata [ 4 ] ,
2019-11-04 20:30:09 +01:00
settings : arrdata [ 5 ] ,
permissions : arrdata [ 6 ] ,
2019-11-12 21:34:03 +01:00
disciplines : arrdata [ 7 ] ,
2019-11-21 00:18:40 +01:00
newstosent : arrdata [ 8 ] ,
mailinglist : arrdata [ 9 ] ,
2019-12-07 00:20:06 +01:00
mypage : arrdata [ 10 ] ,
2019-12-27 12:41:39 +01:00
gallery : arrdata [ 11 ] ,
2020-01-03 01:52:49 +01:00
paymenttypes : arrdata [ 12 ] ,
2020-01-21 01:37:15 +01:00
calzoom : arrdata [ 14 ] ,
2019-12-29 01:53:51 +01:00
myuser ,
2019-10-24 23:30:45 +02:00
} ) ;
2019-10-23 23:47:21 +02:00
} )
. catch ( ( e ) => {
2020-05-04 19:34:41 +02:00
console . log ( e . message ) ;
2019-10-23 23:47:21 +02:00
res . status ( 400 ) . send ( e ) ;
} ) ;
} ) ;
2020-04-24 10:29:25 +02:00
router . get ( process . env . LINK _CHECK _UPDATES , authenticate , async ( req , res ) => {
2019-10-24 23:30:45 +02:00
const userId = req . user . _id ;
2020-04-24 10:29:25 +02:00
const idapp = req . query . idapp ;
2019-10-24 23:30:45 +02:00
// console.log("POST " + process.env.LINK_CHECK_UPDATES + " userId=" + userId);
if ( ! ObjectID . isValid ( userId ) ) {
return res . status ( 404 ) . send ( ) ;
}
2020-04-24 10:29:25 +02:00
await cfgserver . find ( { idapp } ) . then ( ( arrcfgrec ) => {
2019-10-24 23:30:45 +02:00
if ( ! arrcfgrec )
return res . status ( 404 ) . send ( ) ;
// ++Todo: Add to Log Stat ....
// const sall = '0';
2019-10-25 19:08:38 +02:00
// msgs = SendMsg.findAllByUserIdAndIdApp(userId, req.user.username, req.user.idapp);
2019-10-26 02:21:22 +02:00
last _msgs = SendMsg . findLastGroupByUserIdAndIdApp ( userId , req . user . username , req . user . idapp ) ;
2019-10-25 19:08:38 +02:00
2019-10-24 23:30:45 +02:00
let usersList = null ;
if ( req . user ) {
// If User is Admin, then send user Lists
2020-01-03 22:02:18 +01:00
if ( User . isAdmin ( req . user . perm ) ) {
2019-10-24 23:30:45 +02:00
// Send UsersList
2020-03-10 21:44:14 +01:00
// usersList = User.getUsersList(req.user.idapp)
usersList = null ;
2019-10-24 23:30:45 +02:00
}
}
2019-10-26 02:21:22 +02:00
return Promise . all ( [ usersList , last _msgs ] )
2019-10-24 23:30:45 +02:00
. then ( ( arrdata ) => {
// console.table(arrdata);
return res . send ( {
cfgServer : arrcfgrec ,
usersList : arrdata [ 0 ] ,
2019-10-26 02:21:22 +02:00
last _msgs : arrdata [ 1 ] ,
2019-10-24 23:30:45 +02:00
} ) ;
} ) ;
} ) . catch ( ( e ) => {
2020-05-04 19:34:41 +02:00
console . log ( e . message ) ;
2019-10-24 23:30:45 +02:00
res . status ( 400 ) . send ( { code : server _constants . RIS _CODE _ERR , msg : e } ) ;
} ) ;
} ) ;
2019-12-27 12:41:39 +01:00
router . post ( '/upload_from_other_server/:dir' , authenticate , ( req , res ) => {
2019-12-07 00:20:06 +01:00
const dir = req . params . dir ;
const idapp = req . user . idapp ;
2019-12-28 14:30:30 +01:00
/ *
const form = new formidable . IncomingForm ( ) ;
2019-12-07 00:20:06 +01:00
2019-12-28 14:30:30 +01:00
form . parse ( req ) ;
2019-12-07 00:20:06 +01:00
2019-12-28 14:30:30 +01:00
const client = new ftp ( process . env . FTPSERVER _HOST , process . env . FTPSERVER _PORT , process . env . FTPSERVER _USER + idapp + '@associazioneshen.it' , process . env . FTPSERVER _PWD + idapp , false , 134217728 ) ;
2019-12-07 00:20:06 +01:00
2019-12-28 14:30:30 +01:00
// SSL_OP_NO_TLSv1_2 = 134217728
2019-12-07 00:20:06 +01:00
2019-12-28 14:30:30 +01:00
// console.log('client', client);
2019-12-07 00:20:06 +01:00
2019-12-28 14:30:30 +01:00
form . uploadDir = folder + '/' + dir ;
try {
2019-12-07 00:20:06 +01:00
2019-12-28 14:30:30 +01:00
form . on ( 'fileBegin' , async function ( name , file ) {
file . path = folder + '/' + file . name ;
} ) ;
2019-12-07 00:20:06 +01:00
2019-12-28 14:30:30 +01:00
form . on ( 'file' , async function ( name , file ) {
try {
// Create directory remote
if ( ! ! dir )
await client . createDir ( dir ) ;
2020-02-05 00:39:25 +01:00
const miofile = ( dir ) ? dir + ` / ` + file . name : file . name ;
2019-12-28 14:30:30 +01:00
console . log ( 'Upload...' ) ;
const ret = await client . upload ( file . path , miofile , 755 ) ;
console . log ( 'Uploaded ' + file . name , 'status:' , ret ) ;
if ( ! ret )
res . status ( 400 ) . send ( ) ;
else {
// Delete file from local directory
fs . unlinkSync ( file . path ) ;
res . end ( ) ;
}
} catch ( e ) {
console . log ( 'error' , e ) ;
2019-12-07 00:20:06 +01:00
res . status ( 400 ) . send ( ) ;
2019-12-27 12:41:39 +01:00
}
2019-12-28 14:30:30 +01:00
} ) ;
2019-12-07 00:20:06 +01:00
2019-12-28 14:30:30 +01:00
form . on ( 'aborted' , ( ) => {
console . error ( 'Request aborted by the user' ) ;
res . status ( 400 ) . send ( ) ;
} ) ;
2019-12-07 00:20:06 +01:00
2019-12-28 14:30:30 +01:00
form . on ( 'error' , ( err ) => {
console . error ( 'Error Uploading' , err ) ;
res . status ( 400 ) . send ( ) ;
} ) ;
2019-12-07 00:20:06 +01:00
2019-12-28 14:30:30 +01:00
} catch ( e ) {
console . log ( 'Error' , e )
}
* /
2019-12-27 12:41:39 +01:00
} ) ;
router . post ( '/upload/:dir' , authenticate , ( req , res ) => {
const dir = req . params . dir ;
const idapp = req . user . idapp ;
2019-12-28 14:30:30 +01:00
// console.log('/upload dir:' + dir);
2019-12-27 12:41:39 +01:00
const form = new formidable . IncomingForm ( ) ;
form . parse ( req ) ;
form . uploadDir = folder + '/' + dir ;
try {
2019-12-28 14:30:30 +01:00
form . on ( 'fileBegin' , async function ( name , file ) {
2019-12-27 12:41:39 +01:00
file . path = folder + '/' + file . name ;
} ) ;
2019-12-28 14:30:30 +01:00
form . on ( 'file' , async function ( name , file ) {
2019-12-27 12:41:39 +01:00
try {
console . log ( 'Uploaded ' + file . name ) ;
2019-12-28 14:30:30 +01:00
const mydir = tools . getdirByIdApp ( idapp ) + '/statics/upload/' + dir ;
// Create Dir if doesn't exist:
tools . mkdirpath ( mydir ) ;
newname = mydir + '/' + file . name ;
console . log ( 'move from ' , file . path , 'to :' , newname ) ;
2019-12-07 00:20:06 +01:00
2019-12-27 12:41:39 +01:00
// For local: ... resolve this... sending through the static folder...
// res.sendFile(path.resolve(file.name));
2019-12-28 14:30:30 +01:00
// Move in the folder application !
2019-12-27 12:41:39 +01:00
tools . move ( file . path , newname , ( err ) => {
2020-02-02 04:06:32 +01:00
if ( err )
console . log ( 'err:' , err ) ;
2019-12-27 12:41:39 +01:00
res . end ( ) ;
} ) ;
2019-12-28 14:30:30 +01:00
} catch ( e ) {
2019-12-27 12:41:39 +01:00
console . log ( 'error' , e ) ;
res . status ( 400 ) . send ( ) ;
}
} ) ;
form . on ( 'aborted' , ( ) => {
console . error ( 'Request aborted by the user' ) ;
res . status ( 400 ) . send ( ) ;
} ) ;
form . on ( 'error' , ( err ) => {
console . error ( 'Error Uploading' , err ) ;
res . status ( 400 ) . send ( ) ;
} ) ;
} catch ( e ) {
console . log ( 'Error' , e )
}
2019-12-07 00:20:06 +01:00
} ) ;
2019-10-15 20:40:31 +02:00
2019-12-28 02:16:29 +01:00
router . delete ( '/delfile' , authenticate , ( req , res ) => {
const relativefile = req . query . filename ;
const idapp = req . user . idapp ;
try {
try {
console . log ( 'Delete file ' + relativefile ) ;
// ++ Move in the folder application !
fullpathfile = tools . getdirByIdApp ( idapp ) + '/' + relativefile ;
tools . delete ( fullpathfile , ( err ) => {
if ( err ) console . log ( 'err' , err ) ;
if ( err === undefined || err . errno === - 2 )
res . send ( { code : server _constants . RIS _CODE _OK , msg : '' } ) ;
} ) ;
2019-12-28 14:30:30 +01:00
} catch ( e ) {
2019-12-28 02:16:29 +01:00
console . log ( 'error' , e ) ;
res . status ( 400 ) . send ( ) ;
}
} catch ( e ) {
console . log ( 'Error' , e )
}
} ) ;
2019-02-05 03:40:22 +01:00
module . exports = router ;