aggio query

This commit is contained in:
Surya Paolo
2023-12-12 15:42:41 +01:00
parent 5f3c8a65ea
commit a2bd4f6e97
4 changed files with 86 additions and 35 deletions

View File

@@ -1,4 +1,4 @@
DATABASE=test_FreePlanet DATABASE=test_PiuCheBuono
UDB=paofreeplanet UDB=paofreeplanet
PDB=mypassword@1A PDB=mypassword@1A
SEND_EMAIL=0 SEND_EMAIL=0

View File

@@ -24,15 +24,9 @@ const orderSchema = new Schema({
status: { status: {
type: Number, type: Number,
}, },
idProduct: { idProduct: { type: Schema.Types.ObjectId, ref: 'Product' },
type: String idProducer: { type: Schema.Types.ObjectId, ref: 'Producer' },
}, idStorehouse: { type: Schema.Types.ObjectId, ref: 'StoreHouse' },
idProducer: {
type: String
},
idStorehouse: {
type: String
},
price: { price: {
type: Number type: Number
}, },
@@ -98,12 +92,10 @@ module.exports.findAllIdApp = async function (idapp) {
const query = [ const query = [
{ $match: { idapp } }, { $match: { idapp } },
{ "$addFields": { "myidProd": { "$toObjectId": "$idProduct" } } },
{ "$addFields": { "myidProducer": { "$toObjectId": "$idProducer" } } },
{ {
$lookup: { $lookup: {
from: 'products', from: 'products',
localField: 'myidProd', localField: 'idProduct',
foreignField: '_id', foreignField: '_id',
as: 'product' as: 'product'
} }
@@ -111,7 +103,7 @@ module.exports.findAllIdApp = async function (idapp) {
{ {
$lookup: { $lookup: {
from: 'producers', from: 'producers',
localField: 'myidProducer', localField: 'idProducer',
foreignField: '_id', foreignField: '_id',
as: 'producer' as: 'producer'
} }
@@ -148,16 +140,21 @@ module.exports.createOrder = async function (order) {
}); });
} }
module.exports.updateStatusOrders = async function (arrOrders, status) {
for (const order of arrOrders) {
const ret = await this.findOneAndUpdate({ _id: order._id }, { $set: status });
}
}
module.exports.getTotalOrderById = async function (id) { module.exports.getTotalOrderById = async function (id) {
const query = [ const query = [
{ $match: { _id: ObjectID(id) } }, { $match: { _id: ObjectID(id) } },
{ "$addFields": { "myidProd": { "$toObjectId": "$idProduct" } } },
{ "$addFields": { "myidProducer": { "$toObjectId": "$idProducer" } } },
{ "$addFields": { "myidStore": { "$toObjectId": "$idStorehouse" } } },
{ {
$lookup: { $lookup: {
from: 'products', from: 'products',
localField: 'myidProd', localField: 'idProduct',
foreignField: '_id', foreignField: '_id',
as: 'product' as: 'product'
} }
@@ -165,7 +162,7 @@ module.exports.getTotalOrderById = async function (id) {
{ {
$lookup: { $lookup: {
from: 'producers', from: 'producers',
localField: 'myidProducer', localField: 'idProducer',
foreignField: '_id', foreignField: '_id',
as: 'producer' as: 'producer'
} }
@@ -173,7 +170,7 @@ module.exports.getTotalOrderById = async function (id) {
{ {
$lookup: { $lookup: {
from: 'storehouses', from: 'storehouses',
localField: 'myidStore', localField: 'idStorehouse',
foreignField: '_id', foreignField: '_id',
as: 'storehouse' as: 'storehouse'
} }

View File

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

View File

@@ -38,7 +38,7 @@ router.get('/:userId', authenticate, async function (req, res, next) {
if (cart) if (cart)
return res.send({ code: server_constants.RIS_CODE_OK, cart }); return res.send({ code: server_constants.RIS_CODE_OK, cart });
else else
return res.send({ code: server_constants.RIS_CODE_OK, cart: null }); return res.send({ code: server_constants.RIS_CODE_OK, cart: null });
}).catch((err) => { }).catch((err) => {
console.error('Err', err); console.error('Err', err);
return res.send({ code: server_constants.RIS_CODE_ERR, cart: null }); return res.send({ code: server_constants.RIS_CODE_ERR, cart: null });
@@ -214,11 +214,14 @@ router.post('/:userId/cartstatus', authenticate, async function (req, res, next)
created_at: new Date(), created_at: new Date(),
modify_at: new Date(), modify_at: new Date(),
}) })
return await OrdersCart.updateOrdersCartById(-1, newOrderCart, function (err, ris) { return await OrdersCart.updateOrdersCartById(-1, newOrderCart, async function (err, ris) {
//if (err) return next(err) //if (err) return next(err)
if (err) if (err)
return res.send({ code: server_constants.RIS_CODE_ERR, status: 0 }); return res.send({ code: server_constants.RIS_CODE_ERR, status: 0 });
else { else {
await Product.updateStatusOrders(mycart.items, status);
const myris = ris; const myris = ris;
// Cancella il Cart appena salvato in OrdersCart // Cancella il Cart appena salvato in OrdersCart