- 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:
Surya Paolo
2025-06-12 23:49:18 +02:00
parent 133dc6a502
commit 6b9ddc8986
7 changed files with 557 additions and 439 deletions

View File

@@ -99,9 +99,9 @@ const MyElemSchema = new Schema({
path: {
type: String,
},
oldpath: {
/*oldpath: {
type: String,
},
},*/
idPage: { type: String },
type: {
type: Number,

View File

@@ -208,6 +208,7 @@ MyPageSchema.statics.findOnlyStruttRec = async function (idapp) {
path: 1,
active: 1,
onlyif_logged: 1,
isTemplate: 1,
only_residenti: 1,
only_admin: 1,
inmenu: 1,

View File

@@ -476,6 +476,7 @@ class Macro {
for (const recproduct of recproducts) {
await this.elaboraProdotto(recproduct, opt);
const sku = recproduct.IdArticolo;
if (sku) {

View File

@@ -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;

View File

@@ -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) {

View File

@@ -1,5 +1,5 @@
const os = require('os');
const fs = require('fs'); // 👈 Usa il modulo promises
const fs = require('fs-extra'); // 👈 Usa il modulo promises
const xml2js = require('xml2js');
const path = require('path');
const WebSocket = require('ws');
@@ -5765,7 +5765,6 @@ module.exports = {
}
return mydir;
},
getURLImg(idapp, table, username, img, checkifExist) {
@@ -5773,12 +5772,8 @@ module.exports = {
try {
// let dir = this.getdirByIdApp(idapp) + dirmain + '/' + this.getDirUpload();
let dir =
this.getdirByIdApp(idapp)
'/' +
this.getDirUpload() +
shared_consts.getDirectoryImgByTable(table, username) +
dirmain;
let dir = this.getdirByIdApp(idapp);
'/' + this.getDirUpload() + shared_consts.getDirectoryImgByTable(table, username) + dirmain;
img = dir + img;
@@ -6073,10 +6068,7 @@ module.exports = {
if (aggiornatoimg?.filepath.includes('noimg.jpg')) {
// nascondi il prodotto se non trovo l'immagine !
await Product.updateOne(
{ idProductInfo: productInfo._id },
{ $set: { deleted: true } }
);
await Product.updateOne({ idProductInfo: productInfo._id }, { $set: { deleted: true } });
aggiornatoimg = { ris: false, deleted: true };
}
@@ -6199,7 +6191,16 @@ module.exports = {
},
async attendiNSecondi(numsec) {
await new Promise(resolve => setTimeout(resolve, numsec * 1000));
await new Promise((resolve) => setTimeout(resolve, numsec * 1000));
},
async copyDirectory(sourceDir, destinationDir) {
try {
await fs.copy(sourceDir, destinationDir);
console.log('Directory copiata con successo!');
} catch (err) {
console.error('Errore durante la copia della directory:', err);
}
},
fixFilePath(myfilepath) {

File diff suppressed because it is too large Load Diff