- ottimizzato il caricamento del sito

- ottimizzato il caricamento del catalogo.
This commit is contained in:
Surya Paolo
2025-05-15 18:22:43 +02:00
parent 768d299881
commit 3521a88395
9 changed files with 190 additions and 90 deletions

View File

@@ -1,4 +1,4 @@
const mongoose = require('mongoose').set('debug', false)
const mongoose = require('mongoose').set('debug', false);
const Schema = mongoose.Schema;
const tools = require('../tools/general');
@@ -7,15 +7,13 @@ const { ObjectId } = require('mongodb');
const { IImg } = require('../models/myscheda');
mongoose.Promise = global.Promise;
mongoose.level = "F";
mongoose.level = 'F';
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true
mongoose.plugin((schema) => {
schema.options.usePushEach = true;
});
const CatalogSchema = new Schema({
idapp: {
type: String,
@@ -30,16 +28,22 @@ const CatalogSchema = new Schema({
},
foto_collana: IImg,
idCollane: [{
type: String,
}],
idTipoFormato: [{
type: Number,
}],
idCollane: [
{
type: String,
},
],
idTipoFormato: [
{
type: Number,
},
],
argomenti: [{
type: String,
}],
argomenti: [
{
type: String,
},
],
condition_andor: {
type: Number,
default: 0,
@@ -56,9 +60,11 @@ const CatalogSchema = new Schema({
idPageAssigned_stampa: {
type: String,
},
referenti: [{
type: String,
}],
referenti: [
{
type: String,
},
],
img_bordata: IImg,
img_intro: IImg,
@@ -93,15 +99,17 @@ const CatalogSchema = new Schema({
date_created: {
type: Date,
default: Date.now
default: Date.now,
},
date_updated: {
type: Date,
},
lista_prodotti: [{
type: Schema.Types.ObjectId,
ref: 'Product',
}],
lista_prodotti: [
{
type: Schema.Types.ObjectId,
ref: 'Product',
},
],
isCatalogoGenerale: Boolean,
});
@@ -117,7 +125,7 @@ CatalogSchema.pre('save', async function (next) {
*/
CatalogSchema.statics.getFieldsForSearch = function () {
return [{ field: 'title', type: tools.FieldType.string }]
return [{ field: 'title', type: tools.FieldType.string }];
};
CatalogSchema.statics.executeQueryTable = function (idapp, params, user) {
@@ -151,85 +159,95 @@ CatalogSchema.statics.executeQueryTable = function (idapp, params, user) {
};*/
CatalogSchema.statics.findAllIdApp = async function (idapp) {
try {
const arrrec = await this.aggregate([
{ $match: { idapp } },
{ $addFields: { num_lista_prodotti: { $size: { $ifNull: ['$lista_prodotti', []] } } } },
{ $project: { lista_prodotti: 0 } },
{ $sort: { title: 1 } },
]);
return arrrec;
} catch (err) {
console.error('Errore:', err);
throw err;
}
};
CatalogSchema.statics.getCatalogById = async function (id) {
const Catalog = this;
try {
let arrrec = await Catalog.find({ idapp })
.sort({ title: 1 }) // Ordina i risultati per titolo
/*.populate({
path: "idCollane", // Popola il campo idCollane
model: "Collana" // Specifica il modello della collezione Collana
})*/
let arrrec = await Catalog.find({ _id: id })
.populate({
path: "lista_prodotti", // Popola il campo lista_prodotti
path: 'lista_prodotti', // Popola il campo lista_prodotti
populate: {
path: "idProductInfo",
model: "ProductInfo",
path: 'idProductInfo',
model: 'ProductInfo',
populate: [
{
path: "idCatProds",
model: "CatProd"
path: 'idCatProds',
model: 'CatProd',
},
{
path: "idSubCatProds",
model: "SubCatProd"
path: 'idSubCatProds',
model: 'SubCatProd',
},
{
path: "idAuthors",
model: "Author"
}
path: 'idAuthors',
model: 'Author',
},
],
},
})
.populate({
path: "lista_prodotti",
path: 'lista_prodotti',
populate: {
path: "idProducer",
model: "Producer"
}
path: 'idProducer',
model: 'Producer',
},
})
.populate({
path: "lista_prodotti",
path: 'lista_prodotti',
populate: {
path: "idProvider",
model: "Provider"
}
path: 'idProvider',
model: 'Provider',
},
})
.populate({
path: "lista_prodotti",
path: 'lista_prodotti',
populate: {
path: "idStorehouses",
model: "Storehouse"
}
path: 'idStorehouses',
model: 'Storehouse',
},
})
.populate({
path: "lista_prodotti",
path: 'lista_prodotti',
populate: {
path: "idScontisticas",
model: "Scontistica"
}
path: 'idScontisticas',
model: 'Scontistica',
},
})
.populate({
path: "lista_prodotti",
path: 'lista_prodotti',
populate: {
path: "idGasordine",
model: "Gasordine"
}
})
;
path: 'idGasordine',
model: 'Gasordine',
},
});
// controlla prima se nella lista ci sono dei product che non esistono piu allora li devi rimuovere !
for (const catalog of arrrec) {
const originalLength = catalog.lista_prodotti.length;
catalog.lista_prodotti = catalog.lista_prodotti.filter(product => product.idProductInfo);
catalog.lista_prodotti = catalog.lista_prodotti.filter((product) => product.idProductInfo);
if (catalog.lista_prodotti.length !== originalLength) {
await catalog.save();
}
}
const transformedArrRec = arrrec.map(catalog => ({
const transformedArrRec = arrrec.map((catalog) => ({
...catalog.toObject(), // Converte il documento Mongoose in un oggetto JavaScript puro
lista_prodotti: catalog.lista_prodotti.map(product => ({
lista_prodotti: catalog.lista_prodotti.map((product) => ({
...product.toObject(),
productInfo: {
...product.idProductInfo.toObject(), // Copia tutti i campi di idProductInfo
@@ -242,12 +260,14 @@ CatalogSchema.statics.findAllIdApp = async function (idapp) {
storehouse: product.idStorehouses,
scontisticas: product.idScontisticas,
gasordine: product.idGasordine,
idProductInfo: product.idProductInfo._id, // CHECK
})),
}));
return transformedArrRec;
return transformedArrRec && transformedArrRec.length > 0 ? transformedArrRec[0] : null;
} catch (err) {
console.error('Errore: ', err);
console.error('Errore: ', err);
return null;
}
};
@@ -268,16 +288,16 @@ CatalogSchema.statics.executeQueryPickup = async function (idapp, params) {
.limit(10)
.select('title _id')
.lean();
return arrrec;
};
const Catalog = mongoose.model('Catalog', CatalogSchema);
Catalog.createIndexes()
.then(() => { })
.catch((err) => { throw err; });
.then(() => {})
.catch((err) => {
throw err;
});
module.exports = { Catalog };