mongoose = require('mongoose').set('debug', false) const Schema = mongoose.Schema; const tools = require('../tools/general'); const shared_consts = require('../tools/shared_nodejs'); mongoose.Promise = global.Promise; mongoose.level = "F"; // A1P // Resolving error Unknown modifier: $pushAll mongoose.plugin(schema => { schema.options.usePushEach = true }); const productSchema = new Schema({ idapp: { type: String, }, active: { type: Boolean, }, idProducer: { type: Schema.Types.ObjectId, ref: 'Producer' }, idStorehouses: [ { type: Schema.Types.ObjectId, ref: 'Storehouse' } ], code: { type: String, }, codice_EAN: { type: String, }, barcode: { type: String, }, name: { type: String, }, description: { type: String, }, department: { type: String, ref: 'Department' }, category: { type: Array, }, prezzo_ivato: { // Con IVA type: Number }, perc_iva: { // 4, 10, 22 & type: Number }, price: { type: Number, required: true, }, after_price: { type: String }, color: { type: String }, size: { type: String }, weight: { type: Number }, vegan: { type: Boolean }, unit: { type: Number }, stockQty: { // in magazzino type: Number, default: 0, }, quantityAvailable: { type: Number, default: 0, }, quantityLow: { //Soglia disponibilità bassa type: Number, default: 0, }, visibilityProductOutOfStock: { // Visibilità prodotto "esaurito" type: Boolean, default: false, }, canBeShipped: { // è spedibile type: Boolean, default: false, }, canBeBuyOnline: { // è acquistabile online type: Boolean, default: false, }, stars: { type: Number, default: 0, }, dateAvailableFrom: { type: Date }, icon: { type: String, }, img: { type: String, }, img2: { type: String, }, img3: { type: String, }, ingredienti: { type: String, }, valori_nutrizionali: { type: String, }, note: { type: String, }, }); var Product = module.exports = mongoose.model('Product', productSchema); productSchema.index({ idapp: 1 }); module.exports.getFieldsForSearch = function () { return [{ field: 'name', type: tools.FieldType.string }] }; module.exports.executeQueryTable = function (idapp, params) { params.fieldsearch = this.getFieldsForSearch(); return tools.executeQueryTable(this, idapp, params); }; module.exports.findAllIdApp = async function (idapp, code) { let myfind = { idapp, active: true }; if (code) { myfind = { ...myfind, code } } // return await Product.find(myfind); const query = [ { $match: myfind }, { $lookup: { from: 'producers', localField: 'idProducer', foreignField: '_id', as: 'producer' } }, { $unwind: '$producer' }, { $lookup: { from: 'storehouses', localField: 'idStorehouses', foreignField: '_id', as: 'storehouses' } }, { $lookup: { from: "orders", localField: "_id", foreignField: "idProduct", as: "productOrders", }, }, { $unwind: { path: "$productOrders", preserveNullAndEmptyArrays: true, }, }, { $group: { _id: "$_id", products: { $push: "$$ROOT" }, totalQty: { $sum: { $cond: { if: { $lt: ["$productOrders.status", 4] }, // Include stati minori di 4 then: "$productOrders.quantity", else: 0 } } }, }, }, { $project: { _id: 0, products: { $map: { input: "$products", as: "product", in: { $mergeObjects: [ "$$product", { totalQty: { quantity: "$totalQty" } } ] } } } } } ]; let ris = await Product.aggregate(query) return ris; }; module.exports.getAllProducts = function (query, sort, callback) { Product.find(query, null, sort, callback) } module.exports.getProductByDepartment = function (query, sort, callback) { Product.find(query, null, sort, callback) } module.exports.getProductByCategory = function (query, sort, callback) { Product.find(query, null, sort, callback) } module.exports.getProductByTitle = function (query, sort, callback) { Product.find(query, null, sort, callback) } module.exports.getProductByCode = function (idapp, code) { return Product.findOne({ idapp, code }) } module.exports.filterProductByDepartment = function (department, callback) { let regexp = new RegExp(`^${department}$`, 'i') var query = { department: { $regex: regexp } }; Product.find(query, callback) } module.exports.filterProductByCategory = function (category, callback) { let regexp = new RegExp(`^${category}$`, 'i') var query = { category: { $regex: regexp } }; Product.find(query, callback); } module.exports.filterProductByTitle = function (title, callback) { let regexp = new RegExp(`^${title}$`, 'i') var query = { title: { $regex: regexp } }; Product.find(query, callback); } module.exports.getProductByID = function (id, callback) { Product.findById(id, callback); } module.exports.createIndexes((err) => { if (err) throw err; }); // const Product = mongoose.model('Product', ProductSchema); // module.exports = { Product }; // PROVA