- Modifiche a ProductInfo... Continua

This commit is contained in:
Surya Paolo
2025-08-29 23:34:08 +02:00
parent 2ee710b748
commit fcbc64cea8
24 changed files with 1006 additions and 1010 deletions

View File

@@ -17,7 +17,6 @@ const Inventariogm = require('../models/inventariogm');
const Importamacro = require('../models/importamacro');
const ImportaDescr = require('../models/importadescr');
const ImportaIsbn = require('../models/importaisbn');
const ProductInfo = require('../models/productInfo');
const CatProd = require('../models/catprod');
const Collana = require('../models/collana');
const Author = require('../models/author');
@@ -72,7 +71,7 @@ function launchGenPdfIfNeeded() {
}
}
async function updateProductInfo(recproductInfoAttuale, product, idapp, mycatstr) {
/*async function updateProductInfo(recproductInfoAttuale, product, idapp, mycatstr) {
if (!recproductInfoAttuale || !mycatstr) return recproductInfoAttuale;
let idArgomentoNum = null;
@@ -93,7 +92,7 @@ async function updateProductInfo(recproductInfoAttuale, product, idapp, mycatstr
}
return productInfo;
}
}*/
async function findOrCreateCatProd(idapp, idArgomento, DescrArgomento) {
let reccatprod = null;
@@ -1240,28 +1239,28 @@ router.post('/import', authenticate, async (req, res) => {
code: product.code,
};
const myproductInfo = await ProductInfo.findOne({ code: productInfo.code });
const myproduct = await Product.findOne({ 'productInfo.code': productInfo.code });
// IMPOSTA I VALORI SOLO SE NON ESISTONO ! ALTRIMENTI LASCIA QUELLI GIA' IMPORTATI (O MODIFICATI)
if (getvalueByJsonText(product.url)) {
if ((myproductInfo && !myproductInfo.link_macro) || !myproductInfo)
if ((myproduct && !myproduct.productInfo.link_macro) || !myproduct.productInfo)
productInfo.link_macro = getvalueByJsonText(product.url);
}
if (getvalueByJsonText(product.descrizione)) {
if ((myproductInfo && !myproductInfo.descrizione_breve_macro) || !myproductInfo)
if ((myproduct && !myproduct.productInfo.descrizione_breve_macro) || !myproduct.productInfo)
productInfo.descrizione_breve_macro = getvalueByJsonText(product.descrizione);
}
if (getvalueByJsonText(product.descrizione_completa)) {
if ((myproductInfo && !myproductInfo.descrizione_completa_macro) || !myproductInfo)
if ((myproduct && !myproduct.productInfo.descrizione_completa_macro) || !myproduct.productInfo)
productInfo.descrizione_completa_macro = getvalueByJsonText(product.descrizione_completa);
}
if (getvalueByJsonText(product.sottotitolo)) {
if ((myproductInfo && !myproductInfo.sottotitolo) || !myproductInfo)
if ((myproduct && !myproduct.productInfo.sottotitolo) || !myproduct.productInfo)
productInfo.sottotitolo = getvalueByJsonText(product.sottotitolo);
}
if (getvalueByJsonText(product.titolo)) {
if ((myproductInfo && !myproductInfo.name) || !myproductInfo)
if ((myproduct && !myproduct.productInfo.name) || !myproduct.productInfo)
productInfo.name = getvalueByJsonText(product.titolo);
}
@@ -1520,27 +1519,27 @@ router.post('/import', authenticate, async (req, res) => {
let imported = 0;
let errors = 0;
for (const product of dataObjects) {
for (const productImported of dataObjects) {
let isnuovo = false;
let setta = false;
let productInfo = {
idapp: product.idapp,
code: product.code,
name: product.name,
link: product.link,
idapp: productImported.idapp,
code: productImported.code,
name: productImported.name,
link: productImported.link,
idCatProds: [],
idSubCatProds: [],
// img: 'upload/products/' + product.code + '.jpg',
imagefile: product.code + '.jpg',
weight: product.weight,
unit: tools.getIdUnitsByText(product.unit),
imagefile: productImported.code + '.jpg',
weight: productImported.weight,
unit: tools.getIdUnitsByText(productImported.unit),
productTypes: shared_consts.PRODUCTTYPE.PRODUCT,
};
let reccateg = null;
if (product.cat_name) {
arrcat = product.cat_name.trim().split(',');
if (productImported.cat_name) {
arrcat = productImported.cat_name.trim().split(',');
for (const mycat of arrcat) {
let mycatstr = mycat.trim();
@@ -1559,8 +1558,8 @@ router.post('/import', authenticate, async (req, res) => {
}
}
if (product.subcat_name) {
arrsubcat = product.subcat_name.trim().split(',');
if (productImported.subcat_name) {
arrsubcat = productImported.subcat_name.trim().split(',');
productInfo.idSubCatProds = [];
for (const mysubcat of arrsubcat) {
let mysubcatstr = mysubcat.trim();
@@ -1581,24 +1580,26 @@ router.post('/import', authenticate, async (req, res) => {
}
}
if (!product.hasOwnProperty('active')) {
product.active = true;
if (!productImported.hasOwnProperty('active')) {
productImported.active = true;
}
if (product.code) delete product.code;
/*if (product.code) delete product.code;
if (product.name) delete product.name;
if (product.link) delete product.link;
if (product.link) delete product.link;*/
let esisteindb = await ProductInfo.findOne({ code: productInfo.code }).lean();
let esisteindb = await Product.findOne({ 'productInfo.code': productInfo.code }).lean();
// Update ProductInfo
let risrecInfo = await ProductInfo.findOneAndUpdate(
{ code: productInfo.code },
{ $set: productInfo },
let risrecInfo = await Product.findOneAndUpdate(
{ 'productInfo.code': productInfo.code },
{ $set: { productInfo: productInfo } },
{ new: true, upsert: true }
);
// .... ANDARE AVANTI DA QUI... .productInfo.... SISTEMARE!
if (risrecInfo) {
product.idProductInfo = risrecInfo._id;
productImported.productInfo = risrecInfo._id;
recnewInfo = await ProductInfo.findOne({ code: productInfo.code }).lean();
@@ -1613,20 +1614,20 @@ router.post('/import', authenticate, async (req, res) => {
// Cerca il GAS
let recGas = null;
if (product.gas_name) {
if (productImported.gas_name) {
// Cerca il GAS
recGas = await Gasordine.findOne({ idapp, name: product.gas_name }).lean();
recGas = await Gasordine.findOne({ idapp, name: productImported.gas_name }).lean();
}
if (!recGas && !!product.gas_name) {
recGas = new Gasordine({ idapp, name: product.gas_name, active: true });
if (!recGas && !!productImported.gas_name) {
recGas = new Gasordine({ idapp, name: productImported.gas_name, active: true });
// Non esiste questo GAS, quindi lo creo !
ris = await recGas.save();
recGas = await Gasordine.findOne({ idapp, name: product.gas_name }).lean();
recGas = await Gasordine.findOne({ idapp, name: productImported.gas_name }).lean();
}
let recProductExist = null;
let queryprod = { idProductInfo: product.idProductInfo };
let queryprod = { idProductInfo: productImported.idProductInfo };
if (recGas) {
queryprod = { ...queryprod, idGasordine: recGas._id };
@@ -1639,22 +1640,22 @@ router.post('/import', authenticate, async (req, res) => {
}
if (!options.aggiornaStockQty && esisteindb && !isnuovo) {
delete product.stockQty;
delete product.maxbookableGASQty;
delete productImported.stockQty;
delete productImported.maxbookableGASQty;
}
// AGGIORNA PRODUCT
let risrec = await Product.findOneAndUpdate(queryprod, { $set: product }, { new: true, upsert: true });
let risrec = await Product.findOneAndUpdate(queryprod, { $set: productImported }, { new: true, upsert: true });
let recnew = await Product.findOne(queryprod).lean();
if (risrec) {
if (risrec._id) {
// Record existed, so it was updated
let arrfieldchange = tools.differentObjects(product, recnew);
let arrfieldchange = tools.differentObjects(productImported, recnew);
if (arrfieldchange.length > 0) {
updated++;
console.log('Changed:', product.idProductInfo + ': ' + arrfieldchange);
console.log('Changed:', productImported.idProductInfo + ': ' + arrfieldchange);
}
} else {
// Record didn't exist, so it was created
@@ -1662,13 +1663,13 @@ router.post('/import', authenticate, async (req, res) => {
}
} else {
// risrec is null or undefined, indicating an error
console.error('Error: ', product.productInfo.name);
console.error('Error: ', productImported.productInfo.name);
errors++;
}
await Product.singlerecconvert_AfterImport_AndSave(idapp, recnew, isnuovo);
} else {
console.error('Error ProductInfo: ', product.code);
console.error('Error ProductInfo: ', productImported.code);
errors++;
}
}
@@ -1871,6 +1872,7 @@ router.post('/import', authenticate, async (req, res) => {
return res.status(400).send();
});
/*
async function importaCatalogo(data) {
let updated = 0;
let imported = 0;
@@ -2059,11 +2061,6 @@ async function importaCatalogo(data) {
productInfo.productTypes.push(vers);
}
/*if (product.Data) {
productInfo.date_pub = new Date(product.Data * 1000);
// convert data to timestamp
productInfo.date_pub_ts = productInfo.date_pub.getTime();
}*/
productInfo.name = productInfo.name.replace(
/ - Usato$| - Nuovo$| - Epub$| - Ebook$| - Mobi$| - DVD$| - Streaming$| - Download$/,
@@ -2113,7 +2110,7 @@ async function importaCatalogo(data) {
}
if (reccateg) {
productInfo.idCatProds.push(reccateg._id);
product.productInfo.idCatProds.push(reccateg._id);
}
}
}
@@ -2149,7 +2146,7 @@ async function importaCatalogo(data) {
} catch (e) {}
}
}
productInfo.idAuthors = [];
product.productInfo.idAuthors = [];
for (const myauthor of arrAuthor) {
// Cerca l'Autore
@@ -2162,7 +2159,7 @@ async function importaCatalogo(data) {
}
if (recauthor) {
productInfo.idAuthors.push(recauthor._id);
product.productInfo.idAuthors.push(recauthor._id);
}
}
} catch (e) {
@@ -2173,7 +2170,7 @@ async function importaCatalogo(data) {
if (product.subcat_name) {
arrsubcat = product.subcat_name.trim().split(',');
productInfo.idSubCatProds = [];
product.productInfo.idSubCatProds = [];
for (const mysubcat of arrsubcat) {
let mysubcatstr = mysubcat.trim();
@@ -2188,13 +2185,13 @@ async function importaCatalogo(data) {
}
if (recsubcateg) {
productInfo.idSubCatProds.push(recsubcateg._id);
product.productInfo.idSubCatProds.push(recsubcateg._id);
}
}
}
if (productInfo.publisher) {
if (product.productInfo.publisher) {
try {
publisher = productInfo.publisher.trim();
publisher = product.productInfo.publisher.trim();
// Cerca la Sotto Categoria
let recpublisher = await Publisher.findOne({ idapp, name: publisher }).lean();
if (!recpublisher) {
@@ -2205,7 +2202,7 @@ async function importaCatalogo(data) {
}
if (recpublisher) {
productInfo.idPublisher = recpublisher._id;
product.productInfo.idPublisher = recpublisher._id;
}
} catch (e) {
console.error(e);
@@ -2213,19 +2210,19 @@ async function importaCatalogo(data) {
}
// Aggiorna la collana solo se non è stata già impostata nel record attuale
//if (recproductInfoAttuale && !recproductInfoAttuale.idCollana && product.DescrizioneCollana) {
if (productInfo.collezione && productInfo.numCollana) {
if (product.productInfo.collezione && product.productInfo.numCollana) {
reccollana = await Collana.findOne({ idapp, title: collezione }).lean();
if (!reccollana) {
try {
// Non esiste questa collana, quindi la creo !
reccollana = new Collana({ idapp, idCollana: productInfo.numCollana, title: product.DescrizioneCollana });
reccollana = new Collana({ idapp, idCollana: product.productInfo.numCollana, title: product.DescrizioneCollana });
ris = await reccoll.save();
} catch (e) {
console.error('Err', e);
}
}
if (reccollana) productInfo.idCollana = reccollana._id;
if (reccollana) product.productInfo.idCollana = reccollana._id;
}
if (!product.hasOwnProperty('active')) {
@@ -2497,7 +2494,7 @@ async function importaCatalogo(data) {
console.log(e.message);
return res.status(400).send(e);
}
}
}*/
router.post('/exec', authenticate, async (req, res) => {
try {