Corretto Ordini e visualizzazione dei Totali
This commit is contained in:
@@ -2,6 +2,7 @@ DATABASE=test_PiuCheBuono
|
|||||||
UDB=paofreeplanet
|
UDB=paofreeplanet
|
||||||
PDB=mypassword@1A
|
PDB=mypassword@1A
|
||||||
SEND_EMAIL=0
|
SEND_EMAIL=0
|
||||||
|
SEND_EMAIL_ORDERS=0
|
||||||
PORT=3000
|
PORT=3000
|
||||||
appTelegram_TEST=["1","13"]
|
appTelegram_TEST=["1","13"]
|
||||||
appTelegram=["1","13"]
|
appTelegram=["1","13"]
|
||||||
@@ -15,6 +16,7 @@ PORT_APP1=8080
|
|||||||
SIGNCODE=SU2134hncaskjdbo231oihJSDNHEI$1@AB
|
SIGNCODE=SU2134hncaskjdbo231oihJSDNHEI$1@AB
|
||||||
HTTPS_LOCALHOST=true
|
HTTPS_LOCALHOST=true
|
||||||
DEBUG=0
|
DEBUG=0
|
||||||
|
DEBUG=0
|
||||||
TESTING_ON=1
|
TESTING_ON=1
|
||||||
LOCALE=1
|
LOCALE=1
|
||||||
DELAY_SENDEMAIL=2000
|
DELAY_SENDEMAIL=2000
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ DATABASE=PiuCheBuono
|
|||||||
UDB=paofreeplanet
|
UDB=paofreeplanet
|
||||||
PDB=suerteFreePlanet@1A
|
PDB=suerteFreePlanet@1A
|
||||||
SEND_EMAIL=1
|
SEND_EMAIL=1
|
||||||
|
SEND_EMAIL_ORDERS=1
|
||||||
PORT=3000
|
PORT=3000
|
||||||
appTelegram_TEST=["17"]
|
appTelegram_TEST=["17"]
|
||||||
appTelegram=["17"]
|
appTelegram=["17"]
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ DATABASE=test_PiuCheBuono
|
|||||||
UDB=paofreeplanet
|
UDB=paofreeplanet
|
||||||
PDB=suerteFreePlanet@1A
|
PDB=suerteFreePlanet@1A
|
||||||
SEND_EMAIL=1
|
SEND_EMAIL=1
|
||||||
|
SEND_EMAIL_ORDERS=1
|
||||||
PORT=3001
|
PORT=3001
|
||||||
appTelegram_TEST=["17"]
|
appTelegram_TEST=["17"]
|
||||||
appTelegram=["17"]
|
appTelegram=["17"]
|
||||||
|
|||||||
@@ -98,7 +98,6 @@ MovementSchema.statics.addMov = async function (idapp, accountFromIdTable, accou
|
|||||||
|
|
||||||
let mymov = await Movement.create(
|
let mymov = await Movement.create(
|
||||||
{
|
{
|
||||||
_id: new ObjectID().toString(),
|
|
||||||
idapp,
|
idapp,
|
||||||
transactionDate: new Date(),
|
transactionDate: new Date(),
|
||||||
accountFromId: accountFromIdTable._id,
|
accountFromId: accountFromIdTable._id,
|
||||||
|
|||||||
@@ -180,30 +180,33 @@ module.exports.getOrderByID = function (id, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports.createOrder = async function (order) {
|
module.exports.createOrder = async function (order) {
|
||||||
const orderModel = new Order(order);
|
|
||||||
|
|
||||||
return await orderModel.save(order)
|
try {
|
||||||
.then((ris) => {
|
return await Order.create(order)
|
||||||
if (!!ris)
|
.then((ris) => {
|
||||||
return ris._id;
|
if (!!ris)
|
||||||
return null;
|
return ris._id;
|
||||||
});
|
return null;
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
console.error('err', e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.updateStatusOrders = async function (arrOrders, status) {
|
module.exports.updateStatusOrders = async function (arrOrders, status) {
|
||||||
|
|
||||||
for (const order of arrOrders) {
|
for (const order of arrOrders) {
|
||||||
const ret = await this.findOneAndUpdate({ _id: order._id }, { $set: status });
|
const ret = await this.findOneAndUpdate({ _id: order._id }, { $set: status });
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.updateStatusOrdersElements = async function (arrOrders, myelements) {
|
module.exports.updateStatusOrdersElements = async function (arrOrders, myelements) {
|
||||||
|
|
||||||
for (const order of arrOrders) {
|
for (const order of arrOrders) {
|
||||||
const ret = await this.findOneAndUpdate({ _id: order._id }, { $set: myelements });
|
const ret = await this.findOneAndUpdate({ _id: order._id }, { $set: myelements });
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.getTotalOrderById = async function (id) {
|
module.exports.getTotalOrderById = async function (id) {
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ const shared_consts = require('../tools/shared_nodejs');
|
|||||||
const Order = require('../models/order');
|
const Order = require('../models/order');
|
||||||
var { User } = require('../models/user');
|
var { User } = require('../models/user');
|
||||||
|
|
||||||
const Storehouse = require('../models/storehouse');
|
const Storehouse = require('../models/storehouse');
|
||||||
const Provider = require('../models/provider');
|
const Provider = require('../models/provider');
|
||||||
const Product = require('../models/product');
|
const Product = require('../models/product');
|
||||||
|
|
||||||
const tools = require('../tools/general');
|
const tools = require('../tools/general');
|
||||||
@@ -35,7 +35,7 @@ const OrdersCartSchema = new Schema({
|
|||||||
type: Number,
|
type: Number,
|
||||||
Default: 0,
|
Default: 0,
|
||||||
},
|
},
|
||||||
evaso: { // e quindi è stato tolto dal magazzino (aggiornando il campo StockQty)
|
evaso: { // e quindi è stato tolto dal magazzino (aggiornando il campo stockQty)
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: false,
|
default: false,
|
||||||
},
|
},
|
||||||
@@ -141,10 +141,10 @@ module.exports.getStatusCartByUserId = async function (uid, idapp, numorder) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports.getRecCartByUserId = async function (uid, idapp, numorder) {
|
module.exports.getRecCartByUserId = async function (uid, idapp, numorder) {
|
||||||
let query = { userId: uid, idapp, status: { $gte: shared_consts.OrderStatus.CHECKOUT_SENT } }
|
let query = { userId: uid, idapp, status: { $lt: shared_consts.OrderStatus.CHECKOUT_SENT } }
|
||||||
let myorderscart = null;
|
let myorderscart = null;
|
||||||
if (numorder > 0) {
|
if (numorder > 0) {
|
||||||
query = { userId: uid, idapp, numorder, status: { $gte: shared_consts.OrderStatus.CHECKOUT_SENT } }
|
query = { userId: uid, idapp, numorder, status: { $lt: shared_consts.OrderStatus.CHECKOUT_SENT } }
|
||||||
}
|
}
|
||||||
|
|
||||||
myorderscart = await OrdersCart.findOne(query).lean();
|
myorderscart = await OrdersCart.findOne(query).lean();
|
||||||
@@ -160,171 +160,91 @@ module.exports.getOrdersCartById = async function (id) {
|
|||||||
}
|
}
|
||||||
module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder) {
|
module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder) {
|
||||||
|
|
||||||
let query = { idapp, status: { $gte: shared_consts.OrderStatus.CHECKOUT_SENT }, deleted: false }
|
try {
|
||||||
let myorderscart = null;
|
let query = { idapp, status: { $gte: shared_consts.OrderStatus.CHECKOUT_SENT }, deleted: false }
|
||||||
if (numorder > 0) {
|
let myorderscart = null;
|
||||||
query.numorder = numorder;
|
if (numorder > 0) {
|
||||||
}
|
query.numorder = numorder;
|
||||||
|
}
|
||||||
|
|
||||||
if (uid !== 'ALL') {
|
if (uid !== 'ALL') {
|
||||||
query.userId = uid;
|
query.userId = uid;
|
||||||
}
|
}
|
||||||
|
|
||||||
myorderscart = await OrdersCart.find(query).populate('items.order')
|
/*transform: function(doc, populated) {
|
||||||
.populate({
|
// Rinomina 'idProduct' a 'product' nei risultati della popolazione
|
||||||
path: 'items.order',
|
populated.product = populated.idProduct;
|
||||||
populate: {
|
delete populated.idProduct;
|
||||||
path: 'idProduct',
|
return populated;
|
||||||
model: 'Product'
|
},*/
|
||||||
},
|
|
||||||
/*transform: function(doc, populated) {
|
myorderscart = await OrdersCart.find(query)
|
||||||
// Rinomina 'idProduct' a 'product' nei risultati della popolazione
|
.populate('items.order')
|
||||||
populated.product = populated.idProduct;
|
.populate({
|
||||||
delete populated.idProduct;
|
path: 'items.order',
|
||||||
return populated;
|
populate: {
|
||||||
},*/
|
path: 'idProduct',
|
||||||
})
|
model: 'Product'
|
||||||
.populate({
|
},
|
||||||
path: 'items.order',
|
})
|
||||||
populate: {
|
.populate({
|
||||||
path: 'idProducer',
|
path: 'items.order',
|
||||||
model: 'Producer'
|
populate: {
|
||||||
},
|
path: 'idProducer',
|
||||||
})
|
model: 'Producer'
|
||||||
.populate({
|
},
|
||||||
path: 'items.order',
|
})
|
||||||
populate: {
|
.populate({
|
||||||
path: 'idProvider',
|
path: 'items.order',
|
||||||
model: 'Provider'
|
populate: {
|
||||||
},
|
path: 'idProvider',
|
||||||
})
|
model: 'Provider'
|
||||||
.populate({
|
},
|
||||||
path: 'items.order',
|
})
|
||||||
populate: {
|
.populate({
|
||||||
path: 'idStorehouse',
|
path: 'items.order',
|
||||||
model: 'Storehouse'
|
populate: {
|
||||||
},
|
path: 'idStorehouse',
|
||||||
/*transform: function(doc, populated) {
|
model: 'Storehouse'
|
||||||
// Rinomina 'idProduct' a 'product' nei risultati della popolazione
|
},
|
||||||
populated.storehouse = populated.idStorehouse;
|
})
|
||||||
delete populated.idStorehouse;
|
.populate({
|
||||||
return populated;
|
path: 'userId',
|
||||||
},*/
|
model: 'User',
|
||||||
})
|
select: '_id name surname username profile', // Specify the fields you want to retrieve
|
||||||
.populate('userId').lean();
|
})
|
||||||
|
.lean();
|
||||||
|
|
||||||
myorderscart = myorderscart.map(order => {
|
myorderscart = myorderscart.map(order => {
|
||||||
|
order.user = order.userId
|
||||||
|
order.userId = order.user._id
|
||||||
order.items = order.items.map(item => {
|
order.items = order.items.map(item => {
|
||||||
item.order.product = item.order.idProduct;
|
if (item.order) {
|
||||||
delete item.order.idProduct;
|
try {
|
||||||
item.order.producer = item.order.idProducer;
|
item.order.product = item.order.idProduct;
|
||||||
delete item.order.idProducer;
|
item.order.idProduct = item.order.product._id;
|
||||||
item.order.storehouse = item.order.idStorehouse;
|
item.order.producer = item.order.idProducer;
|
||||||
delete item.order.idStorehouse;
|
item.order.idProducer = item.order.producer._id;
|
||||||
item.order.provider = item.order.idProvider;
|
item.order.storehouse = item.order.idStorehouse;
|
||||||
delete item.order.idProvider;
|
item.order.idStorehouse = item.order.storehouse._id;
|
||||||
|
item.order.provider = item.order.idProvider;
|
||||||
|
item.order.idProvider = item.order.provider._id;
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Err: ', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
return item;
|
return item;
|
||||||
});
|
});
|
||||||
return order;
|
return order;
|
||||||
});
|
});
|
||||||
|
|
||||||
/*for (let ind = 0; ind < myorderscart.length; ind++) {
|
return myorderscart
|
||||||
for (const idkey in myorderscart[ind].items) {
|
} catch (e) {
|
||||||
try {
|
console.error('Err:', e);
|
||||||
let idorder = myorderscart[ind].items[idkey]._id.toString();
|
}
|
||||||
const myorder = myorderscart[ind].items[idkey].order;
|
|
||||||
if (!!myorder) {
|
|
||||||
idorder = myorderscart[ind].items[idkey].order._id.toString();
|
|
||||||
}
|
|
||||||
// myorderscart[ind].nameSurname = await User.getNameSurnameById(idapp, myorderscart[ind].userId);
|
|
||||||
// const myord = await Order.getTotalOrderById(idorder);
|
|
||||||
// if (myord.length > 0) {
|
|
||||||
// myorderscart[ind].items[idkey].order = myord[0];
|
|
||||||
// }
|
|
||||||
} catch (e) {
|
|
||||||
console.log('err', e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/* if (!!mycart) {
|
|
||||||
for (const idkey in mycart.items) {
|
|
||||||
try {
|
|
||||||
idorder = mycart.items[idkey]._id.toString();
|
|
||||||
const myorder = mycart.items[idkey].order;
|
|
||||||
if (!!myorder) {
|
|
||||||
idorder = mycart.items[idkey].order._id.toString();
|
|
||||||
}
|
|
||||||
const myord = await Order.getTotalOrderById(idorder);
|
|
||||||
if (myord.length > 0) {
|
|
||||||
mycart.items[idkey]._doc.order = myord[0];
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.log('err', e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return mycart;
|
|
||||||
}*/
|
|
||||||
return myorderscart
|
|
||||||
// return null;
|
// return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.getOrdersCartByDepartmentId = async function (depId, idapp) {
|
|
||||||
let query = {
|
|
||||||
idapp, status: { $gte: shared_consts.OrderStatus.CHECKOUT_SENT },
|
|
||||||
deleted: false,
|
|
||||||
}
|
|
||||||
const myorderscart = await OrdersCart.find(query).lean();
|
|
||||||
|
|
||||||
for (let ind = 0; ind < myorderscart.length; ind++) {
|
|
||||||
for (const idkey in myorderscart[ind].items) {
|
|
||||||
try {
|
|
||||||
let idorder = myorderscart[ind].items[idkey]._id.toString();
|
|
||||||
const myorder = myorderscart[ind].items[idkey].order;
|
|
||||||
if (!!myorder) {
|
|
||||||
idorder = myorderscart[ind].items[idkey].order._id.toString();
|
|
||||||
}
|
|
||||||
myorderscart[ind].nameSurname = await User.getNameSurnameById(idapp, myorderscart[ind].userId);
|
|
||||||
const myord = await Order.getTotalOrderById(idorder);
|
|
||||||
if (myord.length > 0) {
|
|
||||||
myorderscart[ind].items[idkey].order = myord[0];
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.log('err', e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return myorderscart
|
|
||||||
// return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports.getOrderById = async function (Id, idapp) {
|
|
||||||
let query = { _id: Id, idapp, status: { $gte: shared_consts.OrderStatus.CHECKOUT_SENT } }
|
|
||||||
const myorderscart = await OrdersCart.find(query).lean();
|
|
||||||
|
|
||||||
for (let ind = 0; ind < myorderscart.length; ind++) {
|
|
||||||
for (const idkey in myorderscart[ind].items) {
|
|
||||||
try {
|
|
||||||
let idorder = myorderscart[ind].items[idkey]._id.toString();
|
|
||||||
const myorder = myorderscart[ind].items[idkey].order;
|
|
||||||
if (!!myorder) {
|
|
||||||
idorder = myorderscart[ind].items[idkey].order._id.toString();
|
|
||||||
}
|
|
||||||
myorderscart[ind].nameSurname = await User.getNameSurnameById(idapp, myorderscart[ind].userId);
|
|
||||||
const myord = await Order.getTotalOrderById(idorder);
|
|
||||||
if (myord.length > 0) {
|
|
||||||
myorderscart[ind].items[idkey].order = myord[0];
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.log('err', e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return myorderscart
|
|
||||||
// return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports.updateOrdersCartById = function (id, newOrdersCart, callback) {
|
module.exports.updateOrdersCartById = function (id, newOrdersCart, callback) {
|
||||||
let query = {
|
let query = {
|
||||||
@@ -362,23 +282,30 @@ module.exports.updateOrdersCartById = function (id, newOrdersCart, callback) {
|
|||||||
|
|
||||||
module.exports.setFieldInOrdersById = async function (objtoset, myOrderCart) {
|
module.exports.setFieldInOrdersById = async function (objtoset, myOrderCart) {
|
||||||
|
|
||||||
const ris = await OrdersCart.findOneAndUpdate(
|
try {
|
||||||
{ _id: myOrderCart._id },
|
let ris2 = null;
|
||||||
{
|
// Imposta su tutti i singoli prodotti ordinati (Order)
|
||||||
$set: objtoset
|
for (const recitem of myOrderCart.items) {
|
||||||
},
|
ris2 = await Order.findOneAndUpdate(
|
||||||
{ new: false }
|
{ _id: recitem.order._id },
|
||||||
)
|
{
|
||||||
|
$set: objtoset
|
||||||
|
},
|
||||||
|
{ new: false }
|
||||||
|
)
|
||||||
|
// console.log('ris', ris2);
|
||||||
|
}
|
||||||
|
|
||||||
// Imposta su tutti i singoli prodotti ordinati (Order)
|
const ris = await OrdersCart.findOneAndUpdate(
|
||||||
for (const order of myOrderCart.items) {
|
{ _id: myOrderCart._id },
|
||||||
await Order.findOneAndUpdate(
|
|
||||||
{ _id: order._id },
|
|
||||||
{
|
{
|
||||||
$set: objtoset
|
$set: objtoset
|
||||||
},
|
},
|
||||||
{ new: false }
|
{ new: false }
|
||||||
)
|
)
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
console.log('Err', e);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -463,7 +390,6 @@ module.exports.updateStockQtaDalMagazzino = async function (idorderscart) {
|
|||||||
|
|
||||||
if (myorderscart) {
|
if (myorderscart) {
|
||||||
for (const idkey in myorderscart.items) {
|
for (const idkey in myorderscart.items) {
|
||||||
let idorder = myorderscart.items[idkey]._id;
|
|
||||||
let order = myorderscart.items[idkey].order;
|
let order = myorderscart.items[idkey].order;
|
||||||
|
|
||||||
if (!order.evaso) {
|
if (!order.evaso) {
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ const Schema = mongoose.Schema;
|
|||||||
const tools = require('../tools/general');
|
const tools = require('../tools/general');
|
||||||
|
|
||||||
const Producer = require('../models/producer');
|
const Producer = require('../models/producer');
|
||||||
const Storehouse = require('../models/storehouse');
|
const Storehouse = require('../models/storehouse');
|
||||||
const Provider = require('../models/provider');
|
const Provider = require('../models/provider');
|
||||||
|
|
||||||
const shared_consts = require('../tools/shared_nodejs');
|
const shared_consts = require('../tools/shared_nodejs');
|
||||||
|
|
||||||
@@ -92,10 +92,6 @@ const productSchema = new Schema({
|
|||||||
type: Number,
|
type: Number,
|
||||||
default: 0,
|
default: 0,
|
||||||
},
|
},
|
||||||
quantityAvailable: {
|
|
||||||
type: Number,
|
|
||||||
default: 0,
|
|
||||||
},
|
|
||||||
quantityLow: { //Soglia disponibilità bassa
|
quantityLow: { //Soglia disponibilità bassa
|
||||||
type: Number,
|
type: Number,
|
||||||
default: 0,
|
default: 0,
|
||||||
@@ -167,88 +163,124 @@ module.exports.executeQueryTable = function (idapp, params) {
|
|||||||
return tools.executeQueryTable(this, idapp, params);
|
return tools.executeQueryTable(this, idapp, params);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.findAllIdApp = async function (idapp, code) {
|
module.exports.getProductByCode = function (idapp, code) {
|
||||||
let myfind = { idapp, active: true };
|
return Product.findAllIdApp(idapp, code);
|
||||||
|
}
|
||||||
|
|
||||||
if (code) {
|
module.exports.getProductById = async function (id) {
|
||||||
myfind = { ...myfind, code }
|
const arrris = await Product.findAllIdApp('', '', id);
|
||||||
}
|
return arrris && arrris.length > 0 ? arrris[0] : null
|
||||||
|
}
|
||||||
|
|
||||||
// return await Product.find(myfind);
|
module.exports.findAllIdApp = async function (idapp, code, id) {
|
||||||
|
let myfind = {};
|
||||||
|
let myqueryadd = {};
|
||||||
|
let query = [];
|
||||||
|
|
||||||
const query = [
|
try {
|
||||||
{ $match: myfind },
|
|
||||||
{
|
if (idapp)
|
||||||
$lookup: {
|
myfind = { idapp, active: true };
|
||||||
from: 'producers',
|
|
||||||
localField: 'idProducer',
|
if (code) {
|
||||||
foreignField: '_id',
|
myfind = { ...myfind, code }
|
||||||
as: 'producer'
|
}
|
||||||
|
if (id) {
|
||||||
|
myqueryadd = {
|
||||||
|
$addFields: {
|
||||||
|
myId1: {
|
||||||
|
$toObjectId: id,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
},
|
myfind = {
|
||||||
{ $unwind: '$producer' },
|
$expr: {
|
||||||
{
|
$eq: ["$_id", "$myId1"],
|
||||||
$lookup: {
|
},
|
||||||
from: 'providers',
|
|
||||||
localField: 'idProvider',
|
|
||||||
foreignField: '_id',
|
|
||||||
as: 'provider'
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
{ $unwind: '$provider' },
|
query.push(myqueryadd);
|
||||||
{
|
}
|
||||||
$lookup: {
|
|
||||||
from: 'storehouses',
|
// return await Product.find(myfind);
|
||||||
localField: 'idStorehouses',
|
|
||||||
foreignField: '_id',
|
query.push(
|
||||||
as: 'storehouses'
|
{ $match: myfind },
|
||||||
}
|
{
|
||||||
},
|
$lookup: {
|
||||||
{
|
from: 'producers',
|
||||||
$lookup: {
|
localField: 'idProducer',
|
||||||
from: 'orders',
|
foreignField: '_id',
|
||||||
let: { productId: '$_id' },
|
as: 'producer'
|
||||||
pipeline: [
|
}
|
||||||
{
|
},
|
||||||
$match: {
|
{ $unwind: '$producer' },
|
||||||
$expr: {
|
{
|
||||||
$and: [
|
$lookup: {
|
||||||
{ $eq: ['$idProduct', '$$productId'] },
|
from: 'providers',
|
||||||
{ $lt: ['$status', shared_consts.OrderStatus.ORDER_CONFIRMED] }
|
localField: 'idProvider',
|
||||||
]
|
foreignField: '_id',
|
||||||
|
as: 'provider'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ $unwind: '$provider' },
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: 'storehouses',
|
||||||
|
localField: 'idStorehouses',
|
||||||
|
foreignField: '_id',
|
||||||
|
as: 'storehouses'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$lookup: {
|
||||||
|
from: 'orders',
|
||||||
|
let: { productId: '$_id' },
|
||||||
|
pipeline: [
|
||||||
|
{
|
||||||
|
$match: {
|
||||||
|
$expr: {
|
||||||
|
$and: [
|
||||||
|
{ $eq: ['$idProduct', '$$productId'] },
|
||||||
|
{ $lt: ['$status', shared_consts.OrderStatus.ORDER_CONFIRMED] }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
$group: {
|
||||||
|
_id: null,
|
||||||
|
totalQty: { $sum: '$quantity' }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
],
|
||||||
{
|
as: 'productOrders'
|
||||||
$group: {
|
}
|
||||||
_id: null,
|
},
|
||||||
totalQty: { $sum: '$quantity' }
|
{
|
||||||
|
$addFields: {
|
||||||
|
QuantitaOrdinateInAttesa: {
|
||||||
|
$cond: {
|
||||||
|
if: { $isArray: '$productOrders' },
|
||||||
|
then: { $arrayElemAt: ['$productOrders.totalQty', 0] },
|
||||||
|
else: 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
|
||||||
as: 'productOrders'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$addFields: {
|
|
||||||
QuantitaOrdinateInAttesa: {
|
|
||||||
$cond: {
|
|
||||||
if: { $isArray: '$productOrders' },
|
|
||||||
then: { $arrayElemAt: ['$productOrders.totalQty', 0] },
|
|
||||||
else: 0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
},
|
{
|
||||||
{
|
$unset: 'productOrders'
|
||||||
$unset: 'productOrders'
|
},
|
||||||
},
|
|
||||||
|
|
||||||
];
|
);
|
||||||
|
|
||||||
let ris = await Product.aggregate(query)
|
let ris = await Product.aggregate(query)
|
||||||
|
|
||||||
return ris;
|
return ris;
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
console.error('E', e);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
@@ -269,10 +301,6 @@ module.exports.getProductByTitle = function (query, sort, callback) {
|
|||||||
Product.find(query, null, sort, callback)
|
Product.find(query, null, sort, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.getProductByCode = function (idapp, code) {
|
|
||||||
return Product.findOne({ idapp, code })
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports.filterProductByDepartment = function (department, callback) {
|
module.exports.filterProductByDepartment = function (department, callback) {
|
||||||
let regexp = new RegExp(`^${department}$`, 'i')
|
let regexp = new RegExp(`^${department}$`, 'i')
|
||||||
var query = { department: { $regex: regexp } };
|
var query = { department: { $regex: regexp } };
|
||||||
@@ -301,7 +329,7 @@ module.exports.createIndexes((err) => {
|
|||||||
|
|
||||||
module.exports.convertAfterImport = async function (idapp, dataObjects) {
|
module.exports.convertAfterImport = async function (idapp, dataObjects) {
|
||||||
|
|
||||||
const arrprod = await Product.find({idapp}).lean();
|
const arrprod = await Product.find({ idapp }).lean();
|
||||||
for (const prod of arrprod) {
|
for (const prod of arrprod) {
|
||||||
let setta = false;
|
let setta = false;
|
||||||
|
|
||||||
|
|||||||
@@ -55,20 +55,27 @@ router.post('/:userId', authenticate, async function (req, res, next) {
|
|||||||
let order = req.body.order;
|
let order = req.body.order;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const mycart = await Cart.getCartByUserId(userId, idapp);
|
let mycart = await Cart.getCartByUserId(userId, idapp);
|
||||||
|
|
||||||
// const myorder = Order.getOrderByID(order._id);
|
// const myorder = Order.getOrderByID(order._id);
|
||||||
if (!addqty && !subqty)
|
if (!addqty && !subqty)
|
||||||
order._id = await Order.createOrder(order);
|
order._id = await Order.createOrder(order);
|
||||||
|
|
||||||
let cart = null;
|
let cart = null;
|
||||||
|
let product = null;
|
||||||
// no cart save empty cart to database then return response
|
// no cart save empty cart to database then return response
|
||||||
let myqty = 0;
|
let myqty = 0;
|
||||||
|
let nuovo = false;
|
||||||
if (!mycart) {
|
if (!mycart) {
|
||||||
let oldCart = new CartClass(order)
|
let oldCart = new CartClass(order)
|
||||||
cart = await Cart.createCart(oldCart.generateModel());
|
cart = await Cart.createCart(oldCart.generateModel());
|
||||||
} else {
|
|
||||||
let newCart = CartClass.constructByCart(mycart);
|
mycart = await Cart.getCartByUserId(userId, idapp);
|
||||||
|
nuovo = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
let newCart = CartClass.constructByCart(mycart);
|
||||||
|
if (!nuovo) {
|
||||||
if (addqty) {
|
if (addqty) {
|
||||||
myqty = await newCart.addqty(order);
|
myqty = await newCart.addqty(order);
|
||||||
} else if (subqty) {
|
} else if (subqty) {
|
||||||
@@ -76,11 +83,19 @@ router.post('/:userId', authenticate, async function (req, res, next) {
|
|||||||
} else {
|
} else {
|
||||||
const ind = newCart.addItem(order);
|
const ind = newCart.addItem(order);
|
||||||
}
|
}
|
||||||
cart = await Cart.updateCartByCartId(mycart._id, newCart.generateModel());
|
} else {
|
||||||
|
await newCart.updatetotals();
|
||||||
}
|
}
|
||||||
|
cart = await Cart.updateCartByCartId(mycart._id, newCart.generateModel());
|
||||||
|
|
||||||
|
|
||||||
if (cart) {
|
if (cart) {
|
||||||
const carttot = await Cart.getCartByUserId(userId, idapp);
|
const carttot = await Cart.getCartByUserId(userId, idapp);
|
||||||
return res.send({ code: server_constants.RIS_CODE_OK, cart: carttot, qty: myqty });
|
if (order.idProduct)
|
||||||
|
product = await Product.getProductById(order.idProduct);
|
||||||
|
else if (order.product)
|
||||||
|
product = await Product.getProductById(order.product._id);
|
||||||
|
return res.send({ code: server_constants.RIS_CODE_OK, cart: carttot, qty: myqty, product });
|
||||||
} else {
|
} else {
|
||||||
return res.send({ code: server_constants.RIS_CODE_ERR, cart: null });
|
return res.send({ code: server_constants.RIS_CODE_ERR, cart: null });
|
||||||
}
|
}
|
||||||
@@ -112,6 +127,12 @@ router.delete('/:userId', authenticate, async function (req, res) {
|
|||||||
|
|
||||||
const mycart = await Cart.getCartByUserId(userId, idapp);
|
const mycart = await Cart.getCartByUserId(userId, idapp);
|
||||||
|
|
||||||
|
const ord = await Order.findOne({ _id: orderId });
|
||||||
|
let idProduct = ''
|
||||||
|
let product = null;
|
||||||
|
if (ord)
|
||||||
|
idProduct = ord.idProduct;
|
||||||
|
|
||||||
// Rimuovere l'Ordine
|
// Rimuovere l'Ordine
|
||||||
const recremoved = await Order.deleteOne({ _id: orderId });
|
const recremoved = await Order.deleteOne({ _id: orderId });
|
||||||
if (recremoved) {
|
if (recremoved) {
|
||||||
@@ -124,8 +145,12 @@ router.delete('/:userId', authenticate, async function (req, res) {
|
|||||||
|
|
||||||
carttot = await Cart.getCartByUserId(userId, idapp);
|
carttot = await Cart.getCartByUserId(userId, idapp);
|
||||||
|
|
||||||
|
if (idProduct) {
|
||||||
|
product = await Product.getProductById(idProduct);
|
||||||
|
}
|
||||||
|
|
||||||
console.log('carttot', carttot)
|
console.log('carttot', carttot)
|
||||||
return res.send({ code: server_constants.RIS_CODE_OK, cart: carttot });
|
return res.send({ code: server_constants.RIS_CODE_OK, cart: carttot, product });
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.send({ code: server_constants.RIS_CODE_ERR, cart: null });
|
return res.send({ code: server_constants.RIS_CODE_ERR, cart: null });
|
||||||
@@ -140,17 +165,17 @@ router.put('/:userId', authenticate, async function (req, res, next) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
await Cart.getCartByUserId(userId, function (err, c) {
|
await Cart.getCartByUserId(userId, async function (err, c) {
|
||||||
if (err) return next(err)
|
if (err) return next(err)
|
||||||
let oldCart = new CartClass(c[0] || {})
|
let oldCart = new CartClass(c || {})
|
||||||
|
|
||||||
Product.getProductByID(productId, function (err, p) {
|
await Product.getProductByID(productId, async function (err, p) {
|
||||||
if (err) return next(err)
|
if (err) return next(err)
|
||||||
let newCart = oldCart.add(p, productId, { color, size })
|
let newCart = oldCart.add(p, productId, { color, size })
|
||||||
|
|
||||||
//exist cart in databse
|
//exist cart in databse
|
||||||
if (c.length > 0) {
|
if (c.length > 0) {
|
||||||
Cart.updateCartByUserId(
|
await Cart.updateCartByUserId(
|
||||||
userId,
|
userId,
|
||||||
{
|
{
|
||||||
items: newCart.items,
|
items: newCart.items,
|
||||||
@@ -164,13 +189,13 @@ router.put('/:userId', authenticate, async function (req, res, next) {
|
|||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
//no cart in database
|
//no cart in database
|
||||||
newCart = new Cart({
|
let newCartobj = {
|
||||||
items: newCart.items,
|
items: newCart.items,
|
||||||
totalQty: newCart.totalQty,
|
totalQty: newCart.totalQty,
|
||||||
totalPrice: newCart.totalPrice,
|
totalPrice: newCart.totalPrice,
|
||||||
userId: userId
|
userId: userId
|
||||||
})
|
}
|
||||||
Cart.createCart(newCart, function (err, resultCart) {
|
await Cart.createCart(newCartobj, function (err, resultCart) {
|
||||||
if (err) return next(err)
|
if (err) return next(err)
|
||||||
res.status(201).json(resultCart)
|
res.status(201).json(resultCart)
|
||||||
})
|
})
|
||||||
@@ -178,7 +203,9 @@ router.put('/:userId', authenticate, async function (req, res, next) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
return res.send({ code: server_constants.RIS_CODE_OK });
|
const product = await Product.getProductById(productId);
|
||||||
|
|
||||||
|
return res.send({ code: server_constants.RIS_CODE_OK, product });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return res.send({ code: server_constants.RIS_CODE_ERR, status: 0 });
|
return res.send({ code: server_constants.RIS_CODE_ERR, status: 0 });
|
||||||
}
|
}
|
||||||
@@ -192,6 +219,7 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
|
|||||||
let userId = req.params.userId;
|
let userId = req.params.userId;
|
||||||
const user = req.user;
|
const user = req.user;
|
||||||
let status = req.body.status;
|
let status = req.body.status;
|
||||||
|
let note = req.body.note;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const mycart = await Cart.findOne({ _id: cart_id });
|
const mycart = await Cart.findOne({ _id: cart_id });
|
||||||
@@ -213,7 +241,7 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
|
|||||||
totalPrice: mycart.totalPrice,
|
totalPrice: mycart.totalPrice,
|
||||||
userId,
|
userId,
|
||||||
status,
|
status,
|
||||||
note: mycart.note,
|
note,
|
||||||
numorder,
|
numorder,
|
||||||
created_at: new Date(),
|
created_at: new Date(),
|
||||||
modify_at: new Date(),
|
modify_at: new Date(),
|
||||||
@@ -221,6 +249,8 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
|
|||||||
}
|
}
|
||||||
statusOrderCart = myorderCart.status;
|
statusOrderCart = myorderCart.status;
|
||||||
|
|
||||||
|
const idordercart = myorderCart._id;
|
||||||
|
|
||||||
if (!!mycart) {
|
if (!!mycart) {
|
||||||
if (status === shared_consts.OrderStatus.CHECKOUT_SENT) {
|
if (status === shared_consts.OrderStatus.CHECKOUT_SENT) {
|
||||||
|
|
||||||
@@ -246,7 +276,7 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
|
|||||||
// Invia la email dell'Ordine
|
// Invia la email dell'Ordine
|
||||||
sendemail.sendEmail_OrderProduct(user.lang, idapp, orders[0], user)
|
sendemail.sendEmail_OrderProduct(user.lang, idapp, orders[0], user)
|
||||||
.then(async (ris) => {
|
.then(async (ris) => {
|
||||||
myorderCart = await OrdersCart.getRecCartByUserId(userId, idapp, numorder);
|
myorderCart = await OrdersCart.findById(idordercart).lean();
|
||||||
return res.send({
|
return res.send({
|
||||||
code: server_constants.RIS_CODE_OK,
|
code: server_constants.RIS_CODE_OK,
|
||||||
status: myris.status,
|
status: myris.status,
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ var { authenticate, auth_default } = require('../middleware/authenticate');
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
const Product = require('../models/product');
|
const Product = require('../models/product');
|
||||||
|
const OrdersCart = require('../models/orderscart');
|
||||||
const Variant = require('../models/variant');
|
const Variant = require('../models/variant');
|
||||||
|
|
||||||
/*const Department = require('../models/Department')
|
/*const Department = require('../models/Department')
|
||||||
@@ -27,11 +28,13 @@ const Cart = require('../models/cart');
|
|||||||
//GET /products
|
//GET /products
|
||||||
router.post('/', auth_default, async function (req, res, next) {
|
router.post('/', auth_default, async function (req, res, next) {
|
||||||
const idapp = req.body.idapp;
|
const idapp = req.body.idapp;
|
||||||
|
let userId = req.body.userId;
|
||||||
|
|
||||||
var products = await Product.findAllIdApp(idapp, "");
|
var products = await Product.findAllIdApp(idapp, "");
|
||||||
|
var orders = await OrdersCart.getOrdersCartByUserId(userId, idapp, 0);
|
||||||
|
|
||||||
if (products)
|
if (products)
|
||||||
res.send({ code: server_constants.RIS_CODE_OK, products });
|
res.send({ code: server_constants.RIS_CODE_OK, products, orders });
|
||||||
else
|
else
|
||||||
res.status(400).send(e);
|
res.status(400).send(e);
|
||||||
|
|
||||||
|
|||||||
@@ -742,57 +742,76 @@ module.exports = {
|
|||||||
|
|
||||||
sendEmail_OrderProduct: async function (lang, idapp, orders, user) {
|
sendEmail_OrderProduct: async function (lang, idapp, orders, user) {
|
||||||
|
|
||||||
const msginizio = 'Ordine n: ' + orders.numorder + ' ' + user.name + ' ' + user.surname;
|
try {
|
||||||
console.log(msginizio);
|
const msginizio = 'Ordine n: ' + orders.numorder + ' ' + user.name + ' ' + user.surname;
|
||||||
|
console.log(msginizio);
|
||||||
|
|
||||||
await telegrambot.sendMsgTelegramToTheManagers(idapp, msginizio);
|
if (process.env.SEND_EMAIL_ORDERS === '1') {
|
||||||
|
|
||||||
let mylocalsconf = {
|
await telegrambot.sendMsgTelegramToTheManagers(idapp, msginizio);
|
||||||
idapp,
|
|
||||||
locale: lang,
|
|
||||||
nomeapp: tools.getNomeAppByIdApp(idapp),
|
|
||||||
orders,
|
|
||||||
baseurl: tools.getHostByIdApp(idapp),
|
|
||||||
dataemail: await this.getdataemail(idapp),
|
|
||||||
ordernumber: orders.numorder,
|
|
||||||
user,
|
|
||||||
};
|
|
||||||
|
|
||||||
mylocalsconf = this.setParamsForTemplate(user, mylocalsconf);
|
let mylocalsconf = {
|
||||||
|
idapp,
|
||||||
|
locale: lang,
|
||||||
|
nomeapp: tools.getNomeAppByIdApp(idapp),
|
||||||
|
orders,
|
||||||
|
baseurl: tools.getHostByIdApp(idapp),
|
||||||
|
dataemail: await this.getdataemail(idapp),
|
||||||
|
ordernumber: orders.numorder,
|
||||||
|
user,
|
||||||
|
};
|
||||||
|
|
||||||
this.sendEmail_base_e_manager(idapp, 'ecommerce/makeorder/' + lang, mylocalsconf.emailto, mylocalsconf,
|
mylocalsconf = this.setParamsForTemplate(user, mylocalsconf);
|
||||||
mylocalsconf.dataemail.email_reply);
|
|
||||||
|
this.sendEmail_base_e_manager(idapp, 'ecommerce/makeorder/' + lang, mylocalsconf.emailto, mylocalsconf,
|
||||||
|
mylocalsconf.dataemail.email_reply);
|
||||||
|
} else {
|
||||||
|
console.log('Invio Email non eseguito perchè sei in TEST !');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Err:', e);
|
||||||
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
sendEmail_Order: async function (lang, idapp, orders, user, ordertype, status) {
|
sendEmail_Order: async function (lang, idapp, orders, user, ordertype, status) {
|
||||||
|
|
||||||
const msginizio = 'INIZIO - sendEmail_Order ' + ordertype + ': ' + tools.getNomeAppByIdApp(idapp);
|
try {
|
||||||
console.log(msginizio);
|
const msginizio = 'INIZIO - sendEmail_Order ' + ordertype + ': ' + tools.getNomeAppByIdApp(idapp);
|
||||||
|
console.log(msginizio);
|
||||||
|
|
||||||
await telegrambot.sendMsgTelegramToTheManagers(idapp, msginizio);
|
if (process.env.SEND_EMAIL_ORDERS === '1') {
|
||||||
|
|
||||||
let mylocalsconf = {
|
await telegrambot.sendMsgTelegramToTheManagers(idapp, msginizio);
|
||||||
idapp,
|
|
||||||
locale: lang,
|
|
||||||
nomeapp: tools.getNomeAppByIdApp(idapp),
|
|
||||||
orders,
|
|
||||||
baseurl: tools.getHostByIdApp(idapp),
|
|
||||||
dataemail: await this.getdataemail(idapp),
|
|
||||||
ordernumber: orders.numorder,
|
|
||||||
user,
|
|
||||||
};
|
|
||||||
|
|
||||||
mylocalsconf = this.setParamsForTemplate(user, mylocalsconf);
|
let mylocalsconf = {
|
||||||
|
idapp,
|
||||||
|
locale: lang,
|
||||||
|
nomeapp: tools.getNomeAppByIdApp(idapp),
|
||||||
|
orders,
|
||||||
|
baseurl: tools.getHostByIdApp(idapp),
|
||||||
|
dataemail: await this.getdataemail(idapp),
|
||||||
|
ordernumber: orders.numorder,
|
||||||
|
user,
|
||||||
|
};
|
||||||
|
|
||||||
if ((status !== shared_consts.OrderStatus.CANCELED) && (status !== shared_consts.OrderStatus.COMPLETED)) {
|
mylocalsconf = this.setParamsForTemplate(user, mylocalsconf);
|
||||||
const esito = this.sendEmail_base('ecommerce/' + ordertype + '/' + lang, mylocalsconf.emailto, mylocalsconf,
|
|
||||||
mylocalsconf.dataemail.email_reply);
|
|
||||||
|
|
||||||
this.sendEmail_base('ecommerce/' + ordertype + '/' + lang, tools.getAdminEmailByIdApp(idapp), mylocalsconf, '');
|
if ((status !== shared_consts.OrderStatus.CANCELED) && (status !== shared_consts.OrderStatus.COMPLETED)) {
|
||||||
|
const esito = this.sendEmail_base('ecommerce/' + ordertype + '/' + lang, mylocalsconf.emailto, mylocalsconf,
|
||||||
|
mylocalsconf.dataemail.email_reply);
|
||||||
|
|
||||||
if (tools.isManagAndAdminDifferent(idapp)) {
|
this.sendEmail_base('ecommerce/' + ordertype + '/' + lang, tools.getAdminEmailByIdApp(idapp), mylocalsconf, '');
|
||||||
this.sendEmail_base('ecommerce/' + ordertype + '/' + lang, tools.getManagerEmailByIdApp(idapp), mylocalsconf, '');
|
|
||||||
|
if (tools.isManagAndAdminDifferent(idapp)) {
|
||||||
|
this.sendEmail_base('ecommerce/' + ordertype + '/' + lang, tools.getManagerEmailByIdApp(idapp), mylocalsconf, '');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log('Invio Email non eseguito perchè sei in TEST !');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Err:', e);
|
||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user