Circuit table...

This commit is contained in:
paoloar77
2022-08-26 03:33:13 +02:00
parent 5dfcd4f7fa
commit a2019c6ac9
5 changed files with 234 additions and 10 deletions

View File

@@ -1,3 +1,6 @@
/*
Account is a User's single Circuit
*/
const mongoose = require('mongoose').set('debug', false); const mongoose = require('mongoose').set('debug', false);
const Schema = mongoose.Schema; const Schema = mongoose.Schema;
@@ -17,12 +20,15 @@ const AccountSchema = new Schema({
_id: { _id: {
type: Number, type: Number,
}, },
circuitId: { idapp: {
type: Number,
},
userId: {
type: String, type: String,
}, },
username: {
type: String,
},
circuitId: { // ----- REF TO Circuit
type: Number,
},
name: { name: {
type: String, type: String,
}, },
@@ -38,11 +44,11 @@ const AccountSchema = new Schema({
}); });
AccountSchema.statics.findAllIdApp = async function(idapp) { AccountSchema.statics.findAllIdApp = async function(idapp) {
const MyAccount = this; const Account = this;
const myfind = {idapp}; const myfind = {idapp};
return await MyAccount.find(myfind, (err, arrrec) => { return await Account.find(myfind, (err, arrrec) => {
return arrrec; return arrrec;
}); });
}; };
@@ -76,6 +82,22 @@ AccountSchema.statics.executeQueryTable = function(idapp, params) {
return tools.executeQueryTable(this, 0, params); return tools.executeQueryTable(this, 0, params);
}; };
AccountSchema.statics.getAccountsByUsername = async function(idapp, username) {
const Account = this;
if (username === undefined)
return false;
const myquery = {
'idapp': idapp,
'username': username,
};
return await Account.find(myquery);
};
const Account = mongoose.model('Account', AccountSchema); const Account = mongoose.model('Account', AccountSchema);
module.exports = {Account}; module.exports = {Account};

View File

@@ -7,6 +7,7 @@ mongoose.level = 'F';
const tools = require('../tools/general'); const tools = require('../tools/general');
const {ObjectID} = require('mongodb'); const {ObjectID} = require('mongodb');
const {User} = require('./user');
// Resolving error Unknown modifier: $pushAll // Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => { mongoose.plugin(schema => {
@@ -14,6 +15,9 @@ mongoose.plugin(schema => {
}); });
const CircuitSchema = new Schema({ const CircuitSchema = new Schema({
idapp: {
type: String,
},
Num: { Num: {
type: Number, type: Number,
unique: true, unique: true,
@@ -105,17 +109,47 @@ const CircuitSchema = new Schema({
type: Number, type: Number,
}, },
// ------------- // -------------
createdBy: {
type: String,
},
date_created: {
type: Date,
},
date_updated: {
type: Date,
},
admins: [
{
username: {type: String},
date: {type: Date},
},
],
img_logo: { img_logo: {
type: String, type: String,
}, },
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,
},
}); });
CircuitSchema.statics.findAllIdApp = async function(idapp) { CircuitSchema.statics.findAllIdApp = async function(idapp) {
const MyCircuit = this; const Circuit = this;
const myfind = {idapp}; const myfind = {idapp};
return await MyCircuit.find(myfind, (err, arrrec) => { return await Circuit.find(myfind, (err, arrrec) => {
return arrrec; return arrrec;
}); });
}; };
@@ -150,6 +184,106 @@ CircuitSchema.statics.executeQueryTable = function(idapp, params) {
return tools.executeQueryTable(this, 0, params); return tools.executeQueryTable(this, 0, params);
}; };
CircuitSchema.statics.getWhatToShow = function(idapp, username) {
// FOR ME, PERMIT ALL
return {
Num: 1,
groupnameId: 1,
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,
};
};
CircuitSchema.statics.getWhatToShow_Unknown = function(idapp, username) {
return {
Num: 1,
groupnameId: 1,
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,
};
};
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);
// const arrUsernameCircuits = await User.getUsernameCircuitsByUsername(idapp,
// username);
// const arrUsernameReqCircuits = await MyCircuit.getUsernameReqCircuitsByCircuitname(idapp, username);
let listUserAccounts = await Account.getAccountsByUsername(idapp, username);
let listcircuits = await Circuit.find({
idapp,
$or: [
{deleted: {$exists: false}},
{deleted: {$exists: true, $eq: false}}],
}, whatToShow_Unknown);
let listSentRequestCircuits = await Circuit.find({
idapp,
'req_users': {
$elemMatch: {username: {$eq: username}},
},
$or: [
{deleted: {$exists: false}},
{deleted: {$exists: true, $eq: false}}],
}, whatToShow_Unknown);
let listRefusedCircuits = await Circuit.find({
idapp,
'refused_users': {
$elemMatch: {username: {$eq: username}},
},
$or: [
{deleted: {$exists: false}},
{deleted: {$exists: true, $eq: false}}],
}, whatToShow_Unknown);
return {
listUserAccounts,
listcircuits,
listSentRequestCircuits,
listRefusedCircuits,
};
} catch (e) {
console.log('Error', e);
}
return {
listUsersCircuit: [],
listRequestUsersCircuit: [],
listTrusted: [],
listSentRequestCircuits: [],
listRefusedCircuits: [],
};
};
const Circuit = mongoose.model('Circuit', CircuitSchema); const Circuit = mongoose.model('Circuit', CircuitSchema);
module.exports = {Circuit}; module.exports = {Circuit};

