- aggiornati gli argomenti in base a GM

This commit is contained in:
Surya Paolo
2025-02-12 18:31:59 +01:00
parent b643c7cdc3
commit d77f9381e5
8 changed files with 202 additions and 60 deletions

View File

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