100 lines
2.1 KiB
JavaScript
Executable File
100 lines
2.1 KiB
JavaScript
Executable File
|
|
const mongoose = require('mongoose').set('debug', false)
|
|
const Schema = mongoose.Schema;
|
|
|
|
mongoose.Promise = global.Promise;
|
|
mongoose.level = "F";
|
|
|
|
const tools = require('../tools/general');
|
|
|
|
// Resolving error Unknown modifier: $pushAll
|
|
mongoose.plugin(schema => {
|
|
schema.options.usePushEach = true
|
|
});
|
|
|
|
const CatProdSchema = new Schema({
|
|
idapp: {
|
|
type: String,
|
|
},
|
|
idArgomento: {
|
|
type: Number,
|
|
},
|
|
name: {
|
|
type: String,
|
|
},
|
|
descr_estesa: {
|
|
type: String,
|
|
},
|
|
img: {
|
|
type: String,
|
|
},
|
|
icon: {
|
|
type: String,
|
|
},
|
|
color: {
|
|
type: String,
|
|
},
|
|
});
|
|
|
|
CatProdSchema.statics.getAllCategories = function (callback) {
|
|
CatProd.find(callback)
|
|
}
|
|
|
|
CatProdSchema.statics.getCatProdById = function (id, callback) {
|
|
CatProd.findById(id, callback);
|
|
}
|
|
|
|
CatProdSchema.statics.getFieldsForSearch = function () {
|
|
return [{ field: 'name', type: tools.FieldType.string }]
|
|
};
|
|
|
|
CatProdSchema.statics.executeQueryTable = function (idapp, params) {
|
|
return tools.executeQueryTable(this, idapp, params);
|
|
};
|
|
|
|
CatProdSchema.statics.findAllIdApp = async function (idapp) {
|
|
const myfind = { idapp };
|
|
|
|
return await CatProd.find(myfind).sort({ name: 1 }).lean();
|
|
};
|
|
|
|
CatProdSchema.statics.getCatProdWithTitleCount = async function (idapp) {
|
|
try {
|
|
const result = await CatProd.aggregate([
|
|
{ $match: { idapp } },
|
|
{
|
|
$lookup: {
|
|
from: 'productinfos', // Nome della tua collezione productInfo
|
|
localField: '_id',
|
|
foreignField: 'idCatProds',
|
|
as: 'products'
|
|
}
|
|
},
|
|
{
|
|
$project: {
|
|
_id: 1,
|
|
name: 1,
|
|
quanti: { $size: '$products' } // Conta il numero di prodotti per ciascun CatProd
|
|
}
|
|
},
|
|
{ $sort: { name: 1 } } // Ordina i risultati per nome
|
|
]);
|
|
|
|
return result;
|
|
} catch (error) {
|
|
console.error('Error retrieving CatProd with title count:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
|
|
const CatProd = mongoose.model('CatProd', CatProdSchema);
|
|
|
|
CatProd.createIndexes()
|
|
.then(() => { })
|
|
.catch((err) => { throw err; });
|
|
|
|
|
|
|
|
module.exports = CatProd;
|