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
PDB=mypassword@1A
SEND_EMAIL=0

View File

@@ -24,15 +24,9 @@ const orderSchema = new Schema({
status: {
type: Number,
},
idProduct: {
type: String
},
idProducer: {
type: String
},
idStorehouse: {
type: String
},
idProduct: { type: Schema.Types.ObjectId, ref: 'Product' },
idProducer: { type: Schema.Types.ObjectId, ref: 'Producer' },
idStorehouse: { type: Schema.Types.ObjectId, ref: 'StoreHouse' },
price: {
type: Number
},
@@ -98,12 +92,10 @@ module.exports.findAllIdApp = async function (idapp) {
const query = [
{ $match: { idapp } },
{ "$addFields": { "myidProd": { "$toObjectId": "$idProduct" } } },
{ "$addFields": { "myidProducer": { "$toObjectId": "$idProducer" } } },
{
$lookup: {
from: 'products',
localField: 'myidProd',
localField: 'idProduct',
foreignField: '_id',
as: 'product'
}
@@ -111,7 +103,7 @@ module.exports.findAllIdApp = async function (idapp) {
{
$lookup: {
from: 'producers',
localField: 'myidProducer',
localField: 'idProducer',
foreignField: '_id',
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) {
const query = [
{ $match: { _id: ObjectID(id) } },
{ "$addFields": { "myidProd": { "$toObjectId": "$idProduct" } } },
{ "$addFields": { "myidProducer": { "$toObjectId": "$idProducer" } } },
{ "$addFields": { "myidStore": { "$toObjectId": "$idStorehouse" } } },
{
$lookup: {
from: 'products',
localField: 'myidProd',
localField: 'idProduct',
foreignField: '_id',
as: 'product'
}
@@ -165,7 +162,7 @@ module.exports.getTotalOrderById = async function (id) {
{
$lookup: {
from: 'producers',
localField: 'myidProducer',
localField: 'idProducer',
foreignField: '_id',
as: 'producer'
}
@@ -173,7 +170,7 @@ module.exports.getTotalOrderById = async function (id) {
{
$lookup: {
from: 'storehouses',
localField: 'myidStore',
localField: 'idStorehouse',
foreignField: '_id',
as: 'storehouse'
}

View File

@@ -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)

View File

@@ -38,7 +38,7 @@ router.get('/:userId', authenticate, async function (req, res, next) {
if (cart)
return res.send({ code: server_constants.RIS_CODE_OK, cart });
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) => {
console.error('Err', err);
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(),
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 res.send({ code: server_constants.RIS_CODE_ERR, status: 0 });
else {
await Product.updateStatusOrders(mycart.items, status);
const myris = ris;
// Cancella il Cart appena salvato in OrdersCart