From a2019c6ac9c47817a2865e505c80e76fb445ca30 Mon Sep 17 00:00:00 2001 From: paoloar77 Date: Fri, 26 Aug 2022 03:33:13 +0200 Subject: [PATCH] Circuit table... --- src/server/models/account.js | 34 ++++++-- src/server/models/circuit.js | 138 +++++++++++++++++++++++++++++- src/server/models/mygroup.js | 45 +++++++++- src/server/models/user.js | 12 +++ src/server/router/users_router.js | 15 ++++ 5 files changed, 234 insertions(+), 10 deletions(-) diff --git a/src/server/models/account.js b/src/server/models/account.js index 8c07a78..5dd8bb4 100755 --- a/src/server/models/account.js +++ b/src/server/models/account.js @@ -1,3 +1,6 @@ +/* + Account is a User's single Circuit + */ const mongoose = require('mongoose').set('debug', false); const Schema = mongoose.Schema; @@ -17,12 +20,15 @@ const AccountSchema = new Schema({ _id: { type: Number, }, - circuitId: { - type: Number, - }, - userId: { + idapp: { type: String, }, + username: { + type: String, + }, + circuitId: { // ----- REF TO Circuit + type: Number, + }, name: { type: String, }, @@ -38,11 +44,11 @@ const AccountSchema = new Schema({ }); AccountSchema.statics.findAllIdApp = async function(idapp) { - const MyAccount = this; + const Account = this; const myfind = {idapp}; - return await MyAccount.find(myfind, (err, arrrec) => { + return await Account.find(myfind, (err, arrrec) => { return arrrec; }); }; @@ -76,6 +82,22 @@ AccountSchema.statics.executeQueryTable = function(idapp, 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); module.exports = {Account}; diff --git a/src/server/models/circuit.js b/src/server/models/circuit.js index 2c13c26..4a6a600 100755 --- a/src/server/models/circuit.js +++ b/src/server/models/circuit.js @@ -7,6 +7,7 @@ mongoose.level = 'F'; const tools = require('../tools/general'); const {ObjectID} = require('mongodb'); +const {User} = require('./user'); // Resolving error Unknown modifier: $pushAll mongoose.plugin(schema => { @@ -14,6 +15,9 @@ mongoose.plugin(schema => { }); const CircuitSchema = new Schema({ + idapp: { + type: String, + }, Num: { type: Number, unique: true, @@ -105,17 +109,47 @@ const CircuitSchema = new Schema({ type: Number, }, // ------------- + createdBy: { + type: String, + }, + date_created: { + type: Date, + }, + date_updated: { + type: Date, + }, + admins: [ + { + username: {type: String}, + date: {type: Date}, + }, + ], img_logo: { 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) { - const MyCircuit = this; + const Circuit = this; const myfind = {idapp}; - return await MyCircuit.find(myfind, (err, arrrec) => { + return await Circuit.find(myfind, (err, arrrec) => { return arrrec; }); }; @@ -150,6 +184,106 @@ CircuitSchema.statics.executeQueryTable = function(idapp, 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); module.exports = {Circuit}; diff --git a/src/server/models/mygroup.js b/src/server/models/mygroup.js index ac0fb6c..e98f9c3 100755 --- a/src/server/models/mygroup.js +++ b/src/server/models/mygroup.js @@ -242,6 +242,7 @@ MyGroupSchema.statics.getWhatToShow = function(idapp, username) { createdBy: 1, date_created: 1, date_updated: 1, + circuits_list: 1, }; }; @@ -258,6 +259,7 @@ MyGroupSchema.statics.getWhatToShow_Unknown = function(idapp, username) { note: 1, date_created: 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 rec = await MyGroup.findOne({ + const myfind = { idapp, 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; diff --git a/src/server/models/user.js b/src/server/models/user.js index ed6c55c..856c9a9 100755 --- a/src/server/models/user.js +++ b/src/server/models/user.js @@ -15,6 +15,8 @@ const {Graduatoria} = require('../models/graduatoria'); const {MyGroup} = require('../models/mygroup'); +const {Account} = require('../models/account'); + const {ObjectID} = require('mongodb'); const i18n = require('i18n'); @@ -3564,6 +3566,16 @@ UserSchema.statics.addExtraInfo = async function(idapp, recUser) { ? listManageGroups : []; + // UserAccounts + + const listUserAccounts = await Account.getAccountsByUsername(idapp, recUser.username); + + recUser._doc.profile.listUserAccounts = listUserAccounts + ? listUserAccounts + : []; + + + return recUser._doc; } catch (e) { diff --git a/src/server/router/users_router.js b/src/server/router/users_router.js index cb89eca..fd6027e 100755 --- a/src/server/router/users_router.js +++ b/src/server/router/users_router.js @@ -37,6 +37,7 @@ const Variant = require('../models/variant'); const TypedError = require('../modules/ErrorHandler'); const {MyGroup} = require('../models/mygroup'); +const {Circuit} = require('../models/circuit'); const mongoose = require('mongoose').set('debug', false); 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) => { const usernameLogged = req.user.username; const idapp = req.body.idapp;