- Cataloghi: BestSeller, Novità
This commit is contained in:
@@ -307,6 +307,17 @@ function extractNameAndSurnameByComplete(name_complete) {
|
||||
}
|
||||
}
|
||||
|
||||
function getvalueByJsonText(valore) {
|
||||
if (valore && valore['#text']) {
|
||||
if (valore['#text'])
|
||||
return valore['#text'];
|
||||
else
|
||||
return value;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
router.post('/import', authenticate, async (req, res) => {
|
||||
|
||||
try {
|
||||
@@ -339,6 +350,209 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
let risrec = await Inventariogm.findOneAndUpdate(queryprod, { $set: inventario }, { new: true, upsert: true });
|
||||
}
|
||||
|
||||
} else if (cmd === shared_consts.Cmd.MACRO_DESCRELINKSITOWEB) {
|
||||
let updated = 0;
|
||||
let imported = 0;
|
||||
let errors = 0;
|
||||
|
||||
dataObjects = null;
|
||||
|
||||
let myarr = null;
|
||||
|
||||
try {
|
||||
dataObjects = JSON.parse(`[${data.arrdata}]`);
|
||||
|
||||
myarr = dataObjects[0].item_list.item;
|
||||
} catch (e) {
|
||||
dataObjects = null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (myarr) {
|
||||
|
||||
// Cancella la collection ImportaMacros
|
||||
await Importamacro.deleteMany({ idapp });
|
||||
|
||||
// Aggiungi i record su ImportaMacros
|
||||
for (const recinv of myarr) {
|
||||
let isnuovo = false
|
||||
let setta = false
|
||||
|
||||
let recmacro = recinv;
|
||||
|
||||
recmacro.idapp = idapp;
|
||||
|
||||
if (recmacro.ean && recmacro.ean['#text']) {
|
||||
recmacro._id = recmacro.ean['#text'];
|
||||
recmacro.code = recmacro._id;
|
||||
|
||||
let queryprod = { idapp, code: recmacro._id };
|
||||
|
||||
|
||||
try {
|
||||
let risrec = await Importamacro.findOneAndUpdate(queryprod, { $set: recmacro }, { new: true, upsert: true, strict: false });
|
||||
if (risrec) {
|
||||
imported++;
|
||||
// if (imported > 1000)
|
||||
// break;
|
||||
console.log('importato ', imported, 'su ', myarr.length);
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
errors++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rileggi tutti i record di ImportaMacros
|
||||
dataObjects = await Importamacro.find({ idapp }).lean();
|
||||
|
||||
console.log('*** INIZIO IMPORT PRODOTTI ... ');
|
||||
|
||||
let indprod = 0;
|
||||
let newprod = 0;
|
||||
|
||||
let numprod = dataObjects.length;
|
||||
|
||||
|
||||
for (const product of dataObjects) {
|
||||
let isnuovo = false
|
||||
let setta = false
|
||||
|
||||
let importa = true;
|
||||
|
||||
if (!product.code)
|
||||
importa = false;
|
||||
|
||||
if (importa) {
|
||||
let productInfo = {
|
||||
idapp: product.idapp,
|
||||
code: product.code,
|
||||
}
|
||||
|
||||
if (getvalueByJsonText(product.url)) {
|
||||
productInfo.link_macro = getvalueByJsonText(product.url);
|
||||
}
|
||||
if (getvalueByJsonText(product.descrizione)) {
|
||||
productInfo.descrizione_breve_macro = getvalueByJsonText(product.descrizione);
|
||||
}
|
||||
if (getvalueByJsonText(product.descrizione_completa)) {
|
||||
productInfo.descrizione_completa_macro = getvalueByJsonText(product.descrizione_completa)
|
||||
}
|
||||
if (getvalueByJsonText(product.sottotitolo)) {
|
||||
productInfo.sottotitolo = getvalueByJsonText(product.sottotitolo);
|
||||
}
|
||||
|
||||
|
||||
// Update ProductInfo
|
||||
let risrecInfo = await ProductInfo.findOneAndUpdate({ code: productInfo.code }, { $set: productInfo }, { new: true, upsert: true });
|
||||
|
||||
indprod++;
|
||||
console.log(indprod + '/' + numprod);
|
||||
}
|
||||
}
|
||||
|
||||
} else if (cmd === shared_consts.Cmd.MACRO_RANKING) {
|
||||
let updated = 0;
|
||||
let imported = 0;
|
||||
let errors = 0;
|
||||
|
||||
dataObjects = null;
|
||||
|
||||
try {
|
||||
dataObjects = JSON.parse(`[${data.arrdata}]`);
|
||||
} catch (e) {
|
||||
dataObjects = null;
|
||||
}
|
||||
|
||||
if (dataObjects && dataObjects[0]) {
|
||||
// Cancella la collection ImportaMacros
|
||||
await Importamacro.deleteMany({ idapp });
|
||||
|
||||
// Aggiungi i record su ImportaMacros
|
||||
for (const recinv of dataObjects[0]) {
|
||||
let isnuovo = false
|
||||
let setta = false
|
||||
|
||||
let recmacro = recinv;
|
||||
|
||||
recmacro.idapp = idapp;
|
||||
|
||||
recmacro._id = recmacro.id;
|
||||
|
||||
delete recmacro.id;
|
||||
|
||||
let queryprod = { idapp, _id: recmacro._id };
|
||||
|
||||
try {
|
||||
let risrec = await Importamacro.findOneAndUpdate(queryprod, { $set: recmacro }, { new: true, upsert: true, strict: false });
|
||||
if (risrec) {
|
||||
imported++;
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
errors++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rileggi tutti i record di ImportaMacros
|
||||
dataObjects = await Importamacro.find({ idapp }).lean();
|
||||
|
||||
console.log('*** INIZIO IMPORT PRODOTTI ... ');
|
||||
|
||||
let indprod = 0;
|
||||
let newprod = 0;
|
||||
|
||||
let numprod = dataObjects.length;
|
||||
|
||||
|
||||
for (const product of dataObjects) {
|
||||
let isnuovo = false
|
||||
let setta = false
|
||||
|
||||
let importa = true;
|
||||
|
||||
if (!product.title || !product.isbn)
|
||||
importa = false;
|
||||
|
||||
if (importa) {
|
||||
let versGM = product.Versione ? product.Versione : '';
|
||||
|
||||
// split versioneGM in array with separated ","
|
||||
let arrversGM = versGM.split(",").map(x => x.trim());
|
||||
|
||||
let productInfo = {
|
||||
idapp: product.idapp,
|
||||
code: product.isbn,
|
||||
id_wp: product._id,
|
||||
|
||||
// name: product.title,
|
||||
totaleVenduti: product.totaleVenduti,
|
||||
venditeLastM: product.venditeLastM,
|
||||
venditeLast6M: product.venditeLast6M,
|
||||
venditeLastY: product.venditeLastY,
|
||||
venditeLast2Y: product.venditeLast2Y,
|
||||
dataUltimoOrdine: product.dataUltimoOrdine,
|
||||
rank3M: product.rank3M,
|
||||
rank6M: product.rank6M,
|
||||
rank1Y: product.rank1Y,
|
||||
|
||||
}
|
||||
|
||||
console.log(indprod + '/' + numprod);
|
||||
|
||||
// Update ProductInfo
|
||||
let risrecInfo = await ProductInfo.findOneAndUpdate({ code: productInfo.code }, { $set: productInfo }, { new: true, upsert: true });
|
||||
|
||||
indprod++;
|
||||
}
|
||||
}
|
||||
|
||||
} else if (cmd === shared_consts.Cmd.MACRO_CATALOGO_JSON) {
|
||||
let updated = 0;
|
||||
let imported = 0;
|
||||
@@ -420,6 +634,7 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
idapp: product.idapp,
|
||||
code: product.isbn,
|
||||
id_wp: product._id,
|
||||
|
||||
name: product.title,
|
||||
description: product.description,
|
||||
short_descr: product.short_descr,
|
||||
|
||||
Reference in New Issue
Block a user