View File

@@ -242,6 +242,7 @@ MyGroupSchema.statics.getWhatToShow = function(idapp, username) {
createdBy: 1, createdBy: 1,
date_created: 1, date_created: 1,
date_updated: 1, date_updated: 1,
circuits_list: 1,
}; };
}; };
@@ -258,6 +259,7 @@ MyGroupSchema.statics.getWhatToShow_Unknown = function(idapp, username) {
note: 1, note: 1,
date_created: 1, date_created: 1,
date_updated: 1, date_updated: 1,
circuits_list: 1,
}; };
}; };
@@ -286,10 +288,49 @@ MyGroupSchema.statics.getInfoGroupByGroupname = async function(idapp, groupname)
const whatToShow = this.getWhatToShow(idapp, groupname); const whatToShow = this.getWhatToShow(idapp, groupname);
const rec = await MyGroup.findOne({ const myfind = {
idapp, idapp,
groupname, groupname,
}, whatToShow).lean(); };
const query = [
{$match: myfind},
{ $unwind: '$circuits_list' },
{
$lookup: {
from: 'circuits',
localField: 'circuits_list.Num',
foreignField: 'Num',
as: 'mycircuits',
},
},
{
'$replaceRoot': {
'newRoot': {
'$mergeObjects': [
{
'$arrayElemAt': [
'$mycircuits',
0,
],
},
'$$ROOT',
],
},
},
},
{$project: whatToShow},
];
try {
const ris = await MyGroup.aggregate(query);
if (ris && ris.length > 0)
return ris[0];
} catch (e) {
return null;
}
return rec; return rec;

View File

@@ -15,6 +15,8 @@ const {Graduatoria} = require('../models/graduatoria');
const {MyGroup} = require('../models/mygroup'); const {MyGroup} = require('../models/mygroup');
const {Account} = require('../models/account');
const {ObjectID} = require('mongodb'); const {ObjectID} = require('mongodb');
const i18n = require('i18n'); const i18n = require('i18n');
@@ -3564,6 +3566,16 @@ UserSchema.statics.addExtraInfo = async function(idapp, recUser) {
? listManageGroups ? listManageGroups
: []; : [];
// UserAccounts
const listUserAccounts = await Account.getAccountsByUsername(idapp, recUser.username);
recUser._doc.profile.listUserAccounts = listUserAccounts
? listUserAccounts
: [];
return recUser._doc; return recUser._doc;
} catch (e) { } catch (e) {

View File

@@ -37,6 +37,7 @@ const Variant = require('../models/variant');
const TypedError = require('../modules/ErrorHandler'); const TypedError = require('../modules/ErrorHandler');
const {MyGroup} = require('../models/mygroup'); const {MyGroup} = require('../models/mygroup');
const {Circuit} = require('../models/circuit');
const mongoose = require('mongoose').set('debug', false); const mongoose = require('mongoose').set('debug', false);
const Subscription = mongoose.model('subscribers'); const Subscription = mongoose.model('subscribers');
@@ -637,6 +638,20 @@ router.post('/groups', authenticate, (req, res) => {
}); });
router.post('/circuits', authenticate, (req, res) => {
const username = req.user.username;
idapp = req.body.idapp;
locale = req.body.locale;
return Circuit.getCircuitsByUsername(idapp, username, req).then((ris) => {
res.send(ris);
}).catch((e) => {
tools.mylog('ERRORE IN circuits: ' + e.message);
res.status(400).send();
});
});
router.post('/friends/cmd', authenticate, async (req, res) => { router.post('/friends/cmd', authenticate, async (req, res) => {
const usernameLogged = req.user.username; const usernameLogged = req.user.username;
const idapp = req.body.idapp; const idapp = req.body.idapp;