- Modifiche a ProductInfo... Continua
This commit is contained in:
@@ -39,7 +39,181 @@ const productSchema = new Schema({
|
||||
isbn: {
|
||||
type: String,
|
||||
},
|
||||
idProductInfo: { type: Schema.Types.ObjectId, ref: 'ProductInfo', index: true },
|
||||
idProductInfo: { type: Schema.Types.ObjectId, ref: 'ProductInfo', index: true }, //@@@ DA CANCELLARE DOPO DELLA CONVERSIONE
|
||||
productInfo: {
|
||||
department: {
|
||||
type: String, ref: 'Department'
|
||||
},
|
||||
sku: {
|
||||
type: String,
|
||||
},
|
||||
code: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
codice: {
|
||||
type: String,
|
||||
}, // codice interno prodotto
|
||||
id_wp: {
|
||||
type: String,
|
||||
}, // id in wordpress
|
||||
codice_EAN: {
|
||||
type: String,
|
||||
},
|
||||
barcode: {
|
||||
type: String,
|
||||
},
|
||||
name: {
|
||||
type: String,
|
||||
index: true,
|
||||
},
|
||||
description: {
|
||||
type: String,
|
||||
},
|
||||
short_descr: {
|
||||
type: String,
|
||||
},
|
||||
idCatProds: [{
|
||||
type: Schema.Types.ObjectId, ref: 'CatProd', index: true
|
||||
}],
|
||||
idSubCatProds: [{
|
||||
type: Schema.Types.ObjectId, ref: 'SubCatProd'
|
||||
}],
|
||||
idStatoProdotto: {
|
||||
type: Number, index: true
|
||||
},
|
||||
color: {
|
||||
type: String
|
||||
},
|
||||
size: {
|
||||
type: String // 11x4x3
|
||||
},
|
||||
weight: {
|
||||
type: Number
|
||||
},
|
||||
weight_lordo: {
|
||||
type: Number
|
||||
},
|
||||
unit: {
|
||||
type: Number,
|
||||
},
|
||||
unit_lordo: {
|
||||
type: Number,
|
||||
},
|
||||
sfuso: {
|
||||
type: Boolean
|
||||
}, // serve se moltiplicare le qta (es: 12 kg) oppure fare (2 x 20 ml)
|
||||
vegan: {
|
||||
type: Boolean
|
||||
},
|
||||
icon: {
|
||||
type: String,
|
||||
},
|
||||
img: {
|
||||
type: String,
|
||||
},
|
||||
imagefile: {
|
||||
type: String,
|
||||
},
|
||||
image_not_found: {
|
||||
type: Boolean,
|
||||
},
|
||||
vers_img: {
|
||||
type: Number,
|
||||
},
|
||||
image_link: {
|
||||
type: String,
|
||||
},
|
||||
link_scheda: {
|
||||
type: String,
|
||||
},
|
||||
link: {
|
||||
type: String,
|
||||
},
|
||||
checkout_link: {
|
||||
type: String,
|
||||
},
|
||||
versioneGM: {
|
||||
type: String,
|
||||
},
|
||||
img2: {
|
||||
type: String,
|
||||
},
|
||||
img3: {
|
||||
type: String,
|
||||
},
|
||||
img4: {
|
||||
type: String,
|
||||
},
|
||||
ingredienti: {
|
||||
type: String,
|
||||
},
|
||||
valori_nutrizionali: {
|
||||
type: String,
|
||||
},
|
||||
note: {
|
||||
type: String,
|
||||
},
|
||||
idAuthors: [{
|
||||
type: Schema.Types.ObjectId, ref: 'Author', index: true
|
||||
}],
|
||||
idCollana: {
|
||||
type: Schema.Types.ObjectId, ref: 'Collana', index: true
|
||||
},
|
||||
idPublisher: {
|
||||
type: Schema.Types.ObjectId, ref: 'Publisher', index: true
|
||||
},
|
||||
collezione: {
|
||||
type: String,
|
||||
},
|
||||
ListaArgomenti: {
|
||||
type: String,
|
||||
},
|
||||
date_pub: {
|
||||
type: Date,
|
||||
index: 1,
|
||||
},
|
||||
date_pub_ts: {
|
||||
type: Number,
|
||||
},
|
||||
productTypes: [{
|
||||
type: Number,
|
||||
}],
|
||||
|
||||
date_updated: {
|
||||
type: Date,
|
||||
},
|
||||
|
||||
date_updated_fromGM: {
|
||||
type: Date,
|
||||
},
|
||||
|
||||
totVen: Number,
|
||||
totFat: Number,
|
||||
vLast3M: Number,
|
||||
fatLast3M: Number,
|
||||
fatLast6M: Number,
|
||||
fatLast1Y: Number,
|
||||
fatLast2Y: Number,
|
||||
vLast6M: {
|
||||
type: Number,
|
||||
index: true,
|
||||
},
|
||||
vLast1Y: {
|
||||
type: Number, index: true
|
||||
},
|
||||
vLast2Y: Number,
|
||||
dataUltimoOrdine: Date,
|
||||
rank3M: Number,
|
||||
rank6M: Number,
|
||||
rank1Y: Number,
|
||||
|
||||
descrizione_breve_macro: String,
|
||||
descrizione_completa_macro: String,
|
||||
descr_trafiletto_catalogo: String,
|
||||
sottotitolo: String,
|
||||
link_macro: String,
|
||||
},
|
||||
idProducer: { type: Schema.Types.ObjectId, ref: 'Producer', index: true },
|
||||
idStorehouses: [{ type: Schema.Types.ObjectId, ref: 'Storehouse', index: true }],
|
||||
idGasordine: { type: Schema.Types.ObjectId, ref: 'Gasordine', index: true },
|
||||
@@ -367,20 +541,6 @@ module.exports.executeQueryPickup = async function (idapp, params) {
|
||||
}
|
||||
|
||||
let aggr2 = [
|
||||
{
|
||||
$lookup: {
|
||||
from: 'productinfos',
|
||||
localField: 'idProductInfo',
|
||||
foreignField: '_id',
|
||||
as: 'productInfo',
|
||||
},
|
||||
},
|
||||
{
|
||||
$unwind: {
|
||||
path: '$productInfo',
|
||||
preserveNullAndEmptyArrays: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: 'authors',
|
||||
@@ -606,15 +766,6 @@ module.exports.findAllIdApp = async function (idapp, code, id, all, isbn) {
|
||||
},
|
||||
{ $unwind: { path: '$producer', preserveNullAndEmptyArrays: true } },
|
||||
|
||||
{
|
||||
$lookup: {
|
||||
from: 'productinfos',
|
||||
localField: 'idProductInfo',
|
||||
foreignField: '_id',
|
||||
as: 'productInfo',
|
||||
},
|
||||
},
|
||||
{ $unwind: { path: '$productInfo', preserveNullAndEmptyArrays: true } },
|
||||
{
|
||||
$match: {
|
||||
'productInfo.code': { $exists: true, $ne: '' },
|
||||
@@ -1054,3 +1205,208 @@ module.exports.singlerecconvert_AfterImport_AndSave = async function (idapp, pro
|
||||
console.error('Err', e);
|
||||
}
|
||||
};
|
||||
|
||||
// ---------------------------------------------
|
||||
|
||||
module.exports.replaceProductImgToImageFile = async function (abilitaserver) {
|
||||
const Product = this;
|
||||
|
||||
if (abilitaserver) {
|
||||
// const result = await ProductInfo.updateMany({ "img": { $exists: true } }, { $rename: { 'img': 'imagefile' } });
|
||||
|
||||
// Trova tutti i documenti con il campo 'img' che esiste
|
||||
const documents = await Product.find({ "productInfo.img": { $exists: true } });
|
||||
|
||||
// Aggiorna ciascun documento
|
||||
for (let doc of documents) {
|
||||
if (doc.img && doc.img.startsWith('upload/products/')) {
|
||||
// Rimuovi il prefisso '/upload/products' dal campo `img`
|
||||
doc.imagefile = doc.img.replace(/^\upload\/products\//, '');
|
||||
doc.img = undefined; // Può anche rimuovere il campo img corrente se desiderato
|
||||
await doc.save(); // Salva il documento aggiornato
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Updated ${documents.length} document(s) with new imagefile paths.`);
|
||||
|
||||
} else {
|
||||
const documents = await Product.find({ "productInfo.imagefile": { $exists: true } });
|
||||
|
||||
// Aggiorna ciascun documento
|
||||
for (let doc of documents) {
|
||||
if (doc.imagefile && doc.imagefile.startsWith('upload/products/')) {
|
||||
// Rimuovi il prefisso '/upload/products' dal campo `img`
|
||||
doc.imagefile = doc.imagefile.replace(/^\upload\/products\//, '');
|
||||
await doc.save(); // Salva il documento aggiornato
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Updated ${documents.length} document(s) with new imagefile paths.`);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// await Product.updateMany({}, { 'vers_img': 0 });
|
||||
|
||||
console.log(`Updated ${result.modifiedCount} document(s).`);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
module.exports.correggiProductTypes = async function () {
|
||||
const Product = this;
|
||||
const bulkOps = [];
|
||||
|
||||
try {
|
||||
// Fase di aggregazione
|
||||
const cursor = Product.aggregate([
|
||||
{
|
||||
$project: {
|
||||
_id: 1,
|
||||
'productInfo.productType': 1,
|
||||
'productInfo.productTypes': [{ $ifNull: ['$productType', 10] }]
|
||||
}
|
||||
}
|
||||
]).cursor({ batchSize: 1000 }).exec();
|
||||
|
||||
// Itera attraverso ogni documento utilizzando il cursore
|
||||
for await (const doc of cursor) {
|
||||
bulkOps.push({
|
||||
updateOne: {
|
||||
filter: { _id: doc._id },
|
||||
update: {
|
||||
$set: { 'productInfo.productTypes': doc.productTypes },
|
||||
$unset: { 'productInfo.productType': "" }
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Scrivi i batch di operazioni ogni 1000 documenti
|
||||
if (bulkOps.length === 1000) {
|
||||
await Product.bulkWrite(bulkOps);
|
||||
bulkOps.length = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Scrivi eventuali operazioni rimanenti
|
||||
if (bulkOps.length > 0) {
|
||||
await Produc.bulkWrite(bulkOps);
|
||||
}
|
||||
|
||||
console.log('ProductInfo.productType converted to productTypes array and saved');
|
||||
} catch (err) {
|
||||
console.error('Error converting ProductInfo.productType:', err);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.updateProductInfoByStats = async function (idapp) {
|
||||
let mylog = '';
|
||||
let mylog2 = '';
|
||||
let mylogtot = '';
|
||||
try {
|
||||
const Product = this;
|
||||
|
||||
const T_WEB_ArticoliFatturati = require('./t_web_articolifatturati');
|
||||
const T_WEB_Ordini = require('./t_web_ordini');
|
||||
|
||||
mylog = "Inizio Aggiornamento Statistiche... \n";
|
||||
mylogtot += mylog;
|
||||
console.log(mylog);
|
||||
|
||||
let countUpdate = 0;
|
||||
|
||||
countUpdate = await T_WEB_ArticoliFatturati.updateStatisticsFatt('', idapp, true);
|
||||
|
||||
// Itera sui risultati e aggiorna productInfo
|
||||
|
||||
mylog = `Aggiornati ${countUpdate} record di productInfo`;
|
||||
mylogtot += mylog;
|
||||
console.log(mylog);
|
||||
|
||||
mylog2 = "Inizio Aggiornamento Statistiche Ordini ... \n";
|
||||
mylogtot += mylog2;
|
||||
console.log(mylog2);
|
||||
|
||||
countUpdate = await T_WEB_Ordini.updateStatisticsOrders('', idapp, true);
|
||||
|
||||
mylog2 = `Aggiornati ${countUpdate} record di productInfo`;
|
||||
mylogtot += mylog2;
|
||||
|
||||
console.log(mylog2);
|
||||
|
||||
} catch (error) {
|
||||
mylog = "Errore durante l'aggiornamento di productInfo:" + error;
|
||||
console.error(mylog);
|
||||
}
|
||||
|
||||
return mylogtot;
|
||||
}
|
||||
|
||||
// crea setImgNotFound
|
||||
|
||||
module.exports.setImgNotFound = async function (id) {
|
||||
// set sul record id il flag image_not_found
|
||||
try {
|
||||
const Product = this;
|
||||
|
||||
await Product.updateOne(
|
||||
{ _id: id },
|
||||
{ $set: { 'productInfo.image_not_found': true } }
|
||||
);
|
||||
console.log(`Flag image_not_found set for record with id: ${id}`);
|
||||
} catch (error) {
|
||||
console.error(`Error setting image_not_found flag for id ${id}:`, error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// imposta tutti i record con image_not_found: false
|
||||
module.exports.resetImageNotFound = async function () {
|
||||
try {
|
||||
const Product = this;
|
||||
|
||||
await Product.updateMany(
|
||||
{ 'productInfo.image_not_found': true },
|
||||
{ $set: { image_not_found: false } }
|
||||
);
|
||||
console.log('Flag image_not_found reset to false for all records');
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Error resetting image_not_found flag:', error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// crea una funzione che mi rimuove il record "product" che utilizza productInfo, nel caso in cui date_updated_fromGM non esista
|
||||
module.exports.removeProductInfoWithoutDateUpdatedFromGM = async function (idapp) {
|
||||
const Product = this;
|
||||
|
||||
const globalTables = require('../tools/globalTables');
|
||||
|
||||
let mylog;
|
||||
|
||||
try {
|
||||
const arrproduct = await Product.find({ idapp, 'productInfo.date_updated_fromGM': { $exists: false } });
|
||||
|
||||
if (arrproduct.length > 0 && arrproduct.length < 1000) {
|
||||
mylog = `Rimuovo ${arrproduct.length} productInfo senza date_updated_fromGM !!`
|
||||
console.log(mylog);
|
||||
|
||||
for (const product of arrproduct) {
|
||||
await Product.updateOne(
|
||||
{ _id: product._id },
|
||||
{ $set: { deleted: true } }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return mylog;
|
||||
} catch (error) {
|
||||
mylog += 'Error removing productInfo without date_updated_fromGM:' + error;
|
||||
console.error(mylog);
|
||||
}
|
||||
|
||||
return mylog;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user