diff --git a/src/server/models/catprod.js b/src/server/models/catprod.js index ba667b0..dae7283 100755 --- a/src/server/models/catprod.js +++ b/src/server/models/catprod.js @@ -16,6 +16,9 @@ const CatProdSchema = new Schema({ idapp: { type: String, }, + idArgomento: { + type: Number, + }, name: { type: String, }, diff --git a/src/server/models/myelem.js b/src/server/models/myelem.js index afb153c..6c6d075 100755 --- a/src/server/models/myelem.js +++ b/src/server/models/myelem.js @@ -141,6 +141,9 @@ const MyElemSchema = new Schema({ parambool3: { type: Boolean, }, + parambool4: { + type: Boolean, + }, number: { type: Number, }, @@ -429,45 +432,48 @@ MyElemSchema.statics.findallSchedeTemplate = async function (idapp) { : [] ); - const duplicateIds = schedeTemplate.reduce((acc, scheda) => { - const id = scheda.scheda._id; // Ottieni l'ID della scheda - if (!acc[id]) { - acc[id] = []; // Inizializza un array per tenere traccia delle pagine + if (idapp === '18') { + + const duplicateIds = schedeTemplate.reduce((acc, scheda) => { + const id = scheda.scheda._id; // Ottieni l'ID della scheda + if (!acc[id]) { + acc[id] = []; // Inizializza un array per tenere traccia delle pagine + } + acc[id].push(scheda.idPageOrig); // Aggiungi l'idPage all'array + return acc; + }, {}); + + // Filtra i duplicati + const duplicates = Object.entries(duplicateIds) + .filter(([_, pages]) => pages.length > 1) // Mantieni solo gli ID con più di un'istanza + .map(([id, pages]) => ({ id, pages })); // Ottieni ID e pagine corrispondenti + + // Recupera i dettagli delle pagine + const pageIds = duplicates.flatMap(dup => dup.pages); // Estrai tutti gli idPage + const pages = await MyPage.find({ idapp, _id: { $in: pageIds } }).lean(); + + // Crea una mappatura tra idPage e title + const pageMap = pages.reduce((acc, page) => { + acc[page._id] = page.title; // Mappa idPage a title + return acc; + }, {}); + + // Associa i titoli delle pagine agli ID duplicati + const resultWithTitles = duplicates.map(dup => ({ + id: dup.id, + pages: dup.pages.map(_id => ({ + _id, + title: pageMap[_id] || 'Titolo non trovato' // Usa la mappatura per trovare il titolo + })) + })); + + if (resultWithTitles.length > 0) { + console.log('Duplicati e titoli delle pagine:', JSON.stringify(resultWithTitles, null, 2)); + await deleteOldMyElems(idapp); } - acc[id].push(scheda.idPageOrig); // Aggiungi l'idPage all'array - return acc; - }, {}); - - // Filtra i duplicati - const duplicates = Object.entries(duplicateIds) - .filter(([_, pages]) => pages.length > 1) // Mantieni solo gli ID con più di un'istanza - .map(([id, pages]) => ({ id, pages })); // Ottieni ID e pagine corrispondenti - - // Recupera i dettagli delle pagine - const pageIds = duplicates.flatMap(dup => dup.pages); // Estrai tutti gli idPage - const pages = await MyPage.find({ idapp, _id: { $in: pageIds } }).lean(); - - // Crea una mappatura tra idPage e title - const pageMap = pages.reduce((acc, page) => { - acc[page._id] = page.title; // Mappa idPage a title - return acc; - }, {}); - - // Associa i titoli delle pagine agli ID duplicati - const resultWithTitles = duplicates.map(dup => ({ - id: dup.id, - pages: dup.pages.map(_id => ({ - _id, - title: pageMap[_id] || 'Titolo non trovato' // Usa la mappatura per trovare il titolo - })) - })); - - if (resultWithTitles.length > 0) { - console.log('Duplicati e titoli delle pagine:', JSON.stringify(resultWithTitles, null, 2)); - await deleteOldMyElems(idapp); + } - return schedeTemplate; } catch (e) { console.error('Err', e); diff --git a/src/server/models/productInfo.js b/src/server/models/productInfo.js index 05c3271..92204e3 100755 --- a/src/server/models/productInfo.js +++ b/src/server/models/productInfo.js @@ -124,6 +124,9 @@ const productInfoSchema = new Schema({ collezione: { type: String, }, + ListaArgomenti: { + type: String, + }, date_pub: { type: Date, }, diff --git a/src/server/models/user.js b/src/server/models/user.js index 7fc0904..48a1922 100755 --- a/src/server/models/user.js +++ b/src/server/models/user.js @@ -695,6 +695,14 @@ UserSchema.statics.isEditor = function (perm) { return false; } }; +UserSchema.statics.isGrafico = function (perm) { + try { + return ((perm & shared_consts.Permissions.Grafico) === + shared_consts.Permissions.Grafico); + } catch (e) { + return false; + } +}; UserSchema.statics.isZoomeri = function (perm) { try { diff --git a/src/server/router/admin_router.js b/src/server/router/admin_router.js index 25f2384..1d05f46 100755 --- a/src/server/router/admin_router.js +++ b/src/server/router/admin_router.js @@ -55,6 +55,73 @@ const { exec } = require('child_process'); const execPromise = util.promisify(exec); +async function updateProductInfo(recproductInfoAttuale, product, idapp, mycatstr) { + if (!recproductInfoAttuale || !mycatstr) return recproductInfoAttuale; + + let idArgomentoNum = null; + + let productInfo = null; + + if (product.ListaArgomenti) { + idArgomentoNum = parseInt(product.ListaArgomenti); + productInfo = { ...recproductInfoAttuale, IdArgomento: idArgomentoNum }; + } else { + productInfo = { ...recproductInfoAttuale }; + } + + let reccatprod = await findOrCreateCatProd(idapp, idArgomentoNum, mycatstr); + + if (reccatprod) { + updateProductInfoCatProds(productInfo, reccatprod); + } + + return productInfo; +} + +async function findOrCreateCatProd(idapp, IdArgomento, DescrArgomento) { + let reccatprod = null; + if (IdArgomento) { + reccatprod = await CatProd.findOne({ idapp, IdArgomento }).lean(); + } + + if (!reccatprod) { + reccatprod = await CatProd.findOne({ idapp, name: DescrArgomento }).lean(); + if (reccatprod) { + if (IdArgomento) { + await CatProd.findOneAndUpdate( + { _id: reccatprod._id }, + { $set: { IdArgomento } }, + { new: true, upsert: false } + ); + } + } else { + if (IdArgomento) { + try { + reccatprod = new CatProd({ idapp, IdArgomento, name: DescrArgomento }); + await reccatprod.save(); + } catch (e) { + console.error('Errore nella creazione di CatProd:', e); + return null; + } + } + } + } + + return reccatprod; +} + +function updateProductInfoCatProds(productInfo, reccatprod) { + if (productInfo) { + const isChanged = !productInfo.idCatProds || productInfo.idCatProds.length !== 1 || + productInfo.idCatProds[0] !== reccatprod._id; + + if (isChanged) { + productInfo.idCatProds = [reccatprod._id]; + console.log('ARGOMENTO VARIATO!', reccatprod.name); + } + } +} + async function compressPdf(inputFile, outputFile, compressione) { try { const tempFolder = path.join(cwd, "temp"); @@ -776,7 +843,7 @@ router.post('/import', authenticate, async (req, res) => { let imported = 0; let errors = 0; - const ripopola = true; + const ripopola = false; // SETTARE su TRUE if (ripopola) { dataObjects = null; @@ -882,6 +949,7 @@ router.post('/import', authenticate, async (req, res) => { idapp: product.idapp, code: product.isbn ? product.isbn : product.code, sku: product.sku, + idCatProds: recproductInfoAttuale?.idCatProds, // id_wp: product._id, @@ -899,12 +967,18 @@ router.post('/import', authenticate, async (req, res) => { } + if (!productInfo.idCatProds) { + productInfo.idCatProds = []; + } + + // Aggiorna la collana solo se non è stata già impostata nel record attuale //if (recproductInfoAttuale && !recproductInfoAttuale.idCollana && product.DescrizioneCollana) { if (recproductInfoAttuale && product.DescrizioneCollana) { const idCollanaNum = parseInt(product.IdCollana) productInfo.idCollana = idCollanaNum; - + + reccollana = await Collana.findOne({ idapp, idCollana: idCollanaNum }).lean(); if (!reccollana) { try { @@ -918,6 +992,11 @@ router.post('/import', authenticate, async (req, res) => { } + if (recproductInfoAttuale && product.DescrArgomento) { + + productInfo = await updateProductInfo(productInfo, product, idapp, product.DescrArgomento); + } + if (product.DataPubblicazione) { productInfo.date_pub = new Date(product.DataPubblicazione); // convert data to timestamp @@ -1109,32 +1188,56 @@ router.post('/import', authenticate, async (req, res) => { productInfo.name = productInfo.name.replace(/ - Usato$| - Nuovo$| - Epub$| - Ebook$| - Mobi$| - DVD$| - Streaming$| - Download$/, ""); + let reccateg = null; + if (product.categories) { - arrcat = product.categories.trim().split(','); - for (const mycat of arrcat) { - let mycatstr = mycat.trim(); + // Verifica prima se questa categoria è stata aggiornata ! + const recrankingisbn = await ImportaIsbn.findOne({ sku: product.sku }).lean(); + if (recrankingisbn && recrankingisbn.DescrArgomento) { - // Controlla se ci sono le sottocategorie: - arrsubcat = mycatstr.trim().split('>'); + productInfo.idCatProds = []; // Azzera - if (arrsubcat.length > 1) { - // Ci sono delle sottocategorie - mycatstr = arrsubcat[0].trim(); - product.subcat_name = arrsubcat[1].trim();; + if (tools.isArray(recrankingisbn.ListaArgomenti) && recrankingisbn.ListaArgomenti.length > 1) { + console.log('ListaArgomenti STA RITORNANDO UN ARRAY !!!! ', recrankingisbn.ListaArgomenti); } - // Cerca la Categoria - reccateg = await CatProd.findOne({ idapp, name: mycatstr }).lean(); - if (!reccateg) { - // Non esiste questo produttore, quindi lo creo ! - reccateg = new CatProd({ idapp, name: mycatstr }); - ris = await reccateg.save(); + // !!!! + for (const idArgomento of recrankingisbn.ListaArgomenti) { + mycatstr = recrankingisbn.DescrArgomento; + + if (mycatstr) + productInfo = await updateProductInfo(productInfo, product, idapp, mycatstr); + } + + } else { + + arrcat = product.categories.trim().split(','); + for (const mycat of arrcat) { + let mycatstr = mycat.trim(); + + // Controlla se ci sono le sottocategorie: + arrsubcat = mycatstr.trim().split('>'); + + if (arrsubcat.length > 1) { + // Ci sono delle sottocategorie + mycatstr = arrsubcat[0].trim(); + product.subcat_name = arrsubcat[1].trim();; + } + + // Cerca la Categoria reccateg = await CatProd.findOne({ idapp, name: mycatstr }).lean(); - } + if (!reccateg) { + // Non esiste questo produttore, quindi lo creo ! + reccateg = new CatProd({ idapp, name: mycatstr }); + ris = await reccateg.save(); + console.log('CREA con ARGOMENTO VECCHIO... ', mycatstr); + reccateg = await CatProd.findOne({ idapp, name: mycatstr }).lean(); + } - if (reccateg) { - productInfo.idCatProds.push(reccateg._id); + if (reccateg) { + productInfo.idCatProds.push(reccateg._id); + } } } } diff --git a/src/server/router/index_router.js b/src/server/router/index_router.js index 66a697d..f4fe924 100755 --- a/src/server/router/index_router.js +++ b/src/server/router/index_router.js @@ -357,6 +357,7 @@ router.post('/settable', authenticate, async (req, res) => { if ((!User.isAdmin(req.user.perm) && !User.isManager(req.user.perm) && !User.isEditor(req.user.perm) + && !User.isGrafico(req.user.perm) && !User.isFacilitatore(req.user.perm)) && await !tools.ModificheConsentite(req, params.table, fieldsvalue, mydata ? mydata._id : '')) { @@ -412,12 +413,18 @@ router.post('/settable', authenticate, async (req, res) => { delete mydata['__v']; delete mydata['__proto__']; - let mytablerec = new mytable(mydata); + const isNotNew = (mydata['_id'] !== undefined && mydata['_id'] !== 0 && mydata['_id'] !== ''); + + let mytablerec = null; + + mytablerec = new mytable(mydata); + // console.log('mytablerec', mytablerec); const mytablestrutt = globalTables.getTableByTableName(params.table); - if (mydata['_id'] !== undefined && mydata['_id'] !== 0 && mydata['_id'] !== '') { + + if (isNotNew) { mytablerec.isNew = false; } @@ -427,7 +434,9 @@ router.post('/settable', authenticate, async (req, res) => { } else if (params.table === 'hours') { } else { - if ((mydata['_id'] === undefined || mydata['_id'] === '' || (mytablerec.isNew && mydata['_id'] === 0)) && (mytablerec._id === undefined || mytablerec._id === '0')) { + if ((mydata['_id'] === undefined || mydata['_id'] === '' || + (mytablerec.isNew && mydata['_id'] === 0)) + && (mytablerec._id === undefined || mytablerec._id === '0')) { mytablerec._id = new ObjectId(); mydata._id = new ObjectId(); mytablerec.isNew = true; @@ -493,6 +502,11 @@ router.post('/settable', authenticate, async (req, res) => { return await myPromise .then(async (doupdate) => { + if (false) { + let plainObject = mytablerec.toObject(); + console.log(plainObject); + } + if (doupdate) return mytable.updateOne({ _id: mytablerec._id }, mydata, { new: true }) else @@ -673,7 +687,7 @@ router.post('/getobj', authenticate_noerror, async (req, res) => { try { let cmd = req.body.cmd; let idapp = req.user ? req.user.idapp : sanitizeHtml(req.body.idapp); // Cambiato from params.idapp a req.body.idapp - let ris = null; + let ris = null; if (cmd === 'lista_editori') { ris = await User.find( diff --git a/src/server/router/users_router.js b/src/server/router/users_router.js index 0482d94..41b461d 100755 --- a/src/server/router/users_router.js +++ b/src/server/router/users_router.js @@ -1098,6 +1098,10 @@ async function eseguiDbOp(idapp, mydata, locale, req, res) { } else if (mydata.dbop === 'ReplaceAllCircuits') { // ++ Replace All Circuitname with 'Circuito RIS %s' await Circuit.replaceAllCircuitNames(idapp); + } else if (mydata.dbop === 'eliminaCatProds') { + + await CatProd.deleteMany({ idapp }); + await SubCatProd.deleteMany({ idapp }); } else if (mydata.dbop === 'visuStat') { // ris = await User.visuUtentiNonInNavi(idapp); //} else if (mydata.dbop === 'creaNavi') { diff --git a/src/server/tools/shared_nodejs.js b/src/server/tools/shared_nodejs.js index c2f4dfb..2eeae00 100755 --- a/src/server/tools/shared_nodejs.js +++ b/src/server/tools/shared_nodejs.js @@ -358,6 +358,7 @@ module.exports = { Editor: 16, Zoomeri: 32, Department: 64, + Grafico: 128, }, MessageOptions: {