Fare procedura per cambiare gli username in giro nelle varie tabelle

This commit is contained in:
Surya Paolo
2022-12-23 00:36:35 +01:00
parent 786843f115
commit 7e1407cbb1
8 changed files with 352 additions and 274 deletions

View File

@@ -114,7 +114,7 @@ ExtraListSchema.statics.getTotInLista = async function (idapp) {
const myfind = { idapp };
return await ExtraList.count(myfind);
return await ExtraList.countDocuments(myfind);
};
ExtraListSchema.statics.getRegDellaLista = async function (idapp) {
@@ -122,7 +122,7 @@ ExtraListSchema.statics.getRegDellaLista = async function (idapp) {
const myfind = { idapp, registered: true };
return await ExtraList.count(myfind);
return await ExtraList.countDocuments(myfind);
};
ExtraListSchema.statics.getLastUser = function (idapp) {

View File

@@ -138,7 +138,7 @@ GraduatoriaSchema.statics.findByAllRecByUsername = function (idapp, username) {
GraduatoriaSchema.statics.getNumDaImbarcare = async function (idapp) {
const Graduatoria = this;
return await Graduatoria.count({ idapp })
return await Graduatoria. countDocuments({ idapp })
};

View File

@@ -74,7 +74,7 @@ MailingListSchema.statics.getnumSent = async function (idapp, idmailinglist) {
// Extract only the Teacher where in the users table the field permissions is set 'Teacher' bit.
return await MailingList.count(myfind);
return await MailingList.countDocuments(myfind);
};
MailingListSchema.statics.isOk = async function (idapp, iduser, idmailinglist) {
@@ -84,7 +84,7 @@ MailingListSchema.statics.isOk = async function (idapp, iduser, idmailinglist) {
// Extract only the Teacher where in the users table the field permissions is set 'Teacher' bit.
return await MailingList.count(myfind) > 0;
return await MailingList.countDocuments(myfind) > 0;
};
MailingListSchema.statics.findAllIdApp = async function (idapp) {

View File

@@ -384,14 +384,14 @@ const UserSchema = new mongoose.Schema({
_id: false,
groupname: { type: String },
date: { type: Date },
}], // username
}],
mycircuits: [
{
_id: false,
circuitname: { type: String },
date: { type: Date },
}], // username
}],
notifs: [
{
@@ -1102,7 +1102,7 @@ UserSchema.statics.isUserQualified9 = async function(idapp, username) {
UserSchema.statics.getnumPaymentOk = function (idapp) {
const User = this;
return await User.count({
return await User. countDocuments({
idapp,
$and: [
{
@@ -3299,7 +3299,7 @@ UserSchema.statics.getUsersRegistered = async function (idapp) {
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
};
return await User.count(myfind);
return await User. countDocuments(myfind);
};
UserSchema.statics.getUsersRegisteredToday = async function (idapp) {
@@ -3314,7 +3314,7 @@ UserSchema.statics.getUsersRegisteredToday = async function (idapp) {
date_reg: { $gt: starttoday },
};
return await User.count(myfind);
return await User. countDocuments(myfind);
};
UserSchema.statics.getUsersOnLineToday = async function (idapp) {
@@ -3329,7 +3329,7 @@ UserSchema.statics.getUsersOnLineToday = async function (idapp) {
lasttimeonline: { $gt: starttoday },
};
return await User.count(myfind);
return await User. countDocuments(myfind);
};
/*
@@ -3398,7 +3398,7 @@ UserSchema.statics.getEmailNotVerified = async function (idapp) {
verified_email: false,
};
return await User.count(myfind);
return await User. countDocuments(myfind);
};
UserSchema.statics.getUsersTelegramAttivo = async function (idapp) {
@@ -3410,7 +3410,7 @@ UserSchema.statics.getUsersTelegramAttivo = async function (idapp) {
'profile.teleg_id': { $gt: 0 },
};
return await User.count(myfind);
return await User. countDocuments(myfind);
};
UserSchema.statics.getUsersAutorizzati = async function (idapp) {
@@ -3423,7 +3423,7 @@ UserSchema.statics.getUsersAutorizzati = async function (idapp) {
verified_by_aportador: true,
};
return await User.count(myfind);
return await User. countDocuments(myfind);
};
UserSchema.statics.getUsersAutorizzare = async function (idapp) {
@@ -3436,7 +3436,7 @@ UserSchema.statics.getUsersAutorizzare = async function (idapp) {
verified_by_aportador: { $exists: false },
};
return await User.count(myfind);
return await User. countDocuments(myfind);
};
UserSchema.statics.getUsersTelegramPending = async function (idapp) {
@@ -3448,7 +3448,7 @@ UserSchema.statics.getUsersTelegramPending = async function (idapp) {
'profile.teleg_checkcode': { $gt: 0 },
};
return await User.count(myfind);
return await User. countDocuments(myfind);
};
UserSchema.statics.getNumUsers = async function (idapp) {
@@ -3459,7 +3459,7 @@ UserSchema.statics.getNumUsers = async function (idapp) {
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
};
return await User.count(myfind);
return await User. countDocuments(myfind);
};
UserSchema.statics.getUsersZoom = async function (idapp) {
@@ -3471,7 +3471,7 @@ UserSchema.statics.getUsersZoom = async function (idapp) {
'profile.saw_zoom_presentation': true,
};
return await User.count(myfind);
return await User. countDocuments(myfind);
};
UserSchema.statics.getUsersResidenti = async function (idapp) {
@@ -3495,7 +3495,7 @@ UserSchema.statics.getSaw_and_Accepted = async function (idapp) {
'profile.saw_and_accepted': shared_consts.ALL_SAW_AND_ACCEPTED,
};
return await User.count(myfind);
return await User. countDocuments(myfind);
};
UserSchema.statics.getUsersDreams = async function (idapp) {
@@ -3508,7 +3508,7 @@ UserSchema.statics.getUsersDreams = async function (idapp) {
'$expr': { '$gt': [{ '$strLenCP': '$profile.my_dream' }, 10] },
};
return await User.count(myfind);
return await User. countDocuments(myfind);
};
UserSchema.statics.getLastUsers = async function (idapp) {
@@ -4303,6 +4303,7 @@ UserSchema.statics.tooManyReqPassword = async function (idapp, email, set) {
};
UserSchema.statics.createNewSubRecord = async function (idapp, req) {
const User = this;

View File

@@ -1748,6 +1748,7 @@ router.delete('/delfile', authenticate, (req, res) => {
deleteFile(req, res, 0);
});
function deleteFile(req, res, version) {
const relativefile = req.query.filename;
const idapp = req.user.idapp;

View File

@@ -38,6 +38,8 @@ router.post('/', authenticate, async (req, res) => {
subscriptionModel.access = req.body.others.access;
subscriptionModel.browser = req.get('User-Agent');
// console.log('subscriptionModel.browser', subscriptionModel.browser);
// Find if already exist
await Subscription.findOne({
userId: subscriptionModel.userId,

View File

@@ -741,6 +741,7 @@ router.post('/circuits/cmd', authenticate, async (req, res) => {
const value = req.body.value;
const extrarec = req.body.extrarec;
/*if (!User.isAdmin(req.user.perm) || !User.isManager(req.user.perm)) {
// If without permissions, exit
if (usernameOrig !== usernameLogged) {
@@ -767,12 +768,14 @@ router.post('/circuits/cmd', authenticate, async (req, res) => {
});
async function eseguiDbOp(idapp, mydata, locale) {
async function eseguiDbOp(idapp, mydata, locale, req, res) {
let ris = await User.DbOp(idapp, mydata);
const populate = require('../populate/populate');
const globalTables = require('../tools/globalTables');
let mystr = '';
try {
@@ -1081,6 +1084,12 @@ async function eseguiDbOp(idapp, mydata, locale) {
ris = populate.rewriteTable('contribtypes');
} else if (mydata.dbop === 'ReplaceUsername') {
if (User.isAdmin(req.user.perm)) {
ris = globalTables.replaceUsername(req.body.idapp, mydata.search_username, mydata.replace_username);
}
} else if (mydata.dbop === 'copyFrom1To14') {
const idapporig = 1;
const idappdest = 14;
@@ -1236,8 +1245,14 @@ router.post('/dbop', authenticate, async (req, res) => {
idapp = req.body.idapp;
locale = req.body.locale;
if (!User.isAdmin(req.user.perm)) {
// If without permissions, exit
return res.status(404).
send({ code: server_constants.RIS_CODE_ERR_UNAUTHORIZED, msg: '' });
}
try {
const ris = await eseguiDbOp(idapp, mydata, locale);
const ris = await eseguiDbOp(idapp, mydata, locale, req, res);
res.send(ris);

View File

@@ -438,4 +438,63 @@ module.exports = {
return await this.SendMsgToParam(idapp, params);
},
replaceUsername: async function (idapp, search_username, replace_username) {
try {
let ris = null;
console.log('replaceUsername = ', search_username, replace_username);
ris = await User.findOneAndUpdate({ idapp, username: search_username }, { $set: { username: replace_username } });
console.log('username result = ', ris);
ris = await User.findOneAndUpdate({ idapp, 'profile.username_telegram': search_username }, { $set: { 'profile.username_telegram': replace_username } });
console.log('profile.username_telegram result = ', ris);
ris = await User.updateOne({ idapp, 'profile.friends.username': search_username },
{
$set:
{ 'profile.friends.$.username': replace_username }
});
console.log('friends.username result = ', ris);
ris = await User.updateOne({ idapp, 'profile.req_friends.username': search_username },
{
$set:
{ 'profile.req_friends.$.username': replace_username }
});
console.log('req_friends.username result = ', ris);
ris = await Circuit.updateOne({ idapp, 'admins.username': search_username },
{ $set: { 'admins.$.username': replace_username } });
console.log('Circuit.admins.username result = ', ris);
ris = await Circuit.updateOne({ idapp, 'req_users.username': search_username },
{ $set: { 'req_users.$.username': replace_username } });
console.log('Circuit.req_users.username result = ', ris);
ris = await Circuit.updateOne({ idapp, 'refused_users.username': search_username },
{ $set: { 'refused_users.$.username': replace_username } });
console.log('Circuit.refused_users.username result = ', ris);
ris = await MyGroup.updateOne({ idapp, 'admins.username': search_username },
{ $set: { 'admins.$.username': replace_username } });
console.log('MyGroup.refused_users.username result = ', ris);
ris = await MyGroup.updateOne({ idapp, 'req_users.username': search_username },
{ $set: { 'req_users.$.username': replace_username } });
console.log('MyGroup.req_users.username result = ', ris);
// MyGroup.refused_users.username
ris = await MyGroup.updateOne({ idapp, 'refused_users.username': search_username },
{ $set: { 'refused_users.$.username': replace_username } });
console.log('MyGroup.refused_users.username result = ', ris);
} catch (e) {
console.error(e);
}
},
};