2022-09-14 11:32:04 +02:00
|
|
|
const mongoose = require('mongoose').set('debug', false);
|
2022-02-03 00:33:15 +01:00
|
|
|
const Schema = mongoose.Schema;
|
|
|
|
|
|
|
|
|
|
const tools = require('../tools/general');
|
|
|
|
|
|
2022-02-05 23:28:15 +01:00
|
|
|
const shared_consts = require('../tools/shared_nodejs');
|
|
|
|
|
|
2022-02-03 00:33:15 +01:00
|
|
|
mongoose.Promise = global.Promise;
|
|
|
|
|
mongoose.level = 'F';
|
|
|
|
|
|
2024-05-30 12:43:51 +02:00
|
|
|
const { ObjectID } = require('mongodb');
|
|
|
|
|
|
2022-02-03 00:33:15 +01:00
|
|
|
// Resolving error Unknown modifier: $pushAll
|
|
|
|
|
mongoose.plugin(schema => {
|
|
|
|
|
schema.options.usePushEach = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const MyGroupSchema = new Schema({
|
2022-02-22 15:24:32 +01:00
|
|
|
_id: {
|
2023-04-07 02:45:21 +02:00
|
|
|
type: String,
|
|
|
|
|
default: function () {
|
|
|
|
|
return new ObjectID().toString();
|
|
|
|
|
},
|
2022-02-22 15:24:32 +01:00
|
|
|
},
|
2022-02-03 00:33:15 +01:00
|
|
|
idapp: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
groupname: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
title: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
descr: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2022-02-05 23:28:15 +01:00
|
|
|
idCatGrp: {
|
2022-02-03 00:33:15 +01:00
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
userId: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
photos: [
|
|
|
|
|
{
|
|
|
|
|
imagefile: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
alt: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
description: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
}],
|
|
|
|
|
idCity: [
|
|
|
|
|
{
|
|
|
|
|
type: Number,
|
|
|
|
|
}],
|
|
|
|
|
website: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
link_telegram: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2022-02-05 23:28:15 +01:00
|
|
|
note: {
|
|
|
|
|
type: String,
|
|
|
|
|
default: '',
|
|
|
|
|
},
|
2022-02-17 17:43:45 +01:00
|
|
|
//**ADDFIELD_MYGROUPS
|
2022-02-05 23:28:15 +01:00
|
|
|
visibility: [
|
|
|
|
|
{
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
pwd_cryp: {
|
|
|
|
|
type: String,
|
2022-02-03 00:33:15 +01:00
|
|
|
},
|
2022-08-04 17:30:57 +02:00
|
|
|
createdBy: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2022-02-03 00:33:15 +01:00
|
|
|
admins: [
|
|
|
|
|
{
|
2023-01-03 16:51:32 +01:00
|
|
|
username: { type: String },
|
|
|
|
|
perm: { type: Number },
|
|
|
|
|
date: { type: Date },
|
2022-02-03 00:33:15 +01:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
blocked: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
},
|
|
|
|
|
username_who_block: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
date_created: {
|
|
|
|
|
type: Date,
|
2022-08-15 15:10:27 +02:00
|
|
|
},
|
|
|
|
|
date_updated: {
|
|
|
|
|
type: Date,
|
2022-02-03 00:33:15 +01:00
|
|
|
},
|
|
|
|
|
date_blocked: {
|
|
|
|
|
type: Date,
|
|
|
|
|
},
|
|
|
|
|
req_users: [
|
|
|
|
|
{
|
|
|
|
|
_id: false,
|
2023-01-03 16:51:32 +01:00
|
|
|
username: { type: String },
|
|
|
|
|
date: { type: Date },
|
2022-02-03 00:33:15 +01:00
|
|
|
}], // username
|
2022-08-08 16:35:32 +02:00
|
|
|
refused_users: [
|
|
|
|
|
{
|
|
|
|
|
_id: false,
|
2023-01-03 16:51:32 +01:00
|
|
|
username: { type: String },
|
|
|
|
|
date: { type: Date },
|
2022-08-08 16:35:32 +02:00
|
|
|
}], // username
|
2022-02-03 00:33:15 +01:00
|
|
|
deleted: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
2023-01-03 16:51:32 +01:00
|
|
|
mycircuits: [
|
2022-08-17 00:36:47 +02:00
|
|
|
{
|
2023-01-03 16:51:32 +01:00
|
|
|
_id: false,
|
|
|
|
|
circuitname: { type: String },
|
|
|
|
|
date: { type: Date },
|
2022-08-17 00:36:47 +02:00
|
|
|
}],
|
2023-03-11 01:01:11 +01:00
|
|
|
lastdate_reqRisGroup: {
|
|
|
|
|
type: Date,
|
|
|
|
|
},
|
2022-02-03 00:33:15 +01:00
|
|
|
});
|
|
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
MyGroupSchema.statics.getFieldsForSearch = function () {
|
|
|
|
|
return [{ field: 'descr', type: tools.FieldType.string }];
|
2022-02-03 00:33:15 +01:00
|
|
|
};
|
|
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
MyGroupSchema.statics.executeQueryTable = function (idapp, params, user) {
|
2022-02-03 00:33:15 +01:00
|
|
|
params.fieldsearch = this.getFieldsForSearch();
|
2022-02-05 23:28:15 +01:00
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
const { User } = require('./user');
|
2022-02-22 15:24:32 +01:00
|
|
|
|
2022-02-05 23:28:15 +01:00
|
|
|
if (params.options) {
|
2022-02-21 13:12:27 +01:00
|
|
|
if (tools.isBitActive(params.options, shared_consts.OPTIONS_SEARCH_USER_ONLY_FULL_WORDS)) {
|
2022-02-05 23:28:15 +01:00
|
|
|
params.fieldsearch = User.getFieldsForSearchUserFriend();
|
2022-02-21 13:12:27 +01:00
|
|
|
} else if (tools.isBitActive(params.options, shared_consts.OPTIONS_SEARCH_USER_ALL_WORDS)) {
|
|
|
|
|
params.fieldsearch = User.getFieldsForSearchUserFriend_AllWords();
|
2022-02-05 23:28:15 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 20:34:42 +02:00
|
|
|
return tools.executeQueryTable(this, idapp, params, user);
|
2022-02-03 00:33:15 +01:00
|
|
|
};
|
|
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
MyGroupSchema.pre('save', async function (next) {
|
2022-02-22 15:24:32 +01:00
|
|
|
if (this.isNew) {
|
2023-04-07 02:45:21 +02:00
|
|
|
if (!this.date_created)
|
|
|
|
|
this.date_created = new Date();
|
2022-02-22 15:24:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
MyGroupSchema.statics.findAllIdApp = async function (idapp) {
|
|
|
|
|
const myfind = { idapp };
|
2022-02-03 00:33:15 +01:00
|
|
|
|
|
|
|
|
return await MyGroup.find(myfind);
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
MyGroupSchema.statics.findAllGroups = async function (idapp) {
|
2022-06-16 20:34:42 +02:00
|
|
|
|
|
|
|
|
const whatToShow = this.getWhatToShow(idapp, '');
|
|
|
|
|
|
|
|
|
|
return await MyGroup.find({
|
|
|
|
|
idapp,
|
|
|
|
|
$or: [
|
2023-01-03 16:51:32 +01:00
|
|
|
{ deleted: { $exists: false } },
|
|
|
|
|
{ deleted: { $exists: true, $eq: false } }],
|
2022-06-16 20:34:42 +02:00
|
|
|
}, whatToShow);
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-03 00:33:15 +01:00
|
|
|
// Rimuovo la Richiesta del Gruppo
|
2023-01-03 16:51:32 +01:00
|
|
|
MyGroupSchema.statics.removeReqGroup = async function (idapp, username, groupnameDest) {
|
2022-02-03 00:33:15 +01:00
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
return await MyGroup.updateOne({ idapp, groupname: groupnameDest },
|
|
|
|
|
{ $pull: { req_users: { username: { $in: [username] } } } });
|
2022-02-03 00:33:15 +01:00
|
|
|
};
|
|
|
|
|
|
2022-08-08 16:35:32 +02:00
|
|
|
// Aggiungi agli utenti Rifiutati del Gruppo
|
2023-01-03 16:51:32 +01:00
|
|
|
MyGroupSchema.statics.refuseReqGroup = async function (idapp, username, groupnameDest) {
|
2022-08-08 16:35:32 +02:00
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
return await MyGroup.updateOne({ idapp, groupname: groupnameDest },
|
|
|
|
|
{
|
|
|
|
|
$push:
|
2022-08-08 16:35:32 +02:00
|
|
|
{
|
2023-01-03 16:51:32 +01:00
|
|
|
refused_users: {
|
|
|
|
|
username,
|
|
|
|
|
date: new Date(),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
2022-08-08 16:35:32 +02:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2022-08-09 17:32:06 +02:00
|
|
|
// Aggiungi agli Admin del Gruppo
|
2023-01-03 16:51:32 +01:00
|
|
|
MyGroupSchema.statics.addToAdminOfMyGroup = async function (idapp, username, groupnameDest) {
|
2022-08-09 17:32:06 +02:00
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
return await MyGroup.updateOne({ idapp, groupname: groupnameDest },
|
|
|
|
|
{
|
|
|
|
|
$push:
|
2022-08-09 17:32:06 +02:00
|
|
|
{
|
2023-01-03 16:51:32 +01:00
|
|
|
admins: {
|
|
|
|
|
username,
|
|
|
|
|
date: new Date(),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
2022-08-09 17:32:06 +02:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Rimuovi dagli Admin del Gruppo
|
2023-01-03 16:51:32 +01:00
|
|
|
MyGroupSchema.statics.removeAdminOfMyGroup = async function (idapp, username, groupnameDest) {
|
|
|
|
|
|
|
|
|
|
return await MyGroup.updateOne({ idapp, groupname: groupnameDest },
|
|
|
|
|
{ $pull: { admins: { username: { $in: [username] } } } });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MyGroupSchema.statics.getListAdminsByGroupName = async function (idapp, groupname) {
|
2023-03-11 01:01:11 +01:00
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
let arr = await MyGroup.findOne({
|
|
|
|
|
idapp,
|
|
|
|
|
groupname,
|
|
|
|
|
$or: [
|
|
|
|
|
{ deleted: { $exists: false } },
|
|
|
|
|
{ deleted: { $exists: true, $eq: false } }],
|
2023-03-11 01:01:11 +01:00
|
|
|
}, { admins: 1 }).lean();
|
2023-01-03 16:51:32 +01:00
|
|
|
|
|
|
|
|
return arr && arr.admins ? arr.admins : [];
|
2022-08-09 17:32:06 +02:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
MyGroupSchema.statics.getWhatToShow = function (idapp, username) {
|
2022-02-05 23:28:15 +01:00
|
|
|
// FOR ME, PERMIT ALL
|
2022-02-03 00:33:15 +01:00
|
|
|
return {
|
|
|
|
|
groupname: 1,
|
|
|
|
|
title: 1,
|
|
|
|
|
descr: 1,
|
|
|
|
|
visibility: 1,
|
2022-02-05 23:28:15 +01:00
|
|
|
idCatGrp: 1,
|
2022-02-03 00:33:15 +01:00
|
|
|
userId: 1,
|
|
|
|
|
photos: 1,
|
|
|
|
|
idCity: 1,
|
|
|
|
|
website: 1,
|
2022-02-17 17:43:45 +01:00
|
|
|
//**ADDFIELD_MYGROUPS
|
2022-02-03 00:33:15 +01:00
|
|
|
link_telegram: 1,
|
2022-02-05 23:28:15 +01:00
|
|
|
note: 1,
|
2022-02-03 00:33:15 +01:00
|
|
|
admins: 1,
|
|
|
|
|
blocked: 1,
|
2022-02-05 23:28:15 +01:00
|
|
|
req_users: 1,
|
2022-08-08 16:35:32 +02:00
|
|
|
refused_users: 1,
|
2022-08-09 17:32:06 +02:00
|
|
|
createdBy: 1,
|
|
|
|
|
date_created: 1,
|
2022-08-15 15:10:27 +02:00
|
|
|
date_updated: 1,
|
2023-01-03 16:51:32 +01:00
|
|
|
mycircuits: 1,
|
2023-03-11 01:01:11 +01:00
|
|
|
lastdate_reqRisGroup: 1,
|
2022-02-03 00:33:15 +01:00
|
|
|
};
|
|
|
|
|
|
2022-08-08 16:35:32 +02:00
|
|
|
};
|
2022-02-03 00:33:15 +01:00
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
MyGroupSchema.statics.getWhatToShow_Unknown = function (idapp, username) {
|
2022-02-03 00:33:15 +01:00
|
|
|
return {
|
|
|
|
|
groupname: 1,
|
|
|
|
|
title: 1,
|
|
|
|
|
descr: 1,
|
|
|
|
|
photos: 1,
|
|
|
|
|
visibility: 1,
|
2022-02-05 23:28:15 +01:00
|
|
|
idCatGrp: 1,
|
2022-02-03 00:33:15 +01:00
|
|
|
idCity: 1,
|
2022-02-05 23:28:15 +01:00
|
|
|
note: 1,
|
2022-08-15 15:10:27 +02:00
|
|
|
date_created: 1,
|
|
|
|
|
date_updated: 1,
|
2023-01-03 16:51:32 +01:00
|
|
|
mycircuits: 1,
|
2023-03-11 01:01:11 +01:00
|
|
|
lastdate_reqRisGroup: 1,
|
2022-02-03 00:33:15 +01:00
|
|
|
};
|
2022-08-08 16:35:32 +02:00
|
|
|
};
|
2022-02-03 00:33:15 +01:00
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
MyGroupSchema.statics.getArrUsernameFromFieldByGroupname = async function (
|
|
|
|
|
idapp, groupname, field) {
|
2022-02-03 00:33:15 +01:00
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
const { User } = require('../models/user');
|
2022-02-03 00:33:15 +01:00
|
|
|
|
|
|
|
|
const myobj = {};
|
|
|
|
|
myobj[field + '.' + subfield] = 1;
|
|
|
|
|
|
|
|
|
|
let arrrec = await User.findOne({
|
|
|
|
|
idapp,
|
|
|
|
|
groupname,
|
2023-01-03 16:51:32 +01:00
|
|
|
$or: [{ deleted: { $exists: false } }, { deleted: { $exists: true, $eq: false } }],
|
2022-02-03 00:33:15 +01:00
|
|
|
}, myobj).then((ris) => ris ? ris._doc[field] : []);
|
|
|
|
|
|
|
|
|
|
if (arrrec.length > 0) {
|
|
|
|
|
return arrrec.map(m => m.username);
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
MyGroupSchema.statics.getInfoGroupByGroupname = async function (idapp, groupname) {
|
2022-02-03 00:33:15 +01:00
|
|
|
|
2022-02-05 23:28:15 +01:00
|
|
|
const whatToShow = this.getWhatToShow(idapp, groupname);
|
2022-02-03 00:33:15 +01:00
|
|
|
|
2022-08-26 03:33:13 +02:00
|
|
|
const myfind = {
|
2022-02-03 00:33:15 +01:00
|
|
|
idapp,
|
|
|
|
|
groupname,
|
2022-08-26 03:33:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const query = [
|
2023-01-03 16:51:32 +01:00
|
|
|
{ $match: myfind },
|
2022-08-26 03:33:13 +02:00
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'circuits',
|
2023-01-03 16:51:32 +01:00
|
|
|
localField: 'mycircuits.circuitname',
|
|
|
|
|
foreignField: 'name',
|
2022-08-26 03:33:13 +02:00
|
|
|
as: 'mycircuits',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'$replaceRoot': {
|
|
|
|
|
'newRoot': {
|
|
|
|
|
'$mergeObjects': [
|
|
|
|
|
{
|
|
|
|
|
'$arrayElemAt': [
|
|
|
|
|
'$mycircuits',
|
|
|
|
|
0,
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
'$$ROOT',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-01-03 16:51:32 +01:00
|
|
|
{ $project: whatToShow },
|
2022-08-26 03:33:13 +02:00
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
2024-04-05 15:49:06 +02:00
|
|
|
|
2022-08-26 03:33:13 +02:00
|
|
|
try {
|
|
|
|
|
const ris = await MyGroup.aggregate(query);
|
|
|
|
|
|
|
|
|
|
if (ris && ris.length > 0)
|
|
|
|
|
return ris[0];
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-07-28 23:27:51 +02:00
|
|
|
|
2024-01-09 15:31:59 +01:00
|
|
|
return null;
|
2022-02-03 00:33:15 +01:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
MyGroupSchema.statics.deleteGroup = async function (idapp, usernameOrig, groupname) {
|
2022-06-17 12:30:44 +02:00
|
|
|
console.log('Gruppo ' + groupname + ' rimosso da ' + usernameOrig);
|
2023-01-03 16:51:32 +01:00
|
|
|
return await MyGroup.findOneAndRemove({ idapp, groupname });
|
2022-06-17 12:30:44 +02:00
|
|
|
};
|
|
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
MyGroupSchema.statics.getGroupsByUsername = async function (idapp, username, req) {
|
2022-02-03 00:33:15 +01:00
|
|
|
|
|
|
|
|
try {
|
2023-01-03 16:51:32 +01:00
|
|
|
const { User } = require('../models/user');
|
2022-02-03 00:33:15 +01:00
|
|
|
|
2022-02-05 23:28:15 +01:00
|
|
|
const whatToShow = this.getWhatToShow(idapp, username);
|
|
|
|
|
const whatToShow_Unknown = this.getWhatToShow_Unknown(idapp, username);
|
2022-09-02 02:25:38 +02:00
|
|
|
const arrUsernameGroups = await User.getUsernameGroupsByUsername(idapp, username);
|
2022-02-03 00:33:15 +01:00
|
|
|
// const arrUsernameReqGroups = await MyGroup.getUsernameReqGroupsByGroupname(idapp, username);
|
|
|
|
|
|
|
|
|
|
let listUsersGroup = await User.find({
|
|
|
|
|
idapp,
|
2023-01-03 16:51:32 +01:00
|
|
|
username: { $in: arrUsernameGroups },
|
2022-02-03 00:33:15 +01:00
|
|
|
$or: [
|
2023-01-03 16:51:32 +01:00
|
|
|
{ deleted: { $exists: false } },
|
|
|
|
|
{ deleted: { $exists: true, $eq: false } }],
|
2022-02-03 00:33:15 +01:00
|
|
|
}, whatToShow);
|
|
|
|
|
|
|
|
|
|
let listgroups = await MyGroup.find({
|
|
|
|
|
idapp,
|
|
|
|
|
$or: [
|
2023-01-03 16:51:32 +01:00
|
|
|
{ deleted: { $exists: false } },
|
|
|
|
|
{ deleted: { $exists: true, $eq: false } }],
|
2022-02-03 00:33:15 +01:00
|
|
|
}, whatToShow_Unknown);
|
|
|
|
|
|
|
|
|
|
/*let listRequestUsersGroup = await User.find({
|
|
|
|
|
idapp,
|
|
|
|
|
username: {$in: arrUsernameReqGroups},
|
|
|
|
|
$or: [
|
|
|
|
|
{deleted: {$exists: false}},
|
|
|
|
|
{deleted: {$exists: true, $eq: false}}],
|
|
|
|
|
}, whatToShow_Unknown);
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
let listSentRequestGroups = await MyGroup.find({
|
|
|
|
|
idapp,
|
|
|
|
|
'req_users': {
|
2023-01-03 16:51:32 +01:00
|
|
|
$elemMatch: { username: { $eq: username } },
|
2022-02-03 00:33:15 +01:00
|
|
|
},
|
|
|
|
|
$or: [
|
2023-01-03 16:51:32 +01:00
|
|
|
{ deleted: { $exists: false } },
|
|
|
|
|
{ deleted: { $exists: true, $eq: false } }],
|
2022-02-03 00:33:15 +01:00
|
|
|
}, whatToShow_Unknown);
|
|
|
|
|
|
2022-08-08 16:35:32 +02:00
|
|
|
let listRefusedGroups = await MyGroup.find({
|
|
|
|
|
idapp,
|
|
|
|
|
'refused_users': {
|
2023-01-03 16:51:32 +01:00
|
|
|
$elemMatch: { username: { $eq: username } },
|
2022-08-08 16:35:32 +02:00
|
|
|
},
|
|
|
|
|
$or: [
|
2023-01-03 16:51:32 +01:00
|
|
|
{ deleted: { $exists: false } },
|
|
|
|
|
{ deleted: { $exists: true, $eq: false } }],
|
2022-08-08 16:35:32 +02:00
|
|
|
}, whatToShow_Unknown);
|
|
|
|
|
|
2022-02-03 00:33:15 +01:00
|
|
|
return {
|
|
|
|
|
listUsersGroup,
|
|
|
|
|
listgroups,
|
|
|
|
|
listSentRequestGroups,
|
2022-08-08 16:35:32 +02:00
|
|
|
listRefusedGroups,
|
2022-02-08 23:14:08 +01:00
|
|
|
mygroups: req.user.profile.mygroups,
|
2022-02-03 00:33:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log('Error', e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
listUsersGroup: [],
|
|
|
|
|
listRequestUsersGroup: [],
|
|
|
|
|
listTrusted: [],
|
|
|
|
|
listSentRequestGroups: [],
|
2022-08-08 16:35:32 +02:00
|
|
|
listRefusedGroups: [],
|
2022-02-03 00:33:15 +01:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
MyGroupSchema.statics.extractCitiesName = async function (idapp, id) {
|
2022-08-10 17:07:02 +02:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
let aggr1 = [
|
|
|
|
|
{
|
2023-01-03 16:51:32 +01:00
|
|
|
$match: { idapp, _id: id },
|
2022-08-10 17:07:02 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: 'cities',
|
|
|
|
|
localField: 'idCity',
|
|
|
|
|
foreignField: '_id',
|
|
|
|
|
as: 'mycities',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'$replaceRoot': {
|
|
|
|
|
'newRoot': {
|
|
|
|
|
'$mergeObjects': [
|
|
|
|
|
{
|
|
|
|
|
'$arrayElemAt': [
|
|
|
|
|
'$mycities',
|
|
|
|
|
0,
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
'$$ROOT',
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$project: {
|
|
|
|
|
"mycities.comune": 1,
|
|
|
|
|
"mycities.prov": 1
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
ris = await this.aggregate(aggr1);
|
|
|
|
|
|
|
|
|
|
return ris;
|
2023-01-03 16:51:32 +01:00
|
|
|
} catch (e) {
|
2022-08-10 17:07:02 +02:00
|
|
|
console.error('e', e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
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 },
|
2023-03-11 01:01:11 +01:00
|
|
|
{
|
|
|
|
|
$push: {
|
|
|
|
|
'mycircuits': {
|
|
|
|
|
circuitname,
|
|
|
|
|
date: new Date(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-01-03 16:51:32 +01:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Rimuovo il Circuito all'interno del Gruppo
|
|
|
|
|
MyGroupSchema.statics.removeCircuitFromGroup = async function (idapp, groupname, circuitname) {
|
2023-12-06 00:41:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
const ris = await this.updateOne({ idapp, groupname },
|
2023-01-03 16:51:32 +01:00
|
|
|
{ $pull: { 'mycircuits': { circuitname: { $in: [circuitname] } } } });
|
|
|
|
|
|
2023-12-06 00:41:50 +01:00
|
|
|
const circuitId = await Circuit.getCircuitIdByName(idapp, circuitname);
|
|
|
|
|
let remove = false;
|
|
|
|
|
|
|
|
|
|
// Se il mio account non è stato utilizzato, allora lo cancello anche questo
|
|
|
|
|
const myaccount = await Account.getAccountByUsernameAndCircuitId(idapp, username, circuitId, false, false, groupname, '');
|
|
|
|
|
if (myaccount && myaccount.totTransato === 0) {
|
|
|
|
|
remove = true;
|
|
|
|
|
} else {
|
|
|
|
|
remove = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (remove) {
|
|
|
|
|
await Account.removeAccount(myaccount._id);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
};
|
|
|
|
|
|
2023-03-11 01:01:11 +01:00
|
|
|
MyGroupSchema.statics.getQueryReceiveRISGroups = function (idapp, hours) {
|
|
|
|
|
|
|
|
|
|
const query = [
|
|
|
|
|
{
|
|
|
|
|
$match: {
|
|
|
|
|
idapp,
|
|
|
|
|
'lastdate_reqRisGroup': { $gte: tools.IncDateNow(-(1000 * 60 * 60 * hours)) },
|
|
|
|
|
$or: [
|
|
|
|
|
{ deleted: { $exists: false } },
|
|
|
|
|
{ deleted: { $exists: true, $eq: false } }],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$group:
|
|
|
|
|
{
|
|
|
|
|
_id: "$groupname",
|
|
|
|
|
count: {
|
|
|
|
|
$sum: 1,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{ $sort: { 'lastdate_reqRisGroup': -1 } },
|
|
|
|
|
{ $limit: 30 },
|
|
|
|
|
{
|
|
|
|
|
$lookup: {
|
|
|
|
|
from: "mygroups",
|
|
|
|
|
let: {
|
|
|
|
|
groupname: "$_id",
|
|
|
|
|
idapp,
|
|
|
|
|
},
|
|
|
|
|
pipeline: [
|
|
|
|
|
{
|
|
|
|
|
$match: {
|
|
|
|
|
$expr: {
|
|
|
|
|
$and: [
|
|
|
|
|
{
|
|
|
|
|
$eq: [
|
|
|
|
|
"$$groupname",
|
|
|
|
|
"$groupname",
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$eq: [
|
|
|
|
|
"$$idapp",
|
|
|
|
|
"$idapp",
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
as: "mygroup",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{ $unwind: "$mygroup" },
|
|
|
|
|
{
|
|
|
|
|
$replaceRoot: {
|
|
|
|
|
newRoot: {
|
|
|
|
|
$mergeObjects: ["$mygroup", "$$ROOT"],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$project: {
|
|
|
|
|
_id: 0,
|
|
|
|
|
groupname: 1,
|
|
|
|
|
title: 1,
|
|
|
|
|
descr: 1,
|
|
|
|
|
visibility: 1,
|
|
|
|
|
idCatGrp: 1,
|
|
|
|
|
userId: 1,
|
|
|
|
|
photos: 1,
|
|
|
|
|
idCity: 1,
|
|
|
|
|
website: 1,
|
|
|
|
|
link_telegram: 1,
|
|
|
|
|
note: 1,
|
|
|
|
|
admins: 1,
|
|
|
|
|
blocked: 1,
|
|
|
|
|
req_users: 1,
|
|
|
|
|
createdBy: 1,
|
|
|
|
|
date_created: 1,
|
|
|
|
|
date_updated: 1,
|
|
|
|
|
lastdate_reqRisGroup: 1,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
return query;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MyGroupSchema.statics.getReceiveRISGroups = async function (idapp) {
|
|
|
|
|
|
|
|
|
|
return await this.aggregate(this.getQueryReceiveRISGroups(idapp, 8)).then(ris => {
|
|
|
|
|
return ris;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-25 23:26:16 +02:00
|
|
|
|
|
|
|
|
MyGroupSchema.statics.renameCircuitName = async function (idapp, oldcircuitname, newcircuitname) {
|
|
|
|
|
|
|
|
|
|
return await this.updateMany({ idapp, 'mycircuits.circuitname': oldcircuitname }, { $set: { 'profile.mycircuits.$.circuitname': newcircuitname } });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2023-03-11 01:01:11 +01:00
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2022-08-10 17:07:02 +02:00
|
|
|
|
2022-02-03 00:33:15 +01:00
|
|
|
const MyGroup = mongoose.model('MyGroup', MyGroupSchema);
|
|
|
|
|
|
2023-12-09 11:55:58 +01:00
|
|
|
MyGroup.createIndexes((err) => {
|
|
|
|
|
if (err) throw err;
|
|
|
|
|
});
|
|
|
|
|
|
2023-01-03 16:51:32 +01:00
|
|
|
module.exports = { MyGroup };
|