Add to the Circuit
Remove to the Circuit Revoke request Users Admins
This commit is contained in:
@@ -7,7 +7,6 @@ mongoose.level = 'F';
|
||||
const tools = require('../tools/general');
|
||||
|
||||
const {ObjectID} = require('mongodb');
|
||||
const {User} = require('./user');
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
mongoose.plugin(schema => {
|
||||
@@ -49,9 +48,6 @@ const CircuitSchema = new Schema({
|
||||
systemUserId: {
|
||||
type: String,
|
||||
},
|
||||
founderUserId: {
|
||||
type: String,
|
||||
},
|
||||
totCircolante: {
|
||||
type: Number,
|
||||
},
|
||||
@@ -149,6 +145,16 @@ const CircuitSchema = new Schema({
|
||||
},
|
||||
});
|
||||
|
||||
CircuitSchema.pre('save', async function(next) {
|
||||
if (this.isNew) {
|
||||
|
||||
this.date_created = new Date();
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
|
||||
CircuitSchema.statics.findAllIdApp = async function(idapp) {
|
||||
const Circuit = this;
|
||||
|
||||
@@ -186,7 +192,7 @@ CircuitSchema.statics.getFieldsForSearch = function() {
|
||||
|
||||
CircuitSchema.statics.executeQueryTable = function(idapp, params) {
|
||||
params.fieldsearch = this.getFieldsForSearch();
|
||||
return tools.executeQueryTable(this, 0, params);
|
||||
return tools.executeQueryTable(this, idapp, params);
|
||||
};
|
||||
|
||||
CircuitSchema.statics.getWhatToShow = function(idapp, username) {
|
||||
@@ -201,18 +207,23 @@ CircuitSchema.statics.getWhatToShow = function(idapp, username) {
|
||||
longdescr: 1,
|
||||
regulation: 1,
|
||||
systemUserId: 1,
|
||||
founderUserId: 1,
|
||||
createdBy: 1,
|
||||
date_created: 1,
|
||||
date_updated: 1,
|
||||
nome_valuta: 1,
|
||||
symbol: 1,
|
||||
abbrev: 1,
|
||||
data_costituz: 1,
|
||||
img_logo: 1,
|
||||
admins: 1,
|
||||
req_users: 1,
|
||||
refused_users: 1,
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
// Rimuovi dagli Admin del Circuito
|
||||
MyGroupSchema.statics.removeAdminOfMyCircuit = async function(idapp, username, name) {
|
||||
CircuitSchema.statics.removeAdminOfMyCircuit = async function(idapp, username, name) {
|
||||
|
||||
return Circuit.updateOne({idapp, name},
|
||||
{$pull: {admins: {username: {$in: [username]}}}});
|
||||
@@ -236,6 +247,12 @@ CircuitSchema.statics.getWhatToShow_Unknown = function(idapp, username) {
|
||||
abbrev: 1,
|
||||
data_costituz: 1,
|
||||
img_logo: 1,
|
||||
admins: 1,
|
||||
createdBy: 1,
|
||||
date_created: 1,
|
||||
date_updated: 1,
|
||||
req_users: 1,
|
||||
refused_users: 1,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -247,20 +264,29 @@ CircuitSchema.statics.getCircuitsByUsername = async function(idapp, username, re
|
||||
|
||||
const whatToShow = this.getWhatToShow(idapp, username);
|
||||
const whatToShow_Unknown = this.getWhatToShow_Unknown(idapp, username);
|
||||
// const arrUsernameCircuits = await User.getUsernameCircuitsByUsername(idapp,
|
||||
// username);
|
||||
// const arrUsernameCircuits = await User.getUsernameCircuitsByUsername(idapp, username);
|
||||
// const arrUsernameReqCircuits = await MyCircuit.getUsernameReqCircuitsByCircuitname(idapp, username);
|
||||
|
||||
let listUserAccounts = await Account.getAccountsByUsername(idapp, username);
|
||||
|
||||
const manage_mycircuits = await Circuit.find({
|
||||
idapp,
|
||||
'admins': {
|
||||
$elemMatch: {username: {$eq: username}},
|
||||
},
|
||||
$or: [
|
||||
{deleted: {$exists: false}},
|
||||
{deleted: {$exists: true, $eq: false}}],
|
||||
}).lean();
|
||||
|
||||
let listcircuits = await Circuit.find({
|
||||
idapp,
|
||||
$or: [
|
||||
{deleted: {$exists: false}},
|
||||
{deleted: {$exists: true, $eq: false}}],
|
||||
}, whatToShow_Unknown);
|
||||
}, whatToShow_Unknown).lean();
|
||||
|
||||
let listSentRequestCircuits = await Circuit.find({
|
||||
let asked_circuits = await Circuit.find({
|
||||
idapp,
|
||||
'req_users': {
|
||||
$elemMatch: {username: {$eq: username}},
|
||||
@@ -268,9 +294,9 @@ CircuitSchema.statics.getCircuitsByUsername = async function(idapp, username, re
|
||||
$or: [
|
||||
{deleted: {$exists: false}},
|
||||
{deleted: {$exists: true, $eq: false}}],
|
||||
}, whatToShow_Unknown);
|
||||
}, whatToShow_Unknown).lean();
|
||||
|
||||
let listRefusedCircuits = await Circuit.find({
|
||||
let refused_circuits = await Circuit.find({
|
||||
idapp,
|
||||
'refused_users': {
|
||||
$elemMatch: {username: {$eq: username}},
|
||||
@@ -278,29 +304,78 @@ CircuitSchema.statics.getCircuitsByUsername = async function(idapp, username, re
|
||||
$or: [
|
||||
{deleted: {$exists: false}},
|
||||
{deleted: {$exists: true, $eq: false}}],
|
||||
}, whatToShow_Unknown);
|
||||
}, whatToShow_Unknown).lean();
|
||||
|
||||
return {
|
||||
listUserAccounts,
|
||||
listcircuits,
|
||||
listSentRequestCircuits,
|
||||
listRefusedCircuits,
|
||||
asked_circuits,
|
||||
refused_circuits,
|
||||
manage_mycircuits,
|
||||
mycircuits: req.user.profile.mycircuits,
|
||||
};
|
||||
|
||||
} catch (e) {
|
||||
console.log('Error', e);
|
||||
console.log('Error getCircuitsByUsername', e);
|
||||
}
|
||||
|
||||
return {
|
||||
listUsersCircuit: [],
|
||||
listRequestUsersCircuit: [],
|
||||
listTrusted: [],
|
||||
listSentRequestCircuits: [],
|
||||
listRefusedCircuits: [],
|
||||
asked_circuits: [],
|
||||
refused_circuits: [],
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
CircuitSchema.statics.getInfoCircuitByName = async function(idapp, name) {
|
||||
|
||||
const whatToShow = this.getWhatToShow(idapp, '');
|
||||
|
||||
const myfind = {
|
||||
idapp,
|
||||
name,
|
||||
};
|
||||
|
||||
try {
|
||||
return await Circuit.findOne(myfind, whatToShow).lean();
|
||||
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
CircuitSchema.statics.deleteCircuit = async function(idapp, usernameOrig, name) {
|
||||
console.log('Circuito ' + name + ' rimosso da ' + usernameOrig);
|
||||
return Circuit.findOneAndRemove({idapp, name});
|
||||
};
|
||||
|
||||
// Rimuovo la Richiesta del Circuito
|
||||
CircuitSchema.statics.removeReqCircuit = async function(idapp, username, name) {
|
||||
|
||||
return Circuit.updateOne({idapp, name},
|
||||
{$pull: {req_users: {username: {$in: [username]}}}});
|
||||
};
|
||||
|
||||
// Aggiungi agli utenti Rifiutati del Circuito
|
||||
CircuitSchema.statics.refuseReqCircuit = async function(idapp, username, name) {
|
||||
|
||||
return Circuit.updateOne({idapp, name},
|
||||
{
|
||||
$push:
|
||||
{
|
||||
refused_users: {
|
||||
username,
|
||||
date: new Date(),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
|
||||
const Circuit = mongoose.model('Circuit', CircuitSchema);
|
||||
|
||||
module.exports = {Circuit};
|
||||
|
||||
Reference in New Issue
Block a user