- scheda prodotto migliorata
- aggiornamento filtri
This commit is contained in:
@@ -18,37 +18,38 @@ class Macro {
|
||||
this.localoptions = options;
|
||||
this.recProductExist = false;
|
||||
this.queryprod = null;
|
||||
}
|
||||
}
|
||||
|
||||
async updateLocalDbFromGM_T_Web_Articoli(params) {
|
||||
|
||||
let mylog = ''
|
||||
let numrec = 0;
|
||||
const options = {
|
||||
idapp: params.idapp,
|
||||
nameTable: 'T_Web_Articoli',
|
||||
campispeciali: true,
|
||||
recordraw: true,
|
||||
query: '',
|
||||
showQtaDisponibile: true,
|
||||
outhtml: false,
|
||||
cmd: shared_consts.CmdQueryMs.GET,
|
||||
inputdaGM: true,
|
||||
...params,
|
||||
}
|
||||
|
||||
let opt = {
|
||||
updated: 0,
|
||||
imported: 0,
|
||||
errors: 0,
|
||||
inputdaGM: options.inputdaGM,
|
||||
idapp: options.idapp,
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
|
||||
const options = {
|
||||
idapp: params.idapp,
|
||||
nameTable: 'T_Web_Articoli',
|
||||
campispeciali: true,
|
||||
recordraw: true,
|
||||
query: '',
|
||||
showQtaDisponibile: true,
|
||||
outhtml: false,
|
||||
cmd: shared_consts.CmdQueryMs.GET,
|
||||
inputdaGM: true,
|
||||
...params,
|
||||
}
|
||||
|
||||
let opt = {
|
||||
updated: 0,
|
||||
imported: 0,
|
||||
errors: 0,
|
||||
inputdaGM: options.inputdaGM,
|
||||
idapp: options.idapp,
|
||||
}
|
||||
|
||||
let miomatch = {};
|
||||
let miomatch2 = {};
|
||||
let miolimit = 0;
|
||||
|
||||
if (options.caricatutti) {
|
||||
@@ -56,7 +57,37 @@ class Macro {
|
||||
|
||||
if (options.usaDBGMLocale) {
|
||||
mylog += '*** usaDBGMLocale ***\n';
|
||||
miomatch = { IdStatoProdotto: { $in: [1, 4, 34, 45, 46] } };
|
||||
//miomatch2 = { IdStatoProdotto: { $in: [1, 3, 4, 6, 7, 8, 9, 20, 26, 33, 34, 45, 46, 47, 48] } };
|
||||
|
||||
miomatch2 = {
|
||||
$or: [
|
||||
{ DescrizioneStatoProdotto: 'In commercio' },
|
||||
{ DescrizioneStatoProdotto: '2023 in commercio' },
|
||||
{ DescrizioneStatoProdotto: 'Vendita sito' },
|
||||
{ DescrizioneStatoProdotto: 'In prevendita' },
|
||||
{ DescrizioneStatoProdotto: 'Prossima uscita' }
|
||||
]
|
||||
|
||||
};
|
||||
|
||||
/*
|
||||
1 In commercio
|
||||
3 Ristampa
|
||||
4 Prossima uscita/pubblicazione
|
||||
6 In promozione
|
||||
7 In fase di valutazione
|
||||
8 Titolo in esaurimento (in attesa Nuova Edizione)
|
||||
9 Titolo in esaurimento
|
||||
20 Titolo in esaurimento (in att N.E Ricopertinata)
|
||||
26 Titolo in Esaurimento (disponibile N.E.)
|
||||
33 In commercio (digitale)
|
||||
34 In prevendita
|
||||
45 Vendita sito
|
||||
46 2023 in commercio
|
||||
47 Assoluto NO Reso
|
||||
48 Titolo esaurito
|
||||
|
||||
*/
|
||||
|
||||
// options.where = { IdStatoProdotto: { $in: [1, 4, 34, 45, 46] } };
|
||||
} else {
|
||||
@@ -66,7 +97,8 @@ class Macro {
|
||||
DescrizioneStatoProdotto = 'Vendita sito' OR
|
||||
DescrizioneStatoProdotto = 'In prevendita' OR
|
||||
DescrizioneStatoProdotto = 'Prossima uscita') AND
|
||||
(DescrizioneTipologia = 'Libri')
|
||||
(DescrizioneTipologia = 'Libri' OR
|
||||
DescrizioneTipologia = 'Cartonato')
|
||||
`;
|
||||
}
|
||||
|
||||
@@ -84,7 +116,7 @@ class Macro {
|
||||
if (true) {
|
||||
filtroTipologia = {
|
||||
$match: {
|
||||
DescrizioneTipologia: 'Libri',
|
||||
DescrizioneTipologia: { $in: ['Libri', 'Cartolibro'] },
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -93,7 +125,9 @@ class Macro {
|
||||
mylog += '*** usaDBGMLocale ***\n';
|
||||
options.aggregation = [
|
||||
{
|
||||
$match: miomatch
|
||||
$match: {
|
||||
...miomatch,
|
||||
}
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
@@ -130,6 +164,11 @@ class Macro {
|
||||
DescrizioneStatoProdotto: { $arrayElemAt: ['$StatoProdotto.Descrizione', 0] },
|
||||
}
|
||||
},
|
||||
{
|
||||
$match: {
|
||||
...miomatch2
|
||||
}
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 't_web_tipologies',
|
||||
@@ -154,7 +193,7 @@ class Macro {
|
||||
{
|
||||
$match: {
|
||||
$expr: {
|
||||
$eq: ["$DescrizioneTipologia", "Libri"]
|
||||
$in: ["$DescrizioneTipologia", ["Libri", "Cartolibro"]]
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -356,6 +395,8 @@ class Macro {
|
||||
|
||||
const recproducts = await getTableContent(options);
|
||||
|
||||
let idRecUpdated = null;
|
||||
|
||||
if (!tools.isArray(recproducts)) {
|
||||
console.error('Error: ', recproducts);
|
||||
mylog += recproducts + '\n';
|
||||
@@ -370,8 +411,7 @@ class Macro {
|
||||
if (Array.isArray(recproducts)) {
|
||||
for (const recproduct of recproducts) {
|
||||
// if (!options.caricatutti) {
|
||||
await this.
|
||||
elaboraProdotto(recproduct, opt);
|
||||
await this.elaboraProdotto(recproduct, opt);
|
||||
count++;
|
||||
|
||||
if (count % 50 === 0) {
|
||||
@@ -382,15 +422,22 @@ class Macro {
|
||||
}
|
||||
}
|
||||
|
||||
mylog += ' *** IMPORTATI: ' + opt.imported + ' AGGIORNATI = ' + opt.updated + ' (su ' + numrec + ' RECORD)';
|
||||
if (numrec > 1) {
|
||||
opt.idRecUpdated = null;
|
||||
}
|
||||
|
||||
if (opt) {
|
||||
mylog += ' *** IMPORTATI: ' + opt.imported + ' AGGIORNATI = ' + opt.updated + ' (su ' + numrec + ' RECORD)';
|
||||
}
|
||||
|
||||
console.log(mylog);
|
||||
return { updated: opt.updated, imported: opt.imported, errors: opt.errors, mylog };
|
||||
return { updated: opt.updated, imported: opt.imported, errors: opt.errors, mylog, idRecUpdated: opt.idRecUpdated, table: opt.table };
|
||||
|
||||
} catch (e) {
|
||||
mylog += 'ERRORE ! *** IMPORTATI: ' + opt.imported + ' AGGIORNATI = ' + opt.updated + ' (su ' + numrec + ' RECORD)';
|
||||
mylog += 'ERRORE ! *** IMPORTATI: ' + opt?.imported + ' AGGIORNATI = ' + opt?.updated + ' (su ' + numrec + ' RECORD)';
|
||||
opt.logerror = e.message;
|
||||
console.error(e.message);
|
||||
return { updated: opt.updated, imported: opt.imported, errors: opt.errors, mylog };
|
||||
return { updated: opt.updated, imported: opt.imported, errors: opt.errors, mylog, logerror: opt.logerror };
|
||||
}
|
||||
|
||||
}
|
||||
@@ -677,6 +724,8 @@ class Macro {
|
||||
sale_price: productGM.PrezzoIvatoScontatoCampagna,
|
||||
formato: productGM.DescrizioneFormato,
|
||||
Tipologia: productGM.DescrizioneTipologia,
|
||||
idTipologia: productGM.IdTipologia,
|
||||
idTipoFormato: productGM.IdTipoFormato,
|
||||
Edizione: productGM.Edizione,
|
||||
Pagine: productGM.Pagine,
|
||||
misure: productGM.Misure,
|
||||
@@ -690,9 +739,9 @@ class Macro {
|
||||
|
||||
let vers = 0;
|
||||
|
||||
if (productGM.DescrizioneTipologia === 'Usato')
|
||||
vers = shared_consts.PRODUCTTYPE.USATO;
|
||||
else if (productGM.DescrizioneTipologia === 'Download')
|
||||
//if (productGM.DescrizioneTipologia === 'Usato')
|
||||
// vers = shared_consts.PRODUCTTYPE.USATO;
|
||||
if (productGM.DescrizioneTipologia === 'Download')
|
||||
vers = shared_consts.PRODUCTTYPE.DOWNLOAD;
|
||||
else if (productGM.DescrizioneTipologia === 'DVD')
|
||||
vers = shared_consts.PRODUCTTYPE.DVD;
|
||||
@@ -869,8 +918,10 @@ class Macro {
|
||||
status: product.Stato || null,
|
||||
price: product.price ? parseFloat(tools.convertPriceEurToValue(product.price)) : null,
|
||||
sale_price: product.sale_price ? parseFloat(tools.convertPriceEurToValue(product.sale_price)) : null,
|
||||
formato: product.formato || '',
|
||||
tipologia: product.Tipologia || '',
|
||||
// formato: product.formato || '',
|
||||
idTipologia: product.idTipologia || '',
|
||||
idTipoFormato: product.idTipoFormato || '',
|
||||
// tipologia: product.Tipologia || '',
|
||||
edizione: product.Edizione || '',
|
||||
pagine: tools.isValidNumber(product.Pagine) ? tools.convstrToInt(product.Pagine) : 0,
|
||||
misure: product.misure || '',
|
||||
@@ -899,11 +950,11 @@ class Macro {
|
||||
async gestisciVariazioni(product, risrecInfo, options) {
|
||||
const recold = await Product.findOne(this.queryprod).lean();
|
||||
const variazione = this.preparaVariazione(product);
|
||||
|
||||
|
||||
const myproduct = {
|
||||
...product,
|
||||
...(!product.isbn ? [{ isbn: risrecInfo.code }]: []),
|
||||
...(!product.maxbookableGASQty && risrecInfo.maxbookableGASQty ? [{ maxbookableGASQty: risrecInfo.maxbookableGASQty }]: []),
|
||||
...(!product.isbn ? [{ isbn: risrecInfo.code }] : []),
|
||||
...(!product.maxbookableGASQty && risrecInfo.maxbookableGASQty ? [{ maxbookableGASQty: risrecInfo.maxbookableGASQty }] : []),
|
||||
idapp: this.idapp,
|
||||
arrvariazioni: [variazione],
|
||||
};
|
||||
@@ -912,25 +963,68 @@ class Macro {
|
||||
|
||||
if (recold) {
|
||||
const arrvariazioni = this.aggiornaVariazioni(recold.arrvariazioni, variazione);
|
||||
risultupdate = await Product.updateOne(this.queryprod, { $set: { arrvariazioni } }, { new: true });
|
||||
if (risultupdate && risultupdate.modifiedCount > 0) {
|
||||
options.updated++;
|
||||
const updatedDoc = await Product.findOneAndUpdate(this.queryprod, { $set: { arrvariazioni } }, {
|
||||
upsert: true,
|
||||
new: true, // restituisce il documento aggiornato
|
||||
includeResultMetadata: true
|
||||
});
|
||||
if (updatedDoc && updatedDoc.lastErrorObject) {
|
||||
const wasUpserted = updatedDoc.lastErrorObject.upserted !== undefined;
|
||||
const wasUpdated = updatedDoc.lastErrorObject.n === 1 && !wasUpserted;
|
||||
|
||||
if (wasUpserted || wasUpdated) {
|
||||
options.updated++;
|
||||
options.table = 'products';
|
||||
options.idRecUpdated = wasUpserted
|
||||
? updatedDoc.lastErrorObject.upserted
|
||||
: updatedDoc.value._id;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (recold.isbn !== risrecInfo.code) {
|
||||
product.isbn = risrecInfo.code;
|
||||
}
|
||||
}
|
||||
if (!recold || recold.isbn !== myproduct.isbn || !recold.idapp || !this.recProductExist) {
|
||||
risultupdate = await Product.updateOne(this.queryprod, { $set: myproduct }, { new: true, upsert: true });
|
||||
if (risultupdate && risultupdate.modifiedCount > 0) {
|
||||
options.imported++;
|
||||
if (!recold || this.isModified(recold, myproduct) || !this.recProductExist) {
|
||||
const updatedDoc = await Product.findOneAndUpdate(this.queryprod, { $set: myproduct },
|
||||
{
|
||||
new: true, upsert: true,
|
||||
includeResultMetadata: true
|
||||
}
|
||||
);
|
||||
if (updatedDoc && updatedDoc.lastErrorObject) {
|
||||
const wasUpserted = updatedDoc.lastErrorObject.upserted !== undefined;
|
||||
const wasUpdated = updatedDoc.lastErrorObject.n === 1 && !wasUpserted;
|
||||
|
||||
if (wasUpserted || wasUpdated) {
|
||||
options.imported++;
|
||||
options.table = 'products';
|
||||
options.idRecUpdated = wasUpserted
|
||||
? updatedDoc.lastErrorObject.upserted
|
||||
: updatedDoc.value._id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// console.log('risultupdate', risultupdate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifica se i campi nella lista sono stati modificati da una versione all'altra.
|
||||
* @param {Object} recordOld - record vecchio
|
||||
* @param {Object} recordNew - record nuovo
|
||||
* @param {string[]} listaCampi - lista dei campi da verificare
|
||||
* @returns {boolean} true se i campi sono stati modificati
|
||||
*/
|
||||
isModified(recordOld, recordNew) {
|
||||
const listaCampi = [
|
||||
'idapp',
|
||||
'isbn',
|
||||
//++FIELD_PRODUCT
|
||||
]
|
||||
return listaCampi.some((campo) => recordOld[campo] !== recordNew[campo]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = Macro;
|
||||
Reference in New Issue
Block a user