- Editor Pagine Elementi: Sezione, Righe, Colonne, Elementi. (rows, columns, elems)
This commit is contained in:
@@ -314,33 +314,33 @@ router.get('/test1', authenticate_noerror, async (req, res) => {
|
||||
});
|
||||
|
||||
router.post('/settable', authenticate, async (req, res) => {
|
||||
const params = req.body;
|
||||
const mytable = globalTables.getTableByTableName(params.table);
|
||||
|
||||
let mydata = req.body.data;
|
||||
let extrarec = {};
|
||||
if (mydata && mydata.hasOwnProperty('extrarec')) {
|
||||
extrarec = mydata['extrarec'];
|
||||
delete mydata['extrarec'];
|
||||
}
|
||||
|
||||
if (mydata === undefined) {
|
||||
console.error('MYDATA VUOTO !');
|
||||
return res.status(400).send('Mydata VUOTO');
|
||||
}
|
||||
|
||||
const fieldsvalue = { ALL: 1 };
|
||||
|
||||
mydata.idapp = req.user.idapp;
|
||||
const idapp = mydata.idapp;
|
||||
|
||||
if (req.user && req.user.username) {
|
||||
User.setOnLine(req.user.idapp, req.user.username);
|
||||
}
|
||||
|
||||
let consentito = false;
|
||||
|
||||
try {
|
||||
const params = req.body;
|
||||
const mytable = globalTables.getTableByTableName(params.table);
|
||||
|
||||
let mydata = req.body.data;
|
||||
let extrarec = {};
|
||||
if (mydata && mydata.hasOwnProperty('extrarec')) {
|
||||
extrarec = mydata['extrarec'];
|
||||
delete mydata['extrarec'];
|
||||
}
|
||||
|
||||
if (mydata === undefined) {
|
||||
console.error('MYDATA VUOTO !');
|
||||
return res.status(400).send('Mydata VUOTO');
|
||||
}
|
||||
|
||||
const fieldsvalue = { ALL: 1 };
|
||||
|
||||
mydata.idapp = req.user?.idapp;
|
||||
const idapp = mydata.idapp;
|
||||
|
||||
if (req.user && req.user.username) {
|
||||
User.setOnLine(req.user.idapp, req.user.username);
|
||||
}
|
||||
|
||||
let consentito = false;
|
||||
|
||||
if (
|
||||
User.isAdmin(req.user.perm) ||
|
||||
User.isManager(req.user.perm) ||
|
||||
@@ -1214,6 +1214,58 @@ router.patch('/setlang', authenticate, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
function toObjId(id) {
|
||||
try {
|
||||
return Types.ObjectId.isValid(id) ? new Types.ObjectId(id) : id;
|
||||
} catch {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
async function updateElemInsideColumn({ idapp, id, mydata }) {
|
||||
const { fieldsvalue = {} } = mydata || {};
|
||||
|
||||
// 1) id della colonna parent (dove si trova l'elemento da aggiornare)
|
||||
let idElemParent = null;
|
||||
if (fieldsvalue && fieldsvalue.idElemParent) {
|
||||
idElemParent = fieldsvalue.idElemParent;
|
||||
}
|
||||
|
||||
// 2) recupera il documento parent "top-level" (es. section) che contiene la colonna
|
||||
const myelemParent = await MyElem.findParentElem(idapp, idElemParent);
|
||||
if (!myelemParent) {
|
||||
return { ok: false, msg: 'Parent non trovato' };
|
||||
}
|
||||
|
||||
// 3) costruisci il $set campo-per-campo
|
||||
// path target: rows.$[].columns.$[col].elems.$[el].<campo>
|
||||
const setOps = {};
|
||||
for (const [key, value] of Object.entries(fieldsvalue)) {
|
||||
if (key === '_id') continue; // non tocchiamo l'_id
|
||||
setOps[`rows.$[].columns.$[col].elems.$[el].${key}`] = value;
|
||||
}
|
||||
|
||||
// Se non c’è nulla da settare, esci pulito
|
||||
if (Object.keys(setOps).length === 0) {
|
||||
return { ok: true, msg: 'Nulla da aggiornare' };
|
||||
}
|
||||
|
||||
// 4) esegui l’update
|
||||
const filter = { _id: toObjId(myelemParent._id) };
|
||||
const update = { $set: setOps };
|
||||
const options = {
|
||||
arrayFilters: [{ 'col._id': toObjId(idElemParent) }, { 'el._id': toObjId(id) }],
|
||||
};
|
||||
|
||||
const result = await MyElem.updateOne(filter, update, options);
|
||||
|
||||
return {
|
||||
ok: result?.matchedCount > 0,
|
||||
modified: result?.modifiedCount || 0,
|
||||
result,
|
||||
};
|
||||
}
|
||||
|
||||
router.patch('/chval', authenticate, async (req, res) => {
|
||||
// const idapp = req.body.idapp;
|
||||
const id = req.body.data.id;
|
||||
@@ -1328,162 +1380,177 @@ router.patch('/chval', authenticate, async (req, res) => {
|
||||
fieldsvalue = { [`arrvariazioni.0.${chiave}`]: valore };
|
||||
}
|
||||
}
|
||||
return await mytable
|
||||
.findByIdAndUpdate(id, { $set: fieldsvalue }, { new: true })
|
||||
.then(async (rec) => {
|
||||
// tools.mylogshow(' REC TO MODIFY: ', rec);
|
||||
if (!rec) {
|
||||
return res.status(404).send();
|
||||
} else {
|
||||
let addmsg = '';
|
||||
if (mydata.table === 'myelems' && mydata.fieldsvalue.idElemParent) {
|
||||
// se è un myelem, allora cerca l'id anche sugli elementi contenuti in elems, (sotto rows e columns)
|
||||
// quindi devo aggiornare quell'elemento in elems
|
||||
|
||||
if (mydata.notifBot) {
|
||||
// Send Notification to the BOT
|
||||
await telegrambot.sendMsgTelegram(idapp, mydata.notifBot.un, mydata.notifBot.txt);
|
||||
if (!!addmsg) await telegrambot.sendMsgTelegram(idapp, mydata.notifBot.un, addmsg);
|
||||
let addtext = '[Msg Inviato a ' + mydata.notifBot.un + ']:' + '\n' + mydata.notifBot.txt;
|
||||
telegrambot.sendMsgTelegramToTheManagers(idapp, addtext, true);
|
||||
const risult = await updateElemInsideColumn({ idapp, id, mydata });
|
||||
|
||||
if (!!flotta) tools.writeFlottaLog(idapp, addtext, flotta.riga, flotta.col_prima);
|
||||
}
|
||||
if (risult.ok) {
|
||||
res.send({ code: server_constants.RIS_CODE_OK, msg: '' });
|
||||
}
|
||||
|
||||
if (mydata.table === 'accounts') {
|
||||
let msg = '';
|
||||
if (rec.circuitId) circuit = await Circuit.getCircuitByCircuitId(rec.circuitId);
|
||||
/*return await mytable.findByIdAndUpdate(id, { $set: fieldsvalue }, { new: true }).then(async (rec) => {
|
||||
res.send({ code: server_constants.RIS_CODE_OK, msg: '' });
|
||||
});*/
|
||||
} else {
|
||||
return await mytable
|
||||
.findByIdAndUpdate(id, { $set: fieldsvalue }, { new: true })
|
||||
.then(async (rec) => {
|
||||
// tools.mylogshow(' REC TO MODIFY: ', rec);
|
||||
if (!rec) {
|
||||
return res.status(404).send();
|
||||
} else {
|
||||
let addmsg = '';
|
||||
|
||||
let dest = rec.groupname ? rec.groupname : rec.username;
|
||||
let valprec = 0;
|
||||
if (mydata.notifBot) {
|
||||
// Send Notification to the BOT
|
||||
await telegrambot.sendMsgTelegram(idapp, mydata.notifBot.un, mydata.notifBot.txt);
|
||||
if (!!addmsg) await telegrambot.sendMsgTelegram(idapp, mydata.notifBot.un, addmsg);
|
||||
let addtext = '[Msg Inviato a ' + mydata.notifBot.un + ']:' + '\n' + mydata.notifBot.txt;
|
||||
telegrambot.sendMsgTelegramToTheManagers(idapp, addtext, true);
|
||||
|
||||
if ('saldo' in fieldsvalue) {
|
||||
valprec = precRec && precRec.saldo ? precRec.saldo : 0;
|
||||
msg = i18n.__(
|
||||
'SALDO_VARIATO',
|
||||
circuit.name,
|
||||
req.user.username,
|
||||
dest,
|
||||
valprec,
|
||||
fieldsvalue.saldo,
|
||||
circuit.symbol
|
||||
);
|
||||
} else if ('fidoConcesso' in fieldsvalue) {
|
||||
valprec = precRec && precRec.fidoConcesso ? precRec.fidoConcesso : 0;
|
||||
msg = i18n.__(
|
||||
'FIDOCONCESSO_VARIATO',
|
||||
circuit.name,
|
||||
req.user.username,
|
||||
dest,
|
||||
valprec,
|
||||
fieldsvalue.fidoConcesso,
|
||||
circuit.symbol
|
||||
);
|
||||
} else if ('qta_maxConcessa' in fieldsvalue) {
|
||||
valprec = precRec && precRec.qta_maxConcessa ? precRec.qta_maxConcessa : 0;
|
||||
msg = i18n.__(
|
||||
'QTAMAX_VARIATO',
|
||||
circuit.name,
|
||||
req.user.username,
|
||||
dest,
|
||||
valprec,
|
||||
fieldsvalue.qta_maxConcessa,
|
||||
circuit.symbol
|
||||
);
|
||||
if (!!flotta) tools.writeFlottaLog(idapp, addtext, flotta.riga, flotta.col_prima);
|
||||
}
|
||||
|
||||
if (msg) {
|
||||
telegrambot.sendMsgTelegramToTheManagers(idapp, msg);
|
||||
telegrambot.sendMsgTelegramToTheAdminsOfCircuit(idapp, circuit.path, msg);
|
||||
}
|
||||
}
|
||||
|
||||
if (mydata.table === 'users') {
|
||||
if ('profile.resid_province' in fieldsvalue) {
|
||||
const card = fieldsvalue.hasOwnProperty('profile.resid_card') ? fieldsvalue['profile.resid_card'] : '';
|
||||
// Controlla se esiste il Circuito di questa provincia, se non esiste lo crea!
|
||||
await Circuit.createCircuitIfNotExist(req, idapp, fieldsvalue['profile.resid_province'], card);
|
||||
}
|
||||
|
||||
if (camporequisiti) {
|
||||
await User.checkIfSbloccatiRequisiti(idapp, allData, id);
|
||||
}
|
||||
|
||||
if ('aportador_solidario' in fieldsvalue) {
|
||||
let ind_order_ingr = mydata.ind_order_ingr;
|
||||
// SERVE SE CI METTO LE MINUSCOLE/MAIUSCOLE SBAGLIATE in invitante_username!
|
||||
const myuserfound = await User.findByUsername(idapp, fieldsvalue.aportador_solidario, false);
|
||||
if (!!myuserfound) {
|
||||
if (!!myuserfound._id && !myuserfound.deleted) {
|
||||
const aportador = await User.getUsernameById(idapp, myuserfound._id);
|
||||
fieldsvalue.aportador_solidario = aportador;
|
||||
//Aggiorna record !
|
||||
await mytable.findByIdAndUpdate(id, { $set: fieldsvalue });
|
||||
}
|
||||
} else {
|
||||
res.send({
|
||||
code: server_constants.RIS_CODE_ERR,
|
||||
msg: 'Non aggiornato',
|
||||
});
|
||||
res.status(400).send();
|
||||
return false;
|
||||
}
|
||||
} else if ('deleted' in fieldsvalue) {
|
||||
if (mydata.table === 'accounts') {
|
||||
let msg = '';
|
||||
if (fieldsvalue.deleted) msg = 'cancellato (nascosto)';
|
||||
else msg = 'Ripristinato';
|
||||
if (rec.circuitId) circuit = await Circuit.getCircuitByCircuitId(rec.circuitId);
|
||||
|
||||
await telegrambot.sendMsgTelegramToTheManagers(
|
||||
idapp,
|
||||
`L\'utente ` +
|
||||
tools.getNomeCognomeEUserNameByUser(rec) +
|
||||
` è stato ${msg} da ` +
|
||||
tools.getNomeCognomeEUserNameByUser(req.user)
|
||||
);
|
||||
}
|
||||
}
|
||||
let dest = rec.groupname ? rec.groupname : rec.username;
|
||||
let valprec = 0;
|
||||
|
||||
if (await tools.ModificheConsentite(req, mydata.table, fieldsvalue)) {
|
||||
let msg = '';
|
||||
if (mydata.table === 'users') {
|
||||
if ('aportador_solidario' in fieldsvalue) {
|
||||
const nomecognomenuovo = await User.getNameSurnameByUsername(idapp, fieldsvalue.aportador_solidario);
|
||||
const nomecognomeas = await User.getNameSurnameByUsername(idapp, rec.aportador_solidario);
|
||||
msg =
|
||||
`Variato l'invitante di ` +
|
||||
tools.getNomeCognomeEUserNameByUser(rec) +
|
||||
'\nmodificato da ' +
|
||||
tools.getNomeCognomeEUserNameByUser(req.user) +
|
||||
' \n' +
|
||||
'Prima: ' +
|
||||
nomecognomeas +
|
||||
' (' +
|
||||
rec.aportador_solidario +
|
||||
')\n' +
|
||||
'Dopo: ' +
|
||||
nomecognomenuovo +
|
||||
' (' +
|
||||
fieldsvalue.aportador_solidario +
|
||||
') ]';
|
||||
if ('saldo' in fieldsvalue) {
|
||||
valprec = precRec && precRec.saldo ? precRec.saldo : 0;
|
||||
msg = i18n.__(
|
||||
'SALDO_VARIATO',
|
||||
circuit.name,
|
||||
req.user.username,
|
||||
dest,
|
||||
valprec,
|
||||
fieldsvalue.saldo,
|
||||
circuit.symbol
|
||||
);
|
||||
} else if ('fidoConcesso' in fieldsvalue) {
|
||||
valprec = precRec && precRec.fidoConcesso ? precRec.fidoConcesso : 0;
|
||||
msg = i18n.__(
|
||||
'FIDOCONCESSO_VARIATO',
|
||||
circuit.name,
|
||||
req.user.username,
|
||||
dest,
|
||||
valprec,
|
||||
fieldsvalue.fidoConcesso,
|
||||
circuit.symbol
|
||||
);
|
||||
} else if ('qta_maxConcessa' in fieldsvalue) {
|
||||
valprec = precRec && precRec.qta_maxConcessa ? precRec.qta_maxConcessa : 0;
|
||||
msg = i18n.__(
|
||||
'QTAMAX_VARIATO',
|
||||
circuit.name,
|
||||
req.user.username,
|
||||
dest,
|
||||
valprec,
|
||||
fieldsvalue.qta_maxConcessa,
|
||||
circuit.symbol
|
||||
);
|
||||
}
|
||||
|
||||
// Metti l'iniziale
|
||||
if (!(await User.AportadorOrig(id))) {
|
||||
await mytable.findByIdAndUpdate(
|
||||
id,
|
||||
{ $set: { aportador_iniziale: fieldsvalue.aportador_solidario } },
|
||||
{ new: false }
|
||||
);
|
||||
}
|
||||
if (msg) {
|
||||
telegrambot.sendMsgTelegramToTheManagers(idapp, msg);
|
||||
telegrambot.sendMsgTelegramToTheAdminsOfCircuit(idapp, circuit.path, msg);
|
||||
}
|
||||
}
|
||||
|
||||
if (msg !== '') telegrambot.sendMsgTelegramToTheManagers(idapp, msg);
|
||||
}
|
||||
if (mydata.table === 'users') {
|
||||
if ('profile.resid_province' in fieldsvalue) {
|
||||
const card = fieldsvalue.hasOwnProperty('profile.resid_card') ? fieldsvalue['profile.resid_card'] : '';
|
||||
// Controlla se esiste il Circuito di questa provincia, se non esiste lo crea!
|
||||
await Circuit.createCircuitIfNotExist(req, idapp, fieldsvalue['profile.resid_province'], card);
|
||||
}
|
||||
|
||||
res.send({ code: server_constants.RIS_CODE_OK, msg: '' });
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
tools.mylogserr('Error patch USER: ', e.message);
|
||||
res.status(400).send();
|
||||
});
|
||||
if (camporequisiti) {
|
||||
await User.checkIfSbloccatiRequisiti(idapp, allData, id);
|
||||
}
|
||||
|
||||
if ('aportador_solidario' in fieldsvalue) {
|
||||
let ind_order_ingr = mydata.ind_order_ingr;
|
||||
// SERVE SE CI METTO LE MINUSCOLE/MAIUSCOLE SBAGLIATE in invitante_username!
|
||||
const myuserfound = await User.findByUsername(idapp, fieldsvalue.aportador_solidario, false);
|
||||
if (!!myuserfound) {
|
||||
if (!!myuserfound._id && !myuserfound.deleted) {
|
||||
const aportador = await User.getUsernameById(idapp, myuserfound._id);
|
||||
fieldsvalue.aportador_solidario = aportador;
|
||||
//Aggiorna record !
|
||||
await mytable.findByIdAndUpdate(id, { $set: fieldsvalue });
|
||||
}
|
||||
} else {
|
||||
res.send({
|
||||
code: server_constants.RIS_CODE_ERR,
|
||||
msg: 'Non aggiornato',
|
||||
});
|
||||
res.status(400).send();
|
||||
return false;
|
||||
}
|
||||
} else if ('deleted' in fieldsvalue) {
|
||||
let msg = '';
|
||||
if (fieldsvalue.deleted) msg = 'cancellato (nascosto)';
|
||||
else msg = 'Ripristinato';
|
||||
|
||||
await telegrambot.sendMsgTelegramToTheManagers(
|
||||
idapp,
|
||||
`L\'utente ` +
|
||||
tools.getNomeCognomeEUserNameByUser(rec) +
|
||||
` è stato ${msg} da ` +
|
||||
tools.getNomeCognomeEUserNameByUser(req.user)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (await tools.ModificheConsentite(req, mydata.table, fieldsvalue)) {
|
||||
let msg = '';
|
||||
if (mydata.table === 'users') {
|
||||
if ('aportador_solidario' in fieldsvalue) {
|
||||
const nomecognomenuovo = await User.getNameSurnameByUsername(idapp, fieldsvalue.aportador_solidario);
|
||||
const nomecognomeas = await User.getNameSurnameByUsername(idapp, rec.aportador_solidario);
|
||||
msg =
|
||||
`Variato l'invitante di ` +
|
||||
tools.getNomeCognomeEUserNameByUser(rec) +
|
||||
'\nmodificato da ' +
|
||||
tools.getNomeCognomeEUserNameByUser(req.user) +
|
||||
' \n' +
|
||||
'Prima: ' +
|
||||
nomecognomeas +
|
||||
' (' +
|
||||
rec.aportador_solidario +
|
||||
')\n' +
|
||||
'Dopo: ' +
|
||||
nomecognomenuovo +
|
||||
' (' +
|
||||
fieldsvalue.aportador_solidario +
|
||||
') ]';
|
||||
|
||||
// Metti l'iniziale
|
||||
if (!(await User.AportadorOrig(id))) {
|
||||
await mytable.findByIdAndUpdate(
|
||||
id,
|
||||
{ $set: { aportador_iniziale: fieldsvalue.aportador_solidario } },
|
||||
{ new: false }
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (msg !== '') telegrambot.sendMsgTelegramToTheManagers(idapp, msg);
|
||||
}
|
||||
|
||||
res.send({ code: server_constants.RIS_CODE_OK, msg: '' });
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
tools.mylogserr('Error patch USER: ', e.message);
|
||||
res.status(400).send();
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
tools.mylogserr('Error chval: ', e.message);
|
||||
res.status(400).send();
|
||||
|
||||
Reference in New Issue
Block a user