- Ricerca Titolo per nome o autore o ISBN o codice articolo

This commit is contained in:
Surya Paolo
2025-03-31 23:56:01 +02:00
parent 789dc1dcae
commit 5431fe118e
9 changed files with 345 additions and 114 deletions

View File

@@ -29,7 +29,8 @@ const CatalogSchema = new Schema({
},
foto_collana: IImg,
idCollane: [{
type: Number,
type: Schema.Types.ObjectId,
ref: 'Collana',
}],
argomenti: [{
type: String,
@@ -72,6 +73,10 @@ const CatalogSchema = new Schema({
date_updated: {
type: Date,
},
lista_prodotti: [{
type: Schema.Types.ObjectId,
ref: 'Product',
}],
});
/*
@@ -94,7 +99,7 @@ CatalogSchema.statics.executeQueryTable = function (idapp, params, user) {
return tools.executeQueryTable(this, idapp, params, user);
};
CatalogSchema.statics.findAllIdApp = async function (idapp) {
CatalogSchema.statics.OLD_findAllIdApp = async function (idapp) {
const Catalog = this;
const arrrec = await Catalog.aggregate([
@@ -119,6 +124,94 @@ CatalogSchema.statics.findAllIdApp = async function (idapp) {
return arrrec;
};
CatalogSchema.statics.findAllIdApp = async function (idapp) {
const Catalog = this;
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
})
.populate({
path: "lista_prodotti", // Popola il campo lista_prodotti
populate: {
path: "idProductInfo", // Popola il campo idProductInfo dentro ogni prodotto
model: "ProductInfo", // Specifica il modello della collezione ProductInfo
populate: [
{
path: "idCatProds",
model: "CatProd"
},
{
path: "idSubCatProds",
model: "SubCatProd"
},
{
path: "idAuthors",
model: "Author"
}
],
},
})
.populate({
path: "lista_prodotti",
populate: {
path: "idProducer",
model: "Producer"
}
})
.populate({
path: "lista_prodotti",
populate: {
path: "idProvider",
model: "Provider"
}
})
.populate({
path: "lista_prodotti",
populate: {
path: "idStorehouses",
model: "Storehouse"
}
})
.populate({
path: "lista_prodotti",
populate: {
path: "idScontisticas",
model: "Scontistica"
}
})
.populate({
path: "lista_prodotti",
populate: {
path: "idGasordine",
model: "Gasordine"
}
})
;
const transformedArrRec = arrrec.map(catalog => ({
...catalog.toObject(), // Converte il documento Mongoose in un oggetto JavaScript puro
lista_prodotti: catalog.lista_prodotti.map(product => ({
...product.toObject(),
productInfo: {
...product.idProductInfo.toObject(), // Copia tutti i campi di idProductInfo
catprods: product.idProductInfo.idCatProds, // Rinomina idCatProds in catprods
subcatprods: product.idProductInfo.idSubCatProds,
collana: product.idProductInfo.idCollana,
authors: product.idProductInfo.idAuthors,
},
producer: product.idProducer,
storehouse: product.idStorehouses,
scontisticas: product.idScontisticas,
gasordine: product.idGasordine,
})),
}));
return transformedArrRec;
};
const Catalog = mongoose.model('Catalog', CatalogSchema);
Catalog.createIndexes()

View File

@@ -56,6 +56,7 @@ const IDimensioni = new Schema({
});
const IPagina = new Schema({
dimensioni: IDimensioni,
testo_title: IText,
testo_up: IText,
testo_down: IText,
});

View File

@@ -254,55 +254,44 @@ module.exports.executeQueryPickup = async function (idapp, params) {
filterfindexact = { comune: strfind };
}
let limit = 10;
let limit = 20;
let risexact = [];
let filterfind = {
idapp,
'productInfo.name': {
$regex: `\\b${strfind}`, // Usa \\b per trovare solo le parole che iniziano con strfind
$options: 'i' // Rendi la ricerca case-insensitive
}
};
/*
let aggr1 = [
{
$lookup: {
from: 'productinfos',
localField: 'idProductInfo',
foreignField: '_id',
as: 'productInfo'
}
},
{
$lookup: {
from: 'authors',
localField: 'idAuthors',
foreignField: '_id',
as: 'authors'
}
},
{
$match: { 'productInfo.name': strfind },
},
{ $limit: 1 },
{
$project: {
name: { $concat: ["$productInfo.name", " (", "$authors", ")"] },
$or: [
{
'productInfo.name': {
$regex: `\\b${strfind}`, // Cerca parole che iniziano con strfind
$options: 'i' // Rende la ricerca case-insensitive
}
},
},
];
if (params.filter) {
filterfind = { ...params.filter, ...filterfind };
limit = 200;
} else {
// risexact = await City.find(filterfindexact, {comune: 1, prov: 1, reg: 1}).lean();
risexact = await City.aggregate(aggr1);
}
*/
{
'productInfo.code': {
$regex: `\\b${strfind}`, // Cerca parole che iniziano con strfind
$options: 'i' // Rende la ricerca case-insensitive
}
},
{
'productInfo.sku': {
$regex: `\\b${strfind}`, // Cerca parole che iniziano con strfind
$options: 'i' // Rende la ricerca case-insensitive
}
},
{
'productInfo.authors.name': {
$regex: `\\b${strfind}`, // Cerca parole che iniziano con strfind nel nome dell'autore
$options: 'i' // Rende la ricerca case-insensitive
}
},
{
'productInfo.authors.surname': {
$regex: `\\b${strfind}`, // Cerca parole che iniziano con strfind nel cognome dell'autore
$options: 'i' // Rende la ricerca case-insensitive
}
},
]
};
if (params.filter) {
filterfind = { ...params.filter, ...filterfind };
@@ -327,24 +316,34 @@ module.exports.executeQueryPickup = async function (idapp, params) {
{
$lookup: {
from: 'authors',
localField: 'idAuthors',
localField: 'productInfo.idAuthors',
foreignField: '_id',
as: 'authors'
as: 'productInfo.authors'
}
},
{
$unwind: {
path: '$authors',
preserveNullAndEmptyArrays: true,
},
},
{
$match: filterfind,
},
{ $limit: limit },
{
$project: {
name: '$productInfo.name',
},
},
name: '$productInfo.name', // Nome del prodotto
authors: '$productInfo.authors',
productInfo: {
name: '$productInfo.name', // Nome del prodotto
authors: '$productInfo.authors',
},
}
}
];
// let ris = await City.find(filterfind, {comune: 1, prov: 1, reg: 1}).lean().limit(limit);
let ris = await this.aggregate(aggr2).limit(limit);
return [...risexact, ...ris];