ver 0.5.51

This commit is contained in:
Surya Paolo
2023-01-03 16:51:32 +01:00
parent 7a41e6a1d4
commit 18b827f5f4
10 changed files with 1004 additions and 616 deletions

View File

@@ -75,8 +75,9 @@ const MyGroupSchema = new Schema({
},
admins: [
{
username: {type: String},
date: {type: Date},
username: { type: String },
perm: { type: Number },
date: { type: Date },
},
],
blocked: {
@@ -97,34 +98,35 @@ const MyGroupSchema = new Schema({
req_users: [
{
_id: false,
username: {type: String},
date: {type: Date},
username: { type: String },
date: { type: Date },
}], // username
refused_users: [
{
_id: false,
username: {type: String},
date: {type: Date},
username: { type: String },
date: { type: Date },
}], // username
deleted: {
type: Boolean,
default: false,
},
circuits_list: [
mycircuits: [
{
Num: { type: Number },
inscription_date: {type: Date},
_id: false,
circuitname: { type: String },
date: { type: Date },
}],
});
MyGroupSchema.statics.getFieldsForSearch = function() {
return [{field: 'descr', type: tools.FieldType.string}];
MyGroupSchema.statics.getFieldsForSearch = function () {
return [{ field: 'descr', type: tools.FieldType.string }];
};
MyGroupSchema.statics.executeQueryTable = function(idapp, params, user) {
MyGroupSchema.statics.executeQueryTable = function (idapp, params, user) {
params.fieldsearch = this.getFieldsForSearch();
const {User} = require('./user');
const { User } = require('./user');
if (params.options) {
if (tools.isBitActive(params.options, shared_consts.OPTIONS_SEARCH_USER_ONLY_FULL_WORDS)) {
@@ -137,9 +139,9 @@ MyGroupSchema.statics.executeQueryTable = function(idapp, params, user) {
return tools.executeQueryTable(this, idapp, params, user);
};
MyGroupSchema.pre('save', async function(next) {
MyGroupSchema.pre('save', async function (next) {
if (this.isNew) {
const myrec = await MyGroup.findOne().limit(1).sort({_id: -1});
const myrec = await MyGroup.findOne().limit(1).sort({ _id: -1 });
if (!!myrec) {
if (myrec._doc._id === 0)
this._id = 1;
@@ -156,71 +158,85 @@ MyGroupSchema.pre('save', async function(next) {
next();
});
MyGroupSchema.statics.findAllIdApp = async function(idapp) {
const myfind = {idapp};
MyGroupSchema.statics.findAllIdApp = async function (idapp) {
const myfind = { idapp };
return await MyGroup.find(myfind);
};
MyGroupSchema.statics.findAllGroups = async function(idapp) {
MyGroupSchema.statics.findAllGroups = async function (idapp) {
const whatToShow = this.getWhatToShow(idapp, '');
return await MyGroup.find({
idapp,
$or: [
{deleted: {$exists: false}},
{deleted: {$exists: true, $eq: false}}],
{ deleted: { $exists: false } },
{ deleted: { $exists: true, $eq: false } }],
}, whatToShow);
};
// Rimuovo la Richiesta del Gruppo
MyGroupSchema.statics.removeReqGroup = async function(idapp, username, groupnameDest) {
MyGroupSchema.statics.removeReqGroup = async function (idapp, username, groupnameDest) {
return await MyGroup.updateOne({idapp, groupname: groupnameDest},
{$pull: {req_users: {username: {$in: [username]}}}});
return await MyGroup.updateOne({ idapp, groupname: groupnameDest },
{ $pull: { req_users: { username: { $in: [username] } } } });
};
// Aggiungi agli utenti Rifiutati del Gruppo
MyGroupSchema.statics.refuseReqGroup = async function(idapp, username, groupnameDest) {
MyGroupSchema.statics.refuseReqGroup = async function (idapp, username, groupnameDest) {
return await MyGroup.updateOne({idapp, groupname: groupnameDest},
return await MyGroup.updateOne({ idapp, groupname: groupnameDest },
{
$push:
{
$push:
{
refused_users: {
username,
date: new Date(),
},
},
});
refused_users: {
username,
date: new Date(),
},
},
});
};
// Aggiungi agli Admin del Gruppo
MyGroupSchema.statics.addToAdminOfMyGroup = async function(idapp, username, groupnameDest) {
MyGroupSchema.statics.addToAdminOfMyGroup = async function (idapp, username, groupnameDest) {
return await MyGroup.updateOne({idapp, groupname: groupnameDest},
return await MyGroup.updateOne({ idapp, groupname: groupnameDest },
{
$push:
{
$push:
{
admins: {
username,
date: new Date(),
},
},
});
admins: {
username,
date: new Date(),
},
},
});
};
// Rimuovi dagli Admin del Gruppo
MyGroupSchema.statics.removeAdminOfMyGroup = async function(idapp, username, groupnameDest) {
MyGroupSchema.statics.removeAdminOfMyGroup = async function (idapp, username, groupnameDest) {
return await MyGroup.updateOne({idapp, groupname: groupnameDest},
{$pull: {admins: {username: {$in: [username]}}}});
return await MyGroup.updateOne({ idapp, groupname: groupnameDest },
{ $pull: { admins: { username: { $in: [username] } } } });
};
MyGroupSchema.statics.getWhatToShow = function(idapp, username) {
MyGroupSchema.statics.getListAdminsByGroupName = async function (idapp, groupname) {
let arr = await MyGroup.findOne({
idapp,
groupname,
$or: [
{ deleted: { $exists: false } },
{ deleted: { $exists: true, $eq: false } }],
}, {admins: 1}).lean();
return arr && arr.admins ? arr.admins : [];
};
MyGroupSchema.statics.getWhatToShow = function (idapp, username) {
// FOR ME, PERMIT ALL
return {
groupname: 1,
@@ -242,12 +258,12 @@ MyGroupSchema.statics.getWhatToShow = function(idapp, username) {
createdBy: 1,
date_created: 1,
date_updated: 1,
circuits_list: 1,
mycircuits: 1,
};
};
MyGroupSchema.statics.getWhatToShow_Unknown = function(idapp, username) {
MyGroupSchema.statics.getWhatToShow_Unknown = function (idapp, username) {
return {
groupname: 1,
title: 1,
@@ -259,14 +275,14 @@ MyGroupSchema.statics.getWhatToShow_Unknown = function(idapp, username) {
note: 1,
date_created: 1,
date_updated: 1,
circuits_list: 1,
mycircuits: 1,
};
};
MyGroupSchema.statics.getArrUsernameFromFieldByGroupname = async function(
idapp, groupname, field) {
MyGroupSchema.statics.getArrUsernameFromFieldByGroupname = async function (
idapp, groupname, field) {
const {User} = require('../models/user');
const { User } = require('../models/user');
const myobj = {};
myobj[field + '.' + subfield] = 1;
@@ -274,7 +290,7 @@ MyGroupSchema.statics.getArrUsernameFromFieldByGroupname = async function(
let arrrec = await User.findOne({
idapp,
groupname,
$or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}],
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
}, myobj).then((ris) => ris ? ris._doc[field] : []);
if (arrrec.length > 0) {
@@ -284,7 +300,7 @@ MyGroupSchema.statics.getArrUsernameFromFieldByGroupname = async function(
};
MyGroupSchema.statics.getInfoGroupByGroupname = async function(idapp, groupname) {
MyGroupSchema.statics.getInfoGroupByGroupname = async function (idapp, groupname) {
const whatToShow = this.getWhatToShow(idapp, groupname);
@@ -294,13 +310,13 @@ MyGroupSchema.statics.getInfoGroupByGroupname = async function(idapp, groupname)
};
const query = [
{$match: myfind},
{ $unwind: '$circuits_list' },
{ $match: myfind },
{ $unwind: '$mycircuits' },
{
$lookup: {
from: 'circuits',
localField: 'circuits_list.Num',
foreignField: 'Num',
localField: 'mycircuits.circuitname',
foreignField: 'name',
as: 'mycircuits',
},
},
@@ -319,7 +335,7 @@ MyGroupSchema.statics.getInfoGroupByGroupname = async function(idapp, groupname)
},
},
},
{$project: whatToShow},
{ $project: whatToShow },
];
@@ -336,15 +352,15 @@ MyGroupSchema.statics.getInfoGroupByGroupname = async function(idapp, groupname)
};
MyGroupSchema.statics.deleteGroup = async function(idapp, usernameOrig, groupname) {
MyGroupSchema.statics.deleteGroup = async function (idapp, usernameOrig, groupname) {
console.log('Gruppo ' + groupname + ' rimosso da ' + usernameOrig);
return await MyGroup.findOneAndRemove({idapp, groupname});
return await MyGroup.findOneAndRemove({ idapp, groupname });
};
MyGroupSchema.statics.getGroupsByUsername = async function(idapp, username, req) {
MyGroupSchema.statics.getGroupsByUsername = async function (idapp, username, req) {
try {
const {User} = require('../models/user');
const { User } = require('../models/user');
const whatToShow = this.getWhatToShow(idapp, username);
const whatToShow_Unknown = this.getWhatToShow_Unknown(idapp, username);
@@ -353,17 +369,17 @@ MyGroupSchema.statics.getGroupsByUsername = async function(idapp, username, req)
let listUsersGroup = await User.find({
idapp,
username: {$in: arrUsernameGroups},
username: { $in: arrUsernameGroups },
$or: [
{deleted: {$exists: false}},
{deleted: {$exists: true, $eq: false}}],
{ deleted: { $exists: false } },
{ deleted: { $exists: true, $eq: false } }],
}, whatToShow);
let listgroups = await MyGroup.find({
idapp,
$or: [
{deleted: {$exists: false}},
{deleted: {$exists: true, $eq: false}}],
{ deleted: { $exists: false } },
{ deleted: { $exists: true, $eq: false } }],
}, whatToShow_Unknown);
/*let listRequestUsersGroup = await User.find({
@@ -379,21 +395,21 @@ MyGroupSchema.statics.getGroupsByUsername = async function(idapp, username, req)
let listSentRequestGroups = await MyGroup.find({
idapp,
'req_users': {
$elemMatch: {username: {$eq: username}},
$elemMatch: { username: { $eq: username } },
},
$or: [
{deleted: {$exists: false}},
{deleted: {$exists: true, $eq: false}}],
{ deleted: { $exists: false } },
{ deleted: { $exists: true, $eq: false } }],
}, whatToShow_Unknown);
let listRefusedGroups = await MyGroup.find({
idapp,
'refused_users': {
$elemMatch: {username: {$eq: username}},
$elemMatch: { username: { $eq: username } },
},
$or: [
{deleted: {$exists: false}},
{deleted: {$exists: true, $eq: false}}],
{ deleted: { $exists: false } },
{ deleted: { $exists: true, $eq: false } }],
}, whatToShow_Unknown);
return {
@@ -418,12 +434,12 @@ MyGroupSchema.statics.getGroupsByUsername = async function(idapp, username, req)
};
};
MyGroupSchema.statics.extractCitiesName = async function(idapp, id) {
MyGroupSchema.statics.extractCitiesName = async function (idapp, id) {
try {
let aggr1 = [
{
$match: {idapp, _id: id},
$match: { idapp, _id: id },
},
{
$lookup: {
@@ -459,13 +475,42 @@ MyGroupSchema.statics.extractCitiesName = async function(idapp, id) {
ris = await this.aggregate(aggr1);
return ris;
}catch (e) {
} catch (e) {
console.error('e', e);
}
};
MyGroupSchema.statics.ifCircuitAlreadyInGroup = async function (idapp, groupname, circuitname) {
// Controllo se è stato già inserito il circuito sul gruppo
return await this.findOne({
idapp,
groupname,
'mycircuits': {
$elemMatch: { circuitname: { $eq: circuitname } },
},
}).lean();
};
// aggiungo il Circuito all'interno del Gruppo
MyGroupSchema.statics.addCircuitFromGroup = async function (idapp, groupname, circuitname) {
return await this.updateOne({ idapp, groupname },
{ $push: { 'mycircuits': {
circuitname,
date: new Date(),
} } });
};
// Rimuovo il Circuito all'interno del Gruppo
MyGroupSchema.statics.removeCircuitFromGroup = async function (idapp, groupname, circuitname) {
return await this.updateOne({ idapp, groupname },
{ $pull: { 'mycircuits': { circuitname: { $in: [circuitname] } } } });
};
const MyGroup = mongoose.model('MyGroup', MyGroupSchema);
module.exports = {MyGroup};
module.exports = { MyGroup };