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;
});

View File

@@ -299,8 +299,12 @@ module.exports.findAllIdApp = async function (idapp, code, id) {
);
// console.log('query=', query);
let ris = await Product.aggregate(query)
// console.table('ris', ris);
return ris;
} catch (e) {

View File

@@ -743,12 +743,13 @@ module.exports = {
sendEmail_OrderProduct: async function (lang, idapp, orders, user) {
try {
const msginizio = 'Ordine n: ' + orders.numorder + ' ' + user.name + ' ' + user.surname;
const msg = await OrdersCart.getmsgorderTelegram(orders);
const msginizio = msg;
console.log(msginizio);
if (process.env.SEND_EMAIL_ORDERS === '1') {
await telegrambot.sendMsgTelegramToTheManagers(idapp, msginizio);
await telegrambot.sendMsgTelegramToTheManagers(idapp, msginizio);
if (process.env.SEND_EMAIL_ORDERS === '1') {
let mylocalsconf = {
idapp,
@@ -776,12 +777,13 @@ module.exports = {
sendEmail_Order: async function (lang, idapp, orders, user, ordertype, status) {
try {
const msginizio = 'INIZIO - sendEmail_Order ' + ordertype + ': ' + tools.getNomeAppByIdApp(idapp);
const msg = await OrdersCart.getmsgorderTelegram(orders);
const msginizio = msg;
console.log(msginizio);
if (process.env.SEND_EMAIL_ORDERS === '1') {
await telegrambot.sendMsgTelegramToTheManagers(idapp, msginizio);
await telegrambot.sendMsgTelegramToTheManagers(idapp, msginizio);
if (process.env.SEND_EMAIL_ORDERS === '1') {
let mylocalsconf = {
idapp,

View File

@@ -707,10 +707,20 @@ const MyTelegramBot = {
getAppTelegram: function () {
if (process.env.appTelegram) {
return JSON.parse(process.env.appTelegram);
arrTeleg = JSON.parse(process.env.appTelegram);
} else {
return appTelegram;
arrTeleg = appTelegram;
}
if (process.env.NODE_ENV === 'development')
arrTeleg = appTelegram_DEVELOP;
else if (process.env.NODE_ENV === 'test')
arrTeleg = MyTelegramBot.getAppTelegramTest();
const arrTelegFromSite = tools.getArrTelegramFromSite();
if (arrTelegFromSite.length > 0) {
arrTeleg = arrTelegFromSite;
}
return arrTeleg;
},
@@ -3437,14 +3447,14 @@ class Telegram {
if (!msg.from.username) {
// Non ha l'Username:
// Chiedi l'username sul Bot:
if (tools.getConfSiteOptionEnabledByIdApp(this.idapp, shared_consts.ConfSite.AskUsernameSulBot)) {
rec.status = Status.WAITFOR_USERNAME_TELEGRAM;
}
} else {
// Ha l'Username
// Chiedi l'invitante ?
if (tools.getConfSiteOptionEnabledByIdApp(this.idapp, shared_consts.ConfSite.AskInvitantesulBot)) {
rec.status = Status.WAITFOR_USERNAME_INVITANTE;
@@ -4072,15 +4082,6 @@ function getclTelegByidapp(idapp) {
//if (!tools.testing() || true) {
if (true) {
let arrTeleg = MyTelegramBot.getAppTelegram();
if (process.env.NODE_ENV === 'development')
arrTeleg = appTelegram_DEVELOP;
else if (process.env.NODE_ENV === 'test')
arrTeleg = MyTelegramBot.getAppTelegramTest();
const arrTelegFromSite = tools.getArrTelegramFromSite();
if (arrTelegFromSite.length > 0) {
arrTeleg = arrTelegFromSite;
}
var internetAvailable = require('internet-available');

View File

@@ -407,6 +407,50 @@ module.exports = {
CANCELLATI: { label: 'Cancellati', value: 10, icon: 'delete', color: 'text-red' }, //CANCELED
},
OrderStatusStr: [
{
label: 'Nessuno',
value: 0,
},
{
label: 'In Carrello',
value: 1,
},
{
label: 'Ordine in Lavorazione',
value: 2,
},
{
label: 'Ordine Confermato',
value: 3,
},
{
label: 'Pagato',
value: 4,
},
{
label: 'Consegnato',
value: 5,
},
{
label: 'Spedito',
value: 6,
},
{
label: 'Ricevuto',
value: 7,
},
{
label: 'Completato',
value: 8,
},
{
label: 'Cancellato',
value: 10,
},
],
OrderStatusView: {
CHECKOUT_SENT: 2,
ORDER_CONFIRMED: 3,
@@ -864,5 +908,9 @@ module.exports = {
return ''
},
getStatusStr(status) {
const trovatorec = this.OrderStatusStr.find((rec) => rec.value === status)
return (trovatorec) ? trovatorec.label : ''
},
};