2022-02-03 00:33:15 +01:00
|
|
|
const mongoose = require('mongoose').set('debug', false);
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
|
|
// Resolving error Unknown modifier: $pushAll
|
|
|
|
|
mongoose.plugin(schema => {
|
|
|
|
|
schema.options.usePushEach = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const MyGroupSchema = new Schema({
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
admins: [
|
|
|
|
|
{
|
|
|
|
|
username: {type: String},
|
|
|
|
|
date: {type: Date},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
blocked: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
},
|
|
|
|
|
username_who_block: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
date_created: {
|
|
|
|
|
type: Date,
|
|
|
|
|
default: Date.now,
|
|
|
|
|
},
|
|
|
|
|
date_blocked: {
|
|
|
|
|
type: Date,
|
|
|
|
|
},
|
|
|
|
|
req_users: [
|
|
|
|
|
{
|
|
|
|
|
_id: false,
|
|
|
|
|
username: {type: String},
|
|
|
|
|
date: {type: Date},
|
|
|
|
|
}], // username
|
|
|
|
|
deleted: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: false,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
MyGroupSchema.statics.getFieldsForSearch = function() {
|
|
|
|
|
return [{field: 'descr', type: tools.FieldType.string}];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MyGroupSchema.statics.executeQueryTable = function(idapp, params) {
|
|
|
|
|
params.fieldsearch = this.getFieldsForSearch();
|
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-02-03 00:33:15 +01:00
|
|
|
return tools.executeQueryTable(this, idapp, params);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MyGroupSchema.statics.findAllIdApp = async function(idapp) {
|
|
|
|
|
const myfind = {idapp};
|
|
|
|
|
|
|
|
|
|
return await MyGroup.find(myfind);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Rimuovo la Richiesta del Gruppo
|
2022-02-05 23:28:15 +01:00
|
|
|
MyGroupSchema.statics.removeReqGroup = async function(idapp, username, groupnameDest) {
|
2022-02-03 00:33:15 +01:00
|
|
|
|
2022-02-05 23:28:15 +01:00
|
|
|
return MyGroup.updateOne({idapp, groupname: groupnameDest},
|
|
|
|
|
{$pull: {req_users: {username: {$in: [username]}}}});
|
2022-02-03 00:33:15 +01:00
|
|
|
};
|
|
|
|
|
|
2022-02-05 23:28:15 +01:00
|
|
|
MyGroupSchema.statics.getWhatToShow = function (idapp, username) {
|
|
|
|
|
// 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-02-03 00:33:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-05 23:28:15 +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-02-03 00:33:15 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MyGroupSchema.statics.getArrUsernameFromFieldByGroupname = async function(
|
|
|
|
|
idapp, groupname, field) {
|
|
|
|
|
|
|
|
|
|
const {User} = require('../models/user');
|
|
|
|
|
|
|
|
|
|
const myobj = {};
|
|
|
|
|
myobj[field + '.' + subfield] = 1;
|
|
|
|
|
|
|
|
|
|
let arrrec = await User.findOne({
|
|
|
|
|
idapp,
|
|
|
|
|
groupname,
|
|
|
|
|
$or: [{deleted: {$exists: false}}, {deleted: {$exists: true, $eq: false}}],
|
|
|
|
|
}, myobj).then((ris) => ris ? ris._doc[field] : []);
|
|
|
|
|
|
|
|
|
|
if (arrrec.length > 0) {
|
|
|
|
|
return arrrec.map(m => m.username);
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MyGroupSchema.statics.getUsernameReqGroupsByGroupname = async function(
|
|
|
|
|
idapp, groupname) {
|
|
|
|
|
|
|
|
|
|
return this.getArrUsernameFromFieldByGroupname(idapp, groupname, 'req_users');
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-05 23:28:15 +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
|
|
|
|
|
|
|
|
return MyGroup.findOne({
|
|
|
|
|
idapp,
|
|
|
|
|
groupname,
|
|
|
|
|
}, whatToShow).then((rec) => !!rec ? rec._doc : null);
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-08 23:14:08 +01:00
|
|
|
MyGroupSchema.statics.getGroupsByUsername = async function(idapp, username, req) {
|
2022-02-03 00:33:15 +01:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const {User} = require('../models/user');
|
|
|
|
|
|
2022-02-05 23:28:15 +01:00
|
|
|
const whatToShow = this.getWhatToShow(idapp, username);
|
|
|
|
|
const whatToShow_Unknown = this.getWhatToShow_Unknown(idapp, username);
|
2022-02-03 00:33:15 +01:00
|
|
|
const arrUsernameGroups = await User.getUsernameGroupsByUsername(idapp,
|
|
|
|
|
username);
|
|
|
|
|
// const arrUsernameReqGroups = await MyGroup.getUsernameReqGroupsByGroupname(idapp, username);
|
|
|
|
|
|
|
|
|
|
let listUsersGroup = await User.find({
|
|
|
|
|
idapp,
|
|
|
|
|
username: {$in: arrUsernameGroups},
|
|
|
|
|
$or: [
|
|
|
|
|
{deleted: {$exists: false}},
|
|
|
|
|
{deleted: {$exists: true, $eq: false}}],
|
|
|
|
|
}, whatToShow);
|
|
|
|
|
|
|
|
|
|
let listgroups = await MyGroup.find({
|
|
|
|
|
idapp,
|
|
|
|
|
$or: [
|
|
|
|
|
{deleted: {$exists: false}},
|
|
|
|
|
{deleted: {$exists: true, $eq: false}}],
|
|
|
|
|
}, 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': {
|
|
|
|
|
$elemMatch: {username: {$eq: username}},
|
|
|
|
|
},
|
|
|
|
|
$or: [
|
|
|
|
|
{deleted: {$exists: false}},
|
|
|
|
|
{deleted: {$exists: true, $eq: false}}],
|
|
|
|
|
}, whatToShow_Unknown);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
listUsersGroup,
|
|
|
|
|
listgroups,
|
|
|
|
|
//listRequestUsersGroup,
|
|
|
|
|
listSentRequestGroups,
|
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: [],
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const MyGroup = mongoose.model('MyGroup', MyGroupSchema);
|
|
|
|
|
|
|
|
|
|
module.exports = {MyGroup};
|