- 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

@@ -58,9 +58,9 @@ CatProdSchema.statics.findAllIdApp = async function (idapp) {
return await CatProd.find(myfind).sort({ name: 1 }).lean();
};
CatProdSchema.statics.getCatProdWithTitleCount = async function (idapp) {
CatProdSchema.statics.updateCatDeleteEmpty = async function (idapp) {
try {
const result = await CatProd.aggregate([
const toDelete = await CatProd.aggregate([
{ $match: { idapp } },
{
$lookup: {
@@ -77,9 +77,72 @@ CatProdSchema.statics.getCatProdWithTitleCount = async function (idapp) {
quanti: { $size: '$products' } // Conta il numero di prodotti per ciascun CatProd
}
},
{ $sort: { name: 1 } } // Ordina i risultati per nome
{ $match: { quanti: 0 } },
]);
if (toDelete.length > 0) {
const ids = toDelete.map(x => x._id);
const ris = await CatProd.deleteMany({ _id: { $in: ids } });
const deletedRecs = toDelete.map(x => ({ _id: x._id, name: x.name }));
if (deletedRecs.length > 0) {
return `Lista Argomenti cancellati: ${deletedRecs.map(x => x.name).join(', ')}`;
}
}
return "Nessun argomento cancellato";
} catch (error) {
console.error('Error UpdateCatDeleteEmpty:', error);
return error;
}
};
CatProdSchema.statics.getCatProdWithTitleCount = async function (idapp) {
try {
const myquery = [
{ $match: { idapp } },
{
$lookup: {
from: 'productinfos', // Nome della tua collezione productInfo
localField: '_id',
foreignField: 'idCatProds',
as: 'products'
}
},
{
$addFields: {
myproducts: {
$filter: {
input: "$products",
as: "prod",
cond: {
$in: ["$$prod.idStatoProdotto", [1, 4, 34, 45, 46]]
}
}
}
}
}, {
$project: {
_id: 1,
name: 1,
quanti: { $size: '$myproducts' }, // Conta il numero di prodotti per ciascun CatProd
products: {
$map: {
input: "$myproducts",
as: "prod",
in: {
name: "$$prod.name"
}
}
}
}
},
{ $sort: { name: 1 } } // Ordina i risultati per nome
];
const result = await CatProd.aggregate(myquery);
return result;
} catch (error) {
console.error('Error retrieving CatProd with title count:', error);

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
}
}
];

View File

@@ -447,6 +447,7 @@ module.exports.correggiProductTypes = async function () {
}
module.exports.updateProductInfoByStats = async function (idapp) {
let mylog = '';
try {
const ProductInfo = this;
@@ -455,7 +456,7 @@ module.exports.updateProductInfoByStats = async function (idapp) {
// Ottieni le statistiche dalla query
const statistics = await T_WEB_ArticoliFatturati.getStatistics();
let log = "Inizio Aggiornamento Statistiche... \n";
mylog = "Inizio Aggiornamento Statistiche... \n";
console.log(mylog);
// Itera sui risultati e aggiorna productInfo

View File

@@ -1,7 +1,10 @@
// t_web_tipiformato.js
const mongoose = require('mongoose');
const { Schema } = mongoose;
mongoose.Promise = global.Promise;
mongoose.level = "F";
/**
* @typedef {Object} TipoFormato
* @property {bigint} Id
@@ -20,4 +23,32 @@ const TipoFormatoSchema = new Schema({
EnabledAlFresco: Boolean
}, { collection: 't_web_tipiformatos' });
module.exports = mongoose.model('T_WEB_TipiFormato', TipoFormatoSchema);
const T_WEB_TipiFormato = module.exports = mongoose.model('T_WEB_TipiFormato', TipoFormatoSchema);
module.exports.findAllIdApp = async function () {
const myfind = {};
const myquery = [
{
$sort: { IdTipoFormato: 1, DataOra: -1 } // ordina per ID e DataOra decrescente
},
{
$group: {
_id: "$IdTipoFormato",
IdTipoFormato: { $first: "$IdTipoFormato" },
Descrizione: { $first: "$Descrizione" },
DataOra: { $first: "$DataOra" },
// aggiungi altri campi se servono
}
},
{
$sort: { IdTipoFormato: 1 } // opzionale, per ordinare il risultato
}
];
const rec = await T_WEB_TipiFormato.aggregate(myquery);
return rec;
};

View File

@@ -0,0 +1,54 @@
const mongoose = require('mongoose');
const { Schema } = mongoose;
mongoose.Promise = global.Promise;
mongoose.level = "F";
/**
* @typedef {Object} Tipologie
* @property {bigint} Id
* @property {number} IdTipologia
* @property {string} Descrizione
* @property {Date} DataOra
* @property {boolean} Enabled
* @property {boolean} EnabledAlFresco
*/
const TipologieSchema = new Schema({
IdTipologia: Number,
Descrizione: { type: String, maxlength: 100 },
DataOra: Date,
Enabled: Boolean,
EnabledAlFresco: Boolean
}, { collection: 't_web_tipologies' });
const T_WEB_Tipologie = module.exports = mongoose.model('T_WEB_Tipologie', TipologieSchema);
module.exports.findAllIdApp = async function () {
const myfind = {};
const myquery = [
{
$sort: { IdTipologia: 1, DataOra: -1 } // ordina per ID e DataOra decrescente
},
{
$group: {
_id: "$IdTipologia",
IdTipologia: { $first: "$IdTipologia" },
Descrizione: { $first: "$Descrizione" },
DataOra: { $first: "$DataOra" },
// aggiungi altri campi se servono
}
},
{
$sort: { IdTipologia: 1 } // opzionale, per ordinare il risultato
}
];
const rec = await T_WEB_Tipologie.aggregate(myquery);
return rec;
};