aggio query
This commit is contained in:
@@ -3,6 +3,8 @@ const Schema = mongoose.Schema;
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
const shared_consts = require('../tools/shared_nodejs');
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.level = "F";
|
||||
|
||||
@@ -20,9 +22,7 @@ const productSchema = new Schema({
|
||||
active: {
|
||||
type: Boolean,
|
||||
},
|
||||
idProducer: {
|
||||
type: String
|
||||
},
|
||||
idProducer: { type: Schema.Types.ObjectId, ref: 'Producer' },
|
||||
idStorehouses: [
|
||||
{ type: Schema.Types.ObjectId, ref: 'Storehouse' }
|
||||
],
|
||||
@@ -54,7 +54,7 @@ const productSchema = new Schema({
|
||||
type: Number
|
||||
},
|
||||
price: {
|
||||
type: Number,
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
after_price: {
|
||||
@@ -77,25 +77,31 @@ const productSchema = new Schema({
|
||||
},
|
||||
stockQty: { // in magazzino
|
||||
type: Number,
|
||||
required: true,
|
||||
default: 0,
|
||||
},
|
||||
quantityAvailable: {
|
||||
type: Number
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
quantityLow: { //Soglia disponibilità bassa
|
||||
type: Number
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
visibilityProductOutOfStock: { // Visibilità prodotto "esaurito"
|
||||
type: Boolean
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
canBeShipped: { // è spedibile
|
||||
type: Boolean
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
canBeBuyOnline: { // è acquistabile online
|
||||
type: Boolean
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
stars: {
|
||||
type: Number
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
dateAvailableFrom: {
|
||||
type: Date
|
||||
@@ -147,11 +153,10 @@ module.exports.findAllIdApp = async function (idapp, code) {
|
||||
|
||||
const query = [
|
||||
{ $match: myfind },
|
||||
{ "$addFields": { "myidProd": { "$toObjectId": "$idProducer" } } },
|
||||
{
|
||||
$lookup: {
|
||||
from: 'producers',
|
||||
localField: 'myidProd',
|
||||
localField: 'idProducer',
|
||||
foreignField: '_id',
|
||||
as: 'producer'
|
||||
}
|
||||
@@ -165,6 +170,52 @@ module.exports.findAllIdApp = async function (idapp, code) {
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user