Gestione Ordini: evaso...
This commit is contained in:
@@ -79,7 +79,7 @@ router.delete('/:id/:notify/:idapp', authenticate, (req, res) => {
|
||||
const notify = req.params.notify;
|
||||
const idapp = req.params.idapp;
|
||||
|
||||
Booking.findByIdAndRemove(id).then((recbooking) => {
|
||||
Booking.deleteOne({_id: id}).then((recbooking) => {
|
||||
if (!recbooking) {
|
||||
return res.status(404).send();
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ router.delete('/:userId', authenticate, async function (req, res) {
|
||||
const mycart = await Cart.getCartByUserId(userId, idapp);
|
||||
|
||||
// Rimuovere l'Ordine
|
||||
const recremoved = await Order.findByIdAndRemove(orderId);
|
||||
const recremoved = await Order.deleteOne({ _id: orderId });
|
||||
if (recremoved) {
|
||||
// Rimuovere l'id sul Carrello
|
||||
|
||||
@@ -186,41 +186,52 @@ router.put('/:userId', authenticate, async function (req, res, next) {
|
||||
})
|
||||
|
||||
//POST cart
|
||||
router.post('/:userId/cartstatus', authenticate, async function (req, res, next) {
|
||||
router.post('/:userId/createorderscart', authenticate, async function (req, res, next) {
|
||||
let idapp = req.body.idapp;
|
||||
let userId = req.params.userId;
|
||||
let cart_id = req.body.cart_id;
|
||||
let userId = req.params.userId;
|
||||
const user = req.user;
|
||||
let status = req.body.status;
|
||||
|
||||
|
||||
const mycart = await Cart.getCartByUserId(userId, idapp);
|
||||
const numorder = await OrdersCart.getNewNumOrder(userId, idapp);
|
||||
let statusOrderCart = await OrdersCart.getStatusCartByUserId(userId, idapp, numorder);
|
||||
|
||||
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
|
||||
const newOrderCart = 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(),
|
||||
})
|
||||
return await OrdersCart.updateOrdersCartById(-1, newOrderCart, async function (err, ris) {
|
||||
|
||||
// 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 Product.updateStatusOrders(mycart.items, status);
|
||||
await Order.updateStatusOrders(mycart.items, status);
|
||||
|
||||
const myris = ris;
|
||||
// Cancella il Cart appena salvato in OrdersCart
|
||||
@@ -228,15 +239,19 @@ router.post('/:userId/cartstatus', authenticate, async function (req, res, next)
|
||||
return Cart.deleteCartByCartId(mycart._id)
|
||||
.then((ris) => {
|
||||
|
||||
const orders = OrdersCart.getOrdersCartByUserId(userId, idapp, numorder)
|
||||
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((ris) => {
|
||||
.then(async (ris) => {
|
||||
myorderCart = await OrdersCart.getRecCartByUserId(userId, idapp, numorder);
|
||||
return res.send({
|
||||
code: server_constants.RIS_CODE_OK,
|
||||
status: myris.status,
|
||||
orders: orders
|
||||
orders: orders,
|
||||
recOrderCart: myorderCart
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -244,34 +259,22 @@ router.post('/:userId/cartstatus', authenticate, async function (req, res, next)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// mycart.numorder = await Cart.getNewNumOrder(userId, idapp);
|
||||
// mycart.status = shared_consts.OrderStatus.CHECKOUT_CONFIRMED
|
||||
/* const status = await Cart.findOneAndUpdate(
|
||||
{ userId },
|
||||
{
|
||||
$set: {
|
||||
status: mycart.status, numorder: mycart.numorder
|
||||
}
|
||||
},
|
||||
{ new: false })
|
||||
.then((rec) => {
|
||||
return rec.status
|
||||
}) */
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, status: statusOrderCart });
|
||||
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 });
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, status: 0, recOrderCart: null });
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
|
||||
//POST cart
|
||||
router.post('/:userId/orderstatus', authenticate, async function (req, res, next) {
|
||||
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;
|
||||
@@ -290,24 +293,20 @@ router.post('/:userId/orderstatus', authenticate, async function (req, res, next
|
||||
|
||||
let fields_to_update = { status };
|
||||
|
||||
if (status === shared_consts.OrderStatus.RECEIVED) {
|
||||
fields_to_update = {
|
||||
status,
|
||||
completed_at: new Date()
|
||||
}
|
||||
}
|
||||
|
||||
await OrdersCart.findOneAndUpdate({ _id: order_id }, { $set: fields_to_update }
|
||||
, { new: false })
|
||||
.then((ris) => {
|
||||
.then(async (ris) => {
|
||||
|
||||
if (ris) {
|
||||
|
||||
let ordertype = '';
|
||||
// Aggiorna gli Stati Interni !
|
||||
await OrdersCart.updateCmd(orderCart, status, true);
|
||||
|
||||
let ordertype = '';
|
||||
|
||||
if (status === shared_consts.OrderStatus.ORDER_CONFIRMED) {
|
||||
ordertype = 'order_confirmed';
|
||||
} else if (status === shared_consts.OrderStatus.RECEIVED) {
|
||||
} else if (status === shared_consts.OrderStatus.COMPLETED) {
|
||||
ordertype = 'order_completed';
|
||||
} else if (status === shared_consts.OrderStatus.CANCELED) {
|
||||
ordertype = 'order_canceled';
|
||||
|
||||
@@ -1255,7 +1255,7 @@ router.delete('/delrec/:table/:id', authenticate, async (req, res) => {
|
||||
|
||||
if (!cancellato) {
|
||||
// ELIMINA VERAMENTE IL RECORD !!!
|
||||
ris = await mytable.findByIdAndRemove(id).then((rec) => {
|
||||
ris = await mytable.deleteOne({_id: id}).then((rec) => {
|
||||
if (!rec) {
|
||||
// res.status(404).send();
|
||||
return false;
|
||||
|
||||
@@ -73,7 +73,7 @@ router.delete('/:id/:notify/:idapp', authenticate, (req, res) => {
|
||||
const notify = req.params.notify;
|
||||
const idapp = req.params.idapp;
|
||||
|
||||
myevent.findByIdAndRemove(id).then((recmyevent) => {
|
||||
myevent.deleteOne({_id: id}).then((recmyevent) => {
|
||||
if (!recmyevent) {
|
||||
return res.status(404).send();
|
||||
}
|
||||
|
||||
@@ -251,7 +251,7 @@ router.delete('/:id', authenticate, (req, res) => {
|
||||
|
||||
} else {
|
||||
|
||||
Project.findByIdAndRemove(id).then((project) => {
|
||||
Project.deleteOne({_id: id}).then((project) => {
|
||||
if (!project) {
|
||||
return res.status(404).send();
|
||||
}
|
||||
|
||||
@@ -254,7 +254,7 @@ router.delete('/:id', authenticate, (req, res) => {
|
||||
});
|
||||
|
||||
} else {
|
||||
Todo.findByIdAndRemove(id).then((todo) => {
|
||||
Todo.deleteOne({_id: id}).then((todo) => {
|
||||
if (!todo) {
|
||||
return res.status(404).send();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user