- Aggiornati margini.
- Cataloghi: Export ed Import di una pagine ed i suoi elementi !
This commit is contained in:
@@ -842,6 +842,7 @@ async function duplicatePage(pageId, newpath) {
|
||||
// modifiche ai campi se necessario, per esempio:
|
||||
path: newpath,
|
||||
title: newpath,
|
||||
active: true,
|
||||
date_updated: new Date()
|
||||
});
|
||||
|
||||
@@ -849,15 +850,14 @@ async function duplicatePage(pageId, newpath) {
|
||||
await newPage.save();
|
||||
|
||||
// Trova tutti gli elementi associati a Page da duplicare
|
||||
const elemsToDuplicate = await MyElem.find({ idPage: pageId });
|
||||
const elemsToDuplicate = await MyElem.find({ idPage: pageId }).lean();
|
||||
|
||||
// Duplica ogni elemento utilizzando il nuovo idPath
|
||||
const duplicates = elemsToDuplicate.map(elem => {
|
||||
const newElem = new MyElem({
|
||||
...elem.toObject(), // Copia le proprietà dell'elemento
|
||||
...elem, // Copia le proprietà dell'elemento
|
||||
_id: new mongoose.Types.ObjectId(), // Genera un nuovo ID
|
||||
idPage: newPage._id // Imposta il nuovo campo IdPage
|
||||
// Puoi modificare altri campi se necessario qui
|
||||
idPage: newPage._id.toString() // Imposta il nuovo campo IdPage
|
||||
});
|
||||
return newElem;
|
||||
});
|
||||
@@ -873,7 +873,121 @@ async function duplicatePage(pageId, newpath) {
|
||||
}
|
||||
};
|
||||
|
||||
router.post('/duppage', async (req, res) => {
|
||||
async function exportPage(idapp, pageId) {
|
||||
try {
|
||||
|
||||
const myexp = {
|
||||
mypages: [],
|
||||
myelems: [],
|
||||
}
|
||||
|
||||
// Trova il record di Page da duplicare
|
||||
const pageToExp = await MyPage.find({ _id: pageId, idapp }).lean();
|
||||
if (!pageToExp) {
|
||||
console.error('Page not found.');
|
||||
return;
|
||||
}
|
||||
|
||||
myexp.mypages = [...pageToExp];
|
||||
|
||||
// Trova tutti gli elementi associati a Page da duplicare
|
||||
const elemsToExp = await MyElem.find({ idapp, idPage: pageId }).lean();
|
||||
|
||||
myexp.myelems = [...elemsToExp];
|
||||
|
||||
const jsonString = JSON.stringify(myexp);
|
||||
|
||||
if (jsonString) {
|
||||
console.log('Esportazione completata con successo.');
|
||||
|
||||
return jsonString;
|
||||
}
|
||||
|
||||
return '';
|
||||
|
||||
} catch (error) {
|
||||
console.error('Errore durante l\'esportazione:', error);
|
||||
return '';
|
||||
}
|
||||
};
|
||||
|
||||
async function upsertRecord(table, record, appId) {
|
||||
|
||||
const existingRecord = await table.findOne({ idapp: appId, _id: record._id }); // Assumendo che `record` ha un ID
|
||||
if (existingRecord) {
|
||||
const modif = await table.updateOne({ _id: record._id }, { $set: { ...record, idapp: appId } });
|
||||
wasModified = modif.nModified > 0;
|
||||
} else {
|
||||
const ris = await table.create({
|
||||
...record,
|
||||
idapp: appId,
|
||||
});
|
||||
wasModified = !!ris;
|
||||
}
|
||||
|
||||
return wasModified; // Torna il numero di record importati (1 in questo caso)
|
||||
}
|
||||
|
||||
async function importPage(req, idapp, jsonString) {
|
||||
try {
|
||||
// Parsing dei dati JSON
|
||||
const myexp = JSON.parse(jsonString);
|
||||
|
||||
// Assicurati che i dati siano ben strutturati
|
||||
if (!myexp) {
|
||||
console.error('Dati non validi per l\'importazione.');
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
let totalImportedRecords = 0;
|
||||
|
||||
// Ciclo su ogni proprietà di myexp
|
||||
for (const key in myexp) {
|
||||
if (myexp.hasOwnProperty(key)) {
|
||||
const tableName = key;
|
||||
|
||||
// Verifica se la tabella esiste
|
||||
if (tableName) {
|
||||
const table = globalTables.getTableByTableName(tableName);
|
||||
|
||||
if (tableName === 'mypages') {
|
||||
if (User.isEditor(req.user.perm)) {
|
||||
for (const page of myexp.mypages) {
|
||||
totalImportedRecords += await upsertRecord(table, page, idapp) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
} else if (tableName === 'myelems') {
|
||||
if (User.isEditor(req.user.perm)) {
|
||||
for (const elem of myexp.myelems) {
|
||||
totalImportedRecords += await upsertRecord(table, elem, idapp) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
} else if (tableName === 'myusers') {
|
||||
if (User.isManager(req.user.perm)) {
|
||||
for (const user of myexp.myusers) {
|
||||
totalImportedRecords += await upsertRecord(table, user, idapp) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (totalImportedRecords) {
|
||||
console.log(`Importazione completata con successo. Totale record importati: ${totalImportedRecords}`);
|
||||
}
|
||||
|
||||
return { imported: totalImportedRecords };
|
||||
|
||||
} catch (error) {
|
||||
console.error('Errore durante l\'importazione:', error);
|
||||
}
|
||||
}
|
||||
|
||||
router.post('/duppage', authenticate, async (req, res) => {
|
||||
const params = req.body;
|
||||
const idapp = req.body.idapp;
|
||||
const mypath = params.path;
|
||||
@@ -899,6 +1013,52 @@ router.post('/duppage', async (req, res) => {
|
||||
console.error('Error', e);
|
||||
}
|
||||
|
||||
});
|
||||
router.post('/exppage', authenticate, async (req, res) => {
|
||||
const params = req.body;
|
||||
const idapp = req.body.idapp;
|
||||
const mypath = params.path;
|
||||
|
||||
try {
|
||||
|
||||
let found = await MyPage.findOne({ idapp, path: mypath })
|
||||
.then(async (ris) => {
|
||||
const resultJSon = await exportPage(idapp, ris._id);
|
||||
if (resultJSon) {
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, json: resultJSon });
|
||||
} else {
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, msg: '' });
|
||||
}
|
||||
|
||||
}).catch((e) => {
|
||||
console.log(e.message);
|
||||
res.status(400).send(e);
|
||||
});
|
||||
|
||||
} catch (e) {
|
||||
console.error('Error', e);
|
||||
}
|
||||
|
||||
});
|
||||
router.post('/imppage', authenticate, async (req, res) => {
|
||||
const params = req.body;
|
||||
const idapp = req.body.idapp;
|
||||
const jsonString = params.jsonString;
|
||||
|
||||
try {
|
||||
|
||||
const result = await importPage(req, idapp, jsonString);
|
||||
if (result) {
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, ris: result });
|
||||
} else {
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, ris: '' });
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.log(e.message);
|
||||
res.status(400).send(e);
|
||||
};
|
||||
|
||||
});
|
||||
|
||||
router.patch('/setlang', authenticate, async (req, res) => {
|
||||
@@ -1403,7 +1563,7 @@ router.delete('/delrec/:table/:id', authenticate, async (req, res) => {
|
||||
} else if (tablename === shared_consts.TAB_MYCIRCUITS) {
|
||||
// Se è un gruppo, allora cancella anche tutti i suoi riferimenti
|
||||
User.removeAllUsersFromMyCircuits(rec.idapp, rec.name);
|
||||
} else if (tablename === 'mypage') {
|
||||
} else if (tablename === shared_consts.TAB_MYPAGES) {
|
||||
// Cancella tutti gli elementi di quella pagina
|
||||
MyElem.deleteAllFromThisPage(rec._id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user