Struttura Scheda Prodotti...
This commit is contained in:
@@ -30,17 +30,20 @@ const Cart = require('../models/cart');
|
||||
const OrdersCart = require('../models/orderscart');
|
||||
|
||||
//GET cart
|
||||
router.get('/:userId', authenticate, function (req, res, next) {
|
||||
let userId = req.body.userId
|
||||
let idapp = req.body.idapp
|
||||
Cart.getCartByUserId(userId, idapp, function (err, cart) {
|
||||
if (err) return next(err)
|
||||
router.get('/:userId', authenticate, async function (req, res, next) {
|
||||
let userId = req.params.userId
|
||||
let idapp = req.user.idapp
|
||||
return await Cart.getCartByUserId(userId, idapp)
|
||||
.then((cart) => {
|
||||
if (cart)
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, cart });
|
||||
else
|
||||
return res.status(400).send(e);
|
||||
}).catch((err) => {
|
||||
console.error('Err', err);
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, cart: null });
|
||||
});
|
||||
|
||||
if (cart)
|
||||
res.send({ code: server_constants.RIS_CODE_OK, cart });
|
||||
else
|
||||
res.status(400).send(e);
|
||||
})
|
||||
})
|
||||
|
||||
//POST cart
|
||||
@@ -51,46 +54,54 @@ router.post('/:userId', authenticate, async function (req, res, next) {
|
||||
let subqty = req.body.subqty;
|
||||
let order = req.body.order;
|
||||
|
||||
const mycart = await Cart.getCartByUserId(userId, idapp);
|
||||
try {
|
||||
|
||||
// const myorder = Order.getOrderByID(order._id);
|
||||
if (!addqty && !subqty)
|
||||
order._id = await Order.createOrder(order);
|
||||
const mycart = await Cart.getCartByUserId(userId, idapp);
|
||||
|
||||
let cart = null;
|
||||
// no cart save empty cart to database then return response
|
||||
let myqty = 0;
|
||||
if (!mycart) {
|
||||
let oldCart = new CartClass(order)
|
||||
cart = await Cart.createCart(oldCart.generateModel());
|
||||
} else {
|
||||
let newCart = CartClass.constructByCart(mycart);
|
||||
if (addqty) {
|
||||
myqty = await newCart.addqty(order);
|
||||
} else if (subqty) {
|
||||
myqty = await newCart.subqty(order);
|
||||
// const myorder = Order.getOrderByID(order._id);
|
||||
if (!addqty && !subqty)
|
||||
order._id = await Order.createOrder(order);
|
||||
|
||||
let cart = null;
|
||||
// no cart save empty cart to database then return response
|
||||
let myqty = 0;
|
||||
if (!mycart) {
|
||||
let oldCart = new CartClass(order)
|
||||
cart = await Cart.createCart(oldCart.generateModel());
|
||||
} else {
|
||||
const ind = newCart.addItem(order);
|
||||
let newCart = CartClass.constructByCart(mycart);
|
||||
if (addqty) {
|
||||
myqty = await newCart.addqty(order);
|
||||
} else if (subqty) {
|
||||
myqty = await newCart.subqty(order);
|
||||
} else {
|
||||
const ind = newCart.addItem(order);
|
||||
}
|
||||
cart = await Cart.updateCartByCartId(mycart._id, newCart.generateModel());
|
||||
}
|
||||
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 });
|
||||
} else {
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, cart: null });
|
||||
if (cart) {
|
||||
const carttot = await Cart.getCartByUserId(userId, idapp);
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, cart: carttot, qty: myqty });
|
||||
} else {
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, cart: null });
|
||||
}
|
||||
|
||||
/*
|
||||
Cart.updateCartByUserId(
|
||||
userId,
|
||||
newCart,
|
||||
function (err, result) {
|
||||
if (err) return next(err)
|
||||
return res.status(200).json({ cart: result })
|
||||
})
|
||||
|
||||
*/
|
||||
|
||||
|
||||
} catch (e) {
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, cart: 0 });
|
||||
}
|
||||
|
||||
/*
|
||||
Cart.updateCartByUserId(
|
||||
userId,
|
||||
newCart,
|
||||
function (err, result) {
|
||||
if (err) return next(err)
|
||||
return res.status(200).json({ cart: result })
|
||||
})
|
||||
|
||||
*/
|
||||
})
|
||||
|
||||
router.delete('/:userId', authenticate, async function (req, res) {
|
||||
@@ -122,47 +133,56 @@ router.delete('/:userId', authenticate, async function (req, res) {
|
||||
|
||||
|
||||
//PUT cart
|
||||
router.put('/:userId', authenticate, function (req, res, next) {
|
||||
router.put('/:userId', authenticate, async function (req, res, next) {
|
||||
let userId = req.params.userId
|
||||
let requestProduct = req.body
|
||||
let { productId, color, size } = requestProduct.product
|
||||
|
||||
Cart.getCartByUserId(userId, function (err, c) {
|
||||
if (err) return next(err)
|
||||
let oldCart = new CartClass(c[0] || {})
|
||||
Product.getProductByID(productId, function (err, p) {
|
||||
if (err) return next(err)
|
||||
let newCart = oldCart.add(p, productId, { color, size })
|
||||
try {
|
||||
|
||||
//exist cart in databse
|
||||
if (c.length > 0) {
|
||||
Cart.updateCartByUserId(
|
||||
userId,
|
||||
{
|
||||
await Cart.getCartByUserId(userId, function (err, c) {
|
||||
if (err) return next(err)
|
||||
let oldCart = new CartClass(c[0] || {})
|
||||
|
||||
Product.getProductByID(productId, 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(
|
||||
userId,
|
||||
{
|
||||
items: newCart.items,
|
||||
totalQty: newCart.totalQty,
|
||||
totalPrice: newCart.totalPrice,
|
||||
userId: userId
|
||||
},
|
||||
function (err, result) {
|
||||
if (err) return next(err)
|
||||
res.json(result)
|
||||
})
|
||||
} else {
|
||||
//no cart in database
|
||||
newCart = new Cart({
|
||||
items: newCart.items,
|
||||
totalQty: newCart.totalQty,
|
||||
totalPrice: newCart.totalPrice,
|
||||
userId: userId
|
||||
},
|
||||
function (err, result) {
|
||||
if (err) return next(err)
|
||||
res.json(result)
|
||||
})
|
||||
} else {
|
||||
//no cart in database
|
||||
newCart = new Cart({
|
||||
items: newCart.items,
|
||||
totalQty: newCart.totalQty,
|
||||
totalPrice: newCart.totalPrice,
|
||||
userId: userId
|
||||
})
|
||||
Cart.createCart(newCart, function (err, resultCart) {
|
||||
if (err) return next(err)
|
||||
res.status(201).json(resultCart)
|
||||
})
|
||||
}
|
||||
Cart.createCart(newCart, function (err, resultCart) {
|
||||
if (err) return next(err)
|
||||
res.status(201).json(resultCart)
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
return res.send({ code: server_constants.RIS_CODE_OK });
|
||||
} catch (e) {
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, status: 0 });
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
//POST cart
|
||||
|
||||
@@ -1573,6 +1573,8 @@ function load(req, res, version) {
|
||||
myelems: arrdata[38],
|
||||
categories: arrdata[39],
|
||||
});
|
||||
|
||||
const prova = 1;
|
||||
}
|
||||
|
||||
}).catch((e) => {
|
||||
|
||||
Reference in New Issue
Block a user