versione 1.2.14 :

- aggiornati i file di configurazione, ENV e script non funzionanti., package.
- corretto custom-service-worker.js con CORS
- ottimizzato il server, la chiamata Load iniziale (senza promise, con async/await).
This commit is contained in:
Surya Paolo
2025-03-12 21:03:02 +01:00
parent d106a59bb5
commit 7827e49760
19 changed files with 594 additions and 1549 deletions

View File

@@ -298,16 +298,16 @@ MyGroupSchema.statics.getArrUsernameFromFieldByGroupname = async function (
const { User } = require('../models/user');
const myobj = {};
myobj[field + '.' + subfield] = 1;
myobj[field] = 1;
let arrrec = await User.findOne({
const ris = await User.findOne({
idapp,
groupname,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
}, myobj).then((ris) => ris ? ris._doc[field] : []);
}, myobj);
if (arrrec.length > 0) {
return arrrec.map(m => m.username);
if (ris) {
return ris[field].map(m => m.username);
}
return [];
@@ -663,13 +663,13 @@ MyGroupSchema.statics.renameCircuitName = async function (idapp, oldcircuitname,
MyGroupSchema.statics.setReceiveRisGroup = async function (idapp, groupname) {
return await this.findOneAndUpdate({
idapp, groupname,
},
{ $set: { 'lastdate_reqRisGroup': new Date() } }, { new: false }).lean().then((record) => {
return !!record;
});
const record = await this.findOneAndUpdate(
{ idapp, groupname },
{ $set: { 'lastdate_reqRisGroup': new Date() } },
{ new: false }
).lean();
return !!record; // Restituisce true se il record esiste, false altrimenti
};
const MyGroup = mongoose.model('MyGroup', MyGroupSchema);

View File

@@ -141,10 +141,10 @@ NewstosentSchema.statics.getlast = async function (idapp) {
const Newstosent = this;
try {
const mydoc = await Newstosent.find({ idapp }).sort({ datestartJob: -1 }).limit(1);
return mydoc[0]._doc;
const mydoc = await Newstosent.findOne({ idapp }).sort({ datestartJob: -1 }).lean();
return mydoc || null;
} catch (e) {
return null
return null;
}
};

View File

@@ -313,24 +313,21 @@ ProjectSchema.statics.getIdParentByIdProj = function (idProj) {
ProjectSchema.statics.creaProjMain = async function (idapp) {
const projmain = {
idapp,
descr: process.env.PROJECT_DESCR_MAIN,
longdescr: process.env.PROJECT_DESCR_MAIN,
typeproj: 1,
id_main_project: null,
id_parent: null,
privacyread: server_constants.Privacy.all
};
return await new Project(projmain).save()
.then(ris => {
console.log('ris', ris);
if (!!ris)
return ris._doc;
else
return null;
});
return await Project.findOneAndUpdate(
{ idapp, descr: process.env.PROJECT_DESCR_MAIN },
{
$setOnInsert: {
idapp,
descr: process.env.PROJECT_DESCR_MAIN,
longdescr: process.env.PROJECT_DESCR_MAIN,
typeproj: 1,
id_main_project: null,
id_parent: null,
privacyread: server_constants.Privacy.all
}
},
{ upsert: true, new: true, runValidators: true }
);
};

View File

@@ -124,7 +124,7 @@ sendNotifSchema.index({ sender: 1 });*/
sendNotifSchema.index({ idapp: 1, typedir: 1, typeid: 1, status: 1, sender: 1 });
sendNotifSchema.statics.setNotifAsRead = function (idapp, username, idnotif) {
sendNotifSchema.statics.setNotifAsRead = async function (idapp, username, idnotif) {
const SendNotif = this;
if (!username)
@@ -133,7 +133,7 @@ sendNotifSchema.statics.setNotifAsRead = function (idapp, username, idnotif) {
try {
if (idnotif) {
return SendNotif.findOneAndUpdate({
return await SendNotif.findOneAndUpdate({
$and: [
{ idapp },
{ dest: username },
@@ -154,11 +154,11 @@ sendNotifSchema.statics.setNotifAsRead = function (idapp, username, idnotif) {
}
};
sendNotifSchema.statics.getRecNotif = function (id) {
sendNotifSchema.statics.getRecNotif = async function (id) {
const SendNotif = this;
try {
return SendNotif.findById(id).lean();
return await SendNotif.findById(id).lean();
} catch (e) {
return null;
}
@@ -602,7 +602,7 @@ sendNotifSchema.statics.getDescrAndLinkByRecNotif = async function (recnotif, us
recnotif.tag = recnotif.tag ? recnotif.tag : tag;
if (!recnotif.descr) {
if (!recnotif.descr) {
recnotif.descr = newdescr;
}
@@ -895,10 +895,9 @@ sendNotifSchema.statics.updateStatusAndDescr = async function (myrecnotif, onlys
// Cerca il record e se lo trova lo aggiorna
const myrec = await SendNotif.findOneAndUpdate(query, { $set: fields_to_update }, {
new: false,
returnNewDocument: true,
});
const myrec = await SendNotif.findOneAndUpdate(query,
{ $set: fields_to_update },
{ returnDocument: "after" }).lean();
if (myrec) {
return { myrecout: myrec, save: true };

View File

@@ -48,7 +48,7 @@ const SettingsSchema = new Schema({
SettingsSchema.statics.getFieldsForSearch = function () {
return [{ field: 'key', type: tools.FieldType.string },
{ field: 'value_str', type: tools.FieldType.string }, { field: 'value_num', type: tools.FieldType.number }]
{ field: 'value_str', type: tools.FieldType.string }, { field: 'value_num', type: tools.FieldType.number }]
};
SettingsSchema.statics.executeQueryTable = function (idapp, params) {
@@ -94,9 +94,9 @@ SettingsSchema.statics.findAllIdApp = async function (idapp, serv, crypted = fal
try {
let myfind = '';
if (serv) {
myfind = {idapp, serv};
myfind = { idapp, serv };
} else
myfind = {idapp};
myfind = { idapp };
// myfind = {...myfind, $or: [{ crypted: { $exists: false } }, { crypted: { $exists: true, $eq: crypted } }]};
@@ -109,14 +109,14 @@ SettingsSchema.statics.findAllIdApp = async function (idapp, serv, crypted = fal
if (rec.crypted) {
rec.value_str = ''
}
myarr.push({...rec});
myarr.push({ ...rec });
}
} else {
myarr = [...arrorig];
}
return myarr;
}catch (e) {
} catch (e) {
console.error('Settings: findAllIdApp', e);
return null;
}
@@ -136,7 +136,9 @@ SettingsSchema.statics.setKeyNum = async function (idapp, key, value) {
return await myrec.save();
} else {
myrec = await Settings.findOneAndUpdate({ idapp, key }, { $set: { value_num: value } }, { new: false });
myrec = await Settings.findOneAndUpdate({ idapp, key },
{ $set: { value_num: value } },
{ new: false });
}
return myrec

View File

@@ -110,7 +110,10 @@ StatSchema.statics.calculateStats = async function (idapp) {
if (trova_se_oggi) {
// Aggiorna il record di oggi:
const ris = await Stat.findOneAndUpdate({ _id: trova_se_oggi._id }, { $set: datastat }, { new: true });
const ris = await Stat.findOneAndUpdate(
{ _id: trova_se_oggi._id },
{ $set: datastat },
{ returnDocument: "after" });
// console.log('ris', ris);
} else {
// Aggiungi un nuovo record:

View File

@@ -577,11 +577,11 @@ UserSchema.methods.generateAuthToken = function (req) {
});
};
UserSchema.statics.setOnLine = function (idapp, username) {
UserSchema.statics.setOnLine = async function (idapp, username) {
const User = this;
try {
return User.findOneAndUpdate({ idapp, username }, { $set: { lasttimeonline: new Date() } });
return await User.findOneAndUpdate({ idapp, username }, { $set: { lasttimeonline: new Date() } });
} catch (e) {
}
@@ -733,58 +733,41 @@ UserSchema.statics.isFacilitatore = function (perm) {
UserSchema.statics.findByToken = async function (token, typeaccess, con_auth, idapp) {
const User = this;
let decoded;
let code = server_constants.RIS_CODE_HTTP_INVALID_TOKEN;
let user = null;
let decoded;
if (!token) return { user, code };
try {
if (token) {
decoded = jwt.verify(token, process.env.SIGNCODE);
code = server_constants.RIS_CODE_OK;
}
} catch (e) {
if (e.expiredAt) {
decoded = jwt.verify(token, process.env.SIGNCODE);
code = server_constants.RIS_CODE_OK;
} catch (err) {
if (err.expiredAt) {
code = server_constants.RIS_CODE_HTTP_FORBIDDEN_TOKEN_EXPIRED;
if (con_auth) {
return { user: null, code };
}
if (con_auth) return { user: null, code };
} else {
console.error('Err findByToken:', e);
console.error('Err findByToken:', err);
}
return { user: null, code };
}
if (code === server_constants.RIS_CODE_OK) {
user = await User.findOne({
'_id': decoded.smart,
tokens: {
$elemMatch: {
token: token,
access: typeaccess,
},
user = await User.findOne({
_id: decoded.smart,
tokens: {
$elemMatch: {
token,
access: typeaccess,
},
});
},
}).lean();
if (user) {
let check_expiry_date = false
// Controlla se il sito ha attivo il controllo del Token Scaduto
if (tools.getEnableTokenExpiredByIdApp(user.idapp)) {
check_expiry_date = true
}
let tempo = Date.now() / 1000;
if (check_expiry_date && (decoded.exp < tempo)) {
console.log('Il token è scaduto, generazione del nuovo token...');
code = server_constants.RIS_CODE_HTTP_FORBIDDEN_TOKEN_EXPIRED;
} else {
// TOKEN ANCORA VALIDO
code = server_constants.RIS_CODE_OK;
}
if (user) {
const checkExpiry = tools.getEnableTokenExpiredByIdApp(user.idapp);
const currentTime = Date.now() / 1000;
if (checkExpiry && decoded.exp < currentTime) {
console.log('Il token è scaduto, generazione del nuovo token...');
code = server_constants.RIS_CODE_HTTP_FORBIDDEN_TOKEN_EXPIRED;
}
}
@@ -805,7 +788,7 @@ UserSchema.statics.findByTokenAnyAccess = function (token) {
return User.findOne({
'_id': decoded.smart,
'tokens.token': token,
});
}).lean();
};
UserSchema.statics.findByCredentials = function (idapp, username, password, pwdcrypted) {
@@ -2150,17 +2133,14 @@ UserSchema.statics.getArrUsernameFromFieldByUsername = async function (
idapp, username, field, subfield) {
const myobj = {};
myobj[field + '.' + subfield] = 1;
myobj[field + '.' + subfield + '.username'] = 1;
let arrrec = await User.findOne({
const rec = await User.findOne({
idapp, 'username': username,
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
}, myobj).then((ris) => ris ? ris._doc[field][subfield] : []);
}, myobj);
if (arrrec && arrrec.length > 0) {
return arrrec.map(m => m.username);
}
return [];
return rec ? rec[field][subfield].map(m => m.username) : [];
};
@@ -5790,7 +5770,7 @@ UserSchema.statics.getExtraInfoByUsername = async function (idapp, username) {
let myuser = await User.findOne({ idapp, username }).lean();
if (myuser) {
myuserextra = await User.addExtraInfo(idapp, myuser, null);
myuserextra = await User.addExtraInfo(idapp, myuser);
return myuser.profile;
}
@@ -5816,18 +5796,18 @@ UserSchema.statics.getProfilePerActivitiesByUsername = async function (idapp, us
};
UserSchema.statics.addExtraInfo = async function (idapp, recUser, recUserSave, version) {
UserSchema.statics.addExtraInfo = async function (idapp, recUser, version) {
try {
// tools.startTimeLog('addExtraInfo')
if (version) {
if (version && recUser) {
let versattualeuser = 0;
if (!recUser.profile.version) {
if (!recUser?.profile?.version) {
recUser.version = 0;
versattualeuser = 0;
} else {
versattualeuser = recUser.profile.version;
versattualeuser = recUser.profile?.version;
}
// versattualeuser = 0; // TOGLIERE!
@@ -6281,6 +6261,26 @@ UserSchema.statics.getnumAnnunci = async function (idapp) {
};
// crea una funzione per aggiornare il lasttimeonline e useragent
UserSchema.statics.updateLastTimeAndUserAgent = async function (id, useragent) {
const User = this;
// cerca lo user by id e aggiorna i campi
// e ritorna il nuovo record
const ris = await User.findOneAndUpdate(
{ _id: id },
{
$set: {
lasttimeonline: new Date(),
useragent,
retry_pwd: 0
},
},
{ returnDocument: "after" }
).lean();
return ris;
}
UserSchema.statics.createNewSubRecord = async function (idapp, req) {
const User = this;