aggiornamento...

This commit is contained in:
Surya Paolo
2023-12-17 19:19:04 +01:00
parent c71f4af370
commit 50d610b1b1
5 changed files with 195 additions and 92 deletions

View File

@@ -168,9 +168,86 @@ module.exports.getRecCartByUserId = async function (uid, idapp, numorder) {
}
module.exports.getOrdersCartById = async function (id) {
let query = { _id: id };
return await OrdersCart.findOne(query);
let query = { _id: ObjectID(id) };
const arrris = await OrdersCart.getOrdersCartByQuery(query);
return arrris && arrris.length > 0 ? arrris[0] : null;
}
module.exports.getOrdersCartByQuery = async function (query) {
let myorderscart = await OrdersCart.find(query)
.populate('items.order')
.populate({
path: 'items.order',
populate: {
path: 'idProduct',
model: 'Product'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idProducer',
model: 'Producer'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idProvider',
model: 'Provider'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idStorehouse',
model: 'Storehouse'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idScontisticas',
model: 'Scontistica'
},
})
.populate({
path: 'userId',
model: 'User',
select: '_id name surname username profile', // Specify the fields you want to retrieve
})
.lean();
myorderscart = myorderscart.map(order => {
order.user = order.userId
order.userId = order.user._id
order.items = order.items.map(item => {
if (item.order) {
try {
item.order.product = item.order.idProduct;
item.order.idProduct = item.order.product ? item.order.product._id : '';
item.order.producer = item.order.idProducer;
item.order.idProducer = item.order.producer ? item.order.producer._id : '';
item.order.storehouse = item.order.idStorehouse;
item.order.idStorehouse = item.order.storehouse ? item.order.storehouse._id : '';
item.order.provider = item.order.idProvider;
item.order.idProvider = item.order.provider ? item.order.provider._id : '';
item.order.scontisticas = item.order.scontisticas;
item.order.idScontisticas = item.order.idScontisticas ? item.order.idScontisticas._id : '';
} catch (e) {
console.error('Err: ', e);
}
}
return item;
});
return order;
});
return myorderscart;
}
module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder) {
@@ -186,6 +263,12 @@ module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder) {
query.userId = uid;
}
myorderscart = await OrdersCart.getOrdersCartByQuery(query);
if (myorderscart)
console.log('*** Num myorderscart ', myorderscart.length);
/*transform: function(doc, populated) {
// Rinomina 'idProduct' a 'product' nei risultati della popolazione
populated.product = populated.idProduct;
@@ -193,76 +276,7 @@ module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder) {
return populated;
},*/
myorderscart = await OrdersCart.find(query)
.populate('items.order')
.populate({
path: 'items.order',
populate: {
path: 'idProduct',
model: 'Product'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idProducer',
model: 'Producer'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idProvider',
model: 'Provider'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idStorehouse',
model: 'Storehouse'
},
})
.populate({
path: 'items.order',
populate: {
path: 'idScontisticas',
model: 'Scontistica'
},
})
.populate({
path: 'userId',
model: 'User',
select: '_id name surname username profile', // Specify the fields you want to retrieve
})
.lean();
myorderscart = myorderscart.map(order => {
order.user = order.userId
order.userId = order.user._id
order.items = order.items.map(item => {
if (item.order) {
try {
item.order.product = item.order.idProduct;
item.order.idProduct = item.order.product ? item.order.product._id : '';
item.order.producer = item.order.idProducer;
item.order.idProducer = item.order.producer ? item.order.producer._id : '';
item.order.storehouse = item.order.idStorehouse;
item.order.idStorehouse = item.order.storehouse ? item.order.storehouse._id : '';
item.order.provider = item.order.idProvider;
item.order.idProvider = item.order.provider ? item.order.provider._id : '';
item.order.scontisticas = item.order.scontisticas;
item.order.idScontisticas = item.order.idScontisticas ? item.order.idScontisticas._id : '';
} catch (e) {
console.error('Err: ', e);
}
}
return item;
});
return order;
});
console.log('*** Num myorderscart ', myorderscart.length);
return myorderscart
} catch (e) {
console.error('Err:', e);
@@ -438,7 +452,8 @@ module.exports.updateStockQtaDalMagazzino = async function (idorderscart) {
module.exports.updateCmd = async function (ordersCart, status, value) {
let myOrderCart = await OrdersCart.findOne({ _id: ordersCart._id }).populate('items.order').lean();
let myOrderCart = await OrdersCart.findOne({ _id: ordersCart._id })
.populate('items.order').lean();
try {
if (!!myOrderCart) {
@@ -466,7 +481,7 @@ module.exports.updateCmd = async function (ordersCart, status, value) {
await OrdersCart.setFieldInOrdersById({ status }, myOrderCart);
myOrderCart = await OrdersCart.findOne({ _id: ordersCart._id }).populate('items.order').lean();
myOrderCart = await OrdersCart.getOrdersCartById(ordersCart._id)
// myOrderCart = await OrdersCart.findOne({ _id: idorderscart });
return myOrderCart;
@@ -513,6 +528,39 @@ OrdersCartSchema.pre('save', async function (next) {
}
});
module.exports.getmsgorderTelegram = async function (ordersCart) {
try {
const statusstr = shared_consts.getStatusStr(ordersCart.status);
let msg = '🟢✍️ Ordine n. ' + ordersCart.numorder
msg += '<br>Stato: ' + statusstr;
msg += '<br>🙎🏻‍♂️ ' + tools.getNomeCognomeEUserNameByUser(ordersCart.user)
if (ordersCart.note)
msg += '<br>Note: ' + ordersCart.note;
msg += '<br><br>Lista Prodotti:';
for (const ord of ordersCart.items) {
msg += '<br>';
msg += '✅ [' + ord.order.quantity + '] ' + ord.order.product.name + ' (' + ord.order.price + ' € ' + (ord.order.after_price ? ord.order.after_price : '') + ')';
}
msg += '<br>';
msg += '<br>Totale Prodotti: ' + ordersCart.totalQty;
msg += '<br>Totale Ordine: ' + ordersCart.totalPrice + ' € 💰';
return msg;
} catch (e) {
console.error('Err', e);
}
};
module.exports.createIndexes((err) => {
if (err) throw err;
});