- Creazione di un Nuovo Catalogo (e la sua relativa pagina), a partire da un modello ed un catalogo esistente.
- Aggiunta dei bottoni sul Ccatalogocard
This commit is contained in:
@@ -15,7 +15,8 @@ const _ = require('lodash');
|
||||
|
||||
const { Catalog } = require('../models/catalog');
|
||||
|
||||
//GET /catalogs
|
||||
const globalTables = require('../tools/globalTables');
|
||||
|
||||
router.post('/', auth_default, async function (req, res, next) {
|
||||
const idapp = req.body.idapp;
|
||||
const userId = req.body.userId;
|
||||
@@ -46,4 +47,22 @@ router.get('/id/:id', async function (req, res) {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
router.post('/addnew', authenticate, async function (req, res, next) {
|
||||
const idapp = req.body.idapp;
|
||||
const data = req.body.newCatalog;
|
||||
|
||||
try {
|
||||
const newrecs = await globalTables.addNewCatalog(idapp, data);
|
||||
if (newrecs) {
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, data: newrecs });
|
||||
} else {
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, data: null });
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error fetching catalog by ID:', e);
|
||||
return res.status(400).send({ code: server_constants.RIS_CODE_ERR, msg: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -430,7 +430,7 @@ router.post('/settable', authenticate, async (req, res) => {
|
||||
} else {
|
||||
if (
|
||||
(mydata['_id'] === undefined || mydata['_id'] === '' || (mytablerec.isNew && mydata['_id'] === 0)) &&
|
||||
(mytablerec._id === undefined || mytablerec._id === '0'|| mytablerec._id === 0)
|
||||
(mytablerec._id === undefined || mytablerec._id === '0' || mytablerec._id === 0)
|
||||
) {
|
||||
mytablerec._id = new ObjectId();
|
||||
mydata._id = new ObjectId();
|
||||
@@ -891,69 +891,6 @@ router.post('/getpage', async (req, res) => {
|
||||
return found;
|
||||
});
|
||||
|
||||
async function duplicatePage(pageId, newpath) {
|
||||
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: newpath,
|
||||
title: newpath,
|
||||
inmenu: false,
|
||||
active: true,
|
||||
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 }).lean();
|
||||
|
||||
// Duplica ogni elemento utilizzando il nuovo idPath
|
||||
const duplicates = elemsToDuplicate.map((elem) => {
|
||||
const catalogo = elem.catalogo;
|
||||
|
||||
if (catalogo) {
|
||||
for (const recscheda of catalogo.arrSchede) {
|
||||
if (recscheda.scheda?.isTemplate) {
|
||||
// Se è un template allora devo mettergli un altro ID !
|
||||
recscheda.scheda._id = new mongoose.Types.ObjectId();
|
||||
// recscheda.scheda.name = getNewFreeNameTemplate(recscheda.scheda?.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (catalogo) elem.catalogo = { ...catalogo };
|
||||
|
||||
const newElem = new MyElem({
|
||||
...elem, // Copia le proprietà dell'elemento
|
||||
_id: new mongoose.Types.ObjectId(), // Genera un nuovo ID
|
||||
idPage: newPage._id.toString(), // Imposta il nuovo campo IdPage
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
async function exportPage(idapp, pageId) {
|
||||
try {
|
||||
const myexp = {
|
||||
@@ -1144,8 +1081,8 @@ router.post('/duppage', authenticate, async (req, res) => {
|
||||
try {
|
||||
let found = await MyPage.findOne({ idapp, path: mypath })
|
||||
.then(async (ris) => {
|
||||
const result = await duplicatePage(ris._id, newpath);
|
||||
if (result) {
|
||||
const result = await globalTables.duplicatePage(ris._id, newpath);
|
||||
if (result && result.newpage) {
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, msg: '' });
|
||||
} else {
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, msg: '' });
|
||||
@@ -1693,6 +1630,19 @@ router.delete('/delrec/:table/:id', authenticate, async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
if (tablename === 'catalogs') {
|
||||
// Devo cancellare anche la pagina associato al Catalogo!
|
||||
|
||||
const myrec = await mytable.findOne({ _id: id }).lean();
|
||||
if (myrec.idPageAssigned) {
|
||||
await MyPage.deleteOne({ _id: myrec.idPageAssigned }).then((rec) => {
|
||||
if (!rec) {
|
||||
console.log('Errore cancellazione pagina associata al catalogo');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let ris = null;
|
||||
|
||||
if (!cancellato) {
|
||||
|
||||
Reference in New Issue
Block a user