- le categorie visibili solo se ci sono prodotti attivi

This commit is contained in:
Surya Paolo
2024-03-03 14:38:25 +01:00
parent 789e3fde41
commit b5dbaafa91

View File

@@ -580,42 +580,65 @@ module.exports.convertAfterImportALLPROD = async function (idapp, dataObjects) {
module.exports.getArrCatProds = async function (idapp, cosa) { module.exports.getArrCatProds = async function (idapp, cosa) {
try { try {
let addquery = {}; let addquery = [];
let arr = []; let arr = [];
if (cosa === shared_consts.PROD.GAS) { if (cosa === shared_consts.PROD.GAS) {
addquery = { idapp, idGasordine: { $exists: true, $ne: null, $type: 'objectId' } } addquery = [
{ $match: { idapp, idGasordine: { $exists: true, $ne: null, $type: 'objectId' } } }
];
addquery.push(
{
$lookup: {
from: 'gasordines',
localField: 'idGasordine',
foreignField: '_id',
as: 'gasordine'
}
}
);
addquery.push(
{
$match: {
"gasordine.active": true
}
}
);
} else if (cosa === shared_consts.PROD.BOTTEGA) { } else if (cosa === shared_consts.PROD.BOTTEGA) {
addquery = { addquery = [{
idapp, $or: [ $match: {
{ idGasordine: { $exists: false } }, idapp, $or: [
{ idGasordine: { $exists: true, $eq: null } } { idGasordine: { $exists: false } },
] { idGasordine: { $exists: true, $eq: null } }
} ]
}
}]
} else { } else {
addquery = { idapp }; addquery = [{ $match: { idapp } }];
} }
let myquery = [ let myquery = [...addquery,
{ $match: addquery }, {
{ $lookup: {
$lookup: { from: "productinfos",
from: "productinfos", localField: "idProductInfo",
localField: "idProductInfo", foreignField: "_id",
foreignField: "_id", as: "productInfo",
as: "productInfo",
},
}, },
{ },
$lookup: { {
from: "catprods", $lookup: {
localField: "productInfo.idCatProds", from: "catprods",
foreignField: "_id", localField: "productInfo.idCatProds",
as: "category" foreignField: "_id",
} as: "category"
}, }
{ $unwind: "$category" }, },
{ $group: { _id: "$category._id", name: { $first: "$category.name" } } }, { $unwind: "$category" },
{ $sort: { name: 1 } } { $group: { _id: "$category._id", name: { $first: "$category.name" } } },
{ $sort: { name: 1 } }
]; ];
arr = await Product.aggregate(myquery, (err, result) => { arr = await Product.aggregate(myquery, (err, result) => {