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 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({
idapp: {
@@ -31,10 +36,6 @@ const CartSchema = new Schema({
var Cart = module.exports = mongoose.model('Cart', CartSchema);
Cart.createIndexes((err) => {
if (err) throw err;
});
module.exports.findAllIdApp = async function (idapp, userId) {
const myfind = { idapp, userId };
@@ -43,6 +44,8 @@ module.exports.findAllIdApp = async function (idapp, userId) {
module.exports.getCartByUserId = async function (uid, idapp) {
try {
const Order = require('../models/order');
let query = { userId: uid, idapp };
const mycart = await Cart.findOne(query).lean();
@@ -130,3 +133,7 @@ module.exports.createCart = async function (newCart) {
return await newCart.save();
};
Cart.createIndexes((err) => {
if (err) throw err;
});

View File

@@ -43,12 +43,16 @@ const OrdersCartSchema = new Schema({
completed_at: {
type: Date
},
deleted: {
type: Boolean,
default: false,
},
});
var OrdersCart = module.exports = mongoose.model('OrdersCart', OrdersCartSchema);
module.exports.findAllIdApp = async function (idapp, userId) {
const myfind = { idapp, userId };
const myfind = { idapp, userId, deleted: false };
return await await OrdersCart.find(myfind);
};
@@ -64,7 +68,7 @@ module.exports.getFieldsForSearch = function () {
module.exports.getNewNumOrder = async function (uid, idapp) {
let query = { userId: uid, idapp }
let query = { userId: uid, idapp, deleted: false }
let numorder = 1;
let numorderrec = await OrdersCart.find(query).sort({ numorder: -1 }).limit(1);
if (numorderrec.length <= 0)
@@ -99,7 +103,7 @@ module.exports.getStatusCartByUserId = 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;
if (numorder > 0) {
query.numorder = numorder;
@@ -153,7 +157,10 @@ module.exports.getOrdersCartByUserId = async function (uid, idapp, numorder) {
}
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();
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) {
let query = { id: id }
let query = {
id,
deleted: false,
}
OrdersCart.find(query, function (err, c) {
if (err) throw err

View File

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

View File

@@ -45,12 +45,16 @@ class Cart {
}
async subqty(itemorder) {
const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id)
if (!!myitem) {
myitem.order.quantity--;
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
this.updatetotals();
return myitem.order.quantity;
try {
const myitem = this.items.find((rec) => rec.order._id.toString() === itemorder._id)
if (!!myitem && myitem.order.quantity > 0) {
myitem.order.quantity--;
await Order.findOneAndUpdate({ _id: myitem.order._id }, { $set: myitem.order }, { new: false });
this.updatetotals();
return myitem.order.quantity;
}
} catch (e) {
console.error('Err: ', e);
}
}
@@ -86,18 +90,22 @@ class Cart {
}
updatetotals() {
this.totalQty = 0;
this.totalPrice = 0;
for (const rec in this.items) {
let order = this.items[rec].order;
if (!order) {
order = this.items[rec];
try {
this.totalQty = 0;
this.totalPrice = 0;
for (const rec in this.items) {
let order = this.items[rec].order;
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;
try {
const mycart = await Cart.getCartByUserId(userId, idapp);
// const myorder = Order.getOrderByID(order._id);
@@ -223,7 +222,7 @@ router.post('/:userId/cartstatus', authenticate, async function (req, res, next)
const myris = ris;
// Cancella il Cart appena salvato in OrdersCart
return Cart.deleteCartByCartId(mycart.id)
return Cart.deleteCartByCartId(mycart._id)
.then((ris) => {
const orders = OrdersCart.getOrdersCartByUserId(userId, idapp, numorder)

View File

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

View File

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