2022-04-07 08:19:40 +02:00
|
|
|
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 CircuitSchema = new Schema({
|
2022-06-16 20:34:42 +02:00
|
|
|
Num: {
|
2022-04-07 08:19:40 +02:00
|
|
|
type: Number,
|
2022-04-24 17:02:10 +02:00
|
|
|
unique: true,
|
2022-04-07 08:19:40 +02:00
|
|
|
},
|
2022-08-30 17:00:48 +02:00
|
|
|
idapp: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
2022-08-17 00:36:47 +02:00
|
|
|
groupnameId: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2022-04-24 17:02:10 +02:00
|
|
|
name: {
|
2022-04-07 08:19:40 +02:00
|
|
|
type: String,
|
2022-04-24 17:02:10 +02:00
|
|
|
unique: true,
|
2022-04-07 08:19:40 +02:00
|
|
|
},
|
2022-08-30 17:00:48 +02:00
|
|
|
path: {
|
|
|
|
|
type: String,
|
|
|
|
|
unique: true,
|
|
|
|
|
},
|
2022-04-24 17:02:10 +02:00
|
|
|
subname: {
|
2022-04-07 08:19:40 +02:00
|
|
|
type: String,
|
|
|
|
|
},
|
2022-08-17 00:36:47 +02:00
|
|
|
longdescr: {
|
2022-04-07 08:19:40 +02:00
|
|
|
type: String,
|
|
|
|
|
},
|
2022-08-18 05:26:50 +02:00
|
|
|
regulation: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2022-04-07 08:19:40 +02:00
|
|
|
systemUserDescr: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
systemUserId: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
totCircolante: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
totTransato: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
nome_valuta: {
|
|
|
|
|
type: String,
|
|
|
|
|
maxlength: 20,
|
|
|
|
|
},
|
2022-04-24 17:02:10 +02:00
|
|
|
symbol: {
|
2022-04-07 08:19:40 +02:00
|
|
|
type: String,
|
|
|
|
|
maxlength: 3,
|
|
|
|
|
},
|
2022-04-24 17:02:10 +02:00
|
|
|
abbrev: {
|
2022-04-07 08:19:40 +02:00
|
|
|
type: String,
|
|
|
|
|
maxlength: 3,
|
|
|
|
|
},
|
|
|
|
|
compara_valuta: {
|
|
|
|
|
type: Number,
|
|
|
|
|
default: 1,
|
|
|
|
|
},
|
|
|
|
|
compara_euro: {
|
|
|
|
|
type: Number,
|
|
|
|
|
default: 1,
|
|
|
|
|
},
|
|
|
|
|
valuta_per_euro: {
|
|
|
|
|
type: Number,
|
|
|
|
|
default: 1,
|
|
|
|
|
},
|
|
|
|
|
fido_scoperto_default: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
data_costituz: {
|
|
|
|
|
type: Date,
|
|
|
|
|
},
|
|
|
|
|
deperimento: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
default: true,
|
|
|
|
|
},
|
|
|
|
|
freq_deper: { // H, D, W, M, Y
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
minuto_deper: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
ora_deper: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
giorno_deper: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
mese_deper: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
ultimo_deper: {
|
|
|
|
|
type: Date,
|
|
|
|
|
},
|
|
|
|
|
durata_deper: {
|
|
|
|
|
type: Number,
|
|
|
|
|
},
|
|
|
|
|
// -------------
|
2022-08-26 03:33:13 +02:00
|
|
|
createdBy: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
|
|
|
|
date_created: {
|
|
|
|
|
type: Date,
|
|
|
|
|
},
|
|
|
|
|
date_updated: {
|
|
|
|
|
type: Date,
|
|
|
|
|
},
|
|
|
|
|
admins: [
|
|
|
|
|
{
|
|
|
|
|
username: {type: String},
|
|
|
|
|
date: {type: Date},
|
|
|
|
|
},
|
|
|
|
|
],
|
2022-04-07 08:19:40 +02:00
|
|
|
img_logo: {
|
|
|
|
|
type: String,
|
|
|
|
|
},
|
2022-08-26 03:33:13 +02:00
|
|
|
req_users: [
|
|
|
|
|
{
|
|
|
|
|
_id: false,
|
|
|
|
|
username: {type: String},
|
|
|
|
|
date: {type: Date},
|
|
|
|
|
}], // username
|
|
|
|
|
refused_users: [
|
|
|
|
|
{
|
|
|
|
|
_id: false,
|
|
|
|
|
username: {type: String},
|
|
|
|
|
date: {type: Date},
|
|
|
|
|
}], // username
|
|
|
|
|
deleted: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
},
|
2022-04-07 08:19:40 +02:00
|
|
|
});
|
|
|
|
|
|
2022-09-02 02:25:38 +02:00
|
|
|
CircuitSchema.pre('save', async function(next) {
|
|
|
|
|
if (this.isNew) {
|
|
|
|
|
|
|
|
|
|
this.date_created = new Date();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2022-04-07 08:19:40 +02:00
|
|
|
CircuitSchema.statics.findAllIdApp = async function(idapp) {
|
2022-08-26 03:33:13 +02:00
|
|
|
const Circuit = this;
|
2022-04-07 08:19:40 +02:00
|
|
|
|
|
|
|
|
const myfind = {idapp};
|
|
|
|
|
|
2022-08-26 03:33:13 +02:00
|
|
|
return await Circuit.find(myfind, (err, arrrec) => {
|
2022-04-07 08:19:40 +02:00
|
|
|
return arrrec;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CircuitSchema.pre('save', async function(next) {
|
|
|
|
|
if (this.isNew) {
|
2022-06-16 20:34:42 +02:00
|
|
|
const myrec = await Circuit.findOne().limit(1).sort({Num: -1});
|
2022-04-07 08:19:40 +02:00
|
|
|
if (!!myrec) {
|
2022-06-16 20:34:42 +02:00
|
|
|
if (myrec._doc.Num === 0)
|
|
|
|
|
this.Num = 1;
|
2022-04-07 08:19:40 +02:00
|
|
|
else
|
2022-06-16 20:34:42 +02:00
|
|
|
this.Num = myrec._doc.Num + 1;
|
2022-04-07 08:19:40 +02:00
|
|
|
|
|
|
|
|
} else {
|
2022-06-16 20:34:42 +02:00
|
|
|
this.Num = 1;
|
2022-04-07 08:19:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
CircuitSchema.statics.getFieldsForSearch = function() {
|
|
|
|
|
return [
|
|
|
|
|
{field: 'nome_circuito', type: tools.FieldType.string},
|
|
|
|
|
{field: 'sotto_nome', type: tools.FieldType.string},
|
|
|
|
|
{field: 'nome_valuta', type: tools.FieldType.string},
|
|
|
|
|
{field: 'descr', type: tools.FieldType.string}];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CircuitSchema.statics.executeQueryTable = function(idapp, params) {
|
|
|
|
|
params.fieldsearch = this.getFieldsForSearch();
|
2022-09-02 02:25:38 +02:00
|
|
|
return tools.executeQueryTable(this, idapp, params);
|
2022-04-07 08:19:40 +02:00
|
|
|
};
|
|
|
|
|
|
2022-08-26 03:33:13 +02:00
|
|
|
CircuitSchema.statics.getWhatToShow = function(idapp, username) {
|
|
|
|
|
// FOR ME, PERMIT ALL
|
|
|
|
|
return {
|
|
|
|
|
Num: 1,
|
2022-08-30 17:00:48 +02:00
|
|
|
circuitId: 1,
|
2022-08-26 03:33:13 +02:00
|
|
|
groupnameId: 1,
|
2022-08-30 17:00:48 +02:00
|
|
|
path: 1,
|
2022-08-26 03:33:13 +02:00
|
|
|
name: 1,
|
|
|
|
|
subname: 1,
|
|
|
|
|
longdescr: 1,
|
|
|
|
|
regulation: 1,
|
|
|
|
|
systemUserId: 1,
|
2022-09-02 02:25:38 +02:00
|
|
|
createdBy: 1,
|
|
|
|
|
date_created: 1,
|
|
|
|
|
date_updated: 1,
|
2022-08-26 03:33:13 +02:00
|
|
|
nome_valuta: 1,
|
|
|
|
|
symbol: 1,
|
|
|
|
|
abbrev: 1,
|
|
|
|
|
data_costituz: 1,
|
|
|
|
|
img_logo: 1,
|
2022-09-02 02:25:38 +02:00
|
|
|
admins: 1,
|
|
|
|
|
req_users: 1,
|
|
|
|
|
refused_users: 1,
|
2022-08-26 03:33:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2022-08-30 17:00:48 +02:00
|
|
|
// Rimuovi dagli Admin del Circuito
|
2022-09-02 02:25:38 +02:00
|
|
|
CircuitSchema.statics.removeAdminOfMyCircuit = async function(idapp, username, name) {
|
2022-08-30 17:00:48 +02:00
|
|
|
|
|
|
|
|
return Circuit.updateOne({idapp, name},
|
|
|
|
|
{$pull: {admins: {username: {$in: [username]}}}});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2022-08-26 03:33:13 +02:00
|
|
|
CircuitSchema.statics.getWhatToShow_Unknown = function(idapp, username) {
|
|
|
|
|
return {
|
|
|
|
|
Num: 1,
|
2022-08-30 17:00:48 +02:00
|
|
|
circuitId: 1,
|
2022-08-26 03:33:13 +02:00
|
|
|
groupnameId: 1,
|
2022-08-30 17:00:48 +02:00
|
|
|
path: 1,
|
2022-08-26 03:33:13 +02:00
|
|
|
name: 1,
|
|
|
|
|
subname: 1,
|
|
|
|
|
longdescr: 1,
|
|
|
|
|
regulation: 1,
|
|
|
|
|
systemUserId: 1,
|
|
|
|
|
founderUserId: 1,
|
|
|
|
|
nome_valuta: 1,
|
|
|
|
|
symbol: 1,
|
|
|
|
|
abbrev: 1,
|
|
|
|
|
data_costituz: 1,
|
|
|
|
|
img_logo: 1,
|
2022-09-02 02:25:38 +02:00
|
|
|
admins: 1,
|
|
|
|
|
createdBy: 1,
|
|
|
|
|
date_created: 1,
|
|
|
|
|
date_updated: 1,
|
|
|
|
|
req_users: 1,
|
|
|
|
|
refused_users: 1,
|
2022-08-26 03:33:13 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CircuitSchema.statics.getCircuitsByUsername = async function(idapp, username, req) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const {User} = require('../models/user');
|
|
|
|
|
const {Account} = require('../models/account');
|
|
|
|
|
|
|
|
|
|
const whatToShow = this.getWhatToShow(idapp, username);
|
|
|
|
|
const whatToShow_Unknown = this.getWhatToShow_Unknown(idapp, username);
|
2022-09-02 02:25:38 +02:00
|
|
|
// const arrUsernameCircuits = await User.getUsernameCircuitsByUsername(idapp, username);
|
2022-08-26 03:33:13 +02:00
|
|
|
// const arrUsernameReqCircuits = await MyCircuit.getUsernameReqCircuitsByCircuitname(idapp, username);
|
|
|
|
|
|
|
|
|
|
let listUserAccounts = await Account.getAccountsByUsername(idapp, username);
|
|
|
|
|
|
2022-09-02 02:25:38 +02:00
|
|
|
const manage_mycircuits = await Circuit.find({
|
|
|
|
|
idapp,
|
|
|
|
|
'admins': {
|
|
|
|
|
$elemMatch: {username: {$eq: username}},
|
|
|
|
|
},
|
|
|
|
|
$or: [
|
|
|
|
|
{deleted: {$exists: false}},
|
|
|
|
|
{deleted: {$exists: true, $eq: false}}],
|
|
|
|
|
}).lean();
|
|
|
|
|
|
2022-08-26 03:33:13 +02:00
|
|
|
let listcircuits = await Circuit.find({
|
|
|
|
|
idapp,
|
|
|
|
|
$or: [
|
|
|
|
|
{deleted: {$exists: false}},
|
|
|
|
|
{deleted: {$exists: true, $eq: false}}],
|
2022-09-02 02:25:38 +02:00
|
|
|
}, whatToShow_Unknown).lean();
|
2022-08-26 03:33:13 +02:00
|
|
|
|
2022-09-02 02:25:38 +02:00
|
|
|
let asked_circuits = await Circuit.find({
|
2022-08-26 03:33:13 +02:00
|
|
|
idapp,
|
|
|
|
|
'req_users': {
|
|
|
|
|
$elemMatch: {username: {$eq: username}},
|
|
|
|
|
},
|
|
|
|
|
$or: [
|
|
|
|
|
{deleted: {$exists: false}},
|
|
|
|
|
{deleted: {$exists: true, $eq: false}}],
|
2022-09-02 02:25:38 +02:00
|
|
|
}, whatToShow_Unknown).lean();
|
2022-08-26 03:33:13 +02:00
|
|
|
|
2022-09-02 02:25:38 +02:00
|
|
|
let refused_circuits = await Circuit.find({
|
2022-08-26 03:33:13 +02:00
|
|
|
idapp,
|
|
|
|
|
'refused_users': {
|
|
|
|
|
$elemMatch: {username: {$eq: username}},
|
|
|
|
|
},
|
|
|
|
|
$or: [
|
|
|
|
|
{deleted: {$exists: false}},
|
|
|
|
|
{deleted: {$exists: true, $eq: false}}],
|
2022-09-02 02:25:38 +02:00
|
|
|
}, whatToShow_Unknown).lean();
|
2022-08-26 03:33:13 +02:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
listUserAccounts,
|
|
|
|
|
listcircuits,
|
2022-09-02 02:25:38 +02:00
|
|
|
asked_circuits,
|
|
|
|
|
refused_circuits,
|
|
|
|
|
manage_mycircuits,
|
|
|
|
|
mycircuits: req.user.profile.mycircuits,
|
2022-08-26 03:33:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
2022-09-02 02:25:38 +02:00
|
|
|
console.log('Error getCircuitsByUsername', e);
|
2022-08-26 03:33:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
listUsersCircuit: [],
|
|
|
|
|
listRequestUsersCircuit: [],
|
|
|
|
|
listTrusted: [],
|
2022-09-02 02:25:38 +02:00
|
|
|
asked_circuits: [],
|
|
|
|
|
refused_circuits: [],
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CircuitSchema.statics.getInfoCircuitByName = async function(idapp, name) {
|
|
|
|
|
|
|
|
|
|
const whatToShow = this.getWhatToShow(idapp, '');
|
2022-08-26 03:33:13 +02:00
|
|
|
|
2022-09-02 02:25:38 +02:00
|
|
|
const myfind = {
|
|
|
|
|
idapp,
|
|
|
|
|
name,
|
2022-08-26 03:33:13 +02:00
|
|
|
};
|
2022-09-02 02:25:38 +02:00
|
|
|
|
|
|
|
|
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});
|
2022-08-26 03:33:13 +02:00
|
|
|
};
|
|
|
|
|
|
2022-09-02 02:25:38 +02:00
|
|
|
// 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(),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2022-04-07 08:19:40 +02:00
|
|
|
const Circuit = mongoose.model('Circuit', CircuitSchema);
|
|
|
|
|
|
|
|
|
|
module.exports = {Circuit};
|