- Ricerca Titolo per nome o autore o ISBN o codice articolo

This commit is contained in:
Surya Paolo
2025-03-31 23:56:01 +02:00
parent 789dc1dcae
commit 5431fe118e
9 changed files with 345 additions and 114 deletions

View File

@@ -115,16 +115,19 @@ async function findOrCreateCatProd(idapp, idArgomento, DescrArgomento) {
function updateProductInfoCatProds(productInfo, reccatprod) {
if (productInfo) {
const isChanged = (!productInfo.idCatProds || productInfo.idCatProds.length < 1) ||
(productInfo.idCatProds[0].toString() !== reccatprod._id.toString());
// Controllo che nell'array productInfo.idCatProds ci sia esattamente 1 record e che sia uguale a reccatprod._id
const cond3 = Array.isArray(productInfo.idCatProds) && productInfo.idCatProds[0].toString() !== reccatprod._id.toString();
const isChanged =
!Array.isArray(productInfo.idCatProds) || // Assicurati che sia un array
productInfo.idCatProds.length !== 1 || // L'array deve contenere esattamente 1 elemento
cond3; // Il primo elemento deve essere uguale a reccatprod._id
if (isChanged) {
productInfo.idCatProds = [reccatprod._id];
console.log('ARGOMENTO VARIATO!', reccatprod.name);
console.log('ARGOMENTO VARIATO!', reccatprod.name, ' Libro:', productInfo.name);
}
}
}
async function compressPdf(inputFile, outputFile, compressione) {
try {
const tempFolder = path.join(cwd, "temp");
@@ -406,63 +409,138 @@ function fixURL(url) {
async function downloadImgIfMissing(productInfo) {
if (!productInfo.image_link)
return { prodInfo: null, aggiornatoimg: false };
const relativeimg = productInfo.image_link.split('/').pop();
const img = 'upload/products/' + productInfo.image_link.split('/').pop();
const savePath = path.resolve(__dirname, img); // Sostituisci con il percorso dove salvare l'immagine
let scaricaimg = !productInfo.imagefile || !fs.existsSync(savePath);
if (!productInfo.imagefile && fs.existsSync(savePath)) {
// esiste il file, ma sul DB non è corretto
const stats = fs.statSync(savePath); // Ottieni informazioni sul file
if (stats.size > 0) { // Controlla se la dimensione del file è maggiore di zero
// Esiste il file ed è non vuoto, ma sul DB non è corretto
productInfo.imagefile = relativeimg;
return { prodInfo: productInfo, aggiornatoimg: true };
try {
if (tools.sulServer()) {
dirmain = '';
} else {
scaricaimg = true;
dirmain = server_constants.DIR_PUBLIC_LOCALE;
}
}
if (productInfo.imagefile && fs.existsSync(savePath)) {
// esiste il file, ma sul DB non è corretto
const stats = fs.statSync(savePath); // Ottieni informazioni sul file
const vecchiomodo = false;
if (stats.size <= 0) { // Controlla se la dimensione del file è maggiore di zero
scaricaimg = true;
}
}
if (productInfo.image_link && vecchiomodo) {
if (scaricaimg) {
// Download image from the URL productInfo.image_link
productInfo.imagefile = relativeimg;
const relativeimg = productInfo.image_link.split('/').pop();
const img = tools.getdirByIdApp(productInfo.idapp) + dirmain +
server_constants.DIR_UPLOAD + '/products/' + productInfo.image_link.split('/').pop();
const savePath = path.resolve(__dirname, img); // Sostituisci con il percorso dove salvare l'immagine
const downloader = new ImageDownloader();
let scaricaimg = !productInfo.imagefile || !fs.existsSync(savePath);
const aggiornatoimg = await downloader.downloadImage(productInfo.image_link, savePath,
{
maxRetries: 3,
initialDelay: 300,
timeout: 15000,
}).then(result => {
if (result) {
// console.log('Download completato con successo!');
if (!productInfo.imagefile && fs.existsSync(savePath)) {
// esiste il file, ma sul DB non è corretto
const stats = fs.statSync(savePath); // Ottieni informazioni sul file
if (stats.size > 0) { // Controlla se la dimensione del file è maggiore di zero
// Esiste il file ed è non vuoto, ma sul DB non è corretto
productInfo.imagefile = relativeimg;
return { prodInfo: productInfo, aggiornatoimg: true };
} else {
console.log('Download non riuscito.');
scaricaimg = true;
}
}
if (productInfo.imagefile && fs.existsSync(savePath)) {
// esiste il file, ma sul DB non è corretto
const stats = fs.statSync(savePath); // Ottieni informazioni sul file
if (stats.size <= 0) { // Controlla se la dimensione del file è maggiore di zero
scaricaimg = true;
}
}
if (scaricaimg && vecchiomodo) {
// Download image from the URL productInfo.image_link
productInfo.imagefile = relativeimg;
const downloader = new ImageDownloader();
const aggiornatoimg = await downloader.downloadImage(productInfo.image_link, savePath,
{
maxRetries: 3,
initialDelay: 300,
timeout: 15000,
}).then(result => {
if (result) {
// console.log('Download completato con successo!');
} else {
console.log('Download non riuscito.');
}
return result;
});
return { prodInfo: productInfo, aggiornatoimg: aggiornatoimg.ris };
}
}
let fileesistente = false;
if (productInfo.imagefile) {
// controlla se esiste il file
const img = tools.getdirByIdApp(productInfo.idapp) + dirmain +
server_constants.DIR_UPLOAD + '/products/' + productInfo.imagefile.split('/').pop();
const filecompleto = path.resolve(__dirname, img); // Sostituisci con il percorso dove salvare l'immagine
// Se non esiste lo scarico !
fileesistente = fs.existsSync(filecompleto);
}
if (!vecchiomodo && (!productInfo.image_link || !fileesistente)) {
let scarica_da_sito = !productInfo.imagefile;
if (!scarica_da_sito && productInfo.imagefile) {
scarica_da_sito = !fileesistente; // Se non esiste lo scarico !
}
if (scarica_da_sito) {
// date and time
productInfo.imagefile = 'img_' + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
const img = tools.getdirByIdApp(productInfo.idapp) + dirmain +
server_constants.DIR_UPLOAD + '/products/' + productInfo.imagefile.split('/').pop();
let savePath = path.resolve(__dirname, img); // Sostituisci con il percorso dove salvare l'immagine
let link = 'https://www.gruppomacro.com/copertine.php?id_gm=' + productInfo.sku
const downloader = new ImageDownloader();
const aggiornatoimg = await downloader.downloadImage(link, savePath,
{
maxRetries: 3,
initialDelay: 300,
timeout: 15000,
nomefileoriginale: true,
}).then(result => {
if (result) {
// console.log('Download completato con successo!');
} else {
console.log('Download non riuscito.');
}
return result;
});
if (aggiornatoimg.filepath) {
const filenamebase = path.basename(aggiornatoimg.filepath);
// const img = '/upload/products/' + filenamebase;
productInfo.imagefile = filenamebase;
}
return result;
return { prodInfo: productInfo, aggiornatoimg: aggiornatoimg.ris };
} else {
return { prodInfo: null, aggiornatoimg: false };
}
}
});
return { prodInfo: productInfo, aggiornatoimg };
} catch (e) {
console.error('downloadImgIfMissing', e.message);
}
return { prodInfo: null, aggiornatoimg: false };
}
async function completaSettaggioProduct_AndProductInfo(arrcampi_productInfo, arrcampi_product, rec, product, productInfo) {
@@ -1042,8 +1120,7 @@ router.post('/import', authenticate, async (req, res) => {
let imported = 0;
let errors = 0;
const ripopola = true;
const ripopola = false; //++MODIFICARE!
if (ripopola) {
dataObjects = null;
@@ -1131,6 +1208,8 @@ router.post('/import', authenticate, async (req, res) => {
let importa = true;
if (!product.title || !product.sku)
importa = false;
@@ -1145,12 +1224,15 @@ router.post('/import', authenticate, async (req, res) => {
if (!product.isbn) {
const rectrovare = await ImportaIsbn.findOne({ sku: product.sku }).lean();
if (rectrovare) {
product.isbn = rectrovare.isbn;
// se l'isbn non inizia con 'field' allora è buono
if (rectrovare.isbn && !rectrovare.isbn.startsWith('field')) {
product.isbn = rectrovare.isbn;
}
} else {
nontrovati++;
console.log(`${nontrovati} - ISBN non trovato [sku=${product.sku} title=${product.title}]`)
// console.log(`${nontrovati} - ISBN non trovato [sku=${product.sku} title=${product.title}]`)
}
}
let productInfo = {
@@ -1169,12 +1251,21 @@ router.post('/import', authenticate, async (req, res) => {
idCatProds: [],
idSubCatProds: [],
//img: 'upload/products/' + product.code + '.jpg',
image_link: product.image_link ? product.image_link : '',
// image_link: product.image_link ? product.image_link : '',
img2: product.img2 ? product.img2 : '',
img3: product.img3 ? product.img3 : '',
img4: product.img4 ? product.img4 : '',
checkout_link: product.checkout_link ? product.checkout_link : '',
}
const test = false;
if (test) {
productInfo.imagefile = ''
productInfo.image_link = '';
let { prodInfo, aggiornatoimg } = await downloadImgIfMissing(productInfo);
console.log('imagefile=', prodInfo.imagefile)
}
let esisteindb = await ProductInfo.findOne({ code: productInfo.code }).lean();
if (esisteindb) {
@@ -1250,6 +1341,7 @@ router.post('/import', authenticate, async (req, res) => {
} else {
arrcat = product.categories.trim().split(',');
productInfo.idCatProds = [];
for (const mycat of arrcat) {
let mycatstr = mycat.trim();
@@ -1336,6 +1428,7 @@ router.post('/import', authenticate, async (req, res) => {
if (product.subcat_name) {
arrsubcat = product.subcat_name.trim().split(',');
productInfo.idSubCatProds = [];
for (const mysubcat of arrsubcat) {
let mysubcatstr = mysubcat.trim();
@@ -1662,6 +1755,7 @@ router.post('/import', authenticate, async (req, res) => {
if (product.subcat_name) {
arrsubcat = product.subcat_name.trim().split(',');
productInfo.idSubCatProds = [];
for (const mysubcat of arrsubcat) {
let mysubcatstr = mysubcat.trim();