- scheda prodotto migliorata

- aggiornamento filtri
This commit is contained in:
Surya Paolo
2025-04-24 01:03:27 +02:00
parent 85e2df56e1
commit 4b4e3963ac
10 changed files with 393 additions and 95 deletions

View File

@@ -88,6 +88,12 @@ const productSchema = new Schema({
tipologia: {
type: String,
},
idTipologia: {
type: Number,
},
idTipoFormato: {
type: Number,
},
edizione: {
type: String,
},
@@ -241,73 +247,101 @@ module.exports.executeQueryTable = function (idapp, params) {
module.exports.executeQueryPickup = async function (idapp, params) {
let strfind = tools.removeAccents(params.search.trim().toLowerCase());
let strfindInput = tools.removeAccents(params.search.trim().toLowerCase());
// Rimuove le parole "il" e "la" e gli spazi, le @ e i tabulazioni
// per non farli influire sulla ricerca
strfind = strfind.replace(/\b(il|la|gli|le|lo|un|una)\b/g, '').replace(/[-@\t]/g, '').trim();
strfindInput = strfindInput.replace(/\b(il|la|gli|le|lo|un|una)\b/g, '').replace(/[-@\t]/g, '').trim();
if (strfind === '' && !params.filter) {
if (strfindInput === '' && !params.filter) {
return [];
}
let filterfindexact = {};
if (strfind) {
filterfindexact = { comune: strfind };
if (strfindInput) {
filterfindexact = { comune: strfindInput };
}
let limit = 20;
let risexact = [];
let filterfind = {
const cleanInput = tools.removeAccents(strfindInput.trim());
const words = cleanInput.split(/\s+/);
const escapeRegex = w => w.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// 🔹 Pattern per productInfo.name: tutte le parole devono essere presenti
const patternAllWords = words.map(w => `(?=.*\\b${escapeRegex(w)}\\b)`).join('') + '.*';
// 🔹 Condizioni per autori
let authorConditions = [];
// Se ci sono esattamente 2 parole (es. "antonio graziano")
if (words.length === 2) {
const [w1, w2] = words.map(escapeRegex);
authorConditions = [
{
'productInfo.authors': {
$elemMatch: {
name: { $regex: `^${w1}`, $options: 'i' },
surname: { $regex: `^${w2}`, $options: 'i' }
}
}
},
{
'productInfo.authors': {
$elemMatch: {
name: { $regex: `^${w2}`, $options: 'i' },
surname: { $regex: `^${w1}`, $options: 'i' }
}
}
}
];
}
// Se c'è solo una parola (es. "antonio")
if (words.length === 1) {
const word = escapeRegex(words[0]);
authorConditions = [
{
'productInfo.authors': {
$elemMatch: {
$or: [
{ name: { $regex: `^${word}`, $options: 'i' } },
{ surname: { $regex: `^${word}`, $options: 'i' } }
]
}
}
}
];
}
// 🔹 Filtro finale
const filterfind = {
idapp,
$or: [
{
'productInfo.name': {
$regex: `(?i).*${strfind}.*`, // Cerca una o più parole che sono contenute
$options: 'i' // Rende la ricerca case-insensitive
$regex: patternAllWords,
$options: 'i'
}
},
{
'productInfo.code': {
$regex: `\\b${strfind}`, // Cerca parole che iniziano con strfind
$options: 'i' // Rende la ricerca case-insensitive
$regex: `\\b${escapeRegex(cleanInput)}`,
$options: 'i'
}
},
{
'productInfo.sku': strfind
},
{
$and: [
{
'productInfo.authors.name': {
$regex: `(?i).*${strfind.split(' ').shift()}.*`, // Cerca la prima parola
$options: 'i' // Rende la ricerca case-insensitive
}
},
{
'productInfo.authors.surname': {
$regex: `(?i).*${strfind.split(' ').pop()}.*`, // Cerca la seconda parola
$options: 'i' // Rende la ricerca case-insensitive
}
},
]
},
{
'productInfo.authors.name': {
$regex: `(?i).*${strfind}.*`, // Cerca una o più parole che sono contenute
$options: 'i' // Rende la ricerca case-insensitive
}
},
{
'productInfo.authors.surname': {
$regex: `(?i).*${strfind}.*`, // Cerca una o più parole che sono contenute
$options: 'i' // Rende la ricerca case-insensitive
}
'productInfo.sku': cleanInput
},
...authorConditions
]
};
if (params.filter) {
filterfind = { ...params.filter, ...filterfind };
limit = 200;
@@ -357,6 +391,13 @@ module.exports.executeQueryPickup = async function (idapp, params) {
},
arrvariazioni: "$arrvariazioni",
}
},
{
$sort: {
'arrvariazioni.0.quantita': -1, // Ordina per arrvariazioni[0].quantita , decrescente
'productInfo.date_pub': -1,
'productInfo.name': 1 // Ordina per name in ordine crescente
}
}
];