- 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);