346 lines
10 KiB
JavaScript
Executable File
346 lines
10 KiB
JavaScript
Executable File
const shared_consts = require('../tools/shared_nodejs');
|
|
|
|
const express = require('express');
|
|
const router = express.Router();
|
|
|
|
const sendemail = require('../sendemail');
|
|
const tools = require('../tools/general');
|
|
|
|
var server_constants = require('../tools/server_constants');
|
|
|
|
var { Project } = require('../models/project');
|
|
|
|
var { authenticate, auth_default } = require('../middleware/authenticate');
|
|
|
|
const _ = require('lodash');
|
|
|
|
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');
|
|
const OrdersCart = require('../models/orderscart');
|
|
|
|
//GET cart
|
|
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.send({ code: server_constants.RIS_CODE_OK, cart: null });
|
|
}).catch((err) => {
|
|
console.error('Err', err);
|
|
return res.send({ code: server_constants.RIS_CODE_ERR, cart: null });
|
|
});
|
|
|
|
})
|
|
|
|
//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;
|
|
|
|
try {
|
|
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 })
|
|
})
|
|
|
|
*/
|
|
|
|
|
|
} catch (e) {
|
|
console.error('Err:', e);
|
|
return res.send({ code: server_constants.RIS_CODE_ERR, cart: 0 });
|
|
}
|
|
|
|
})
|
|
|
|
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.deleteOne({ _id: 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, async function (req, res, next) {
|
|
let userId = req.params.userId
|
|
let requestProduct = req.body
|
|
let { productId, color, size } = requestProduct.product
|
|
|
|
try {
|
|
|
|
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
|
|
})
|
|
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
|
|
router.post('/:userId/createorderscart', authenticate, async function (req, res, next) {
|
|
let idapp = req.body.idapp;
|
|
let cart_id = req.body.cart_id;
|
|
let userId = req.params.userId;
|
|
const user = req.user;
|
|
let status = req.body.status;
|
|
|
|
try {
|
|
const mycart = await Cart.findOne({ _id: cart_id });
|
|
|
|
let numorder = await OrdersCart.getLastNumOrder(userId, idapp);
|
|
|
|
// Esiste l'ordine ?
|
|
let myorderCart = await OrdersCart.getRecCartByUserId(userId, idapp, numorder);
|
|
if (!myorderCart) {
|
|
|
|
// crea il nuovo numero d'ordine
|
|
numorder++;
|
|
|
|
// SE non esiste allora lo creo !
|
|
myorderCart = new OrdersCart({
|
|
idapp,
|
|
items: mycart.items,
|
|
totalQty: mycart.totalQty,
|
|
totalPrice: mycart.totalPrice,
|
|
userId,
|
|
status,
|
|
note: mycart.note,
|
|
numorder,
|
|
created_at: new Date(),
|
|
modify_at: new Date(),
|
|
})
|
|
}
|
|
statusOrderCart = myorderCart.status;
|
|
|
|
if (!!mycart) {
|
|
if (status === shared_consts.OrderStatus.CHECKOUT_SENT) {
|
|
|
|
// Porta tutto il Cart nell'Ordine e lo CREO
|
|
return await OrdersCart.updateOrdersCartById(-1, myorderCart, async function (err, ris) {
|
|
//if (err) return next(err)
|
|
if (err)
|
|
return res.send({ code: server_constants.RIS_CODE_ERR, status: 0 });
|
|
else {
|
|
|
|
await Order.updateStatusOrders(mycart.items, status);
|
|
|
|
const myris = ris;
|
|
// Cancella il Cart appena salvato in OrdersCart
|
|
|
|
return Cart.deleteCartByCartId(mycart._id)
|
|
.then((ris) => {
|
|
|
|
return OrdersCart.getOrdersCartByUserId(userId, idapp, numorder)
|
|
.then((orders) => {
|
|
if (!!orders) {
|
|
|
|
// Invia la email dell'Ordine
|
|
sendemail.sendEmail_OrderProduct(user.lang, idapp, orders[0], user)
|
|
.then(async (ris) => {
|
|
myorderCart = await OrdersCart.getRecCartByUserId(userId, idapp, numorder);
|
|
return res.send({
|
|
code: server_constants.RIS_CODE_OK,
|
|
status: myris.status,
|
|
orders: orders,
|
|
recOrderCart: myorderCart
|
|
});
|
|
});
|
|
}
|
|
});
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
}
|
|
return res.send({
|
|
code: server_constants.RIS_CODE_OK,
|
|
status: statusOrderCart,
|
|
recOrderCart: myorderCart
|
|
});
|
|
} catch (e) {
|
|
return res.send({ code: server_constants.RIS_CODE_ERR, status: 0, recOrderCart: null });
|
|
}
|
|
|
|
});
|
|
|
|
//POST cart
|
|
router.post('/:userId/ordercartstatus', authenticate, async function (req, res, next) {
|
|
let idapp = req.body.idapp;
|
|
let userId = req.params.userId;
|
|
let order_id = req.body.order_id;
|
|
const user = req.user;
|
|
let status = req.body.status;
|
|
|
|
const { User } = require('../models/user');
|
|
|
|
let orderCart = await OrdersCart.findOne({ idapp, _id: order_id }).lean();
|
|
|
|
if ((userId !== String(req.user._id)) && !User.isManager(req.user.perm)) {
|
|
// I'm trying to write something not mine!
|
|
return res.status(404).send({ code: server_constants.RIS_CODE_TODO_CREATING_NOTMYUSER });
|
|
}
|
|
|
|
try {
|
|
if (!!orderCart) {
|
|
|
|
let fields_to_update = { status };
|
|
|
|
await OrdersCart.findOneAndUpdate({ _id: order_id }, { $set: fields_to_update }
|
|
, { new: false })
|
|
.then(async (ris) => {
|
|
|
|
if (ris) {
|
|
|
|
// Aggiorna gli Stati Interni !
|
|
orderCart = await OrdersCart.updateCmd(orderCart, status, true);
|
|
|
|
let ordertype = '';
|
|
|
|
if (status === shared_consts.OrderStatus.ORDER_CONFIRMED) {
|
|
ordertype = 'order_confirmed';
|
|
} else if (status === shared_consts.OrderStatus.COMPLETED) {
|
|
ordertype = 'order_completed';
|
|
} else if (status === shared_consts.OrderStatus.CANCELED) {
|
|
ordertype = 'order_canceled';
|
|
}
|
|
|
|
if (ordertype !== '') {
|
|
sendemail.sendEmail_Order(user.lang, idapp, orderCart, user, ordertype)
|
|
.then((ris) => {
|
|
|
|
})
|
|
}
|
|
}
|
|
|
|
})
|
|
let orderscart = null;
|
|
|
|
if (User.isManager(user.perm)) {
|
|
// Prende Tutti gli Ordini !
|
|
orderscart = await OrdersCart.getOrdersCartByUserId('ALL', idapp, 0);
|
|
} else {
|
|
orderscart = await OrdersCart.getOrdersCartByUserId(req.user.id, idapp, 0);
|
|
}
|
|
|
|
return res.send({ code: server_constants.RIS_CODE_OK, status, orders: orderscart });
|
|
|
|
}
|
|
} catch (e) {
|
|
return res.send({ code: server_constants.RIS_CODE_ERR, status: 0 });
|
|
}
|
|
|
|
|
|
});
|
|
|
|
module.exports = router;
|