diff --git a/src/server/models/circuit.js b/src/server/models/circuit.js index 2e00921..468dc9a 100755 --- a/src/server/models/circuit.js +++ b/src/server/models/circuit.js @@ -11,6 +11,9 @@ const { ObjectID } = require('mongodb'); const { Movement } = require('../models/movement'); const { Account } = require('../models/account'); +const { Province } = require('../models/province'); +const shared_consts = require('../tools/shared_nodejs'); + const i18n = require('i18n'); // Resolving error Unknown modifier: $pushAll @@ -48,9 +51,9 @@ const CircuitSchema = new Schema({ type: Number, }], strProv: - { - type: String, - }, + { + type: String, + }, pub_to_share: { type: Number, // PUB_TO_SHARE_ALL, PUB_TO_SHARE_ONLY_TABLE_FOLLOW }, @@ -461,6 +464,22 @@ CircuitSchema.statics.getCircuitByName = async function (idapp, name) { }; +CircuitSchema.statics.getCircuitByProvince = async function (idapp, strProv) { + + const myfind = { + idapp, + strProv, + }; + + try { + return await Circuit.findOne(myfind); + + } catch (e) { + return null; + } + +}; + CircuitSchema.statics.getCircuitById = async function (circuitId) { const myfind = { @@ -894,6 +913,66 @@ CircuitSchema.statics.SetDefMinMaxPersonali = async function (idapp, valmin, val }; // Imposta a tutti i Conti Collettivi, i seguenti minimi e massimi +CircuitSchema.statics.createCircuitIfNotExist = async function (req, idapp, province) { + const { User } = require('../models/user'); + + let myrec = null; + try { + const circuit = await this.getCircuitByProvince(idapp, province); + + const nomeprovincia = await Province.getStrProvinceByProv(province); + + if (!circuit && nomeprovincia) { + const circ = new Circuit({ + idapp, + name: 'RIS ' + nomeprovincia, + path: 'ris' + tools.convertSpaces_ToUScore(nomeprovincia.toLowerCase()), + strProv: province, + photos: [], + admins: [], + color: '#ff5500', + deperimento: false, + transactionsEnabled: false, + status: shared_consts.CIRCUIT_STATUS.FASE1_CREAZIONE_GRUPPO, + symbol: 'RIS', + fido_scoperto_default: 100, + qta_max_default: 200, + fido_scoperto_default_grp: shared_consts.CIRCUIT_PARAMS.SCOPERTO_MIN_GRP, + qta_max_default_grp: shared_consts.CIRCUIT_PARAMS.SCOPERTO_MAX_GRP, + valuta_per_euro: 1, + totTransato: 0, + totCircolante: 0, + date_created: new Date(), + }); + + myrec = await circ.save(); + + if (myrec) { + + // nuovo Circuito: + await User.setCircuitCmd(idapp, tools.USER_ADMIN_CIRCUITS, myrec.name, + shared_consts.CIRCUITCMD.CREATE, true, tools.USER_ADMIN_CIRCUITS, myrec).then((ris) => { + + }); + + // aggiungi il creatore al Circuito stesso + await User.setCircuitCmd(idapp, tools.USER_ADMIN_CIRCUITS, myrec.name, + shared_consts.CIRCUITCMD.SET, true, tools.USER_ADMIN_CIRCUITS, myrec).then((ris) => { + + }); + + } + + msg = 'Nuovo Circuito Creato in Automatico: ' + myrec.name + ' (da ' + req.user.username + ')'; + tools.sendMsgTelegramToAdmin(idapp, msg); + } + } catch (e) { + console.error('Error', e); + } + + return myrec; + +}; CircuitSchema.statics.SetDefMinMaxCollettivi = async function (idapp, valmin, valmax) { ris = await Circuit.updateMany({ idapp }, diff --git a/src/server/models/province.js b/src/server/models/province.js index 0ebd35c..0139450 100755 --- a/src/server/models/province.js +++ b/src/server/models/province.js @@ -46,7 +46,14 @@ ProvinceSchema.statics.getRegionByStrProvince = async function(strprovince) { return ''; } +ProvinceSchema.statics.getStrProvinceByProv = async function(prov) { + const myrec = await Province.findOne({prov}).lean(); + if (myrec) { + return myrec.descr; + } + return ''; +} ProvinceSchema.statics.getFieldsForSearch = function() { return [ diff --git a/src/server/models/user.js b/src/server/models/user.js index 4517213..0febc9b 100755 --- a/src/server/models/user.js +++ b/src/server/models/user.js @@ -2357,6 +2357,26 @@ UserSchema.statics.setGroupsCmd = async function (idapp, usernameOrig, groupname return ris; }; +UserSchema.statics.updateMyData = async function (outres, idapp, username) { + + try { + //++Todo: Ottimizzare ! Non occorre inviare tutti questi dati !!! Solo per il Circuito ?! + const userprofile = await User.getExtraInfoByUsername(idapp, username); + if (userprofile) { + outres.userprofile = userprofile; + } + outres.listcircuits = await Circuit.findAllIdApp(idapp); + outres.mygroups = await MyGroup.findAllGroups(idapp); + + } catch (e) { + console.error('ERR', e); + } + + return outres; + +}; + + UserSchema.statics.setCircuitCmd = async function (idapp, usernameOrig, circuitname, cmd, value, username_action, extrarec) { // console.log('setCircuitCmd', cmd); @@ -2657,14 +2677,7 @@ UserSchema.statics.setCircuitCmd = async function (idapp, usernameOrig, circuitn } if (ris && username_action) { - - //++Todo: Ottimizzare ! Non occorre inviare tutti questi dati !!! Solo per il Circuito ?! - const userprofile = await User.getExtraInfoByUsername(idapp, username_action); - if (userprofile) { - outres.userprofile = userprofile; - } - outres.listcircuits = await Circuit.findAllIdApp(idapp); - outres.mygroups = await MyGroup.findAllGroups(idapp); + outres = await this.updateMyData(outres, idapp, username_action); } if (circuitname) diff --git a/src/server/populate/skills.js b/src/server/populate/skills.js index 50356af..2587507 100644 --- a/src/server/populate/skills.js +++ b/src/server/populate/skills.js @@ -48,7 +48,7 @@ module.exports = { {_id: 46, idSector: [7], descr: 'Idraulico'}, {_id: 47, idSector: [7], descr: 'Giardiniere'}, {_id: 48, idSector: [7], descr: 'Canne fumarie e camini e stufe'}, - {_id: 49, idSector: [7], descr: 'Pannelli solari e pompe calore'}, + {_id: 49, idSector: [7], descr: 'Pannelli solari'}, {_id: 50, idSector: [7], descr: 'Riparazioni varie'}, {_id: 51, idSector: [7], descr: 'Tuttofare'}, {_id: 52, idSector: [7], descr: 'Traslochi'}, @@ -122,5 +122,7 @@ module.exports = { {_id: 127, idSector: [1], descr: 'Stanza in affitto'}, {_id: 128, idSector: [1], descr: 'Stanza in condivisione'}, {_id: 129, idSector: [3], descr: 'Home Restaurant'}, + {_id: 130, idSector: [7], descr: 'Pompe di calore'}, + {_id: 131, idSector: [7], descr: 'Impianti Fotovoltaici'}, ], }; diff --git a/src/server/router/index_router.js b/src/server/router/index_router.js index 007e9b6..d9c1f42 100755 --- a/src/server/router/index_router.js +++ b/src/server/router/index_router.js @@ -919,6 +919,12 @@ router.patch('/chval', authenticate, async (req, res) => { if (mydata.table === 'users') { + + if ('profile.resid_province' in fieldsvalue) { + // Controlla se esiste il Circuito di questa provincia, se non esiste lo crea! + await Circuit.createCircuitIfNotExist(req, idapp, fieldsvalue['profile.resid_province']); + } + if (camporequisiti) { await User.checkIfSbloccatiRequisiti(idapp, allData, id); } diff --git a/src/server/router/users_router.js b/src/server/router/users_router.js index d341e74..c5754dc 100755 --- a/src/server/router/users_router.js +++ b/src/server/router/users_router.js @@ -1122,40 +1122,6 @@ async function eseguiDbOp(idapp, mydata, locale, req, res) { ris = populate.rewriteTable('contribtypes'); - } else if (mydata.dbop === 'ImpostaMinMaxPersonali') { - - await Account.SetMinMaxPersonali(idapp, mydata.valmin, mydata.valmax); - - } else if (mydata.dbop === 'ImpostaMinMaxComunitari') { - - await Account.SetMinMaxComunitari(idapp, mydata.valmin, mydata.valmax); - - } else if (mydata.dbop === 'ImpostaMinMaxCollettivi') { - - await Account.SetMinMaxCollettivi(idapp, mydata.valmin, mydata.valmax); - - } else if (mydata.dbop === 'ImpostaDefMinMaxPersonali') { - - await Circuit.SetDefMinMaxPersonali(idapp, mydata.valmin, mydata.valmax); - - } else if (mydata.dbop === 'ImpostaDefMinMaxCollettivi') { - - await Circuit.SetDefMinMaxCollettivi(idapp, mydata.valmin, mydata.valmax); - - } else if (mydata.dbop === 'setstrProvByIdCityCircuits') { - - await Circuit.setstrProvByIdCityCircuits(idapp); - - } else if (mydata.dbop === 'CreateAccountCircuits') { - - const allcirc = await Circuit.find({ idapp }); - - for (const mycirc of allcirc) { - // Il Conto Comunitario prende il nome del circuito ! - await Account.createAccount(idapp, '', mycirc.name, '', mycirc.path); - - } - } else if (mydata.dbop === 'ReplaceUsername') { if (User.isAdmin(req.user.perm)) { @@ -1240,15 +1206,6 @@ async function eseguiDbOp(idapp, mydata, locale, req, res) { ris = await populate.popolaTabelleNuove(); - } else if (mydata.dbop === 'saveStepTut') { - await User.findOneAndUpdate({ _id: mydata._id }, - { $set: { 'profile.stepTutorial': mydata.value } }); - } else if (mydata.dbop === 'noNameSurname') { - await User.findOneAndUpdate({ _id: mydata._id }, - { $set: { 'profile.noNameSurname': mydata.value } }); - } else if (mydata.dbop === 'noFoto') { - await User.findOneAndUpdate({ _id: mydata._id }, - { $set: { 'profile.noFoto': mydata.value } }); } else if (mydata.dbop === 'ricreaTabCitiesProvinces') { // Svuota e Ricrea @@ -1284,6 +1241,30 @@ async function eseguiDbOp(idapp, mydata, locale, req, res) { await City.remove({}); await Province.remove({}); + } else if (mydata.dbop === 'ImpostaMinMaxPersonali') { + + await Account.SetMinMaxPersonali(idapp, mydata.valmin, mydata.valmax); + + } else if (mydata.dbop === 'ImpostaMinMaxComunitari') { + + await Account.SetMinMaxComunitari(idapp, mydata.valmin, mydata.valmax); + + } else if (mydata.dbop === 'ImpostaMinMaxCollettivi') { + + await Account.SetMinMaxCollettivi(idapp, mydata.valmin, mydata.valmax); + + } else if (mydata.dbop === 'ImpostaDefMinMaxPersonali') { + + await Circuit.SetDefMinMaxPersonali(idapp, mydata.valmin, mydata.valmax); + + } else if (mydata.dbop === 'ImpostaDefMinMaxCollettivi') { + + await Circuit.SetDefMinMaxCollettivi(idapp, mydata.valmin, mydata.valmax); + + } else if (mydata.dbop === 'setstrProvByIdCityCircuits') { + + await Circuit.setstrProvByIdCityCircuits(idapp); + /*} else if (mydata.dbop === 'visuNave') { mystr = await Nave.getNavePos(idapp, parseInt(mydata.riga), parseInt(mydata.col)); @@ -1318,7 +1299,46 @@ async function eseguiDbOp(idapp, mydata, locale, req, res) { } catch (e) { console.log(e.message); } -} +}; + +async function eseguiDbOpUser(idapp, mydata, locale, req, res) { + + let ris = await User.DbOp(idapp, mydata); + + const populate = require('../populate/populate'); + + const globalTables = require('../tools/globalTables'); + + let mystr = ''; + + try { + + if (mydata.dbop === 'CreateAccountCircuits') { + + const allcirc = await Circuit.find({ idapp }); + + for (const mycirc of allcirc) { + // Il Conto Comunitario prende il nome del circuito ! + await Account.createAccount(idapp, '', mycirc.name, '', mycirc.path); + + } + + } else if (mydata.dbop === 'saveStepTut') { + await User.findOneAndUpdate({ _id: mydata._id }, + { $set: { 'profile.stepTutorial': mydata.value } }); + } else if (mydata.dbop === 'noNameSurname') { + await User.findOneAndUpdate({ _id: mydata._id }, + { $set: { 'profile.noNameSurname': mydata.value } }); + } else if (mydata.dbop === 'noFoto') { + await User.findOneAndUpdate({ _id: mydata._id }, + { $set: { 'profile.noFoto': mydata.value } }); + } + + } catch (e) { + + } + +}; router.post('/dbop', authenticate, async (req, res) => { @@ -1333,9 +1353,40 @@ router.post('/dbop', authenticate, async (req, res) => { } try { - const ris = await eseguiDbOp(idapp, mydata, locale, req, res); + let ris = await eseguiDbOp(idapp, mydata, locale, req, res); - res.send(ris); + if (!ris) { + ris = {}; + } + + ris = await User.updateMyData(ris, idapp, req.user.username); + + res.send({ code: server_constants.RIS_CODE_OK, data: ris.data }); + + } catch (e) { + res.status(400).send({ code: server_constants.RIS_CODE_ERR, msg: e }); + + console.log(e.message); + } + +}); + +router.post('/dbopuser', authenticate, async (req, res) => { + + const mydata = req.body.mydata; + idapp = req.body.idapp; + locale = req.body.locale; + + try { + let ris = await eseguiDbOpUser(idapp, mydata, locale, req, res); + + if (!ris) { + ris = {}; + } + + ris = await User.updateMyData(ris, idapp, req.user.username); + + res.send({ code: server_constants.RIS_CODE_OK, ris }); } catch (e) { res.status(400).send({ code: server_constants.RIS_CODE_ERR, msg: e }); diff --git a/src/server/tools/general.js b/src/server/tools/general.js index f5fe100..60c7efd 100755 --- a/src/server/tools/general.js +++ b/src/server/tools/general.js @@ -32,6 +32,7 @@ const webpush = require('web-push'); const i18n = require('i18n'); + const FILELOG = 'filelog.txt'; const FILEEVENTS = 'logevents.txt'; const FILEMANAGERS = 'logmanagers.txt'; @@ -425,6 +426,8 @@ module.exports = { SEP: '|', + USER_ADMIN_CIRCUITS: 'paoloar77', + TYPE_PROJECT: 1, TYPE_TODO: 2, @@ -1482,6 +1485,12 @@ module.exports = { return ''; }, + convertSpaces_ToUScore(mystr) { + if (mystr) + return mystr.replace(/\s+/g, '_'); + return ''; + }, + convertHTMLtoText(myhtml) { let msg = myhtml; if (msg) {