Carrello Spesa
This commit is contained in:
170
src/server/router/cart_router.js
Executable file
170
src/server/router/cart_router.js
Executable file
@@ -0,0 +1,170 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
var server_constants = require('../tools/server_constants');
|
||||
|
||||
var { Project } = require('../models/project');
|
||||
|
||||
var { authenticate, auth_default } = require('../middleware/authenticate');
|
||||
|
||||
var mongoose = require('mongoose');
|
||||
const Subscription = mongoose.model('subscribers');
|
||||
|
||||
const _ = require('lodash');
|
||||
|
||||
const { ObjectID } = require('mongodb');
|
||||
|
||||
const Product = require('../models/product');
|
||||
const Order = require('../models/order');
|
||||
const Variant = require('../models/variant');
|
||||
|
||||
/*const Department = require('../models/Department')
|
||||
const Category = require('../models/Category')
|
||||
const TypedError = require('../modules/ErrorHandler')
|
||||
const paypal_config = require('../configs/paypal-config')
|
||||
const paypal = require('paypal-rest-sdk')
|
||||
*/
|
||||
|
||||
const CartClass = require('../modules/Cart')
|
||||
const Cart = require('../models/cart');
|
||||
|
||||
//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)
|
||||
|
||||
if (cart)
|
||||
res.send({ code: server_constants.RIS_CODE_OK, cart });
|
||||
else
|
||||
res.status(400).send(e);
|
||||
})
|
||||
})
|
||||
|
||||
//POST cart
|
||||
router.post('/:userId', authenticate, async function (req, res, next) {
|
||||
let idapp = req.body.idapp;
|
||||
let userId = req.params.userId;
|
||||
let addqty = req.body.addqty;
|
||||
let subqty = req.body.subqty;
|
||||
let order = req.body.order;
|
||||
|
||||
const mycart = await Cart.getCartByUserId(userId, idapp);
|
||||
|
||||
// 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 {
|
||||
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());
|
||||
}
|
||||
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 })
|
||||
})
|
||||
|
||||
*/
|
||||
})
|
||||
|
||||
router.delete('/:userId', authenticate, async function (req, res) {
|
||||
console.log('DELETE Item');
|
||||
let idapp = req.query.idapp;
|
||||
let userId = req.params.userId;
|
||||
let orderId = req.query.orderId;
|
||||
|
||||
const mycart = await Cart.getCartByUserId(userId, idapp);
|
||||
|
||||
// Rimuovere l'Ordine
|
||||
const recremoved = await Order.findByIdAndRemove(orderId);
|
||||
if (recremoved) {
|
||||
// Rimuovere l'id sul Carrello
|
||||
|
||||
let newCart = CartClass.constructByCart(mycart);
|
||||
newCart.removeItem(orderId);
|
||||
let carttot = null;
|
||||
const cart = await Cart.updateCartByCartId(mycart._id, newCart.generateModel());
|
||||
|
||||
carttot = await Cart.getCartByUserId(userId, idapp);
|
||||
|
||||
console.log('carttot', carttot)
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, cart: carttot });
|
||||
}
|
||||
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, cart: null });
|
||||
});
|
||||
|
||||
|
||||
//PUT cart
|
||||
router.put('/:userId', authenticate, 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 })
|
||||
|
||||
//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
|
||||
})
|
||||
Cart.createCart(newCart, function (err, resultCart) {
|
||||
if (err) return next(err)
|
||||
res.status(201).json(resultCart)
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
module.exports = router;
|
||||
@@ -52,7 +52,10 @@ const { SendMsg } = require('../models/sendmsg');
|
||||
const { Permission } = require('../models/permission');
|
||||
const { MsgTemplate } = require('../models/msg_template');
|
||||
const Product = require('../models/product');
|
||||
|
||||
const Producer = require('../models/producer');
|
||||
const Cart = require('../models/cart');
|
||||
const Storehouse = require('../models/storehouse');
|
||||
const Order = require('../models/order');
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
@@ -195,6 +198,14 @@ function getTableByTableName(tablename) {
|
||||
mytable = Operator;
|
||||
else if (tablename === 'products')
|
||||
mytable = Product;
|
||||
else if (tablename === 'storehouses')
|
||||
mytable = Storehouse;
|
||||
else if (tablename === 'orders')
|
||||
mytable = Order;
|
||||
else if (tablename === 'producers')
|
||||
mytable = Producer;
|
||||
else if (tablename === 'carts')
|
||||
mytable = Cart;
|
||||
else if (tablename === 'sendmsgs')
|
||||
mytable = SendMsg;
|
||||
else if (tablename === 'wheres')
|
||||
@@ -1094,16 +1105,21 @@ router.get('/loadsite/:userId/:idapp/:sall', authenticate_noerror, (req, res) =>
|
||||
let mypage = MyPage.findAllIdApp(idapp);
|
||||
let calzoom = CalZoom.findAllIdApp(idapp);
|
||||
let gallery = Gallery.findAllIdApp(idapp);
|
||||
let producers = Producer.findAllIdApp(idapp);
|
||||
let storehouses = Storehouse.findAllIdApp(idapp);
|
||||
let cart = null;
|
||||
if (sall) {
|
||||
newstosent = Newstosent.findAllIdApp(idapp);
|
||||
}
|
||||
|
||||
let calcstat = null;
|
||||
if (req.user)
|
||||
if (req.user) {
|
||||
calcstat = User.calculateStat(idapp, req.user.username);
|
||||
cart = Cart.getCartByUserId(req.user.id, idapp);
|
||||
}
|
||||
|
||||
|
||||
return Promise.all([bookedevent, eventlist, operators, wheres, contribtype, settings, permissions, disciplines, newstosent, mailinglist, mypage, gallery, paymenttype, calcstat, calzoom])
|
||||
return Promise.all([bookedevent, eventlist, operators, wheres, contribtype, settings, permissions, disciplines, newstosent, mailinglist, mypage, gallery, paymenttype, calcstat, calzoom, producers, cart, storehouses])
|
||||
.then((arrdata) => {
|
||||
// console.table(arrdata);
|
||||
const myuser = req.user;
|
||||
@@ -1127,6 +1143,9 @@ router.get('/loadsite/:userId/:idapp/:sall', authenticate_noerror, (req, res) =>
|
||||
gallery: arrdata[11],
|
||||
paymenttypes: arrdata[12],
|
||||
calzoom: arrdata[14],
|
||||
producers: arrdata[15],
|
||||
cart: arrdata[16],
|
||||
storehouses: arrdata[17],
|
||||
myuser,
|
||||
});
|
||||
})
|
||||
|
||||
@@ -7,7 +7,7 @@ var server_constants = require('../tools/server_constants');
|
||||
|
||||
var { Project } = require('../models/project');
|
||||
|
||||
var { authenticate } = require('../middleware/authenticate');
|
||||
var { authenticate, auth_default } = require('../middleware/authenticate');
|
||||
|
||||
var mongoose = require('mongoose');
|
||||
const Subscription = mongoose.model('subscribers');
|
||||
@@ -30,8 +30,8 @@ const CartClass = require('../modules/Cart')
|
||||
const Cart = require('../models/cart');
|
||||
|
||||
//GET /products
|
||||
router.get('/', async function (req, res, next) {
|
||||
const idapp = req.query.idapp;
|
||||
router.post('/', auth_default, async function (req, res, next) {
|
||||
const idapp = req.body.idapp;
|
||||
|
||||
var products = await Product.findAllIdApp(idapp);
|
||||
|
||||
|
||||
@@ -799,140 +799,5 @@ router.post('/dbop', authenticate, async (req, res) => {
|
||||
|
||||
});
|
||||
|
||||
//GET cart
|
||||
router.get('/:userId/cart', authenticate, function (req, res, next) {
|
||||
let userId = req.params.userId
|
||||
Cart.getCartByUserId(userId, function (err, cart) {
|
||||
if (err) return next(err)
|
||||
if (cart.length < 1) {
|
||||
let err = new TypedError('cart error', 404, 'not_found', { message: "create a cart first" })
|
||||
return next(err)
|
||||
}
|
||||
return res.json({ cart: cart[0] })
|
||||
})
|
||||
})
|
||||
|
||||
//POST cart
|
||||
router.post('/:userId/cart', authenticate, function (req, res, next) {
|
||||
let userId = req.params.userId
|
||||
let { productId, increase, decrease } = req.body
|
||||
|
||||
Cart.getCartByUserId(userId, function (err, c) {
|
||||
if (err) return next(err)
|
||||
let oldCart = new CartClass(c[0] || { userId })
|
||||
// no cart save empty cart to database then return response
|
||||
if (c.length < 1 && !productId) {
|
||||
return Cart.createCart(oldCart.generateModel(), function (err, resultCart) {
|
||||
if (err) return next(err)
|
||||
return res.status(201).json({ cart: resultCart })
|
||||
})
|
||||
}
|
||||
Product.findById(productId, function (e, product) {
|
||||
if (e) {
|
||||
e.status = 406;
|
||||
return next(e);
|
||||
}
|
||||
if (product) {
|
||||
if (decrease) {
|
||||
oldCart.decreaseQty(product.id);
|
||||
} else if (increase) {
|
||||
oldCart.increaseQty(product.id);
|
||||
} else {
|
||||
oldCart.add(product, product.id);
|
||||
}
|
||||
let newCart = oldCart.generateModel()
|
||||
Cart.updateCartByUserId(
|
||||
userId,
|
||||
newCart,
|
||||
function (err, result) {
|
||||
if (err) return next(err)
|
||||
return res.status(200).json({ cart: result })
|
||||
})
|
||||
} else {
|
||||
// apply variant
|
||||
Variant.getVariantByID(productId, function (e, variant) {
|
||||
if (e) {
|
||||
e.status = 406;
|
||||
return next(e);
|
||||
}
|
||||
if (variant) {
|
||||
Product.getProductByID(variant.productID, function (e, p) {
|
||||
let color = (variant.color) ? "- " + variant.color : "";
|
||||
let size = (variant.size) ? "- " + variant.size : "";
|
||||
variant.title = p.title + " " + color + size
|
||||
variant.price = p.price
|
||||
if (decrease) {
|
||||
oldCart.decreaseQty(variant.id);
|
||||
} else if (increase) {
|
||||
oldCart.increaseQty(variant.id);
|
||||
} else {
|
||||
oldCart.add(variant, variant.id);
|
||||
}
|
||||
let newCart = oldCart.generateModel()
|
||||
Cart.updateCartByUserId(
|
||||
userId,
|
||||
newCart,
|
||||
function (err, result) {
|
||||
if (err) return next(err)
|
||||
res.status(200).json({ cart: result })
|
||||
})
|
||||
})
|
||||
}
|
||||
// no product and no variant find
|
||||
else {
|
||||
let err = new TypedError('/cart', 400, 'invalid_field', {
|
||||
message: "invalid request body"
|
||||
})
|
||||
return next(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
//PUT cart
|
||||
router.put('/:userId/cart', authenticate, 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 })
|
||||
|
||||
//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
|
||||
})
|
||||
Cart.createCart(newCart, function (err, resultCart) {
|
||||
if (err) return next(err)
|
||||
res.status(201).json(resultCart)
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user