- Gruppi (3) - lista degli utenti del gruppo
This commit is contained in:
83
src/server/models/catgrp.js
Executable file
83
src/server/models/catgrp.js
Executable file
@@ -0,0 +1,83 @@
|
||||
const mongoose = require('mongoose').set('debug', false)
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.level = "F";
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
const { ObjectID } = require('mongodb');
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
mongoose.plugin(schema => {
|
||||
schema.options.usePushEach = true
|
||||
});
|
||||
|
||||
const CatGrpSchema = new Schema({
|
||||
_id: {
|
||||
type: Number,
|
||||
},
|
||||
descr: {
|
||||
type: String,
|
||||
},
|
||||
idCatGrp: {
|
||||
type: Number
|
||||
},
|
||||
icon: {
|
||||
type: String,
|
||||
},
|
||||
img: {
|
||||
type: String,
|
||||
},
|
||||
color: {
|
||||
type: String,
|
||||
},
|
||||
theme: {
|
||||
type: String,
|
||||
},
|
||||
});
|
||||
|
||||
CatGrpSchema.pre('save', async function (next) {
|
||||
if (this.isNew) {
|
||||
const myrec = await CatGrp.findOne().limit(1).sort({_id:-1});
|
||||
if (!!myrec) {
|
||||
if (myrec._doc._id === 0)
|
||||
this._id = 1;
|
||||
else
|
||||
this._id = myrec._doc._id + 1;
|
||||
} else {
|
||||
this._id = 1;
|
||||
}
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
CatGrpSchema.statics.findAllIdApp = async function (idapp) {
|
||||
const CatGrp = this;
|
||||
|
||||
const query = [
|
||||
{ $sort: { descr: 1 } }
|
||||
];
|
||||
|
||||
return CatGrp
|
||||
.aggregate(query)
|
||||
.then((arrrec) => {
|
||||
return arrrec
|
||||
})
|
||||
|
||||
};
|
||||
|
||||
CatGrpSchema.statics.getFieldsForSearch = function () {
|
||||
return [{ field: 'descr', type: tools.FieldType.string }]
|
||||
};
|
||||
|
||||
CatGrpSchema.statics.executeQueryTable = function (idapp, params) {
|
||||
params.fieldsearch = this.getFieldsForSearch();
|
||||
return tools.executeQueryTable(this, 0, params);
|
||||
};
|
||||
|
||||
|
||||
const CatGrp = mongoose.model('CatGrp', CatGrpSchema);
|
||||
|
||||
module.exports = { CatGrp };
|
||||
@@ -3,6 +3,8 @@ const Schema = mongoose.Schema;
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
const shared_consts = require('../tools/shared_nodejs');
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.level = 'F';
|
||||
|
||||
@@ -24,7 +26,7 @@ const MyGroupSchema = new Schema({
|
||||
descr: {
|
||||
type: String,
|
||||
},
|
||||
idSector: {
|
||||
idCatGrp: {
|
||||
type: Number,
|
||||
},
|
||||
userId: {
|
||||
@@ -52,12 +54,20 @@ const MyGroupSchema = new Schema({
|
||||
link_telegram: {
|
||||
type: String,
|
||||
},
|
||||
visibility: {
|
||||
type: Number,
|
||||
note: {
|
||||
type: String,
|
||||
default: '',
|
||||
},
|
||||
visibility: [
|
||||
{
|
||||
type: Number,
|
||||
},
|
||||
],
|
||||
pwd_cryp: {
|
||||
type: String,
|
||||
},
|
||||
admins: [
|
||||
{
|
||||
_id: false,
|
||||
username: {type: String},
|
||||
date: {type: Date},
|
||||
},
|
||||
@@ -93,6 +103,14 @@ MyGroupSchema.statics.getFieldsForSearch = function() {
|
||||
|
||||
MyGroupSchema.statics.executeQueryTable = function(idapp, params) {
|
||||
params.fieldsearch = this.getFieldsForSearch();
|
||||
|
||||
if (params.options) {
|
||||
if (tools.isBitActive(params.options,
|
||||
shared_consts.OPTIONS_SEARCH_USER_ONLY_FULL_WORDS)) {
|
||||
params.fieldsearch = User.getFieldsForSearchUserFriend();
|
||||
}
|
||||
}
|
||||
|
||||
return tools.executeQueryTable(this, idapp, params);
|
||||
};
|
||||
|
||||
@@ -103,42 +121,43 @@ MyGroupSchema.statics.findAllIdApp = async function(idapp) {
|
||||
};
|
||||
|
||||
// Rimuovo la Richiesta del Gruppo
|
||||
MyGroupSchema.statics.removeReqGroup = async function(
|
||||
idapp, username, groupnameDest) {
|
||||
const {User} = require('../models/user');
|
||||
MyGroupSchema.statics.removeReqGroup = async function(idapp, username, groupnameDest) {
|
||||
|
||||
return User.updateOne({idapp, username: username},
|
||||
{$pull: {'profile.req_groups': {groupname: {$in: [groupnameDest]}}}});
|
||||
return MyGroup.updateOne({idapp, groupname: groupnameDest},
|
||||
{$pull: {req_users: {username: {$in: [username]}}}});
|
||||
};
|
||||
|
||||
function getWhatToShow(idapp, username) {
|
||||
// ++Todo: MyGroup what to show
|
||||
MyGroupSchema.statics.getWhatToShow = function (idapp, username) {
|
||||
// FOR ME, PERMIT ALL
|
||||
return {
|
||||
groupname: 1,
|
||||
title: 1,
|
||||
descr: 1,
|
||||
visibility: 1,
|
||||
idSector: 1,
|
||||
idCatGrp: 1,
|
||||
userId: 1,
|
||||
photos: 1,
|
||||
idCity: 1,
|
||||
website: 1,
|
||||
link_telegram: 1,
|
||||
note: 1,
|
||||
admins: 1,
|
||||
blocked: 1,
|
||||
req_users: 1,
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
function getWhatToShow_Unknown(idapp, username) {
|
||||
MyGroupSchema.statics.getWhatToShow_Unknown = function (idapp, username) {
|
||||
return {
|
||||
groupname: 1,
|
||||
title: 1,
|
||||
descr: 1,
|
||||
photos: 1,
|
||||
visibility: 1,
|
||||
idSector: 1,
|
||||
idCatGrp: 1,
|
||||
idCity: 1,
|
||||
note: 1,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -170,10 +189,9 @@ MyGroupSchema.statics.getUsernameReqGroupsByGroupname = async function(
|
||||
|
||||
};
|
||||
|
||||
MyGroupSchema.statics.getInfoGroupByGroupname = async function(
|
||||
idapp, groupname) {
|
||||
MyGroupSchema.statics.getInfoGroupByGroupname = async function(idapp, groupname) {
|
||||
|
||||
const whatToShow = getWhatToShow(idapp, groupname);
|
||||
const whatToShow = this.getWhatToShow(idapp, groupname);
|
||||
|
||||
return MyGroup.findOne({
|
||||
idapp,
|
||||
@@ -187,8 +205,8 @@ MyGroupSchema.statics.getGroupsByUsername = async function(idapp, username) {
|
||||
try {
|
||||
const {User} = require('../models/user');
|
||||
|
||||
const whatToShow = getWhatToShow(idapp, username);
|
||||
const whatToShow_Unknown = getWhatToShow_Unknown(idapp, username);
|
||||
const whatToShow = this.getWhatToShow(idapp, username);
|
||||
const whatToShow_Unknown = this.getWhatToShow_Unknown(idapp, username);
|
||||
const arrUsernameGroups = await User.getUsernameGroupsByUsername(idapp,
|
||||
username);
|
||||
// const arrUsernameReqGroups = await MyGroup.getUsernameReqGroupsByGroupname(idapp, username);
|
||||
|
||||
@@ -64,6 +64,7 @@ const MySkillSchema = new Schema({
|
||||
}],
|
||||
note: {
|
||||
type: String,
|
||||
default: '',
|
||||
},
|
||||
subTitle: {
|
||||
type: String,
|
||||
|
||||
@@ -342,7 +342,7 @@ const UserSchema = new mongoose.Schema({
|
||||
username: {type: String},
|
||||
date: {type: Date},
|
||||
}], // username
|
||||
groups: [
|
||||
mygroups: [
|
||||
{
|
||||
_id: false,
|
||||
groupname: {type: String},
|
||||
@@ -1314,7 +1314,7 @@ UserSchema.statics.removeFriend = async function(idapp, username, usernameDest)
|
||||
// Rimuovo il Gruppo
|
||||
UserSchema.statics.removeFromMyGroups = async function(idapp, username, groupnameDest) {
|
||||
return User.updateOne({idapp, username},
|
||||
{$pull: {'profile.groups': {groupname: {$in: [groupnameDest]}}}});
|
||||
{$pull: {'profile.mygroups': {groupname: {$in: [groupnameDest]}}}});
|
||||
};
|
||||
|
||||
// Rimuovo la Richiesta di Amicizia
|
||||
@@ -1463,11 +1463,11 @@ UserSchema.statics.setGroupsCmd = async function(idapp, usernameOrig, groupnameD
|
||||
let update = {};
|
||||
try {
|
||||
if (cmd === shared_consts.GROUPSCMD.SETGROUP) {
|
||||
// Aggiungo l'Amicizia a me
|
||||
// Controllo se è stato già inserito
|
||||
const foundIfAlreadyGroup = await User.findOne({
|
||||
idapp,
|
||||
username: usernameOrig,
|
||||
'profile.groups': {
|
||||
'profile.mygroups': {
|
||||
$elemMatch: {groupname: {$eq: groupnameDest}},
|
||||
},
|
||||
});
|
||||
@@ -1475,7 +1475,7 @@ UserSchema.statics.setGroupsCmd = async function(idapp, usernameOrig, groupnameD
|
||||
if (!foundIfAlreadyGroup) {
|
||||
update = {
|
||||
$push: {
|
||||
'profile.groups': {
|
||||
'profile.mygroups': {
|
||||
groupname: groupnameDest,
|
||||
date: new Date(),
|
||||
},
|
||||
@@ -1483,8 +1483,11 @@ UserSchema.statics.setGroupsCmd = async function(idapp, usernameOrig, groupnameD
|
||||
};
|
||||
ris = await User.updateOne({idapp, username: usernameOrig}, update);
|
||||
|
||||
update = {$pull: {'profile.req_groups': {groupname: {$in: [groupnameDest]}}}};
|
||||
ris = await User.updateOne({idapp, username: usernameOrig}, update);
|
||||
// Elimina la richiesta:
|
||||
update = {$pull: {req_users: {username: {$in: [usernameOrig]}}}};
|
||||
ris = await MyGroup.updateOne({idapp, groupname: groupnameDest}, update);
|
||||
} else {
|
||||
ris = false;
|
||||
}
|
||||
|
||||
if (ris) {
|
||||
@@ -1495,7 +1498,7 @@ UserSchema.statics.setGroupsCmd = async function(idapp, usernameOrig, groupnameD
|
||||
const foundIfAlreadyAskGroup = await MyGroup.findOne({
|
||||
idapp,
|
||||
groupname: groupnameDest,
|
||||
'req_groups': {
|
||||
'req_users': {
|
||||
$elemMatch: { username: {$eq: usernameOrig}},
|
||||
},
|
||||
});
|
||||
@@ -1504,31 +1507,33 @@ UserSchema.statics.setGroupsCmd = async function(idapp, usernameOrig, groupnameD
|
||||
if (!foundIfAlreadyAskGroup) {
|
||||
update = {
|
||||
$push: {
|
||||
'profile.req_groups': {
|
||||
'req_users': {
|
||||
username: usernameOrig,
|
||||
date: new Date(),
|
||||
},
|
||||
},
|
||||
};
|
||||
ris = await User.updateOne({idapp, username: groupnameDest}, update);
|
||||
ris = await MyGroup.updateOne({idapp, groupname: groupnameDest}, update);
|
||||
}
|
||||
if (ris) {
|
||||
// Invia una notifica alla persona
|
||||
tools.sendNotificationByGroupName(idapp, groupnameDest, cmd, true);
|
||||
tools.sendNotificationByGroupname(idapp, groupnameDest, cmd, true);
|
||||
}
|
||||
} else {
|
||||
if (foundIfAlreadyAskGroup) {
|
||||
ris = await this.removeFromMyGroups(idapp, groupnameDest, usernameOrig); // Rimuovo l'Amicizia da me
|
||||
ris = await this.removeFromMyGroups(idapp, usernameOrig, groupnameDest); // Rimuovo il Gruppo da me
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (ris) {
|
||||
ris = await User.getInfoAskGroupByUsername(idapp, groupnameDest);
|
||||
ris = await MyGroup.getInfoGroupByGroupname(idapp, groupnameDest);
|
||||
}
|
||||
|
||||
} else if (cmd === shared_consts.GROUPSCMD.REMOVE_FROM_MYGROUPS) {
|
||||
} else if (cmd === shared_consts.GROUPSCMD.REMOVE_FROM_MYGROUP) {
|
||||
|
||||
ris = await this.removeFromMyGroups(idapp, usernameOrig, groupnameDest); // Rimuovo l'Amicizia da me
|
||||
ris = await User.removeFromMyGroups(idapp, usernameOrig, groupnameDest); // Rimuovo l'Amicizia da me
|
||||
console.log('ris', ris);
|
||||
|
||||
} else if (cmd === shared_consts.GROUPSCMD.CANCEL_REQ_GROUP) {
|
||||
|
||||
@@ -1536,10 +1541,10 @@ UserSchema.statics.setGroupsCmd = async function(idapp, usernameOrig, groupnameD
|
||||
|
||||
} else if (cmd === shared_consts.GROUPSCMD.BLOCK_GROUP) {
|
||||
|
||||
await this.removeFromMyGroups(idapp, usernameOrig, groupnameDest); // Rimuovo l'Amicizia da me
|
||||
await User.removeFromMyGroups(idapp, usernameOrig, groupnameDest); // Rimuovo l'Amicizia da me
|
||||
|
||||
// Blocco il Gruppo
|
||||
ris = await MyGroup.updateOne({idapp, username: groupnameDest}, {
|
||||
ris = await MyGroup.updateOne({idapp, groupname: groupnameDest}, {
|
||||
$set: {
|
||||
blocked: true,
|
||||
username_who_block: usernameOrig,
|
||||
@@ -1547,6 +1552,7 @@ UserSchema.statics.setGroupsCmd = async function(idapp, usernameOrig, groupnameD
|
||||
},
|
||||
});
|
||||
//++Todo: Send Notification to Admin and Group's manager
|
||||
tools.sendNotificationByGroupname(idapp, groupnameDest, cmd, true);
|
||||
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -1601,6 +1607,26 @@ function getWhatToShow_Unknown(idapp, username) {
|
||||
|
||||
}
|
||||
|
||||
UserSchema.statics.getWhatToShow_IfFriends = async function(idapp, username) {
|
||||
return {
|
||||
username: 1,
|
||||
aportador_solidario: 1,
|
||||
name: 1,
|
||||
// deleted: 1,
|
||||
// sospeso: 1,
|
||||
verified_email: 1,
|
||||
verified_by_aportador: 1,
|
||||
'profile.img': 1,
|
||||
'profile.sex': 1,
|
||||
'profile.born_province': 1,
|
||||
'profile.born_country': 1,
|
||||
date_reg: 1,
|
||||
groups: 1,
|
||||
friends: 1,
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
UserSchema.statics.getInfoFriendByUsername = async function(idapp, username) {
|
||||
|
||||
const whatToShow = getWhatToShow(idapp, username);
|
||||
@@ -2935,17 +2961,32 @@ UserSchema.statics.addExtraInfo = async function(idapp, recUser) {
|
||||
|
||||
recUser._doc.profile.asked_friends = listSentMyRequestFriends ? listSentMyRequestFriends : [];
|
||||
|
||||
const listSentMyRequestGroups = await User.find({
|
||||
const listSentMyRequestGroups = await MyGroup.find({
|
||||
idapp,
|
||||
'profile.req_groups': {
|
||||
'req_users': {
|
||||
$elemMatch: {username: {$eq: recUser.username}},
|
||||
},
|
||||
$or: [
|
||||
{deleted: {$exists: false}},
|
||||
{deleted: {$exists: true, $eq: false}}],
|
||||
}, {username: 1});
|
||||
}, MyGroup.getWhatToShow_Unknown());
|
||||
|
||||
recUser._doc.profile.asked_groups = listSentMyRequestGroups ? listSentMyRequestGroups : [];
|
||||
|
||||
const listManageGroups = await MyGroup.find({
|
||||
idapp,
|
||||
'admins': {
|
||||
$elemMatch: {username: {$eq: recUser.username}},
|
||||
},
|
||||
$or: [
|
||||
{deleted: {$exists: false}},
|
||||
{deleted: {$exists: true, $eq: false}}],
|
||||
});
|
||||
|
||||
recUser._doc.profile.manage_mygroups = listManageGroups ? listManageGroups : [];
|
||||
|
||||
|
||||
|
||||
}catch (e){
|
||||
console.error('Err', e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user