aggiornamento visualizzazione Ordini e Carrello

This commit is contained in:
Surya Paolo
2023-12-09 19:38:23 +01:00
parent 023ba26003
commit daacde7455
7 changed files with 68 additions and 32 deletions

View File

@@ -1,9 +1,14 @@
const mongoose = require('mongoose').set('debug', false); const mongoose = require('mongoose').set('debug', false);
const Schema = mongoose.Schema; const Schema = mongoose.Schema;
const shared_consts = require('../tools/shared_nodejs'); mongoose.Promise = global.Promise;
mongoose.level = "F";
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true
});
const Order = require('../models/order');
const CartSchema = new Schema({ const CartSchema = new Schema({
idapp: { idapp: {
@@ -31,10 +36,6 @@ const CartSchema = new Schema({
var Cart = module.exports = mongoose.model('Cart', CartSchema); var Cart = module.exports = mongoose.model('Cart', CartSchema);
Cart.createIndexes((err) => {
if (err) throw err;
});
module.exports.findAllIdApp = async function (idapp, userId) { module.exports.findAllIdApp = async function (idapp, userId) {
const myfind = { idapp, userId }; const myfind = { idapp, userId };
@@ -43,6 +44,8 @@ module.exports.findAllIdApp = async function (idapp, userId) {
module.exports.getCartByUserId = async function (uid, idapp) { module.exports.getCartByUserId = async function (uid, idapp) {
try { try {
const Order = require('../models/order');
let query = { userId: uid, idapp }; let query = { userId: uid, idapp };
const mycart = await Cart.findOne(query).lean(); const mycart = await Cart.findOne(query).lean();
@@ -130,3 +133,7 @@ module.exports.createCart = async function (newCart) {
return await newCart.save(); return await newCart.save();
}; };
Cart.createIndexes((err) => {
if (err) throw err;
});

View File

@@ -43,12 +43,16 @@ const OrdersCartSchema = new Schema({
completed_at: { completed_at: {
type: Date type: Date
}, },
deleted: {
type: Boolean,
default: false,
},
}); });
var OrdersCart = module.exports = mongoose.model('OrdersCart', OrdersCartSchema); var OrdersCart = module.exports = mongoose.model('OrdersCart', OrdersCartSchema);
module.exports.findAllIdApp = async function (idapp, userId) { module.exports.findAllIdApp = async function (idapp, userId) {
const myfind = { idapp, userId }; const myfind = { idapp, userId, deleted: false };
return await await OrdersCart.find(myfind); return await await OrdersCart.find(myfind);
}; };
@@ -64,7 +68,7 @@ module.exports.getFieldsForSearch = function () {
module.exports.getNewNumOrder = async function (uid, idapp) { module.exports.getNewNumOrder = async function (uid, idapp) {
let query = { userId: uid, idapp } let query = { userId: uid, idapp, deleted: false }
let numorder = 1; let numorder = 1;
let numorderrec = await OrdersCart.find(query).sort({ numorder: -1 }).limit(1); let numorderrec = await OrdersCart.find(query).sort({ numorder: -1 }).limit(1);
if (numorderrec.length <= 0) if (numorderrec.length <= 0)
@@ -99,7 +103,7 @@ module.exports.getStatusCartByUserId = async function (uid, idapp, numorder) {
} }
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 } } let query = { idapp, status: { $gte: shared_consts.OrderStatus.CHECKOUT_SENT }, deleted: false }
let myorderscart = null; let myorderscart = null;
if (numorder > 0) { if (numorder > 0) {
query.numorder = numorder; query.numorder = numorder;
@@ -153,7 +157,10 @@ module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder) {
} }
module.exports.getOrdersCartByDepartmentId = async function (depId, idapp) { module.exports.getOrdersCartByDepartmentId = async function (depId, idapp) {
let query = { idapp, status: { $gte: shared_consts.OrderStatus.CHECKOUT_SENT } } let query = {
idapp, status: { $gte: shared_consts.OrderStatus.CHECKOUT_SENT },
deleted: false,
}
const myorderscart = await OrdersCart.find(query).lean(); const myorderscart = await OrdersCart.find(query).lean();
for (let ind = 0; ind < myorderscart.length; ind++) { for (let ind = 0; ind < myorderscart.length; ind++) {
@@ -207,7 +214,10 @@ module.exports.getOrderById = async function (Id, idapp) {
} }
module.exports.updateOrdersCartById = function (id, newOrdersCart, callback) { module.exports.updateOrdersCartById = function (id, newOrdersCart, callback) {
let query = { id: id } let query = {
id,
deleted: false,
}
OrdersCart.find(query, function (err, c) { OrdersCart.find(query, function (err, c) {
if (err) throw err if (err) throw err

View File

@@ -53,7 +53,8 @@ const productSchema = new Schema({
type: Number type: Number
}, },
price: { price: {
type: Number type: Number,
required: true,
}, },
after_price: { after_price: {
type: String type: String
@@ -74,7 +75,8 @@ const productSchema = new Schema({
type: Number type: Number
}, },
stockQty: { // in magazzino stockQty: { // in magazzino
type: Number type: Number,
required: true,
}, },
quantityAvailable: { quantityAvailable: {
type: Number type: Number

View File

@@ -45,12 +45,16 @@ class Cart {
} }
async subqty(itemorder) { async subqty(itemorder) {
const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id) try {
if (!!myitem) { const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id)
myitem.order.quantity--; if (!!myitem && myitem.order.quantity > 0) {
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false }); myitem.order.quantity--;
this.updatetotals(); await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
return myitem.order.quantity; this.updatetotals();
return myitem.order.quantity;
}
} catch (e) {
console.error('Err: ', e);
} }
} }
@@ -86,18 +90,22 @@ class Cart {
} }
updatetotals() { updatetotals() {
this.totalQty = 0; try {
this.totalPrice = 0; this.totalQty = 0;
for (const rec in this.items) { this.totalPrice = 0;
let order = this.items[rec].order; for (const rec in this.items) {
if (!order) { let order = this.items[rec].order;
order = this.items[rec]; if (!order) {
order = this.items[rec];
}
this.totalQty += order.quantity;
this.totalPrice += order.price * order.quantity;
} }
this.totalQty += order.quantity;
this.totalPrice += order.price * order.quantity; this.totalPrice = parseFloat(this.totalPrice.toFixed(2))
} catch (e) {
console.error('Err: ', e);
} }
this.totalPrice = parseFloat(this.totalPrice.toFixed(2))
} }

View File

@@ -55,7 +55,6 @@ 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); const mycart = await Cart.getCartByUserId(userId, idapp);
// const myorder = Order.getOrderByID(order._id); // const myorder = Order.getOrderByID(order._id);
@@ -223,7 +222,7 @@ router.post('/:userId/cartstatus', authenticate, async function (req, res, next)
const myris = ris; const myris = ris;
// Cancella il Cart appena salvato in OrdersCart // Cancella il Cart appena salvato in OrdersCart
return Cart.deleteCartByCartId(mycart.id) return Cart.deleteCartByCartId(mycart._id)
.then((ris) => { .then((ris) => {
const orders = OrdersCart.getOrdersCartByUserId(userId, idapp, numorder) const orders = OrdersCart.getOrdersCartByUserId(userId, idapp, numorder)

View File

@@ -1799,6 +1799,7 @@ module.exports = {
return (myapp) ? this.decryptdata(myapp.email_pwd) : ''; return (myapp) ? this.decryptdata(myapp.email_pwd) : '';
}, },
getTelegramBotNameByIdApp: function (idapp) { getTelegramBotNameByIdApp: function (idapp) {
const myapp = this.MYAPPS.find((item) => item.idapp === idapp); const myapp = this.MYAPPS.find((item) => item.idapp === idapp);
if (process.env.NODE_ENV === 'test') if (process.env.NODE_ENV === 'test')
@@ -3477,7 +3478,7 @@ module.exports = {
}, },
decryptdata(mydatacrypted) { decryptdata(mydatacrypted) {
if (mydatacrypted === '') if (mydatacrypted === '' || mydatacrypted === undefined)
return ''; return '';
// Decrypt // Decrypt
// const bytes = CryptoJS.AES.decrypt(mydatacrypted.toString(), process.env.SECRK); // const bytes = CryptoJS.AES.decrypt(mydatacrypted.toString(), process.env.SECRK);

View File

@@ -376,6 +376,7 @@ module.exports = {
ENTRA_RIS_ITALIA: 30, ENTRA_RIS_ITALIA: 30,
}, },
OrderStatus: { OrderStatus: {
NONE: 0, NONE: 0,
IN_CART: 1, IN_CART: 1,
@@ -387,6 +388,14 @@ module.exports = {
CANCELED: 10, CANCELED: 10,
}, },
OrderStatStr: {
IN_CORSO: 1,
CONFERMATI: 2,
PAGATI: 3,
COMPLETATI: 4,
CANCELLATI: 5,
},
OrderStatusView: { OrderStatusView: {
CHECKOUT_SENT: 2, CHECKOUT_SENT: 2,
ORDER_CONFIRMED: 3, ORDER_CONFIRMED: 3,