lista ordini aggiornata

This commit is contained in:
Surya Paolo
2023-12-15 23:36:43 +01:00
parent 4d9eccd1ae
commit 1ca72118f1
5 changed files with 44 additions and 4 deletions

View File

@@ -19,6 +19,7 @@ const OrdersCartSchema = new Schema({
type: String
},
numorder: { type: Number },
numord_pers: { type: Number },
userId: { type: Schema.Types.ObjectId, ref: 'User' },
totalQty: { type: Number, default: 0 },
totalPrice: { type: Number, default: 0 },
@@ -110,7 +111,21 @@ module.exports.getFieldsForSearch = function () {
};
module.exports.getLastNumOrder = async function (uid, idapp) {
module.exports.getLastNumOrder = async function (idapp) {
let query = { idapp, deleted: false }
let numorder = 100;
let numorderrec = await OrdersCart.find(query).sort({ numorder: -1 }).limit(1);
if (numorderrec && numorderrec.length > 0)
numorder = numorderrec[0].numorder;
else
numorder = 100;
return numorder;
};
module.exports.getLastNumOrdPers = async function (uid, idapp) {
let query = { userId: uid, idapp, deleted: false }
let numorder = 1;
let numorderrec = await OrdersCart.find(query).sort({ numorder: -1 }).limit(1);
@@ -266,6 +281,7 @@ module.exports.updateOrdersCartById = function (id, newOrdersCart, callback) {
userId: userId,
status: newOrdersCart.status,
numorder: newOrdersCart.numorder,
numord_pers: newOrdersCart.numord_pers,
note: newOrdersCart.note,
modify_at: new Date(),
}

View File

@@ -176,6 +176,9 @@ const SiteSchema = new Schema({
info2: { type: String, default: '' },
cell: { type: String, default: '' },
},
ecomm: {
enablePreOrders: { type: Boolean, default: false },
}
});
var Site = module.exports = mongoose.model('Site', SiteSchema);

View File

@@ -625,6 +625,16 @@ UserSchema.statics.isManager = function (perm) {
}
};
UserSchema.statics.isManagerById = async function (id) {
try {
const ris = await User.findOne({ _id: id }, { perm: 1 }).lean();
return ((ris.perm & shared_consts.Permissions.Manager) ===
shared_consts.Permissions.Manager);
} catch (e) {
return false;
}
};
UserSchema.statics.isEditor = function (perm) {
try {
return ((perm & shared_consts.Permissions.Editor) ===

View File

@@ -224,7 +224,8 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
try {
const mycart = await Cart.findOne({ _id: cart_id });
let numorder = await OrdersCart.getLastNumOrder(userId, idapp);
let numorder = await OrdersCart.getLastNumOrder(idapp);
let numord_pers = await OrdersCart.getLastNumOrdPers(userId, idapp);
// Esiste l'ordine ?
let myorderCart = await OrdersCart.getRecCartByUserId(userId, idapp, numorder);
@@ -232,6 +233,7 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
// crea il nuovo numero d'ordine
numorder++;
numord_pers++;
// SE non esiste allora lo creo !
myorderCart = new OrdersCart({
@@ -243,6 +245,7 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
status,
note,
numorder,
numord_pers,
created_at: new Date(),
modify_at: new Date(),
})

View File

@@ -7,6 +7,8 @@ var server_constants = require('../tools/server_constants');
var { Project } = require('../models/project');
const { User } = require('../models/user');
var { authenticate, auth_default } = require('../middleware/authenticate');
const _ = require('lodash');
@@ -30,8 +32,14 @@ router.post('/', auth_default, async function (req, res, next) {
const idapp = req.body.idapp;
let userId = req.body.userId;
var products = await Product.findAllIdApp(idapp, "");
var orders = await OrdersCart.getOrdersCartByUserId(userId, idapp, 0);
let products = await Product.findAllIdApp(idapp, "");
let orders = null;
if (await User.isManagerById(userId)) {
// Prende Tutti gli Ordini !
orders = await OrdersCart.getOrdersCartByUserId('ALL', idapp, 0);
} else {
orders = await OrdersCart.getOrdersCartByUserId(userId, idapp, 0);
}
if (products)
res.send({ code: server_constants.RIS_CODE_OK, products, orders });