- MultiDominio: api.riso.app, api.gruppomacro.app
This commit is contained in:
@@ -7,6 +7,8 @@ const shared_consts = require('../tools/shared_nodejs');
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
const fs = require('fs');
|
||||
|
||||
const { City } = require('../models/city');
|
||||
const Product = require('../models/product');
|
||||
const Inventariogm = require('../models/inventariogm');
|
||||
@@ -18,6 +20,10 @@ const Publisher = require('../models/publisher');
|
||||
const SubCatProd = require('../models/subcatprod');
|
||||
const Gasordine = require('../models/gasordine');
|
||||
|
||||
const { ImageDownloader } = require('../tools/general.js');
|
||||
|
||||
const path = require('path');
|
||||
|
||||
var { authenticate } = require('../middleware/authenticate');
|
||||
|
||||
router.post('/updateval', authenticate, async (req, res) => {
|
||||
@@ -45,6 +51,35 @@ function fixURL(url) {
|
||||
return url.replace(/https:\//g, 'https://');
|
||||
}
|
||||
|
||||
async function downloadImgIfMissing(productInfo) {
|
||||
|
||||
if (!productInfo.image_link)
|
||||
return { prodInfo: null, aggiornatoimg: false };
|
||||
|
||||
const img = 'upload/products/' + productInfo.image_link.split('/').pop();
|
||||
const savePath = path.resolve(__dirname, img); // Sostituisci con il percorso dove salvare l'immagine
|
||||
|
||||
if (!productInfo.img || !fs.existsSync(savePath)) {
|
||||
// Download image from the URL productInfo.image_link
|
||||
productInfo.img = img;
|
||||
|
||||
const downloader = new ImageDownloader();
|
||||
|
||||
const aggiornatoimg = await downloader.downloadImage(productInfo.image_link, savePath).then(result => {
|
||||
if (result) {
|
||||
// console.log('Download completato con successo!');
|
||||
} else {
|
||||
console.log('Download non riuscito.');
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
});
|
||||
return { prodInfo: productInfo, aggiornatoimg };
|
||||
}
|
||||
|
||||
return { prodInfo: null, aggiornatoimg: false };
|
||||
}
|
||||
|
||||
async function completaSettaggioProduct_AndProductInfo(arrcampi_productInfo, arrcampi_product, rec, product, productInfo) {
|
||||
|
||||
@@ -329,6 +364,8 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
let indprod = 0;
|
||||
let newprod = 0;
|
||||
|
||||
let numprod = dataObjects.length;
|
||||
|
||||
for (const product of dataObjects) {
|
||||
let isnuovo = false
|
||||
let setta = false
|
||||
@@ -367,6 +404,8 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
|
||||
let versione = 0;
|
||||
|
||||
console.log(indprod + '/' + numprod);
|
||||
|
||||
|
||||
productInfo.productTypes = [];
|
||||
// console.log('indprod', indprod, 'arrversGM', arrversGM, 'versione', product.Versione);
|
||||
@@ -553,6 +592,12 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
product.idProductInfo = risrecInfo._id;
|
||||
recnewInfo = await ProductInfo.findOne({ code: productInfo.code }).lean();
|
||||
|
||||
let { prodInfo, aggiornatoimg } = await downloadImgIfMissing(recnewInfo);
|
||||
|
||||
if (aggiornatoimg) {
|
||||
await ProductInfo.findOneAndUpdate({ code: productInfo.code }, { $set: prodInfo });
|
||||
}
|
||||
|
||||
if (risrecInfo._id) {
|
||||
// Record existed, so it was updated
|
||||
let arrfieldchange = tools.differentObjects(productInfo, recnewInfo);
|
||||
|
||||
Reference in New Issue
Block a user