From 8a77dabc22be043bdaae07a2a37436e1b7a9eaec Mon Sep 17 00:00:00 2001 From: Surya Paolo Date: Tue, 4 Apr 2023 15:26:56 +0200 Subject: [PATCH] new version Visualizzazione Service --- src/server/models/mybacheca.js | 37 +++++++++++-------- src/server/models/mygood.js | 59 +++++++++++++++++++------------ src/server/models/mygroup.js | 2 +- src/server/models/myhosp.js | 29 ++++++++++++--- src/server/models/myskill.js | 35 ++++++++++-------- src/server/models/user.js | 43 +++++++++++++++++++++- src/server/router/users_router.js | 39 ++++++++++++++++++++ src/server/tools/shared_nodejs.js | 17 ++++++--- 8 files changed, 198 insertions(+), 63 deletions(-) diff --git a/src/server/models/mybacheca.js b/src/server/models/mybacheca.js index 1a34d6c..fca2f6e 100755 --- a/src/server/models/mybacheca.js +++ b/src/server/models/mybacheca.js @@ -15,7 +15,10 @@ mongoose.plugin(schema => { const MyBachecaSchema = new Schema({ _id: { - type: Number, + type: String, + default: function () { + return new ObjectID().toString(); + }, }, idapp: { type: String, @@ -88,17 +91,6 @@ const MyBachecaSchema = new Schema({ MyBachecaSchema.pre('save', async function (next) { if (this.isNew) { - const myrec = await MyBacheca.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; - } - this.date_created = new Date(); } @@ -171,6 +163,8 @@ MyBachecaSchema.statics.executeQueryTable = function (idapp, params, user) { 'profile.img': 1, "profile.mygroups": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, + 'profile.username_telegram': 1, reported: 1, date_report: 1, username_who_report: 1, @@ -186,13 +180,21 @@ MyBachecaSchema.statics.executeQueryTable = function (idapp, params, user) { MyBachecaSchema.statics.getMyRecById = function (idapp, id) { const MyBacheca = this; + let myparsid = { + $or: [ + { + '_id': parseInt(idGood) + }, + { + '_id': idGood, + }] + }; + const query = [ { '$match': { '$and': [ - { - '_id': parseInt(id), - }, + myparsid, ], }, }, @@ -268,6 +270,7 @@ MyBachecaSchema.statics.getMyRecById = function (idapp, id) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -326,6 +329,7 @@ MyBachecaSchema.statics.getMyRecById = function (idapp, id) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -384,6 +388,7 @@ MyBachecaSchema.statics.getMyRecById = function (idapp, id) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -442,6 +447,7 @@ MyBachecaSchema.statics.getMyRecById = function (idapp, id) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -500,6 +506,7 @@ MyBachecaSchema.statics.getMyRecById = function (idapp, id) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, diff --git a/src/server/models/mygood.js b/src/server/models/mygood.js index e4510d9..7b0b297 100755 --- a/src/server/models/mygood.js +++ b/src/server/models/mygood.js @@ -6,7 +6,7 @@ mongoose.level = 'F'; const tools = require('../tools/general'); -const {ObjectID} = require('mongodb'); +const { ObjectID } = require('mongodb'); // Resolving error Unknown modifier: $pushAll mongoose.plugin(schema => { @@ -15,13 +15,13 @@ mongoose.plugin(schema => { const MyGoodSchema = new Schema({ _id: { - type: Number, + type: String, }, idapp: { type: String, required: true, }, - userId: {type: Schema.Types.ObjectId, ref: 'User'}, + userId: { type: Schema.Types.ObjectId, ref: 'User' }, idSectorGood: { type: Number, }, @@ -87,9 +87,9 @@ const MyGoodSchema = new Schema({ }, }); -MyGoodSchema.pre('save', async function(next) { +MyGoodSchema.pre('save', async function (next) { if (this.isNew) { - const myrec = await MyGood.findOne().limit(1).sort({_id: -1}); + const myrec = await MyGood.findOne().limit(1).sort({ _id: -1 }); if (!!myrec) { if (myrec._doc._id === 0) this._id = 1; @@ -106,12 +106,12 @@ MyGoodSchema.pre('save', async function(next) { next(); }); -MyGoodSchema.statics.findAllIdApp = async function(idapp) { +MyGoodSchema.statics.findAllIdApp = async function (idapp) { const MyGood = this; const query = [ - {$match: {idapp}}, - {$sort: {descr: 1}}, + { $match: { idapp } }, + { $sort: { descr: 1 } }, ]; return await MyGood.aggregate(query).then((arrrec) => { @@ -120,21 +120,21 @@ MyGoodSchema.statics.findAllIdApp = async function(idapp) { }; -MyGoodSchema.statics.getFieldsForSearch = function() { +MyGoodSchema.statics.getFieldsForSearch = function () { return []; }; -MyGoodSchema.statics.getFieldsLastForSearch = function() { +MyGoodSchema.statics.getFieldsLastForSearch = function () { return [ - {field: 'note', type: tools.FieldType.string}, - {field: 'descr', type: tools.FieldType.string}, - {field: 'recGood.descr', type: tools.FieldType.string}, - {field: 'MyGood.descr', type: tools.FieldType.string}, + { field: 'note', type: tools.FieldType.string }, + { field: 'descr', type: tools.FieldType.string }, + { field: 'recGood.descr', type: tools.FieldType.string }, + { field: 'MyGood.descr', type: tools.FieldType.string }, ]; }; -MyGoodSchema.statics.executeQueryTable = function(idapp, params, user) { +MyGoodSchema.statics.executeQueryTable = function (idapp, params, user) { params.fieldsearch = this.getFieldsForSearch(); params.fieldsearch_last = this.getFieldsLastForSearch(); @@ -151,6 +151,7 @@ MyGoodSchema.statics.executeQueryTable = function(idapp, params, user) { MyGood: 1, idStatusGood: 1, idContribType: 1, + 'profile.username_telegram': 1, idCity: 1, pub_to_share: 1, numLevel: 1, @@ -172,26 +173,35 @@ MyGoodSchema.statics.executeQueryTable = function(idapp, params, user) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, }; - params = {...params, ...otherparams}; + params = { ...params, ...otherparams }; return tools.executeQueryTable(this, idapp, params, user); }; -MyGoodSchema.statics.getMyRecById = function(idapp, idGood) { +MyGoodSchema.statics.getMyRecById = function (idapp, idGood) { const MyGood = this; + let myparsid = { + $or: [ + { + '_id': parseInt(idGood) + }, + { + '_id': idGood, + }] + }; + const query = [ { '$match': { '$and': [ - { - '_id': parseInt(idGood), - }, + myparsid, ], }, }, @@ -266,6 +276,7 @@ MyGoodSchema.statics.getMyRecById = function(idapp, idGood) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -323,6 +334,7 @@ MyGoodSchema.statics.getMyRecById = function(idapp, idGood) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -381,6 +393,7 @@ MyGoodSchema.statics.getMyRecById = function(idapp, idGood) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -438,6 +451,7 @@ MyGoodSchema.statics.getMyRecById = function(idapp, idGood) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -495,6 +509,7 @@ MyGoodSchema.statics.getMyRecById = function(idapp, idGood) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -505,7 +520,7 @@ MyGoodSchema.statics.getMyRecById = function(idapp, idGood) { }); }; -MyGoodSchema.statics.getCompleteRecord = function(idapp, id) { +MyGoodSchema.statics.getCompleteRecord = function (idapp, id) { const MyGood = this; return MyGood.getMyRecById(idapp, id); @@ -515,4 +530,4 @@ MyGoodSchema.statics.getCompleteRecord = function(idapp, id) { const MyGood = mongoose.model('MyGood', MyGoodSchema); -module.exports = {MyGood}; +module.exports = { MyGood }; diff --git a/src/server/models/mygroup.js b/src/server/models/mygroup.js index eb75e18..d11ab9e 100755 --- a/src/server/models/mygroup.js +++ b/src/server/models/mygroup.js @@ -15,7 +15,7 @@ mongoose.plugin(schema => { const MyGroupSchema = new Schema({ _id: { - type: Number, + type: String, }, idapp: { type: String, diff --git a/src/server/models/myhosp.js b/src/server/models/myhosp.js index 2d59441..842f298 100755 --- a/src/server/models/myhosp.js +++ b/src/server/models/myhosp.js @@ -15,7 +15,7 @@ mongoose.plugin(schema => { const MyHospSchema = new Schema({ _id: { - type: Number, + type: String, }, idapp: { type: String, @@ -172,6 +172,7 @@ MyHospSchema.statics.executeQueryTable = function(idapp, params, user) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, 'mycities': 1, reported: 1, }, @@ -186,13 +187,27 @@ MyHospSchema.statics.executeQueryTable = function(idapp, params, user) { MyHospSchema.statics.getMyRecById = function(idapp, id) { const MyHosp = this; + let myparsid = { + $or: [ + { + '_id': parseInt(idGood) + }, + { + '_id': idGood, + }] + }; + + if (tools.isNumber(id)) { + myparsid = {'_id': parseInt(id)}; + } else { + myparsid = {'_id': id}; + } + const query = [ { '$match': { '$and': [ - { - '_id': parseInt(id), - }, + myparsid, ], }, }, @@ -263,6 +278,7 @@ MyHospSchema.statics.getMyRecById = function(idapp, id) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -316,6 +332,7 @@ MyHospSchema.statics.getMyRecById = function(idapp, id) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -369,6 +386,7 @@ MyHospSchema.statics.getMyRecById = function(idapp, id) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -422,6 +440,7 @@ MyHospSchema.statics.getMyRecById = function(idapp, id) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -457,6 +476,7 @@ MyHospSchema.statics.getMyRecById = function(idapp, id) { preferences: 1, photos: 1, idContribType: 1, + 'profile.username_telegram': 1, idCity: 1, pub_to_share: 1, note: 1, @@ -475,6 +495,7 @@ MyHospSchema.statics.getMyRecById = function(idapp, id) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, diff --git a/src/server/models/myskill.js b/src/server/models/myskill.js index 3be9365..2247f74 100755 --- a/src/server/models/myskill.js +++ b/src/server/models/myskill.js @@ -15,7 +15,10 @@ mongoose.plugin(schema => { const MySkillSchema = new Schema({ _id: { - type: Number, + type: String, + default: function () { + return new ObjectID().toString(); + }, }, idapp: { type: String, @@ -92,17 +95,6 @@ const MySkillSchema = new Schema({ MySkillSchema.pre('save', async function(next) { if (this.isNew) { - const myrec = await MySkill.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; - } - this.date_created = new Date(); } @@ -153,6 +145,7 @@ MySkillSchema.statics.executeQueryTable = function(idapp, params, user) { myskill: 1, idStatusSkill: 1, idContribType: 1, + 'profile.username_telegram': 1, idCity: 1, pub_to_share: 1, numLevel: 1, @@ -173,6 +166,7 @@ MySkillSchema.statics.executeQueryTable = function(idapp, params, user) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -186,13 +180,19 @@ MySkillSchema.statics.executeQueryTable = function(idapp, params, user) { MySkillSchema.statics.getMyRecById = function(idapp, idSkill) { const MySkill = this; + let myparskill = {}; + + if (tools.isNumber(idSkill)) { + myparskill = {'_id': parseInt(idSkill)}; + } else { + myparskill = {'_id': idSkill}; + } + const query = [ { '$match': { '$and': [ - { - '_id': parseInt(idSkill), - }, + myparskill, ], }, }, @@ -266,6 +266,7 @@ MySkillSchema.statics.getMyRecById = function(idapp, idSkill) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -322,6 +323,7 @@ MySkillSchema.statics.getMyRecById = function(idapp, idSkill) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -378,6 +380,7 @@ MySkillSchema.statics.getMyRecById = function(idapp, idSkill) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -436,6 +439,7 @@ MySkillSchema.statics.getMyRecById = function(idapp, idSkill) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, @@ -492,6 +496,7 @@ MySkillSchema.statics.getMyRecById = function(idapp, idSkill) { "profile.mygroups": 1, "profile.mycircuits": 1, 'profile.qualifica': 1, + 'profile.resid_province': 1, reported: 1, }, }, diff --git a/src/server/models/user.js b/src/server/models/user.js index 7bedcc2..79e7b11 100755 --- a/src/server/models/user.js +++ b/src/server/models/user.js @@ -449,7 +449,21 @@ const UserSchema = new mongoose.Schema({ }, noFoto: { type: Boolean, - } + }, + bookmark: [ + { + _id: false, + id: { type: String }, + tab: { type: Number }, + }, + ], + favorite: [ + { + _id: false, + id: { type: String }, + tab: { type: Number }, + }, + ], }, }); @@ -1882,6 +1896,33 @@ UserSchema.statics.removeReqFriend = async function ( { $pull: { 'profile.req_friends': { username: { $in: [usernameDest] } } } }); }; +// Rimuovo il Favorite +UserSchema.statics.removeFavorite = async function ( + idapp, id, tab) { + return await User.updateOne({ idapp, username }, + { $pull: { 'profile.favorite': { id: { $in: [id] }, tab } } }); +}; + +// Aggiungo il Favorite +UserSchema.statics.addFavorite = async function ( + idapp, username, id, tab) { + return await User.updateOne({ idapp, username }, + { $push: { 'profile.favorite': { id, tab } } }); +}; +// Rimuovo il Bookmark +UserSchema.statics.removeBookmark = async function ( + idapp, id, tab) { + return await User.updateOne({ idapp, username }, + { $pull: { 'profile.bookmark': { id: { $in: [id] }, tab } } }); +}; + +// Aggiungo il Bookmark +UserSchema.statics.addBookmark = async function ( + idapp, username, id, tab) { + return await User.updateOne({ idapp, username }, + { $push: { 'profile.bookmark': { id, tab } } }); +}; + UserSchema.statics.setFriendsCmd = async function (req, idapp, usernameOrig, usernameDest, cmd, value, disablenotif) { const { SendNotif } = require('../models/sendnotif'); diff --git a/src/server/router/users_router.js b/src/server/router/users_router.js index f5f49ec..0952deb 100755 --- a/src/server/router/users_router.js +++ b/src/server/router/users_router.js @@ -1436,4 +1436,43 @@ router.post('/mgt', authenticate, async (req, res) => { }); +router.post('/cmd', authenticate, async (req, res) => { + + const mydata = req.body.mydata; + const idapp = req.body.idapp; + const cmd = req.body.cmd; + const id = req.body.id; + const tab = req.body.tab; + const value = req.body.value; + + try { + const username = req.user.username; + + let ris = null; + + if (cmd === shared_consts.CMD_USER.SET_FAVORITE) { + if (value) + ris = await User.addFavorite(idapp, username, id, tab); + else + ris = await User.removeFavorite(idapp, username, id, tab); + } else if (cmd === shared_consts.CMD_USER.SET_BOOKMARK) { + if (value) + ris = await User.addBookmark(idapp, username, id, tab); + else + ris = await User.removeBookmark(idapp, username, id, tab); + } + + let state = (value && ris && ris.ok === 1) ? 1 : ((!value && ris && ris.ok === 1) ? -1 : 0); + + return res.send({ state }); + + } catch (e) { + res.status(400).send(); + res.send({ code: server_constants.RIS_CODE_ERR, msg: e }); + + console.log(e.message); + } + +}); + module.exports = router; diff --git a/src/server/tools/shared_nodejs.js b/src/server/tools/shared_nodejs.js index adee9ae..7802260 100755 --- a/src/server/tools/shared_nodejs.js +++ b/src/server/tools/shared_nodejs.js @@ -134,6 +134,13 @@ module.exports = { TABLES_MYGOODS: 'mygoods', TABLES_MYEVENTS: 'myevents', + MYTABS: ['none', 'myskills', 'mybachecas', 'myhosps', 'mygoods', 'myevents'], + + CMD_USER: { + SET_FAVORITE: 1, + SET_BOOKMARK: 2, + }, + TABLES_ENABLE_GETREC_BYID: ['mybachecas', 'myhosps', 'myskills', 'mygoods'], TABLES_USER_INCLUDE_MY: ['mygroups', 'circuits'], @@ -173,11 +180,11 @@ module.exports = { 'subskills', 'cities', 'provinces', - 'myskills', - 'mybachecas', - 'myhosps', - 'mygoods', - 'mygroups' + // 'myskills', + // 'mybachecas', + // 'myhosps', + // 'mygoods', + // 'mygroups' ], TABLES_USER_ID: ['mygroups', 'myskills', 'mybachecas', 'myhosps', 'mygoods'], TABLES_CREATEDBY: ['mygroups', 'circuits'],