2019-02-05 03:40:22 +01:00
const express = require ( 'express' ) ;
2019-12-07 00:20:06 +01:00
const router = express . Router ( ) ,
2022-12-11 02:57:35 +01:00
fs = require ( 'fs' ) ,
path = require ( 'path' ) ;
2019-12-07 00:20:06 +01:00
2020-01-30 01:19:25 +01:00
const telegrambot = require ( '../telegram/telegrambot' ) ;
2023-02-23 16:07:43 +01:00
const i18n = require ( 'i18n' ) ;
2022-02-16 19:21:12 +01:00
const sharp = require ( 'sharp' ) ;
2022-12-11 02:57:35 +01:00
const { authenticate , authenticate _noerror } = require (
'../middleware/authenticate' ) ;
2019-02-22 10:23:39 +01:00
2022-12-11 02:57:35 +01:00
const { ObjectID } = require ( 'mongodb' ) ;
2022-02-03 00:33:15 +01:00
// const {ListaIngresso} = require('../models/listaingresso');
2022-12-11 02:57:35 +01:00
const { Graduatoria } = require ( '../models/graduatoria' ) ;
2019-02-22 10:23:39 +01:00
2022-09-14 11:32:04 +02:00
const mongoose = require ( 'mongoose' ) . set ( 'debug' , false ) ;
2022-12-11 02:57:35 +01:00
const { CfgServer } = require ( '../models/cfgserver' ) ;
2019-02-22 10:23:39 +01:00
2022-03-09 14:53:09 +01:00
// const uuidv4 = require('uuid/v4'); // I chose v4 ‒ you can select others
2022-01-23 23:25:34 +01:00
2022-03-09 14:53:09 +01:00
// const ftp = require('../ftp/FTPClient');
const formidable = require ( 'formidable' ) ;
const folder = path . join ( _ _dirname , 'upload' ) ;
2019-12-07 00:20:06 +01:00
2024-05-04 14:49:02 +02:00
const sanitizeHtml = require ( 'sanitize-html' ) ;
2019-12-07 00:20:06 +01:00
if ( ! fs . existsSync ( folder ) ) {
2021-11-22 18:29:22 +01:00
fs . mkdirSync ( folder ) ;
2019-12-07 00:20:06 +01:00
}
2019-02-06 18:48:32 +01:00
const _ = require ( 'lodash' ) ;
2022-12-11 02:57:35 +01:00
const { User } = require ( '../models/user' ) ;
const { MyGroup } = require ( '../models/mygroup' ) ;
const { Circuit } = require ( '../models/circuit' ) ;
const { Booking } = require ( '../models/booking' ) ;
const { Operator } = require ( '../models/operator' ) ;
const { Where } = require ( '../models/where' ) ;
const { MyEvent } = require ( '../models/myevent' ) ;
const { Contribtype } = require ( '../models/contribtype' ) ;
const { PaymentType } = require ( '../models/paymenttype' ) ;
const { Discipline } = require ( '../models/discipline' ) ;
const { MyElem } = require ( '../models/myelem' ) ;
const { Skill } = require ( '../models/skill' ) ;
const { Good } = require ( '../models/good' ) ;
const { StatusSkill } = require ( '../models/statusSkill' ) ;
const { Province } = require ( '../models/province' ) ;
const { Sector } = require ( '../models/sector' ) ;
const { SectorGood } = require ( '../models/sectorgood' ) ;
const { CatGrp } = require ( '../models/catgrp' ) ;
2022-05-05 00:38:41 +02:00
const Site = require ( '../models/site' ) ;
2022-12-11 02:57:35 +01:00
const { Level } = require ( '../models/level' ) ;
const { AdType } = require ( '../models/adtype' ) ;
const { AdTypeGood } = require ( '../models/adtypegood' ) ;
const { Newstosent } = require ( '../models/newstosent' ) ;
const { MyPage } = require ( '../models/mypage' ) ;
const { CalZoom } = require ( '../models/calzoom' ) ;
const { Gallery } = require ( '../models/gallery' ) ;
const { Settings } = require ( '../models/settings' ) ;
const { SendMsg } = require ( '../models/sendmsg' ) ;
const { SendNotif } = require ( '../models/sendnotif' ) ;
const { Permission } = require ( '../models/permission' ) ;
2020-12-25 03:54:16 +01:00
const Producer = require ( '../models/producer' ) ;
const Cart = require ( '../models/cart' ) ;
2021-01-18 00:48:17 +01:00
const OrdersCart = require ( '../models/orderscart' ) ;
2020-12-25 03:54:16 +01:00
const Storehouse = require ( '../models/storehouse' ) ;
2023-12-15 00:57:14 +01:00
const Provider = require ( '../models/provider' ) ;
2023-12-27 02:58:15 +01:00
const CatProd = require ( '../models/catprod' ) ;
2024-01-30 14:00:37 +01:00
const CatAI = require ( '../models/catai' ) ;
2024-01-12 13:02:59 +01:00
const SubCatProd = require ( '../models/subcatprod' ) ;
2023-12-21 02:23:52 +01:00
const Gasordine = require ( '../models/gasordine' ) ;
2024-01-02 15:24:44 +01:00
const Product = require ( '../models/product' ) ;
2024-05-09 23:36:46 +02:00
const Author = require ( '../models/author' ) ;
2024-06-21 16:11:03 +02:00
const Publisher = require ( '../models/publisher' ) ;
2023-12-28 23:48:03 +01:00
const ProductInfo = require ( '../models/productInfo' ) ;
2023-12-16 00:51:03 +01:00
const Scontistica = require ( '../models/scontistica' ) ;
2021-02-03 01:33:30 +01:00
const Department = require ( '../models/department' ) ;
2023-11-28 14:20:22 +01:00
const { Category } = require ( '../models/category' ) ;
2021-02-03 01:33:30 +01:00
const Group = require ( '../models/group' ) ;
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' ) ;
2022-02-16 19:21:12 +01:00
const globalTables = require ( '../tools/globalTables' ) ;
2021-02-18 12:19:35 +01:00
const UserCost = {
2022-03-02 20:18:29 +01:00
FIELDS _UPDATE _TELEGRAM _BOT : [
'profile.teleg_id' ,
'profile.manage_telegram' ,
2023-12-29 15:19:15 +01:00
'profile.admin_telegram' ,
2022-03-02 20:18:29 +01:00
'deleted' ,
2022-08-04 17:30:57 +02:00
'reported' ,
2022-03-02 20:18:29 +01:00
] ,
2021-11-22 18:29:22 +01:00
FIELDS _REQUISITI : [
'verified_email' ,
2020-03-10 21:44:14 +01:00
'profile.teleg_id' ,
'profile.saw_and_accepted' ,
2020-09-04 00:06:49 +02:00
'profile.revolut' ,
'profile.payeer_id' ,
'profile.advcash_id' ,
'profile.link_payment' ,
2020-03-10 21:44:14 +01:00
'profile.email_paypal' ,
2021-11-22 18:29:22 +01:00
'profile.paymenttypes' ] ,
2020-03-10 21:44:14 +01:00
} ;
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 ;
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 ,
2021-11-22 18:29:22 +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 ,
2021-11-22 18:29:22 +01:00
msg : tools . getres _ _ ( 'EMAIL' , res ) + ' ' +
2022-12-11 02:57:35 +01:00
tools . getres _ _ ( 'VERIF' , res ) ,
2020-03-10 21:44:14 +01:00
} ) ;
2019-02-05 03:40:22 +01:00
} ) ;
}
}
} ) . catch ( ( e ) => {
2022-02-22 15:24:32 +01:00
console . log ( process . env . LINKVERIF _REG , e . message ) ;
2019-02-05 03:40:22 +01:00
res . status ( 400 ) . send ( ) ;
} ) ;
} ) ;
2023-12-08 14:07:32 +01:00
router . post ( process . env . ADD _NEW _SITE , async ( req , res ) => {
try {
const body = req . body ;
const idapp = body . idappSent ;
const name = body . name ;
const email = body . email . toLowerCase ( ) . trim ( ) ;
console . log ( 'Add New Site: idapp = ' , idapp , 'email=' , email , 'name=' , name ) ;
const ris = await User . addNewSite ( idapp , body ) ;
2024-03-21 21:22:57 +01:00
2023-12-08 14:07:32 +01:00
if ( ris ) {
res . send ( ris ) ;
} else {
res . status ( 400 ) . send ( { code : server _constants . RIS _CODE _ERR , msg : 'Errore' } ) ;
}
} catch ( e ) {
console . log ( process . env . ADD _NEW _SITE , e . message ) ;
res . status ( 400 ) . send ( { code : server _constants . RIS _CODE _ERR , msg : e } ) ;
}
2024-03-21 21:22:57 +01:00
2023-12-08 14:07:32 +01:00
} ) ;
2019-02-05 03:40:22 +01:00
// Faccio richiesta di una Nuova Password
2022-03-08 01:01:20 +01:00
router . post ( process . env . LINK _REQUEST _NEWPASSWORD , async ( req , res ) => {
2019-02-05 03:40:22 +01:00
2022-03-08 01:01:20 +01:00
try {
2022-12-11 18:04:02 +01:00
const body = _ . pick ( req . body , [ 'idapp' , 'email' , 'codetocheck' ] ) ;
const idapp = body . idapp ;
const email = body . email . toLowerCase ( ) . trim ( ) ;
const codetocheck = body . codetocheck ? body . codetocheck . trim ( ) : '' ;
2023-01-29 22:22:48 +01:00
console . log ( 'Request Reset Pwd:' , email , ' idapp=' , idapp ) ;
2022-12-11 18:04:02 +01:00
// Check if too many requests
if ( await User . tooManyReqPassword ( idapp , email , true ) ) {
2024-03-21 21:22:57 +01:00
let text = 'Troppe richieste di Password: ' + email ;
2024-03-28 20:25:48 +01:00
telegrambot . sendMsgTelegramToTheManagers ( idapp , text ) ;
2024-03-21 21:22:57 +01:00
console . log ( process . env . LINK _REQUEST _NEWPASSWORD , text , email ) ;
res . status ( 400 ) . send ( { code : server _constants . RIS _CODE _ERR , msg : text } ) ;
2022-12-11 18:04:02 +01:00
return false ;
}
console . log (
'POST ' + process . env . LINK _REQUEST _NEWPASSWORD + ' idapp= ' + idapp +
' email = ' + email ) ;
const reqpwd = await User . createNewRequestPwd ( idapp , email , codetocheck ) ;
if ( reqpwd && reqpwd . ris ) {
res . send ( { code : server _constants . RIS _CODE _OK , msg : '' , link : reqpwd . link } ) ;
2022-03-08 01:01:20 +01:00
} else {
2021-11-22 18:29:22 +01:00
return res . status ( 200 ) .
2022-12-11 02:57:35 +01:00
send ( { code : server _constants . RIS _CODE _EMAIL _NOT _EXIST , msg : '' } ) ;
2019-02-05 03:40:22 +01:00
}
2022-07-10 01:25:19 +02:00
} catch ( e ) {
2022-02-22 15:24:32 +01:00
console . log ( process . env . LINK _REQUEST _NEWPASSWORD , e . message ) ;
2022-12-11 18:04:02 +01:00
res . status ( 400 ) . send ( { code : server _constants . RIS _CODE _ERR , msg : e } ) ;
2022-03-08 01:01:20 +01:00
}
2019-02-05 03:40:22 +01:00
} ) ;
// Invio la Nuova Password richiesta dal reset!
// Ritorna il token per poter effettuare le chiamate...
2022-12-11 02:57:35 +01:00
router . post ( process . env . LINK _UPDATE _PWD , async ( req , res ) => {
const body = _ . pick ( req . body , [ 'idapp' , 'email' , 'tokenforgot' , 'tokenforgot_code' , 'password' ] ) ;
2020-01-30 01:19:25 +01:00
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 ;
2022-12-11 02:57:35 +01:00
const tokenforgot _code = body . tokenforgot _code ;
2020-01-30 01:19:25 +01:00
const password = body . password ;
2021-11-22 18:29:22 +01:00
const msg = 'Richiesta Nuova Password: idapp= ' + idapp + ' email = ' + email ;
2020-01-30 01:19:25 +01:00
console . log ( msg ) ;
2020-02-19 16:09:16 +01:00
// telegrambot.sendMsgTelegramToTheManagers(body.idapp, msg);
2019-02-05 03:40:22 +01:00
2022-12-11 02:57:35 +01:00
let user = null ;
2019-02-05 03:40:22 +01:00
2022-12-11 02:57:35 +01:00
user = await User . findByLinkTokenforgot ( idapp , email , tokenforgot )
. then ( ( user ) => {
return user ;
} ) . catch ( ( e ) => {
console . log ( process . env . LINK _UPDATE _PWD , e . message ) ;
res . status ( 400 ) . send ( ) ;
} ) ;
2019-02-05 03:40:22 +01:00
2022-12-11 02:57:35 +01:00
if ( ! user ) {
user = await User . findByLinkTokenforgotCode ( idapp , email , tokenforgot _code )
. then ( ( user ) => {
return user ;
} ) . catch ( ( e ) => {
console . log ( process . env . LINK _UPDATE _PWD , e . message ) ;
res . status ( 400 ) . send ( ) ;
2021-11-22 18:29:22 +01:00
} ) ;
2022-12-11 02:57:35 +01:00
}
2019-02-05 03:40:22 +01:00
2022-12-11 02:57:35 +01:00
if ( ! user ) {
return res . send (
{ code : server _constants . RIS _CODE _TOKEN _RESETPASSWORD _NOT _FOUND } ) ;
} else {
// aggiorna la nuova password
user . password = password ;
user . lasttimeonline = new Date ( ) ;
// Crea token
2024-04-09 21:56:50 +02:00
user . generateAuthToken ( req ) . then ( ris => {
2022-12-11 02:57:35 +01:00
user . tokenforgot = '' ; // Svuota il tokenforgot perché non ti servirà più...
user . tokenforgot _code = '' ; // Svuota il tokenforgot perché non ti servirà più...
// Salva lo User
user . save ( ) . then ( ( ) => {
2024-04-09 21:56:50 +02:00
res . header ( 'x-auth' , ris . token )
2024-04-11 11:43:19 +02:00
. header ( 'x-refrtok' , ris . refreshToken )
2024-04-09 21:56:50 +02:00
. send ( { code : server _constants . RIS _CODE _OK } ) ; // Ritorna il token di ritorno
2022-12-11 02:57:35 +01:00
} ) ;
} ) ;
}
2019-02-05 03:40:22 +01:00
} ) ;
2022-09-11 11:45:33 +02:00
router . post ( '/testServer' , authenticate _noerror , async ( req , res ) => {
2022-02-16 19:21:12 +01:00
try {
const test = req . body . test ;
2022-12-11 02:57:35 +01:00
let ris = { test } ;
2022-02-16 19:21:12 +01:00
2022-02-22 18:27:59 +01:00
if ( req . user ) {
2022-09-11 11:45:33 +02:00
await tools . sendNotificationToUser ( req . user . _id , 'Test Server' ,
2022-12-11 02:57:35 +01:00
'Test Server OK' ,
'/' , '' , 'server' , [ ] ) ;
2022-02-22 18:27:59 +01:00
}
2022-02-16 19:21:12 +01:00
return res . send ( ris ) ;
} catch ( e ) {
2022-02-22 15:24:32 +01:00
console . error ( 'testServer' , e . message ) ;
2022-02-16 19:21:12 +01:00
return res . status ( 400 ) . send ( e ) ;
}
} ) ;
2019-10-15 20:40:31 +02:00
2022-09-16 17:38:49 +02:00
router . get ( '/test1' , authenticate _noerror , async ( req , res ) => {
try {
const test = req . query . test ;
2022-12-11 02:57:35 +01:00
let ris = { test } ;
2022-09-16 17:38:49 +02:00
if ( req . user ) {
await tools . sendNotificationToUser ( req . user . _id , 'Test Server' ,
2022-12-11 02:57:35 +01:00
'Test Server OK' ,
'/' , '' , 'server' , [ ] ) ;
2022-09-16 17:38:49 +02:00
}
return res . send ( ris ) ;
} catch ( e ) {
console . error ( 'testServer' , e . message ) ;
return res . status ( 400 ) . send ( e ) ;
}
} ) ;
2022-02-28 17:20:47 +01:00
router . post ( '/settable' , authenticate , async ( req , res ) => {
2019-10-20 01:21:54 +02:00
const params = req . body ;
2022-02-16 19:21:12 +01:00
const mytable = globalTables . getTableByTableName ( params . table ) ;
2024-03-21 21:22:57 +01:00
2022-09-03 13:06:58 +02:00
let mydata = req . body . data ;
let extrarec = { } ;
2022-09-11 11:45:33 +02:00
if ( mydata && mydata . hasOwnProperty ( 'extrarec' ) ) {
2022-09-03 13:06:58 +02:00
extrarec = mydata [ 'extrarec' ] ;
delete mydata [ 'extrarec' ] ;
}
2019-10-20 01:21:54 +02:00
2023-10-21 15:27:53 +02:00
if ( mydata === undefined ) {
console . error ( 'MYDATA VUOTO !' ) ;
return res . status ( 400 ) . send ( 'Mydata VUOTO' ) ;
}
2022-12-11 02:57:35 +01:00
const fieldsvalue = { 'ALL' : 1 } ;
2022-01-16 23:21:03 +01:00
2019-10-20 01:21:54 +02:00
mydata . idapp = req . user . idapp ;
2022-03-30 22:51:03 +02:00
const idapp = mydata . idapp ;
2019-10-20 01:21:54 +02:00
2022-12-12 18:25:13 +01:00
if ( req . user && req . user . username ) {
User . setOnLine ( req . user . idapp , req . user . username ) ;
}
2022-12-23 00:36:35 +01:00
2022-01-28 00:57:39 +01:00
let consentito = false ;
2021-12-23 14:13:40 +01:00
2022-01-28 00:57:39 +01:00
try {
if ( User . isAdmin ( req . user . perm ) || User . isManager ( req . user . perm ) ||
2022-12-11 02:57:35 +01:00
User . isEditor ( req . user . perm ) || User . isFacilitatore ( req . user . perm ) ) {
2022-01-28 00:57:39 +01:00
consentito = true ;
}
2021-10-08 00:38:35 +02:00
2024-04-04 18:43:17 +02:00
if ( ( ! User . isAdmin ( req . user . perm )
&& ! User . isManager ( req . user . perm )
&& ! User . isEditor ( req . user . perm )
&& ! User . isFacilitatore ( req . user . perm ) )
&&
2024-04-09 21:56:50 +02:00
await ! tools . ModificheConsentite ( req , params . table , fieldsvalue , mydata ? mydata . _id : '' ) ) {
2022-01-28 00:57:39 +01:00
// If without permissions, exit
return res . status ( 404 ) .
2022-12-11 02:57:35 +01:00
send ( { code : server _constants . RIS _CODE _ERR _UNAUTHORIZED , msg : '' } ) ;
2022-01-28 00:57:39 +01:00
}
2021-12-02 10:13:27 +01:00
2022-01-28 00:57:39 +01:00
if ( shared _consts . TABLES _USER _ID . includes ( params . table ) ) {
2022-02-05 23:28:15 +01:00
if ( ! mydata . userId )
mydata . userId = req . user . _id ;
2022-01-28 00:57:39 +01:00
}
2021-10-08 00:38:35 +02:00
2022-08-04 17:30:57 +02:00
if ( shared _consts . TABLES _CREATEDBY . includes ( params . table ) ) {
if ( ! mydata . createdBy )
mydata . createdBy = req . user . username ;
}
2022-08-15 15:10:27 +02:00
if ( shared _consts . TABLES _UPDATE _LASTMODIFIED . includes ( params . table ) ) {
mydata . date _updated = new Date ( ) ;
}
2022-01-28 00:57:39 +01:00
if ( shared _consts . TABLES _PERM _NEWREC . includes ( params . table ) ) {
if ( ! consentito ) {
mydata . verifyrec = false ;
}
}
2021-10-08 00:38:35 +02:00
2022-02-05 23:28:15 +01:00
if ( params . table === shared _consts . TAB _MYGROUPS ) {
if ( shared _consts . MYGROUPS _KEY _TO _CRYPTED in mydata ) {
if ( mydata [ shared _consts . MYGROUPS _KEY _TO _CRYPTED ] ) {
2022-02-16 09:40:16 +01:00
mydata [ shared _consts . MYGROUPS _KEY _TO _CRYPTED +
2022-12-11 02:57:35 +01:00
shared _consts . SUFFIX _CRYPTED ] = tools . cryptdata (
2022-02-16 09:40:16 +01:00
mydata [ shared _consts . MYGROUPS _KEY _TO _CRYPTED ] ) ;
2022-02-05 23:28:15 +01:00
}
}
}
2022-02-03 00:33:15 +01:00
if ( shared _consts . TABLES _USER _INCLUDE _MY . includes ( params . table ) ) {
2022-09-02 02:25:38 +02:00
if ( ! mydata . admins || mydata . admins . length <= 0 ) {
2022-02-28 00:07:47 +01:00
// Aggiungi solo se non esistono Admin:
2022-02-03 00:33:15 +01:00
mydata . admins = [ ] ;
2022-02-28 00:07:47 +01:00
const indfind = mydata . admins . findIndex (
2022-12-11 02:57:35 +01:00
( rec ) => ( rec . username === req . user . username ) ) ;
2022-02-03 00:33:15 +01:00
2022-02-28 00:07:47 +01:00
if ( indfind < 0 ) {
2022-12-11 02:57:35 +01:00
mydata . admins . push ( { username : req . user . username } ) ;
2022-02-28 00:07:47 +01:00
}
2022-02-03 00:33:15 +01:00
}
}
2022-01-28 00:57:39 +01:00
delete mydata [ '__v' ] ;
delete mydata [ '__proto__' ] ;
let mytablerec = new mytable ( mydata ) ;
// console.log('mytablerec', mytablerec);
2021-12-02 10:13:27 +01:00
2022-02-16 19:21:12 +01:00
const mytablestrutt = globalTables . getTableByTableName ( params . table ) ;
2021-12-21 01:27:45 +01:00
2023-11-24 17:52:17 +01:00
if ( mydata [ '_id' ] !== undefined && mydata [ '_id' ] !== 0 && mydata [ '_id' ] !== '' ) {
2022-01-28 00:57:39 +01:00
mytablerec . isNew = false ;
2021-12-21 01:27:45 +01:00
}
2022-09-14 11:32:04 +02:00
if ( shared _consts . TABLES _ID _NUMBER . includes ( params . table ) ||
2022-12-11 02:57:35 +01:00
shared _consts . TABLES _ID _STRING . includes ( params . table ) ) {
2022-09-03 13:06:58 +02:00
2022-01-28 00:57:39 +01:00
} else if ( params . table === 'hours' ) {
2021-10-08 00:38:35 +02:00
2021-11-22 18:29:22 +01:00
} else {
2023-06-01 11:39:53 +02:00
if ( ( mydata [ '_id' ] === undefined || mydata [ '_id' ] === '' || ( mytablerec . isNew && mydata [ '_id' ] === 0 ) ) && ( mytablerec . _id === undefined || mytablerec . _id === '0' ) ) {
2022-09-19 19:40:30 +02:00
mytablerec . _id = new ObjectID ( ) ;
2023-03-05 22:04:14 +01:00
mydata . _id = new ObjectID ( ) ;
2023-11-28 14:20:22 +01:00
mytablerec . isNew = true ;
2022-01-28 00:57:39 +01:00
}
2021-11-22 18:29:22 +01:00
}
2022-01-28 00:57:39 +01:00
2022-02-28 17:20:47 +01:00
const isnewrec = mytablerec . isNew ;
if ( params . table === shared _consts . TAB _MYGROUPS && isnewrec ) {
// Controlla se esiste già con lo stesso nome
2022-12-11 02:57:35 +01:00
let alreadyexist = await MyGroup . findOne ( { idapp , groupname : mydata . groupname } ) ;
2022-02-28 17:20:47 +01:00
if ( alreadyexist ) {
2022-12-11 02:57:35 +01:00
return res . send ( { code : server _constants . RIS _CODE _REC _ALREADY _EXIST _CODE } ) ;
2022-02-28 17:20:47 +01:00
}
2022-12-11 02:57:35 +01:00
alreadyexist = await MyGroup . findOne ( { idapp , title : mydata . title } ) ;
2022-02-28 17:20:47 +01:00
if ( alreadyexist ) {
2022-12-11 02:57:35 +01:00
return res . send ( { code : server _constants . RIS _CODE _REC _ALREADY _EXIST _NAME } ) ;
2022-02-28 17:20:47 +01:00
}
2022-09-02 02:25:38 +02:00
} else if ( params . table === shared _consts . TAB _MYCIRCUITS && isnewrec ) {
// Controlla se esiste già con lo stesso nome
2022-12-11 02:57:35 +01:00
let alreadyexist = await Circuit . findOne ( { idapp , name : mydata . name } ) ;
2022-09-02 02:25:38 +02:00
if ( alreadyexist ) {
2022-12-11 02:57:35 +01:00
return res . send ( { code : server _constants . RIS _CODE _REC _ALREADY _EXIST _CODE } ) ;
2022-09-02 02:25:38 +02:00
}
2022-12-11 02:57:35 +01:00
alreadyexist = await Circuit . findOne ( { idapp , path : mydata . path } ) ;
2022-09-02 02:25:38 +02:00
if ( alreadyexist ) {
2022-12-11 02:57:35 +01:00
return res . send ( { code : server _constants . RIS _CODE _REC _ALREADY _EXIST _NAME } ) ;
2022-09-02 02:25:38 +02:00
}
2022-09-14 17:37:29 +02:00
/ * a l r e a d y e x i s t = a w a i t C i r c u i t . f i n d O n e ( { i d a p p , s y m b o l : m y d a t a . s y m b o l } ) ;
2022-09-12 18:37:08 +02:00
if ( alreadyexist ) {
return res . send ( { code : server _constants . RIS _CODE _REC _ALREADY _EXIST _SYMBOL } ) ;
}
2022-09-14 17:37:29 +02:00
* /
2022-02-28 17:20:47 +01:00
}
2022-03-06 00:48:33 +01:00
if ( shared _consts . TABLES _UPDATE _LASTMODIFIED . includes ( params . table ) ) {
mytablerec . date _updated = new Date ( ) ;
2023-03-05 22:04:14 +01:00
mydata . date _updated = new Date ( ) ;
2022-03-06 00:48:33 +01:00
}
2022-09-18 20:17:24 +02:00
// console.log('mydata',mydata);
2023-03-05 22:04:14 +01:00
// return await mytablerec.save().
// then(async (rec) => {
2022-09-14 11:32:04 +02:00
2023-03-05 22:04:14 +01:00
const myPromise = new Promise ( ( resolve , reject ) => {
resolve ( mytablerec . _id && mytable . findById ( mytablerec . _id ) )
} ) ;
2023-06-19 00:47:13 +02:00
// Controlla se esiste già questo record:
if ( shared _consts . TABLES _FIELDS _DESCR _AND _CITY _AND _USER . includes ( params . table ) ) {
if ( mytablerec . isNew ) {
2023-06-20 01:07:57 +02:00
const trovatoDuplicato = await mytable . findOne ( { idapp : mytablerec . idapp , descr : mytablerec . descr , idCity : mytablerec . idCity , userId : mytablerec . userId } ) . lean ( ) ;
2023-06-19 00:47:13 +02:00
if ( trovatoDuplicato ) {
// trovatoDuplicato
return res . status ( 200 ) . send ( { code : server _constants . RIS _CODE _REC _DUPLICATED _DESCR _CITY _USER , msg : '' } ) ;
2023-06-20 01:07:57 +02:00
2023-06-19 00:47:13 +02:00
}
}
}
2023-03-05 22:04:14 +01:00
return await myPromise
. then ( async ( doupdate ) => {
2023-10-01 01:24:47 +02:00
2023-03-05 22:04:14 +01:00
if ( doupdate )
return mytable . updateOne ( { _id : mytablerec . _id } , mydata , { new : true } )
else
return mytablerec . save ( )
} )
. then ( async ( risult ) => {
let rec = null ;
if ( risult && risult . ok === 1 ) {
rec = await mytable . findById ( mytablerec . _id ) . lean ( ) ;
} else {
rec = risult ;
}
2022-01-28 00:57:39 +01:00
2022-12-11 02:57:35 +01:00
if ( shared _consts . TABLES _GETCOMPLETEREC . includes ( params . table ) ) {
return await mytablestrutt . getCompleteRecord ( rec . idapp , rec . _id ) ;
} else {
return rec ;
}
2022-07-26 15:46:39 +02:00
2022-12-11 02:57:35 +01:00
// tools.mylog('rec', rec);
} ) . then ( async ( myrec ) => {
2022-08-10 17:07:02 +02:00
2022-12-11 02:57:35 +01:00
let setnotif = false ;
let typedir = 0 ;
let typeid = 0 ;
let groupnameDest = '' ;
let circuitnameDest = '' ;
2022-08-10 17:07:02 +02:00
2022-12-11 02:57:35 +01:00
if ( isnewrec ) {
// New Record created
2022-08-10 17:07:02 +02:00
2022-12-11 02:57:35 +01:00
if ( shared _consts . TABLES _ADV _NOTIFICATION . includes ( params . table ) ) {
typedir = shared _consts . TypeNotifs . TYPEDIR _BACHECA ;
2023-04-13 13:46:48 +02:00
if ( params . table === shared _consts . TABLES _MYGOODS )
typeid = shared _consts . TypeNotifs . ID _BACHECA _NEW _GOOD
else if ( params . table === shared _consts . TABLES _MYSKILLS )
typeid = shared _consts . TypeNotifs . ID _BACHECA _NEW _SERVICE
else if ( params . table === shared _consts . TABLES _MYHOSPS )
typeid = shared _consts . TypeNotifs . ID _BACHECA _NEW _HOSP
2022-12-11 02:57:35 +01:00
setnotif = true ;
2022-07-28 21:47:23 +02:00
}
2022-12-11 02:57:35 +01:00
if ( shared _consts . TABLES _EVENTS _NOTIFICATION . includes ( params . table ) ) {
typedir = shared _consts . TypeNotifs . TYPEDIR _EVENTS ;
typeid = shared _consts . TypeNotifs . ID _EVENTS _NEW _REC ;
setnotif = true ;
2022-07-23 17:48:33 +02:00
}
2022-12-11 02:57:35 +01:00
if ( shared _consts . TABLES _GROUPS _NOTIFICATION . includes ( params . table ) ) {
typedir = shared _consts . TypeNotifs . TYPEDIR _GROUPS ;
typeid = shared _consts . TypeNotifs . ID _GROUP _NEW _REC ;
groupnameDest = myrec ? myrec . groupname : '' ;
setnotif = true ;
}
2023-10-21 15:27:53 +02:00
/ * i f ( s h a r e d _ c o n s t s . T A B L E S _ C I R C U I T S _ N O T I F I C A T I O N . i n c l u d e s ( p a r a m s . t a b l e ) ) {
2022-12-11 02:57:35 +01:00
typedir = shared _consts . TypeNotifs . TYPEDIR _CIRCUITS ;
typeid = shared _consts . TypeNotifs . ID _CIRCUIT _NEW _REC ;
circuitnameDest = myrec ? myrec . name : '' ;
setnotif = ( myrec . visibility === 0 ) ; // Not send a notification to others if the Circuit is HIDDEN or PRIVATE
2023-10-21 15:27:53 +02:00
} * /
2022-12-11 02:57:35 +01:00
}
2022-11-06 13:39:01 +01:00
2022-12-11 02:57:35 +01:00
if ( setnotif ) {
2023-06-20 01:07:57 +02:00
const myreq = { ... req } ;
const myres = { ... res } ;
2023-06-18 22:29:06 +02:00
SendNotif . createNewNotification ( myreq , myres , { groupnameDest , circuitnameDest } , params . table , myrec , typedir , typeid ) ;
2022-12-11 02:57:35 +01:00
}
if ( params . table === 'circuits' ) {
await Circuit . updateData ( myrec . idapp , myrec . name ) ;
}
if ( params . table === shared _consts . TAB _MYGROUPS && isnewrec ) {
// nuovo Record:
// aggiungi il creatore al gruppo stesso
return await User . setGroupsCmd ( mydata . idapp , req . user . username ,
myrec . groupname ,
shared _consts . GROUPSCMD . SETGROUP , true , req . user . username ) . then ( ( ris ) => {
return res . send ( { rec : myrec , ris } ) ;
2022-07-28 23:27:51 +02:00
} ) ;
2022-12-11 02:57:35 +01:00
} else if ( params . table === shared _consts . TAB _MYCIRCUITS && isnewrec ) {
2023-02-01 01:19:50 +01:00
// nuovo Circuito:
await User . setCircuitCmd ( mydata . idapp , req . user . username , myrec . name ,
shared _consts . CIRCUITCMD . CREATE , true , req . user . username , extrarec ) . then ( ( ris ) => {
return res . send ( { rec : myrec , ris } ) ;
} ) ;
2023-02-23 16:07:43 +01:00
// aggiungi il creatore al Circuito stesso
2022-12-11 02:57:35 +01:00
return await User . setCircuitCmd ( mydata . idapp , req . user . username , myrec . name ,
shared _consts . CIRCUITCMD . SET , true , req . user . username , extrarec ) . then ( ( ris ) => {
return res . send ( { rec : myrec , ris } ) ;
2022-09-02 02:25:38 +02:00
} ) ;
2022-12-11 02:57:35 +01:00
}
2022-07-28 23:27:51 +02:00
2022-12-11 02:57:35 +01:00
return res . send ( { rec : myrec , ris : null } ) ;
} ) . catch ( async ( e ) => {
console . error ( 'settable' , e . message ) ;
if ( e . code === 11000 ) {
const id = mytablerec . _id ;
delete mytablerec . _doc [ '_id' ] ;
const myfields = mytablerec . _doc ;
if ( ! myfields . userId ) {
myfields . userId = req . user . _id . toString ( ) ;
2022-02-16 09:40:16 +01:00
}
2022-12-11 02:57:35 +01:00
return await mytablestrutt . findByIdAndUpdate ( id , { $set : myfields } ) .
then ( async ( rec ) => {
return res . send ( { rec } ) ;
} ) .
catch ( ( err ) => {
tools . mylog ( 'error: ' , err . message ) ;
return res . status ( 400 ) . send ( err ) ;
} ) ;
} else {
console . log ( e . message ) ;
}
} ) ;
2022-01-28 00:57:39 +01:00
2022-02-03 00:33:15 +01:00
} catch ( e ) {
2022-02-22 15:24:32 +01:00
console . error ( 'settable' , e . message ) ;
2022-01-28 00:57:39 +01:00
return res . status ( 400 ) . send ( e ) ;
}
2021-10-08 00:38:35 +02:00
} ) ;
router . post ( '/setsubrec' , authenticate , ( req , res ) => {
const params = req . body ;
2022-02-16 19:21:12 +01:00
const mytable = globalTables . getTableByTableName ( params . table ) ;
2021-10-08 00:38:35 +02:00
const mydata = req . body . data ;
mydata . idapp = req . user . idapp ;
2019-10-28 16:01:28 +01:00
2021-01-18 00:48:17 +01:00
let mytablerec = new mytable ( mydata ) ;
2021-10-05 00:20:12 +02:00
// console.log('mytablerec', mytablerec);
2019-10-28 16:01:28 +01:00
2022-02-16 19:21:12 +01:00
const mytablestrutt = globalTables . getTableByTableName ( params . table ) ;
2019-10-20 01:21:54 +02:00
2021-11-22 18:29:22 +01:00
const rec = mytablestrutt . createNewSubRecord ( mydata . idapp , req ) . then ( rec => {
2021-10-08 00:38:35 +02:00
// tools.mylog('rec', rec);
return res . send ( rec ) ;
} ) . catch ( ( e ) => {
} ) ;
return res . send ( rec ) ;
2022-09-11 11:45:33 +02:00
2021-11-22 18:29:22 +01:00
return mytablerec . save ( ) . then ( rec => {
// tools.mylog('rec', rec);
return res . send ( rec ) ;
2019-10-20 01:21:54 +02:00
2022-09-11 11:45:33 +02:00
2021-11-22 18:29:22 +01:00
} ) . catch ( ( e ) => {
if ( e . code === 11000 ) {
const id = mytablerec . _id ;
delete mytablerec . _doc [ '_id' ] ;
const myfields = mytablerec . _doc ;
if ( ! myfields . userId ) {
myfields . userId = req . user . _id . toString ( ) ;
2021-02-11 02:20:35 +01:00
}
2022-12-11 02:57:35 +01:00
return mytablestrutt . findByIdAndUpdate ( id , { $set : myfields } ) .
then ( async ( rec ) => {
return res . send ( rec ) ;
} ) .
catch ( ( err ) => {
tools . mylog ( 'error: ' , err . message ) ;
return res . status ( 400 ) . send ( err ) ;
} ) ;
2021-11-22 18:29:22 +01:00
} else {
console . log ( e . message ) ;
}
} ) ;
2019-10-20 01:21:54 +02:00
} ) ;
2024-09-13 19:42:48 +02:00
router . post ( '/gettable' , authenticate _noerror , ( req , res ) => {
2024-05-04 14:49:02 +02:00
let params = req . body ;
params . table = sanitizeHtml ( params . table ) ;
2024-09-06 19:57:09 +02:00
2024-09-13 19:42:48 +02:00
if ( ! shared _consts . TABLES _ENABLE _GETTABLE _FOR _NOT _LOGGED . includes ( params . table ) && ! req . user ) {
return res . status ( 403 ) . send ( { } ) ;
}
2024-05-04 14:49:02 +02:00
let idapp = req . user ? req . user . idapp : sanitizeHtml ( params . idapp ) ;
2022-02-16 19:21:12 +01:00
const mytable = globalTables . getTableByTableName ( params . table ) ;
2023-11-03 12:49:10 +01:00
//console.log('mytable', mytable);
2019-10-20 01:21:54 +02:00
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
2022-02-22 15:24:32 +01:00
try {
2019-10-20 01:21:54 +02:00
2022-12-12 18:25:13 +01:00
if ( req . user && req . user . username ) {
User . setOnLine ( req . user . idapp , req . user . username ) ;
}
2023-09-27 18:38:57 +02:00
2022-02-27 16:56:02 +01:00
return mytable . executeQueryTable ( idapp , params , req . user ) . then ( ris => {
2023-11-03 12:49:10 +01:00
// console.log('ris=', ris);
2022-02-22 15:24:32 +01:00
return res . send ( ris ) ;
} ) . catch ( ( e ) => {
console . error ( 'gettable: ' + e . message ) ;
res . status ( 400 ) . send ( e ) ;
} ) ;
2022-02-28 03:33:47 +01:00
} catch ( e ) {
console . error ( ` ERROR gettable ${ params . table } : ` , e . message , 'params' ,
2022-12-11 02:57:35 +01:00
params ) ;
2022-02-22 15:24:32 +01:00
res . status ( 500 ) . send ( e ) ;
}
2019-10-14 20:31:57 +02:00
} ) ;
2022-07-10 01:25:19 +02:00
router . post ( '/getexp' , authenticate , ( req , res ) => {
const params = req . body ;
let idapp = req . user . idapp ;
const myUser = globalTables . getTableByTableName ( 'users' ) ;
// console.log('mytable', mytable);
if ( ! myUser || params . filtersearch2 !== 'fdsgas1' ) {
return res . status ( 400 ) . send ( { } ) ;
}
if ( ( ! User . isAdmin ( req . user . perm ) && ! User . isManager ( req . user . perm ) &&
2022-12-11 02:57:35 +01:00
! User . isFacilitatore ( req . user . perm ) ) ) {
2022-07-10 01:25:19 +02:00
// If without permissions, exit
return res . status ( 404 ) .
2022-12-11 02:57:35 +01:00
send ( { code : server _constants . RIS _CODE _ERR _UNAUTHORIZED , msg : '' } ) ;
2022-07-10 01:25:19 +02:00
}
try {
if ( params . table === 'exp' ) {
return myUser . find ( {
2022-12-11 02:57:35 +01:00
idapp ,
$or : [
{ deleted : { $exists : false } } ,
{ deleted : { $exists : true , $eq : false } } ] ,
} ,
{
username : 1 ,
2022-12-15 21:57:38 +01:00
name : 1 ,
surname : 1 ,
2022-12-11 02:57:35 +01:00
email : 1 ,
'reported' : 1 ,
date _report : 1 ,
username _who _report : 1 ,
'profile.teleg_id' : 1 ,
'verified_by_aportador' : 1 ,
'profile.username_telegram' : 1 ,
'profile.firstname_telegram' : 1 ,
'profile.lastname_telegram' : 1 ,
} ) . then ( ris => {
return res . send ( ris ) ;
} ) . catch ( ( e ) => {
console . error ( 'getexp: ' + e . message ) ;
res . status ( 400 ) . send ( e ) ;
} ) ;
2022-07-10 01:25:19 +02:00
}
} catch ( e ) {
console . error ( ` ERROR getexp ${ params . table } : ` , e . message , 'params' ,
2022-12-11 02:57:35 +01:00
params ) ;
2022-07-10 01:25:19 +02:00
res . status ( 500 ) . send ( e ) ;
}
} ) ;
2021-12-21 01:27:45 +01:00
router . post ( '/pickup' , authenticate , ( req , res ) => {
const params = req . body ;
2022-02-28 03:33:47 +01:00
let idapp = req . user . idapp ;
2022-02-16 19:21:12 +01:00
let mytable = globalTables . getTableByTableName ( params . table ) ;
2021-12-21 01:27:45 +01:00
// console.log('mytable', mytable);
2022-01-20 00:39:06 +01:00
if ( ! mytable ) {
2021-12-21 01:27:45 +01:00
console . log ( ` Table ${ params . table } not found ` ) ;
return res . status ( 400 ) . send ( { } ) ;
}
2022-02-27 16:56:02 +01:00
return mytable . executeQueryPickup ( idapp , params ) . then ( ris => {
2021-12-21 01:27:45 +01:00
return res . send ( ris ) ;
} ) . catch ( ( e ) => {
console . log ( e . message ) ;
res . status ( 400 ) . send ( e ) ;
} ) ;
} ) ;
2021-04-30 01:31:12 +02:00
router . post ( '/getpage' , async ( req , res ) => {
const params = req . body ;
const idapp = req . body . idapp ;
const mypath = params . path ;
2023-01-05 01:37:20 +01:00
let found = await MyPage . findOne ( { idapp , path : mypath } ) . then ( ( ris ) => {
2024-04-09 21:56:50 +02:00
if ( ris && ris . _doc )
return res . send ( { mypage : ris . _doc } ) ;
2023-01-05 01:37:20 +01:00
else
return null ;
2021-11-22 18:29:22 +01:00
} ) . catch ( ( e ) => {
console . log ( e . message ) ;
res . status ( 400 ) . send ( e ) ;
} ) ;
2024-04-09 21:56:50 +02:00
2023-01-05 01:37:20 +01:00
if ( ! found ) {
// trova quelli con il :
let regexp = new RegExp ( ` : ` , 'ig' )
const searchpagesSpec = await MyPage . find ( { idapp , path : { $regex : regexp } } ) ;
if ( searchpagesSpec ) {
let arrsubpath = mypath . split ( '/' ) ;
for ( let i = 0 ; i < searchpagesSpec . length ; i ++ ) {
let arrsubstr = searchpagesSpec [ i ] . path . split ( '/' ) ;
if ( arrsubpath . length === arrsubpath . length ) {
let mypathbuild = '' ;
for ( let j = 0 ; j < arrsubstr . length ; j ++ ) {
if ( arrsubstr [ j ] . includes ( ':' ) ) {
mypathbuild += arrsubpath [ j ] + '/' ;
} else {
mypathbuild += arrsubstr [ j ] + '/' ;
}
}
if ( mypath + '/' === mypathbuild ) {
return res . send ( { mypage : searchpagesSpec [ i ] } ) ;
}
}
}
}
return await MyPage . findOne ( { idapp , path : mypath } ) . then ( ( ris ) => {
return res . send ( { mypage : ris } ) ;
} ) ;
}
return found ;
2021-04-30 01:31:12 +02:00
} ) ;
2020-03-31 20:34:24 +02:00
router . patch ( '/setlang' , authenticate , async ( req , res ) => {
const username = req . body . data . username ;
2022-02-27 16:56:02 +01:00
const idapp = req . user . idapp ;
2020-03-31 20:34:24 +02:00
const mydata = req . body . data ;
const lang = mydata . lang ;
const fieldsvalue = {
2021-11-22 18:29:22 +01:00
lang ,
2020-03-31 20:34:24 +02:00
} ;
if ( ! ! lang ) {
const rec = await User . findByUsername ( idapp , username , false ) ;
let ris = null ;
if ( ! ! rec )
2022-12-11 02:57:35 +01:00
ris = await User . findByIdAndUpdate ( rec . id , { $set : fieldsvalue } ) ;
2020-03-31 20:34:24 +02:00
if ( ! ! ris ) {
2022-12-11 02:57:35 +01:00
return res . send ( { code : server _constants . RIS _CODE _OK , msg : '' } ) ;
2020-03-31 20:34:24 +02:00
}
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 ;
2022-02-27 16:56:02 +01:00
const idapp = req . user . idapp ;
2019-10-15 20:40:31 +02:00
const mydata = req . body . data ;
2022-02-28 03:33:47 +01:00
try {
const mytable = globalTables . getTableByTableName ( mydata . table ) ;
const fieldsvalue = mydata . fieldsvalue ;
const unset = mydata . unset ;
2019-10-15 20:40:31 +02:00
2023-06-20 01:07:57 +02:00
const { Account } = require ( '../models/account' ) ;
2022-02-28 03:33:47 +01:00
// tools.mylogshow('PATCH CHVAL: ', id, fieldsvalue);
2019-10-15 20:40:31 +02:00
2022-02-28 03:33:47 +01:00
// If I change my record...
2024-03-26 15:36:49 +01:00
if ( (
( ! User . isAdmin ( req . user . perm )
2024-03-28 20:25:48 +01:00
&& ! User . isManager ( req . user . perm )
&& ! User . isEditor ( req . user . perm )
&& ! User . isFacilitatore ( req . user . perm ) )
2024-04-04 18:43:17 +02:00
&& ( await ! tools . ModificheConsentite ( req , mydata . table , fieldsvalue , id ) ) )
2024-03-28 20:25:48 +01:00
&& ! ( ( mydata . table === 'accounts' )
&& await Account . canEditAccountAdmins ( req . user . username , mydata . id ) )
2023-06-20 01:07:57 +02:00
) {
2022-02-28 03:33:47 +01:00
// If without permissions, exit
return res . status ( 404 ) .
2022-12-11 02:57:35 +01:00
send ( { code : server _constants . RIS _CODE _ERR _UNAUTHORIZED , msg : '' } ) ;
2020-03-10 21:44:14 +01:00
}
2020-07-02 22:00:58 +02:00
2022-02-28 03:33:47 +01:00
const camporequisiti = UserCost . FIELDS _REQUISITI . includes (
2022-12-11 02:57:35 +01:00
Object . keys ( fieldsvalue ) [ 0 ] ) ;
2022-02-28 03:33:47 +01:00
let allData = { } ;
2022-03-04 15:30:11 +01:00
let username = '' ;
2022-02-28 03:33:47 +01:00
if ( mydata . table === 'users' ) {
if ( camporequisiti ) {
allData = { } ;
allData . myuser = await User . getUserById ( idapp , id ) ;
2022-03-04 15:30:11 +01:00
username = allData . myuser . username ;
2022-02-28 03:33:47 +01:00
if ( ! ! allData . myuser )
allData . precDataUser = await User . getInfoUser ( idapp ,
2022-12-11 02:57:35 +01:00
allData . myuser . username ) ;
2022-02-28 03:33:47 +01:00
else
allData . precDataUser = null ;
// allData.useraportador = await ListaIngresso.getUserByInvitante_Username(idapp, allData.myuser.aportador_solidario);
// allData.precDataAportador = await getInfoUser(idapp, allData.myuser.aportador_solidario);
}
}
2020-07-02 22:00:58 +02:00
2022-02-28 03:33:47 +01:00
let index = 0 ;
2020-07-02 22:00:58 +02:00
2022-02-28 03:33:47 +01:00
let recoldnave = null ;
2021-10-08 00:38:35 +02:00
2022-02-28 03:33:47 +01:00
let myuser = null ;
let mydatamsg = { } ;
2021-12-03 22:48:05 +01:00
2022-02-28 03:33:47 +01:00
let flotta = null ;
let strflotta = '' ;
2022-02-27 16:56:02 +01:00
2022-02-28 03:33:47 +01:00
if ( shared _consts . TABLES _UPDATE _LASTMODIFIED . includes ( mydata . table ) ) {
fieldsvalue . date _updated = new Date ( ) ;
2021-12-23 14:13:40 +01:00
}
2022-02-28 03:33:47 +01:00
const numobj = tools . getNumObj ( fieldsvalue ) ;
if ( numobj === 1 && fieldsvalue [ '_id' ] ) {
const myrec = await mytable . findById ( id ) ;
const myidDel = myrec [ '_id' ] ;
myrec [ '_id' ] = fieldsvalue [ '_id' ] ;
return await mytable . insertMany ( myrec ) . then ( ( ris ) => {
if ( ris ) {
2022-12-11 02:57:35 +01:00
return mytable . deleteMany ( { _id : myidDel } ) . then ( ( risdel ) => {
return res . send ( { code : server _constants . RIS _CODE _OK , msg : '' } ) ;
2022-02-28 03:33:47 +01:00
} ) ;
} else {
return res . status ( 404 ) . send ( ) ;
}
} ) ;
2022-01-07 01:18:01 +01:00
}
2022-03-02 20:18:29 +01:00
const updatebot = UserCost . FIELDS _UPDATE _TELEGRAM _BOT . includes ( Object . keys ( fieldsvalue ) [ 0 ] ) ;
2022-03-04 15:30:11 +01:00
tools . refreshAllTablesInMem ( idapp , mydata . table , updatebot , username ) ;
2022-01-07 01:18:01 +01:00
2022-02-28 03:33:47 +01:00
if ( mydata . table === shared _consts . TAB _SETTINGS ) {
if ( shared _consts . KEY _TO _CRYPTED . includes ( fieldsvalue . key ) ) {
fieldsvalue . crypted = true ;
fieldsvalue . value _str = tools . cryptdata ( fieldsvalue . value _str ) ;
2020-02-02 04:06:32 +01:00
}
2022-02-28 03:33:47 +01:00
}
2020-02-05 00:39:25 +01:00
2022-02-28 03:33:47 +01:00
if ( mydata . table === shared _consts . TAB _SITES ) {
if ( shared _consts . SITES _KEY _TO _CRYPTED in fieldsvalue ) {
fieldsvalue [ shared _consts . SITES _KEY _TO _CRYPTED ] = tools . cryptdata (
2022-12-11 02:57:35 +01:00
fieldsvalue [ shared _consts . SITES _KEY _TO _CRYPTED ] ) ;
2022-02-28 03:33:47 +01:00
}
2020-09-04 00:06:49 +02:00
2022-02-28 03:33:47 +01:00
}
2020-05-10 21:07:51 +02:00
2023-02-23 16:07:43 +01:00
let precRec = null
if ( mydata . table === 'accounts' ) {
precRec = await mytable . findById ( id ) ;
}
2022-12-11 02:57:35 +01:00
return await mytable . findByIdAndUpdate ( id , { $set : fieldsvalue } ) .
then ( async ( rec ) => {
// tools.mylogshow(' REC TO MODIFY: ', rec);
if ( ! rec ) {
return res . status ( 404 ) . send ( ) ;
} else {
let addmsg = '' ;
2022-02-28 03:33:47 +01:00
2022-12-11 02:57:35 +01:00
if ( mydata . notifBot ) {
// Send Notification to the BOT
await telegrambot . sendMsgTelegram ( idapp , mydata . notifBot . un ,
mydata . notifBot . txt ) ;
if ( ! ! addmsg )
2022-02-28 03:33:47 +01:00
await telegrambot . sendMsgTelegram ( idapp , mydata . notifBot . un ,
2022-12-11 02:57:35 +01:00
addmsg ) ;
let addtext = '[Msg Inviato a ' + mydata . notifBot . un + ']:' +
'\n' +
mydata . notifBot . txt ;
telegrambot . sendMsgTelegramToTheManagers ( idapp , addtext , true ) ;
if ( ! ! flotta )
tools . writeFlottaLog ( idapp , addtext , flotta . riga ,
flotta . col _prima ) ;
}
2020-03-10 21:44:14 +01:00
2022-12-11 02:57:35 +01:00
if ( mydata . table === 'accounts' ) {
2023-02-23 16:07:43 +01:00
let msg = '' ;
if ( rec . circuitId )
circuit = await Circuit . getCircuitByCircuitId ( rec . circuitId ) ;
let dest = rec . groupname ? rec . groupname : rec . username ;
let valprec = 0
2022-12-11 02:57:35 +01:00
if ( 'saldo' in fieldsvalue ) {
2023-03-05 22:04:14 +01:00
valprec = precRec && precRec . saldo ? precRec . saldo : 0
2023-02-23 16:07:43 +01:00
msg = i18n . _ _ ( 'SALDO_VARIATO' , circuit . name , req . user . username , dest , valprec , fieldsvalue . saldo , circuit . symbol ) ;
} else if ( 'fidoConcesso' in fieldsvalue ) {
2023-03-05 22:04:14 +01:00
valprec = precRec && precRec . fidoConcesso ? precRec . fidoConcesso : 0
2023-02-23 16:07:43 +01:00
msg = i18n . _ _ ( 'FIDOCONCESSO_VARIATO' , circuit . name , req . user . username , dest , valprec , fieldsvalue . fidoConcesso , circuit . symbol ) ;
} else if ( 'qta_maxConcessa' in fieldsvalue ) {
2023-03-05 22:04:14 +01:00
valprec = precRec && precRec . qta _maxConcessa ? precRec . qta _maxConcessa : 0
2023-02-23 16:07:43 +01:00
msg = i18n . _ _ ( 'QTAMAX_VARIATO' , circuit . name , req . user . username , dest , valprec , fieldsvalue . qta _maxConcessa , circuit . symbol ) ;
}
if ( msg ) {
2022-12-11 02:57:35 +01:00
telegrambot . sendMsgTelegramToTheManagers ( idapp , msg ) ;
2023-02-23 16:07:43 +01:00
telegrambot . sendMsgTelegramToTheAdminsOfCircuit ( idapp , circuit . path , msg ) ;
2022-09-18 01:21:04 +02:00
}
2022-12-11 02:57:35 +01:00
}
2022-09-18 01:21:04 +02:00
2022-11-06 13:39:01 +01:00
2022-12-11 02:57:35 +01:00
if ( mydata . table === 'users' ) {
2023-03-21 18:11:56 +01:00
if ( 'profile.resid_province' in fieldsvalue ) {
2023-11-26 01:38:02 +01:00
const card = fieldsvalue . hasOwnProperty ( 'profile.resid_card' ) ? fieldsvalue [ 'profile.resid_card' ] : '' ;
2023-03-21 18:11:56 +01:00
// Controlla se esiste il Circuito di questa provincia, se non esiste lo crea!
2023-11-26 01:38:02 +01:00
await Circuit . createCircuitIfNotExist ( req , idapp , fieldsvalue [ 'profile.resid_province' ] , card ) ;
2023-03-21 18:11:56 +01:00
}
2022-12-11 02:57:35 +01:00
if ( camporequisiti ) {
await User . checkIfSbloccatiRequisiti ( idapp , allData , id ) ;
}
2022-02-28 03:33:47 +01:00
2022-12-11 02:57:35 +01:00
if ( 'aportador_solidario' in fieldsvalue ) {
let ind _order _ingr = mydata . ind _order _ingr ;
// SERVE SE CI METTO LE MINUSCOLE/MAIUSCOLE SBAGLIATE in invitante_username!
const myuserfound = await User . findByUsername ( idapp ,
fieldsvalue . aportador _solidario , false ) ;
if ( ! ! myuserfound ) {
if ( ! ! myuserfound . _id && ! myuserfound . deleted ) {
const aportador = await User . getUsernameById ( idapp ,
myuserfound . _id ) ;
fieldsvalue . aportador _solidario = aportador ;
//Aggiorna record !
await mytable . findByIdAndUpdate ( id , { $set : fieldsvalue } ) ;
2022-02-28 03:33:47 +01:00
}
2022-12-11 02:57:35 +01:00
} else {
res . send (
{
code : server _constants . RIS _CODE _ERR ,
msg : 'Non aggiornato' ,
} ) ;
res . status ( 400 ) . send ( ) ;
return false ;
2022-02-28 03:33:47 +01:00
}
2022-12-11 02:57:35 +01:00
} else if ( 'deleted' in fieldsvalue ) {
let msg = '' ;
if ( fieldsvalue . deleted )
msg = 'cancellato (nascosto)' ;
else
msg = 'Ripristinato' ;
await telegrambot . sendMsgTelegramToTheManagers ( idapp ,
` L \' utente ` + tools . getNomeCognomeEUserNameByUser ( rec ) +
` è stato ${ msg } da ` +
tools . getNomeCognomeEUserNameByUser ( req . user ) ) ;
2022-02-28 03:33:47 +01:00
}
2022-12-11 02:57:35 +01:00
}
2020-02-05 00:39:25 +01:00
2024-04-04 18:43:17 +02:00
if ( await tools . ModificheConsentite ( req , mydata . table , fieldsvalue ) ) {
2022-12-11 02:57:35 +01:00
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 ` +
tools . getNomeCognomeEUserNameByUser ( rec ) +
'\nmodificato da ' +
tools . getNomeCognomeEUserNameByUser ( req . user ) +
' \n' +
'Prima: ' + nomecognomeas + ' (' +
rec . aportador _solidario +
')\n' +
'Dopo: ' + nomecognomenuovo + ' (' +
fieldsvalue . aportador _solidario + ') ]' ;
// Metti l'iniziale
if ( ! await User . AportadorOrig ( id ) ) {
await mytable . findByIdAndUpdate ( id ,
{ $set : { aportador _iniziale : fieldsvalue . aportador _solidario } } ,
{ new : false } ) ;
2022-02-28 03:33:47 +01:00
}
}
}
2020-02-05 00:39:25 +01:00
2022-12-11 02:57:35 +01:00
if ( msg !== '' )
telegrambot . sendMsgTelegramToTheManagers ( idapp , msg ) ;
2022-02-28 03:33:47 +01:00
}
2019-10-15 20:40:31 +02:00
2022-12-11 02:57:35 +01:00
res . send ( { code : server _constants . RIS _CODE _OK , msg : '' } ) ;
}
} ) .
catch ( ( e ) => {
tools . mylogserr ( 'Error patch USER: ' , e . message ) ;
res . status ( 400 ) . send ( ) ;
} ) ;
2022-02-28 03:33:47 +01:00
} catch ( e ) {
tools . mylogserr ( 'Error chval: ' , e . message ) ;
2019-10-15 20:40:31 +02:00
res . status ( 400 ) . send ( ) ;
2022-02-28 03:33:47 +01:00
}
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 ;
2021-02-18 12:19:35 +01:00
// const ind_order = req.body.data.ind_order;
// const username = req.body.data.username;
2022-02-27 16:56:02 +01:00
const idapp = req . user . idapp ;
2020-05-19 00:18:13 +02:00
const mydata = req . body . data ;
let entra = false ;
if ( ! entra ) {
// If I change my record...
2021-11-22 18:29:22 +01:00
if ( ( ! User . isAdmin ( req . user . perm ) && ! User . isManager ( req . user . perm ) &&
2022-12-11 02:57:35 +01:00
! User . isFacilitatore ( req . user . perm ) ) && ( req . user . _id . toString ( ) !== id ) ) {
2020-05-19 00:18:13 +02:00
// If without permissions, exit
2021-11-22 18:29:22 +01:00
return res . status ( 404 ) .
2022-12-11 02:57:35 +01:00
send ( { code : server _constants . RIS _CODE _ERR _UNAUTHORIZED , msg : '' } ) ;
2020-05-19 00:18:13 +02:00
}
}
if ( mydata . myfunc === shared _consts . CallFunz . DAMMI _PRIMO _UTENTE _LIBERO ) {
const userfree = await Graduatoria . getFirstUserGradFree ( idapp ) ;
if ( ! ! userfree )
2022-12-11 02:57:35 +01:00
return res . send ( { code : server _constants . RIS _CODE _OK , out : userfree } ) ;
2020-06-08 13:31:05 +02:00
/ * } e l s e i f ( m y d a t a . m y f u n c = = = s h a r e d _ c o n s t s . C a l l F u n z . G E T _ V A L B Y T A B L E ) {
2022-02-16 19:21:12 +01:00
const mytable = globalTables . getTableByTableName ( mydata . table ) ;
2020-06-08 13:31:05 +02:00
const coltoshow = {
[ mydata . coltoshow ] : 1
} ;
const ris = await mytable . findOne ( { _id : id } , coltoshow ) ;
return ris ;
} else if ( mydata . myfunc === shared _consts . CallFunz . SET _VALBYTABLE ) {
2022-02-16 19:21:12 +01:00
const mytable = globalTables . getTableByTableName ( mydata . table ) ;
2020-06-08 13:31:05 +02:00
const value = mydata . value ;
const coltoset = {
[ mydata . coltoshow ] : value
} ;
const ris = await mytable . findOneAndUpdate ( { _id : id } , { $set : coltoset } , { new : false } ) ;
if ( ! ! ris )
return res . send ( { code : server _constants . RIS _CODE _OK } ) ; * /
2020-05-19 00:18:13 +02:00
}
2022-12-11 02:57:35 +01:00
return res . send ( { code : server _constants . RIS _CODE _ERR } ) ;
2020-06-08 13:31:05 +02:00
2020-05-19 00:18:13 +02:00
} ) ;
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 ;
2021-02-18 12:19:35 +01:00
// const ind_order = req.body.data.ind_order;
2020-05-10 21:07:51 +02:00
const username = req . body . data . username ;
2022-02-27 16:56:02 +01:00
const idapp = req . user . idapp ;
2020-04-24 10:29:25 +02:00
const mydata = req . body . data ;
2021-02-18 12:19:35 +01:00
// const telegrambot = require('../telegram/telegrambot');
2020-07-11 17:10:19 +02:00
2020-04-24 10:29:25 +02:00
try {
2020-05-04 19:34:41 +02:00
let entra = false ;
if ( mydata . myfunc === shared _consts . CallFunz . AGGIUNGI _NUOVO _IMBARCO ||
2022-12-11 02:57:35 +01:00
mydata . myfunc === shared _consts . CallFunz . CANCELLA _IMBARCO ) {
2021-11-22 18:29:22 +01:00
entra = true ;
2020-05-04 19:34:41 +02:00
}
if ( ! entra ) {
// If I change my record...
2021-11-22 18:29:22 +01:00
if ( ( ! User . isAdmin ( req . user . perm ) && ! User . isManager ( req . user . perm ) &&
2022-12-11 02:57:35 +01:00
! User . isFacilitatore ( req . user . perm ) ) && ( req . user . _id . toString ( ) !== id ) ) {
2020-05-04 19:34:41 +02:00
// If without permissions, exit
2021-11-22 18:29:22 +01:00
return res . status ( 404 ) .
2022-12-11 02:57:35 +01:00
send ( { code : server _constants . RIS _CODE _ERR _UNAUTHORIZED , msg : '' } ) ;
2020-05-04 19:34:41 +02:00
}
2020-04-24 10:29:25 +02:00
}
2022-12-11 02:57:35 +01:00
let myuser = await User . findOne ( { idapp , username } ) ;
2020-04-24 10:29:25 +02:00
2021-11-22 18:29:22 +01:00
let fieldsvalue = { } ;
2020-05-04 19:34:41 +02:00
2022-02-03 00:33:15 +01:00
if ( mydata . myfunc === shared _consts . CallFunz . ZOOM _GIA _PARTECIPATO ) {
2020-09-04 00:06:49 +02:00
if ( ! ! myuser . username ) {
2021-11-22 18:29:22 +01:00
let FormDaMostrare = telegrambot . getFormDaMostrare ( idapp , mydata . myfunc ,
2022-12-11 02:57:35 +01:00
myuser ) ;
2020-09-04 00:06:49 +02:00
2021-11-22 18:29:22 +01:00
await telegrambot . sendMsgTelegramToTheManagers ( idapp ,
2022-12-11 02:57:35 +01:00
` L \' utente ${ myuser . name } ${ myuser . surname } ( ${ myuser . username } ) ha detto di aver già visto lo Zoom di Benvenuto ` ,
false , FormDaMostrare ) ;
2020-09-04 00:06:49 +02:00
2021-11-22 18:29:22 +01:00
const ris = await User . findByIdAndUpdate ( myuser . id ,
2022-12-11 02:57:35 +01:00
{ $set : { 'profile.ask_zoom_partecipato' : true } } ) ;
2020-09-04 00:06:49 +02:00
if ( ris )
2022-12-11 02:57:35 +01:00
return res . send ( { code : server _constants . RIS _CODE _OK , msg : '' } ) ;
2020-09-04 00:06:49 +02:00
}
2020-04-24 10:29:25 +02:00
}
2020-05-19 00:18:13 +02:00
2022-12-11 02:57:35 +01:00
return res . send ( { code : server _constants . RIS _CODE _ERR } ) ;
2020-05-19 00:18:13 +02:00
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 ) => {
2022-02-12 22:12:49 +01:00
// const idapporig = req.params.idapporig;
// const idappdest = req.params.idappdest;
// if (!idapporig || !idappdest)
// res.status(400).send();
//
// const mytablesstr = ['settings', 'users', 'templemail'];
//
// try {
// let numrectot = 0;
// for (const table of mytablesstr) {
2022-02-16 19:21:12 +01:00
// const mytable = globalTables.getTableByTableName(table);
2022-02-12 22:12:49 +01:00
//
// 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 ;
2021-02-18 12:19:35 +01:00
// const idapp = req.user.idapp;
2019-10-15 20:40:31 +02:00
const tablename = req . params . table ;
2021-02-18 12:19:35 +01:00
// let notifBot = false;
2019-10-15 20:40:31 +02:00
// const idapp = req.body.idapp;
2024-03-28 20:25:48 +01:00
console . log ( 'delete RECORD: id' , id , 'table' , tablename ) ;
2019-10-15 20:40:31 +02:00
2022-02-16 19:21:12 +01:00
const mytable = globalTables . getTableByTableName ( tablename ) ;
2019-10-15 20:40:31 +02:00
2022-12-11 02:57:35 +01:00
const fields = { 'ALL' : 1 } ;
2020-03-10 21:44:14 +01:00
2021-11-22 18:29:22 +01:00
if ( ( ! User . isAdmin ( req . user . perm ) && ! User . isManager ( req . user . perm ) ) &&
2022-12-11 02:57:35 +01:00
( tablename !== 'extralist' ) &&
2024-04-04 18:43:17 +02:00
await ! tools . ModificheConsentite ( req , tablename , fields , id , req . user ) ) {
2019-10-15 20:40:31 +02:00
// If without permissions, exit
2021-11-22 18:29:22 +01:00
return res . status ( 404 ) .
2022-12-11 02:57:35 +01:00
send ( { code : server _constants . RIS _CODE _ERR _UNAUTHORIZED , msg : '' } ) ;
2019-10-15 20:40:31 +02:00
}
2020-04-24 10:29:25 +02:00
let cancellato = false ;
2022-02-03 00:33:15 +01:00
//++Tools: Notify...
tools . NotifyIfDelRecord ( tablename ) ;
2020-02-05 00:39:25 +01:00
2024-03-28 20:25:48 +01:00
// if (!User.isAdmin(req.user.perm) && !User.isManager(req.user.perm)) {
if ( true ) {
2020-04-24 10:29:25 +02:00
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
} ;
2024-03-28 20:25:48 +01:00
const utente = await mytable . findById ( id ) ;
if ( utente ) {
idapp = utente . idapp ;
await mytable . findByIdAndUpdate ( id , { $set : fieldsvalue } ) ;
2020-04-24 10:29:25 +02:00
2024-03-28 20:25:48 +01:00
// ...
let text = ` L \' utente ${ utente . username } ( ${ utente . name } ${ utente . surname } ) si è cancellato dal sito ` + tools . getNomeAppByIdApp ( utente . idapp ) + ` Deleted = true ` ;
telegrambot . sendMsgTelegramToTheManagers ( idapp , text ) ;
}
2024-09-12 14:49:00 +02:00
if ( ! User . isAdmin ( req . user . perm ) ) {
cancellato = true ;
return res . send ( { code : server _constants . RIS _CODE _OK , msg : '' } ) ;
}
2024-03-28 20:25:48 +01:00
2019-10-15 20:40:31 +02:00
}
2020-04-24 10:29:25 +02:00
}
let ris = null ;
if ( ! cancellato ) {
2022-03-04 15:30:11 +01:00
// ELIMINA VERAMENTE IL RECORD !!!
2024-03-21 21:22:57 +01:00
ris = await mytable . deleteOne ( { _id : id } ) . then ( ( rec ) => {
2020-04-24 10:29:25 +02:00
if ( ! rec ) {
2021-01-18 00:48:17 +01:00
// res.status(404).send();
return false ;
2020-04-24 10:29:25 +02:00
}
2022-02-28 17:20:47 +01:00
if ( tablename === shared _consts . TAB _MYGROUPS ) {
// Se è un gruppo, allora cancella anche tutti i suoi riferimenti
User . removeAllUsersFromMyGroups ( rec . idapp , rec . groupname ) ;
2022-09-02 02:25:38 +02:00
} else if ( tablename === shared _consts . TAB _MYCIRCUITS ) {
// Se è un gruppo, allora cancella anche tutti i suoi riferimenti
User . removeAllUsersFromMyCircuits ( rec . idapp , rec . name ) ;
2022-02-28 17:20:47 +01:00
}
2022-03-04 15:30:11 +01:00
tools . refreshAllTablesInMem ( rec . idapp , tablename , true , rec . username ) ;
2020-04-24 10:29:25 +02:00
cancellato = true ;
2019-10-15 20:40:31 +02:00
2020-04-24 10:29:25 +02:00
tools . mylog ( 'DELETED ' , rec . _id ) ;
2020-10-17 22:11:29 +02:00
return true ;
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 ( ) ;
} ) ;
}
2019-10-15 20:40:31 +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
2021-01-18 00:48:17 +01:00
//let ris2 = await actions.doOtherlasThingsAfterDeleted(tablename, myrec, notifBot, req);
2020-09-04 00:06:49 +02:00
if ( ! ! ris ) {
2022-12-11 02:57:35 +01:00
return res . send ( { code : server _constants . RIS _CODE _OK , msg : '' } ) ;
2020-09-04 00:06:49 +02:00
}
2020-04-24 10:29:25 +02:00
}
2019-10-20 01:21:54 +02:00
2022-12-11 02:57:35 +01:00
res . send ( { code : server _constants . RIS _CODE _ERR , msg : '' } ) ;
2020-04-24 10:29:25 +02:00
return ris ;
2019-10-20 22:45:09 +02:00
2019-10-15 20:40:31 +02:00
} ) ;
2022-09-11 11:45:33 +02:00
router . post ( '/duprec/:table/:id' , authenticate , async ( req , res ) => {
2019-10-20 22:45:09 +02:00
const id = req . params . id ;
const tablename = req . params . table ;
// const idapp = req.body.idapp;
console . log ( 'id' , id , 'table' , tablename ) ;
2022-02-16 19:21:12 +01:00
const mytable = globalTables . getTableByTableName ( tablename ) ;
2019-10-20 22:45:09 +02:00
2020-01-03 22:02:18 +01:00
if ( ! req . user ) {
2021-11-22 18:29:22 +01:00
return res . status ( 404 ) .
2022-12-11 02:57:35 +01:00
send ( { code : server _constants . RIS _CODE _ERR _UNAUTHORIZED , msg : '' } ) ;
2020-01-03 22:02:18 +01:00
}
2023-06-01 11:39:53 +02:00
/ * i f ( ! U s e r . i s A d m i n ( r e q . u s e r . p e r m ) & & ! U s e r . i s M a n a g e r ( r e q . u s e r . p e r m ) ) {
2019-10-20 22:45:09 +02:00
// If without permissions, exit
2021-11-22 18:29:22 +01:00
return res . status ( 404 ) .
2022-12-11 02:57:35 +01:00
send ( { code : server _constants . RIS _CODE _ERR _UNAUTHORIZED , msg : '' } ) ;
2023-06-01 11:39:53 +02:00
} * /
2019-10-20 22:45:09 +02:00
2022-09-11 11:45:33 +02:00
return await mytable . findById ( id ) . then ( async ( 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
2022-09-11 11:45:33 +02:00
return await mynewrec . save ( ) . then ( async ( rec ) => {
2021-11-22 18:29:22 +01:00
if ( ! rec ) {
return res . status ( 404 ) . send ( ) ;
}
2019-10-20 22:45:09 +02:00
2021-11-22 18:29:22 +01:00
tools . mylog ( 'DUPLICATED ' , rec ) ;
2019-10-20 22:45:09 +02:00
2024-06-21 16:11:03 +02:00
2021-11-22 18:29:22 +01:00
// Do extra things after deleted
2022-09-11 11:45:33 +02:00
return await actions . doOtherThingsAfterDuplicated ( tablename , rec ) .
2022-12-11 02:57:35 +01:00
then ( ( { myrec } ) => {
// ...
mytable . findById ( myrec . _id ) . then ( ( record ) => {
return res . send (
{ code : server _constants . RIS _CODE _OK , record , msg : '' } ) ;
2021-11-22 18:29:22 +01:00
} ) ;
2019-10-21 20:38:10 +02:00
2022-12-11 02:57:35 +01:00
} ) ;
2021-11-22 18:29:22 +01:00
} ) . catch ( ( e ) => {
console . error ( e . message ) ;
res . status ( 400 ) . send ( ) ;
} ) ;
} ) ;
2019-10-20 22:45:09 +02:00
} ) ;
2021-05-10 01:50:40 +02:00
router . get ( '/loadsite/:userId/:idapp' , authenticate _noerror , ( req , res ) => {
2021-06-04 10:07:57 +02:00
load ( req , res , '0' ) ;
} ) ;
2021-11-22 18:29:22 +01:00
router . get ( '/loadsite/:userId/:idapp/:vers' , authenticate _noerror ,
2024-04-11 11:43:19 +02:00
async ( req , res ) => {
2022-12-11 02:57:35 +01:00
let versionstr = req . params . vers ;
2021-10-05 00:20:12 +02:00
2022-12-11 02:57:35 +01:00
let version = tools . getVersionint ( versionstr ) ;
2021-06-04 10:07:57 +02:00
2024-04-11 11:43:19 +02:00
return await load ( req , res , version ) ;
2022-07-27 20:56:01 +02:00
2022-12-11 02:57:35 +01:00
} ) ;
2021-06-04 10:07:57 +02:00
function load ( req , res , version ) {
2024-09-06 19:57:09 +02:00
2019-10-23 23:47:21 +02:00
const userId = req . params . userId ;
const idapp = req . params . idapp ;
2021-06-04 10:07:57 +02:00
2024-04-11 11:43:19 +02:00
let status = 200
if ( req . code === server _constants . RIS _CODE _HTTP _FORBIDDEN _TOKEN _EXPIRED ) {
status = server _constants . RIS _CODE _HTTP _FORBIDDEN _TOKEN _EXPIRED
}
2024-09-06 19:57:09 +02:00
2024-04-11 11:43:19 +02:00
2021-06-04 10:07:57 +02:00
if ( ! version ) {
version = '0' ;
}
2021-12-23 14:13:40 +01:00
let gestoredelSito = '0' ;
2021-06-04 10:07:57 +02:00
if ( ! ! req . user ) {
2022-02-03 00:33:15 +01:00
gestoredelSito = ( User . isAdmin ( req . user . perm ) ||
2022-12-11 02:57:35 +01:00
User . isManager ( req . user . perm ) ||
User . isEditor ( req . user . perm ) ) ? '1' : '0' ;
2021-06-04 10:07:57 +02:00
}
2021-02-18 12:19:35 +01:00
2019-10-23 23:47:21 +02:00
// 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
2024-06-21 16:11:03 +02:00
2019-10-23 23:47:21 +02:00
let bookedevent = [ ] ;
2021-02-18 12:19:35 +01:00
// let msgs = [];
2021-02-24 04:48:31 +01:00
let socioresidente = false ;
2024-04-09 21:56:50 +02:00
if ( req . user )
2021-02-24 04:48:31 +01:00
socioresidente = req . user . profile . socioresidente ;
2019-10-23 23:47:21 +02:00
if ( userId !== '0' ) {
// LOGGED WITH USERID
2022-02-03 00:33:15 +01:00
bookedevent = Booking . findAllByUserIdAndIdApp ( userId , idapp ,
2022-12-11 02:57:35 +01:00
gestoredelSito ) ;
2019-10-23 23:47:21 +02:00
}
// Extract all the todos of the userId only
2021-02-18 12:19:35 +01:00
const eventlist = MyEvent . findAllIdApp ( socioresidente , idapp ) ;
2019-10-23 23:47:21 +02:00
const operators = Operator . findAllIdApp ( idapp ) ;
2021-06-04 10:07:57 +02:00
const internalpages = MyPage . findInternalPages ( idapp ) ;
2019-10-23 23:47:21 +02:00
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 ) ;
2022-10-27 11:22:58 +02:00
const myelems = MyElem . findAllIdApp ( idapp ) ;
2021-12-23 14:13:40 +01:00
const settings = Settings . findAllIdApp ( idapp , false , false ) ;
2019-12-04 02:03:44 +01:00
2019-10-28 16:01:28 +01:00
const permissions = Permission . findAllIdApp ( ) ;
2019-10-23 23:47:21 +02:00
2023-04-12 15:37:54 +02:00
// const versionstr = User....
// let version = tools.getVersionint(versionstr);
2019-11-21 00:18:40 +01:00
let newstosent = Promise . resolve ( [ ] ) ;
let mailinglist = Promise . resolve ( [ ] ) ;
2021-06-04 10:07:57 +02:00
let mypage ;
if ( version > 91 )
mypage = MyPage . findOnlyStruttRec ( idapp ) ;
else
mypage = MyPage . findAllIdApp ( idapp ) ;
2020-01-21 01:37:15 +01:00
let calzoom = CalZoom . findAllIdApp ( idapp ) ;
2021-12-23 14:13:40 +01:00
let gallery = Promise . resolve ( [ ] ) ;
if ( gestoredelSito ) {
gallery = Gallery . findAllIdApp ( idapp ) ;
}
2020-12-25 03:54:16 +01:00
let producers = Producer . findAllIdApp ( idapp ) ;
2021-02-03 01:33:30 +01:00
let groups = Group . findAllIdApp ( idapp ) ;
2021-12-23 14:13:40 +01:00
// ....
2021-02-11 02:20:35 +01:00
let resps = User . getusersRespList ( idapp ) ;
let workers = User . getusersWorkersList ( idapp ) ;
2020-12-25 03:54:16 +01:00
let storehouses = Storehouse . findAllIdApp ( idapp ) ;
2023-12-15 00:57:14 +01:00
let providers = Provider . findAllIdApp ( idapp ) ;
2024-01-23 00:10:40 +01:00
let catprods = Product . getArrCatProds ( idapp , shared _consts . PROD . BOTTEGA ) ;
let catprods _gas = Product . getArrCatProds ( idapp , shared _consts . PROD . GAS ) ;
2024-01-12 13:02:59 +01:00
let subcatprods = SubCatProd . findAllIdApp ( idapp ) ;
2023-12-21 02:23:52 +01:00
let gasordines = Gasordine . findAllIdApp ( idapp ) ;
2024-01-30 14:00:37 +01:00
let catAI = CatAI . findAllIdApp ( idapp ) ;
2024-01-16 11:16:31 +01:00
let ismanager = false ;
try {
2024-01-16 11:22:22 +01:00
if ( req . user )
ismanager = User . isManager ( req . user . perm )
2024-03-21 21:22:57 +01:00
} catch ( e ) {
2024-01-16 11:16:31 +01:00
}
let products = Product . findAllIdApp ( idapp , undefined , undefined , ismanager ) ;
2024-05-09 23:36:46 +02:00
let authors = Author . findAllIdApp ( idapp ) ;
2024-06-21 16:11:03 +02:00
let publishers = Publisher . findAllIdApp ( idapp ) ;
2023-12-27 02:58:15 +01:00
let productInfos = ProductInfo . findAllIdApp ( idapp ) ;
2023-12-16 18:40:17 +01:00
let scontisticas = Scontistica . findAllIdApp ( idapp ) ;
2021-02-03 01:33:30 +01:00
let departments = Department . findAllIdApp ( idapp ) ;
2023-11-28 14:20:22 +01:00
let categories = Category . findAllIdApp ( idapp ) ;
2021-12-23 14:13:40 +01:00
2024-01-23 00:10:40 +01:00
2021-12-23 14:13:40 +01:00
// SKILLS:
2021-10-05 00:20:12 +02:00
let levels = Level . findAllIdApp ( idapp ) ;
2022-02-12 02:20:07 +01:00
let adtypes = AdType . findAllIdApp ( idapp ) ;
2022-02-21 13:12:27 +01:00
let adtypegoods = AdTypeGood . findAllIdApp ( idapp ) ;
2021-10-05 00:20:12 +02:00
let skills = Skill . findAllIdApp ( idapp ) ;
2022-02-21 13:12:27 +01:00
let goods = Good . findAllIdApp ( idapp ) ;
//let subSkills = SubSkill.findAllIdApp(idapp);
2021-10-08 00:38:35 +02:00
let statusSkills = StatusSkill . findAllIdApp ( idapp ) ;
2021-10-05 00:20:12 +02:00
let sectors = Sector . findAllIdApp ( idapp ) ;
2022-02-21 13:12:27 +01:00
let sectorgoods = SectorGood . findAllIdApp ( idapp ) ;
2022-02-05 23:28:15 +01:00
let catgrps = CatGrp . findAllIdApp ( idapp ) ;
2022-05-05 00:38:41 +02:00
let site = Site . findAllIdApp ( idapp ) ;
2022-06-16 20:34:42 +02:00
let mygroups = MyGroup . findAllGroups ( idapp ) ;
2022-09-03 13:06:58 +02:00
let listcircuits = Circuit . findAllIdApp ( idapp ) ;
2022-03-09 14:53:09 +01:00
let provinces = Province . findAllIdApp ( idapp ) ;
2020-12-25 03:54:16 +01:00
let cart = null ;
2021-01-18 00:48:17 +01:00
let orderscart = null ;
2021-12-23 14:13:40 +01:00
if ( gestoredelSito ) {
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 ;
2020-12-25 03:54:16 +01:00
if ( req . user ) {
2020-01-20 01:48:25 +01:00
calcstat = User . calculateStat ( idapp , req . user . username ) ;
2020-12-25 03:54:16 +01:00
cart = Cart . getCartByUserId ( req . user . id , idapp ) ;
2024-01-16 11:17:48 +01:00
if ( ismanager ) {
2021-04-30 01:31:12 +02:00
// Prende Tutti gli Ordini !
2023-12-20 21:52:17 +01:00
orderscart = OrdersCart . getOrdersCartByUserId ( 'ALL' , idapp , 0 , false ) ;
2021-04-30 01:31:12 +02:00
} else {
2023-12-20 21:52:17 +01:00
orderscart = OrdersCart . getOrdersCartByUserId ( req . user . id , idapp , 0 , false ) ;
2021-04-30 01:31:12 +02:00
}
2020-12-25 03:54:16 +01:00
}
2022-02-03 00:33:15 +01:00
let askedfriends = [ ] ;
2022-01-20 00:39:06 +01:00
let myuserextra = null ;
2022-01-14 23:54:33 +01:00
if ( req . user ) {
// askedfriends = User.getAskedFriendsByUsername(idapp, req.user.username);
2023-04-12 15:37:54 +02:00
myuserextra = User . addExtraInfo ( idapp , req . user . _doc , req . user , version ) ;
2022-01-14 23:54:33 +01:00
}
2020-01-20 01:48:25 +01:00
2021-11-22 18:29:22 +01:00
return Promise . all ( [
bookedevent ,
eventlist ,
operators ,
wheres ,
contribtype ,
settings ,
permissions ,
disciplines ,
newstosent ,
mailinglist ,
mypage ,
gallery ,
paymenttype ,
calcstat ,
calzoom ,
producers ,
cart ,
storehouses ,
departments ,
orderscart ,
groups ,
resps ,
workers ,
internalpages ,
levels ,
2022-03-10 00:28:23 +01:00
skills , //25
2022-02-21 13:12:27 +01:00
//subSkills,
2022-03-10 00:28:23 +01:00
myuserextra , // 26
sectors , // 27
statusSkills , //28
2022-03-09 14:53:09 +01:00
provinces ,
2022-02-05 23:28:15 +01:00
catgrps ,
2022-02-12 02:20:07 +01:00
adtypes ,
2022-02-21 13:12:27 +01:00
adtypegoods ,
sectorgoods ,
goods ,
2022-05-05 00:38:41 +02:00
site ,
2022-06-16 20:34:42 +02:00
mygroups ,
2022-09-03 13:06:58 +02:00
listcircuits , // 37
2022-10-27 11:22:58 +02:00
myelems , // 38
2023-11-28 14:20:22 +01:00
categories , // 39
2023-12-15 00:57:14 +01:00
providers ,
2023-12-16 18:40:17 +01:00
scontisticas ,
2023-12-21 02:23:52 +01:00
gasordines ,
2024-01-02 15:24:44 +01:00
products ,
2023-12-27 02:58:15 +01:00
productInfos ,
catprods ,
2024-01-12 13:02:59 +01:00
subcatprods ,
2024-01-23 00:10:40 +01:00
catprods _gas ,
2024-01-30 14:00:37 +01:00
catAI ,
2024-05-09 23:36:46 +02:00
authors ,
2024-06-21 16:11:03 +02:00
publishers ,
2022-02-16 09:40:16 +01:00
] ) . then ( ( arrdata ) => {
2021-11-22 18:29:22 +01:00
// console.table(arrdata);
2022-01-20 00:39:06 +01:00
let myuser = req . user ;
2021-11-22 18:29:22 +01:00
if ( myuser ) {
2022-01-14 23:54:33 +01:00
try {
2022-02-21 13:12:27 +01:00
myuser = arrdata [ 26 ] ;
2022-01-20 00:39:06 +01:00
if ( myuser ) {
myuser . password = '' ;
2022-03-06 00:48:33 +01:00
myuser . calcstat = arrdata [ 13 ] ;
2022-01-20 00:39:06 +01:00
}
2022-12-11 02:57:35 +01:00
} catch ( e ) { }
2021-11-22 18:29:22 +01:00
}
if ( version < 91 ) {
2024-04-11 11:43:19 +02:00
res . status ( status ) . send ( {
2021-11-22 18:29:22 +01:00
bookedevent : arrdata [ 0 ] ,
eventlist : arrdata [ 1 ] ,
operators : arrdata [ 2 ] ,
wheres : arrdata [ 3 ] ,
contribtype : arrdata [ 4 ] ,
settings : arrdata [ 5 ] ,
permissions : arrdata [ 6 ] ,
disciplines : arrdata [ 7 ] ,
newstosent : arrdata [ 8 ] ,
mailinglist : arrdata [ 9 ] ,
mypage : arrdata [ 10 ] ,
gallery : arrdata [ 11 ] ,
paymenttypes : arrdata [ 12 ] ,
calzoom : arrdata [ 14 ] ,
producers : arrdata [ 15 ] ,
cart : arrdata [ 16 ] ,
storehouses : arrdata [ 17 ] ,
departments : arrdata [ 18 ] ,
orders : arrdata [ 19 ] ,
groups : arrdata [ 20 ] ,
resps : arrdata [ 21 ] ,
workers : arrdata [ 22 ] ,
myuser ,
internalpages : arrdata [ 23 ] ,
} ) ;
} else {
2024-04-11 11:43:19 +02:00
res . status ( status ) . send ( {
2021-11-22 18:29:22 +01:00
bookedevent : arrdata [ 0 ] ,
eventlist : arrdata [ 1 ] ,
operators : arrdata [ 2 ] ,
wheres : arrdata [ 3 ] ,
contribtype : arrdata [ 4 ] ,
settings : arrdata [ 5 ] ,
permissions : arrdata [ 6 ] ,
disciplines : arrdata [ 7 ] ,
newstosent : arrdata [ 8 ] ,
mailinglist : arrdata [ 9 ] ,
mypage : arrdata [ 10 ] ,
gallery : arrdata [ 11 ] ,
paymenttypes : arrdata [ 12 ] ,
calzoom : arrdata [ 14 ] ,
producers : arrdata [ 15 ] ,
cart : arrdata [ 16 ] ,
storehouses : arrdata [ 17 ] ,
departments : arrdata [ 18 ] ,
orders : arrdata [ 19 ] ,
groups : arrdata [ 20 ] ,
resps : arrdata [ 21 ] ,
workers : arrdata [ 22 ] ,
myuser ,
internalpages : arrdata [ 23 ] ,
levels : arrdata [ 24 ] ,
skills : arrdata [ 25 ] ,
2022-02-21 13:12:27 +01:00
// subSkills: arrdata[26],
// myuser arrdata[26]
2021-12-31 01:44:28 +01:00
sectors : arrdata [ 27 ] ,
statusSkills : arrdata [ 28 ] ,
2022-03-09 14:53:09 +01:00
provinces : arrdata [ 29 ] ,
2022-02-21 13:12:27 +01:00
catgrps : arrdata [ 30 ] ,
adtypes : arrdata [ 31 ] ,
adtypegoods : arrdata [ 32 ] ,
sectorgoods : arrdata [ 33 ] ,
goods : arrdata [ 34 ] ,
2022-05-05 00:38:41 +02:00
site : arrdata [ 35 ] ,
2022-06-16 20:34:42 +02:00
mygroups : arrdata [ 36 ] ,
2022-09-03 13:06:58 +02:00
listcircuits : arrdata [ 37 ] ,
2022-10-27 11:22:58 +02:00
myelems : arrdata [ 38 ] ,
2023-11-28 14:20:22 +01:00
categories : arrdata [ 39 ] ,
2023-12-15 00:57:14 +01:00
providers : arrdata [ 40 ] ,
2023-12-16 18:40:17 +01:00
scontisticas : arrdata [ 41 ] ,
2023-12-21 02:23:52 +01:00
gasordines : arrdata [ 42 ] ,
2024-01-02 15:24:44 +01:00
products : arrdata [ 43 ] ,
productInfos : arrdata [ 44 ] ,
catprods : arrdata [ 45 ] ,
2024-01-12 13:02:59 +01:00
subcatprods : arrdata [ 46 ] ,
2024-01-23 00:10:40 +01:00
catprods _gas : arrdata [ 47 ] ,
2024-01-30 14:00:37 +01:00
catAI : arrdata [ 48 ] ,
2024-04-11 11:43:19 +02:00
code : req . code ,
2024-05-09 23:36:46 +02:00
authors : arrdata [ 49 ] ,
2024-06-21 16:11:03 +02:00
publishers : arrdata [ 50 ] ,
2021-11-22 18:29:22 +01:00
} ) ;
2023-11-30 14:27:37 +01:00
const prova = 1 ;
2021-11-22 18:29:22 +01:00
}
2020-01-20 01:48:25 +01:00
2021-11-22 18:29:22 +01:00
} ) . catch ( ( e ) => {
console . log ( e . message ) ;
res . status ( 400 ) . send ( e ) ;
} ) ;
2019-10-23 23:47:21 +02:00
2021-06-04 10:07:57 +02:00
}
2019-10-23 23:47:21 +02:00
2024-04-09 21:56:50 +02:00
router . get ( process . env . LINK _CHECK _UPDATES , authenticate _noerror , async ( req , res ) => {
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);
2024-04-09 21:56:50 +02:00
if ( ! req . user ) {
2024-04-11 11:43:19 +02:00
return res . status ( 200 ) . send ( ) ;
2019-10-24 23:30:45 +02:00
}
2024-05-09 23:36:46 +02:00
await CfgServer . find ( { idapp } ) . then ( async ( arrcfgrec ) => {
2019-10-24 23:30:45 +02:00
2024-09-09 21:49:25 +02:00
if ( arrcfgrec . length === 0 ) {
if ( User . isAdmin ( req . user . perm ) ) {
// crea un nuovo record
const mycfgServer = new CfgServer ( ) ;
mycfgServer . idapp = idapp ;
mycfgServer . chiave = 'vers' ;
mycfgServer . userId = 'ALL' ;
mycfgServer . valore = await tools . getVersServer ( ) ;
mycfgServer . save ( ) ;
arrcfgrec = await CfgServer . find ( { idapp } )
} else {
return res . status ( 404 ) . send ( ) ;
}
}
2019-10-24 23:30:45 +02:00
2022-09-02 02:25:38 +02:00
// ++Add to Log Stat ....
2019-10-24 23:30:45 +02:00
2024-04-09 21:56:50 +02:00
let last _msgs = null ;
let last _notifs = null ;
2024-05-09 23:36:46 +02:00
let last _notifcoins = null ;
2024-04-09 21:56:50 +02:00
let usersList = null ;
2019-10-24 23:30:45 +02:00
// const sall = '0';
2019-10-25 19:08:38 +02:00
// msgs = SendMsg.findAllByUserIdAndIdApp(userId, req.user.username, req.user.idapp);
2019-10-24 23:30:45 +02:00
if ( req . user ) {
2021-12-23 14:13:40 +01:00
2024-04-09 21:56:50 +02:00
const userId = req . user . _id ;
if ( ! ObjectID . isValid ( userId ) ) {
return res . status ( 404 ) . send ( ) ;
}
2024-04-11 11:43:19 +02:00
2024-04-09 21:56:50 +02:00
last _msgs = SendMsg . findLastGroupByUserIdAndIdApp ( userId , req . user . username , idapp ) ;
last _notifs = SendNotif . findLastNotifsByUserIdAndIdApp ( req . user . username , idapp , 40 ) ;
2024-05-09 23:36:46 +02:00
last _notifcoins _inattesa = SendNotif . findLastNotifCoinsByUserIdAndIdApp ( req . user . username , idapp , 200 , true ) ;
last _notifcoins = SendNotif . findLastNotifCoinsByUserIdAndIdApp ( req . user . username , idapp , 1 , false ) ;
2024-04-09 21:56:50 +02:00
if ( req . user ) {
// If User is Admin, then send user Lists
if ( User . isAdmin ( req . user . perm ) || User . isEditor ( req . user . perm ) ||
User . isManager ( req . user . perm ) ) {
// Send UsersList
usersList = User . getUsersList ( idapp ) ;
// usersList = null;
}
2019-10-24 23:30:45 +02:00
}
}
2024-05-09 23:36:46 +02:00
return Promise . all ( [ usersList , last _msgs , last _notifs , last _notifcoins , last _notifcoins _inattesa ] ) . then ( ( arrdata ) => {
2021-11-22 18:29:22 +01:00
// console.table(arrdata);
return res . send ( {
2022-05-14 00:31:53 +02:00
CfgServer : arrcfgrec ,
2021-11-22 18:29:22 +01:00
usersList : arrdata [ 0 ] ,
last _msgs : arrdata [ 1 ] ,
2022-07-10 01:25:19 +02:00
last _notifs : arrdata [ 2 ] ,
2024-05-09 23:36:46 +02:00
last _notifcoins : [ ... arrdata [ 4 ] , ... arrdata [ 3 ] ] ,
2019-10-24 23:30:45 +02:00
} ) ;
2021-11-22 18:29:22 +01:00
} ) ;
2019-10-24 23:30:45 +02:00
} ) . catch ( ( e ) => {
2020-05-04 19:34:41 +02:00
console . log ( e . message ) ;
2022-12-11 02:57:35 +01:00
res . status ( 400 ) . send ( { code : server _constants . RIS _CODE _ERR , msg : e } ) ;
2019-10-24 23:30:45 +02:00
} ) ;
} ) ;
2019-12-27 12:41:39 +01:00
router . post ( '/upload_from_other_server/:dir' , authenticate , ( req , res ) => {
2021-02-18 12:19:35 +01:00
// const dir = req.params.dir;
// const idapp = req.user.idapp;
2019-12-07 00:20:06 +01:00
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
} ) ;
2021-10-05 00:20:12 +02:00
function uploadFile ( req , res , version ) {
// console.log('/upload dir:' + dir);
2021-10-28 00:38:10 +02:00
const dir = tools . invertescapeslash ( req . params . dir ) ;
2022-09-19 19:40:30 +02:00
console . log ( 'inverted = ' , dir ) ;
2022-02-28 17:20:47 +01:00
const idapp = req . user . idapp ;
2019-12-27 12:41:39 +01:00
const form = new formidable . IncomingForm ( ) ;
form . parse ( req ) ;
2021-10-05 00:20:12 +02:00
let dirmain = '/statics' ;
if ( version > 0 ) {
2022-02-09 19:48:39 +01:00
if ( tools . sulServer ( ) ) {
2021-10-28 00:38:10 +02:00
dirmain = '' ;
} else {
dirmain = '/public' ;
}
2021-10-05 00:20:12 +02:00
}
2022-09-19 19:40:30 +02:00
const mydir2 = folder + '/' + dir ;
tools . mkdirpath ( mydir2 ) ;
form . uploadDir = mydir2 ;
2019-12-27 12:41:39 +01:00
try {
2022-12-11 02:57:35 +01:00
form . on ( 'fileBegin' , async function ( name , file ) {
2022-09-19 19:40:30 +02:00
const mydir = folder + '/' + file . newFilename ;
// tools.mkdirpath(mydir);
file . path = mydir
2019-12-27 12:41:39 +01:00
} ) ;
2022-12-11 02:57:35 +01:00
form . on ( 'file' , async function ( name , file ) {
2019-12-27 12:41:39 +01:00
try {
2022-09-11 11:45:33 +02:00
console . log ( '1) Uploading ' + file . originalFilename ) ;
2022-02-03 00:33:15 +01:00
const mydir = tools . getdirByIdApp ( idapp ) + dirmain +
2022-12-11 02:57:35 +01:00
server _constants . DIR _UPLOAD + '/' + dir ;
2019-12-28 14:30:30 +01:00
2022-09-19 19:40:30 +02:00
// console.log('mydir', mydir);
2019-12-28 14:30:30 +01:00
// Create Dir if doesn't exist:
2022-02-21 18:14:56 +01:00
const rismk = tools . mkdirpath ( mydir ) ;
2022-01-23 23:25:34 +01:00
2022-09-11 11:45:33 +02:00
let filename = file . originalFilename ;
2022-01-23 23:25:34 +01:00
let ext = path . extname ( filename ) ;
//++Todo: Modifica del nomefile... da passare al frontend
//if (mydir.includes('profile')) {
// filename = uuidv4() + ext;
//}
2022-02-03 00:33:15 +01:00
file . name = filename ;
2022-01-23 23:25:34 +01:00
let resized _img = mydir + '/' + server _constants . PREFIX _IMG + filename ;
2019-12-28 14:30:30 +01:00
2022-09-11 11:45:33 +02:00
oldpath = file . newFilename ;
2022-09-19 19:40:30 +02:00
2024-03-21 21:22:57 +01:00
2022-12-11 02:57:35 +01:00
let fromfile = '.' + server _constants . DIR _UPLOAD + '/' + dir + '/' + oldpath ;
let tofile = '.' + server _constants . DIR _UPLOAD + '/' + dir + '/' + file . originalFilename ;
2022-09-19 19:40:30 +02:00
let mydircurrent = process . cwd ( ) + '/src/server/router/upload/' + dir ;
2022-12-11 02:57:35 +01:00
fromfile = mydircurrent + '/' + oldpath ;
2022-09-19 19:40:30 +02:00
tofile = mydir + '/' + file . originalFilename ;
let newname = tofile ;
2022-01-23 23:25:34 +01:00
file . path = newname ;
2024-09-09 21:49:25 +02:00
console . log ( 'fromfile' , fromfile , 'tofile' , tofile )
2019-12-27 12:41:39 +01:00
2024-09-06 19:57:09 +02:00
2022-03-30 22:51:03 +02:00
if ( ! tools . sulServer ( ) ) {
2024-09-13 19:42:48 +02:00
// Se faccio eseguire questo in Locale, lui mi fa l'aggiornamento della pagina, quindi poi non posso salvare!
// allora mi conviene che lo faccio dopo, manualmente.
2024-02-03 23:04:56 +01:00
console . log ( 'Dovresti copiare fromfile' , fromfile , 'tofile' , tofile ) ;
2024-09-17 17:38:47 +02:00
console . log ( 'sudo cp -R ' + fromfile + ' ' + tofile ) ;
2024-09-13 19:42:48 +02:00
// await tools.execScriptNoOutput('sudo cp -R ' + fromfile + ' ' + tofile)
2022-12-11 02:57:35 +01:00
res . end ( ) ;
return ;
2022-03-30 22:51:03 +02:00
}
2022-12-11 02:57:35 +01:00
2022-03-30 22:51:03 +02:00
2019-12-28 14:30:30 +01:00
// Move in the folder application !
2024-09-13 19:42:48 +02:00
await tools . move ( fromfile , tofile , ( err ) => {
2022-02-28 17:20:47 +01:00
if ( err ) {
2022-03-01 23:50:58 +01:00
console . log ( 'err uploadDir:' , err ) ;
2022-02-28 17:20:47 +01:00
res . status ( 400 ) . send ( ) ;
} else {
2021-12-21 18:12:28 +01:00
2022-02-28 17:20:47 +01:00
// Salva le immagini in formato compresso
try {
let resized _img _small = tools . extractFilePath ( newname ) + '/' +
2022-12-11 02:57:35 +01:00
server _constants . PREFIX _IMG _SMALL +
tools . extractFileName ( newname ) ;
2022-02-28 17:20:47 +01:00
// SMALL
2022-03-01 23:50:58 +01:00
// questa opzione 'failOnError' serve per risolvere l'errore (Error: VipsJpeg: Invalid SOS parameters for sequential JPEG
2022-12-11 02:57:35 +01:00
sharp ( newname , { failOnError : false } ) .
resize ( 64 , 64 ) .
withMetadata ( ) .
toFile ( resized _img _small ) ;
2022-02-28 17:20:47 +01:00
// MEDIUM
2022-03-01 23:50:58 +01:00
let resized _img = tools . extractFilePath ( newname ) + '/' + server _constants . PREFIX _IMG + tools . extractFileName ( newname ) ;
2022-12-11 02:57:35 +01:00
sharp ( newname , { failOnError : false } ) .
resize ( {
width : 512 ,
// height: 512,
//fit: sharp.fit.cover,
fit : sharp . fit . contain ,
// position: sharp.strategy.entropy,
} ) . withMetadata ( ) . toFile ( resized _img , function ( err ) {
// console.log('3) Ridimensionata Immagine ' + newname, 'in', resized_img);
if ( tools . isFileExists ( resized _img ) ) {
// console.log('4) Cancella l \'immagine grande originale:', newname);
// DELETE THE ORIGINAL BIG
tools . delete ( newname , false , ( ) => { } ) ;
// console.log('5) Rinomina l\'immagine Media da', resized_img, 'a:', newname);
// RENAME THE MEDIUM IN THE ORIGINAL NAME
tools . move ( resized _img , newname , ( err ) => {
if ( err )
console . error ( 'err' , err ) ;
else
console . log ( 'move' , newname ) ;
} ) ;
}
2022-07-10 01:25:19 +02:00
2022-12-11 02:57:35 +01:00
if ( err )
console . error ( 'Error Upload: ' , err ) ;
} ) ;
2022-02-28 17:20:47 +01:00
} catch ( e ) {
2022-03-01 23:50:58 +01:00
console . error ( 'Error Upload(2) ' , e ) ;
2022-02-28 17:20:47 +01:00
}
2022-02-16 09:40:16 +01:00
}
2021-12-21 18:12:28 +01:00
2022-01-26 01:31:22 +01:00
res . end ( ) ;
2022-03-01 23:50:58 +01:00
// console.log('res.end');
2022-01-23 23:25:34 +01:00
// return res.send({filename: newname });
2021-12-21 18:12:28 +01:00
2019-12-27 12:41:39 +01:00
} ) ;
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 ( ) ;
}
} ) ;
2022-12-11 02:57:35 +01:00
form . on ( 'end' , function ( ) {
2022-09-19 19:40:30 +02:00
console . log ( '-> upload done' ) ;
2022-01-23 23:25:34 +01:00
} ) ;
2019-12-27 12:41:39 +01:00
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 ) {
2021-11-22 18:29:22 +01:00
console . log ( 'Error' , e ) ;
2019-12-27 12:41:39 +01:00
}
2021-10-05 00:20:12 +02:00
}
router . post ( '/upload/:dir' , authenticate , ( req , res ) => {
2022-01-23 23:25:34 +01:00
return uploadFile ( req , res , 0 ) ;
2021-10-05 00:20:12 +02:00
} ) ;
2021-10-28 00:38:10 +02:00
router . post ( '/uploadnew/:vers/:dir/' , authenticate , ( req , res ) => {
2021-10-05 00:20:12 +02:00
let versionstr = req . params . vers ;
let version = tools . getVersionint ( versionstr ) ;
2021-12-21 18:12:28 +01:00
try {
2022-01-23 23:25:34 +01:00
return uploadFile ( req , res , version ) ;
2021-12-21 18:12:28 +01:00
} catch ( e ) {
console . log ( 'error' , e ) ;
res . status ( 400 ) . send ( ) ;
}
2019-12-07 00:20:06 +01:00
} ) ;
2021-11-22 18:29:22 +01:00
router . delete ( '/delfile/:vers' , authenticate , ( req , res ) => {
let versionstr = req . params . vers ;
let version = tools . getVersionint ( versionstr ) ;
deleteFile ( req , res , version ) ;
} ) ;
2019-10-15 20:40:31 +02:00
2019-12-28 02:16:29 +01:00
router . delete ( '/delfile' , authenticate , ( req , res ) => {
2021-11-22 18:29:22 +01:00
deleteFile ( req , res , 0 ) ;
} ) ;
2022-12-23 00:36:35 +01:00
2021-11-22 18:29:22 +01:00
function deleteFile ( req , res , version ) {
2019-12-28 02:16:29 +01:00
const relativefile = req . query . filename ;
const idapp = req . user . idapp ;
2023-09-27 18:38:57 +02:00
if ( ! relativefile || relativefile . endsWith ( '/' ) ) {
res . send ( { code : server _constants . RIS _CODE _OK , msg : '' } ) ;
2023-08-27 23:55:31 +02:00
}
2019-12-28 02:16:29 +01:00
try {
2021-11-22 18:29:22 +01:00
let dirmain = '' ;
if ( version > 0 ) {
2023-08-27 23:55:31 +02:00
if ( ! tools . sulServer ( ) ) {
2021-11-22 18:29:22 +01:00
dirmain = '/public' ;
}
}
2019-12-28 02:16:29 +01:00
try {
console . log ( 'Delete file ' + relativefile ) ;
// ++ Move in the folder application !
2021-12-03 22:48:05 +01:00
let fullpathfile = tools . getdirByIdApp ( idapp ) + dirmain + '/' +
2022-12-11 02:57:35 +01:00
relativefile ;
2019-12-28 02:16:29 +01:00
2021-12-21 18:12:28 +01:00
tools . delete ( fullpathfile , true , ( err ) => {
2019-12-28 02:16:29 +01:00
if ( err ) console . log ( 'err' , err ) ;
if ( err === undefined || err . errno === - 2 )
2022-12-11 02:57:35 +01:00
res . send ( { code : server _constants . RIS _CODE _OK , msg : '' } ) ;
2019-12-28 02:16:29 +01:00
} ) ;
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 ) {
2021-11-22 18:29:22 +01:00
console . log ( 'Error' , e ) ;
2019-12-28 02:16:29 +01:00
}
2021-11-22 18:29:22 +01:00
}
2019-12-28 02:16:29 +01:00
2019-02-05 03:40:22 +01:00
module . exports = router ;