- Catalogo: Aggiunta di Schede
This commit is contained in:
@@ -44,6 +44,7 @@ const { Contribtype } = require('../models/contribtype');
|
||||
const { PaymentType } = require('../models/paymenttype');
|
||||
const { Discipline } = require('../models/discipline');
|
||||
const { MyElem } = require('../models/myelem');
|
||||
const { MySCheda } = require('../models/myscheda');
|
||||
const { Skill } = require('../models/skill');
|
||||
const { Good } = require('../models/good');
|
||||
const { StatusSkill } = require('../models/statusSkill');
|
||||
@@ -826,6 +827,80 @@ router.post('/getpage', async (req, res) => {
|
||||
|
||||
});
|
||||
|
||||
async function duplicatePage(pageId) {
|
||||
try {
|
||||
// Trova il record di Page da duplicare
|
||||
const pageToDuplicate = await MyPage.findById(pageId);
|
||||
if (!pageToDuplicate) {
|
||||
console.error('Page not found.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Crea una copia del record di Page
|
||||
const newPage = new MyPage({
|
||||
...pageToDuplicate.toObject(), // Converte il documento Moongose in oggetto
|
||||
_id: new mongoose.Types.ObjectId(), // Genera un nuovo ID
|
||||
// modifiche ai campi se necessario, per esempio:
|
||||
path: `${pageToDuplicate.path}-copia`, // Modifica il campo path per identificare la copia
|
||||
title: `${pageToDuplicate.title}-copia`, // Modifica il campo path per identificare la copia
|
||||
date_updated: new Date()
|
||||
});
|
||||
|
||||
// Salva il nuovo record di Page
|
||||
await newPage.save();
|
||||
|
||||
// Trova tutti gli elementi associati a Page da duplicare
|
||||
const elemsToDuplicate = await MyElem.find({ idPage: pageId });
|
||||
|
||||
// Duplica ogni elemento utilizzando il nuovo idPath
|
||||
const duplicates = elemsToDuplicate.map(elem => {
|
||||
const newElem = new MyElem({
|
||||
...elem.toObject(), // 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
|
||||
});
|
||||
return newElem;
|
||||
});
|
||||
|
||||
// Salva tutti gli elementi duplicati
|
||||
await MyElem.insertMany(duplicates);
|
||||
|
||||
console.log('Duplicazione completata con successo.');
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Errore durante la duplicazione:', error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
router.post('/duppage', 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 result = await duplicatePage(ris._id);
|
||||
if (result) {
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, msg: '' });
|
||||
} 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.patch('/setlang', authenticate, async (req, res) => {
|
||||
const username = req.body.data.username;
|
||||
const idapp = req.user.idapp;
|
||||
@@ -1313,6 +1388,9 @@ 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') {
|
||||
// Cancella tutti gli elementi di quella pagina
|
||||
MyElem.deleteAllFromThisPage(rec._id);
|
||||
}
|
||||
|
||||
tools.refreshAllTablesInMem(rec.idapp, tablename, true, rec.username);
|
||||
|
||||
Reference in New Issue
Block a user