Corretto Ordini e visualizzazione dei Totali

This commit is contained in:
Surya Paolo
2023-12-15 21:50:21 +01:00
parent 05ec283882
commit 4d9eccd1ae
10 changed files with 326 additions and 314 deletions

View File

@@ -55,20 +55,27 @@ router.post('/:userId', authenticate, async function (req, res, next) {
let order = req.body.order;
try {
const mycart = await Cart.getCartByUserId(userId, idapp);
let mycart = await Cart.getCartByUserId(userId, idapp);
// const myorder = Order.getOrderByID(order._id);
if (!addqty && !subqty)
order._id = await Order.createOrder(order);
let cart = null;
let product = null;
// no cart save empty cart to database then return response
let myqty = 0;
let nuovo = false;
if (!mycart) {
let oldCart = new CartClass(order)
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) {
myqty = await newCart.addqty(order);
} else if (subqty) {
@@ -76,11 +83,19 @@ router.post('/:userId', authenticate, async function (req, res, next) {
} else {
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) {
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 {
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 ord = await Order.findOne({ _id: orderId });
let idProduct = ''
let product = null;
if (ord)
idProduct = ord.idProduct;
// Rimuovere l'Ordine
const recremoved = await Order.deleteOne({ _id: orderId });
if (recremoved) {
@@ -124,8 +145,12 @@ router.delete('/:userId', authenticate, async function (req, res) {
carttot = await Cart.getCartByUserId(userId, idapp);
if (idProduct) {
product = await Product.getProductById(idProduct);
}
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 });
@@ -140,17 +165,17 @@ router.put('/:userId', authenticate, async function (req, res, next) {
try {
await Cart.getCartByUserId(userId, function (err, c) {
await Cart.getCartByUserId(userId, async function (err, c) {
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)
let newCart = oldCart.add(p, productId, { color, size })
//exist cart in databse
if (c.length > 0) {
Cart.updateCartByUserId(
await Cart.updateCartByUserId(
userId,
{
items: newCart.items,
@@ -164,13 +189,13 @@ router.put('/:userId', authenticate, async function (req, res, next) {
})
} else {
//no cart in database
newCart = new Cart({
let newCartobj = {
items: newCart.items,
totalQty: newCart.totalQty,
totalPrice: newCart.totalPrice,
userId: userId
})
Cart.createCart(newCart, function (err, resultCart) {
}
await Cart.createCart(newCartobj, function (err, resultCart) {
if (err) return next(err)
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) {
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;
const user = req.user;
let status = req.body.status;
let note = req.body.note;
try {
const mycart = await Cart.findOne({ _id: cart_id });
@@ -213,7 +241,7 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
totalPrice: mycart.totalPrice,
userId,
status,
note: mycart.note,
note,
numorder,
created_at: new Date(),
modify_at: new Date(),
@@ -221,6 +249,8 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
}
statusOrderCart = myorderCart.status;
const idordercart = myorderCart._id;
if (!!mycart) {
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
sendemail.sendEmail_OrderProduct(user.lang, idapp, orders[0], user)
.then(async (ris) => {
myorderCart = await OrdersCart.getRecCartByUserId(userId, idapp, numorder);
myorderCart = await OrdersCart.findById(idordercart).lean();
return res.send({
code: server_constants.RIS_CODE_OK,
status: myris.status,

View File

@@ -12,6 +12,7 @@ var { authenticate, auth_default } = require('../middleware/authenticate');
const _ = require('lodash');
const Product = require('../models/product');
const OrdersCart = require('../models/orderscart');
const Variant = require('../models/variant');
/*const Department = require('../models/Department')
@@ -27,11 +28,13 @@ const Cart = require('../models/cart');
//GET /products
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);
if (products)
res.send({ code: server_constants.RIS_CODE_OK, products });
res.send({ code: server_constants.RIS_CODE_OK, products, orders });
else
res.status(400).send(e);