Ordini
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
const shared_consts = require('../tools/shared_nodejs');
|
||||
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
@@ -29,6 +31,7 @@ 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, function (req, res, next) {
|
||||
@@ -166,5 +169,70 @@ router.put('/:userId', authenticate, function (req, res, next) {
|
||||
})
|
||||
})
|
||||
|
||||
//POST cart
|
||||
router.post('/:userId/cartstatus', authenticate, async function (req, res, next) {
|
||||
let idapp = req.body.idapp;
|
||||
let userId = req.params.userId;
|
||||
let cart_id = req.body.cart_id;
|
||||
let status = req.body.status;
|
||||
|
||||
const mycart = await Cart.getCartByUserId(userId, idapp);
|
||||
|
||||
try {
|
||||
if (!!mycart) {
|
||||
if (status === shared_consts.OrderStatus.CHECKOUT_CONFIRMED) {
|
||||
// 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: await OrdersCart.getNewNumOrder(userId, idapp),
|
||||
created_at: new Date(),
|
||||
modify_at: new Date(),
|
||||
})
|
||||
const myorderlist = OrdersCart.updateOrdersCartById(-1, newOrderCart, function (err, ris) {
|
||||
//if (err) return next(err)
|
||||
if (err)
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, status: 0 });
|
||||
else {
|
||||
const myris = ris;
|
||||
// Cancella il Cart appena salvato in OrdersCart
|
||||
Cart.deleteCartByCartId(mycart.id)
|
||||
.then((ris) => {
|
||||
const orders = OrdersCart.getOrdersCartByUserId(userId, idapp)
|
||||
.then((orders) => {
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, status: myris.status, orders });
|
||||
});
|
||||
})
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
// 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
|
||||
}) */
|
||||
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, status: 0 });
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -54,7 +54,9 @@ const { MsgTemplate } = require('../models/msg_template');
|
||||
const Product = require('../models/product');
|
||||
const Producer = require('../models/producer');
|
||||
const Cart = require('../models/cart');
|
||||
const OrdersCart = require('../models/orderscart');
|
||||
const Storehouse = require('../models/storehouse');
|
||||
const ShareWithUs = require('../models/sharewithus');
|
||||
const Order = require('../models/order');
|
||||
|
||||
const tools = require('../tools/general');
|
||||
@@ -200,6 +202,8 @@ function getTableByTableName(tablename) {
|
||||
mytable = Product;
|
||||
else if (tablename === 'storehouses')
|
||||
mytable = Storehouse;
|
||||
else if (tablename === 'sharewithus')
|
||||
mytable = ShareWithUs;
|
||||
else if (tablename === 'orders')
|
||||
mytable = Order;
|
||||
else if (tablename === 'producers')
|
||||
@@ -270,7 +274,7 @@ router.post('/settable', authenticate, (req, res) => {
|
||||
}
|
||||
|
||||
|
||||
mytablerec = new mytable(mydata);
|
||||
let mytablerec = new mytable(mydata);
|
||||
console.log('mytablerec', mytablerec);
|
||||
|
||||
|
||||
@@ -993,7 +997,8 @@ router.delete('/delrec/:table/:id', authenticate, async (req, res) => {
|
||||
if (!cancellato) {
|
||||
ris = await mytable.findByIdAndRemove(id).then((rec) => {
|
||||
if (!rec) {
|
||||
return res.status(404).send();
|
||||
// res.status(404).send();
|
||||
return false;
|
||||
}
|
||||
myrec = rec;
|
||||
|
||||
@@ -1012,7 +1017,7 @@ router.delete('/delrec/:table/:id', authenticate, async (req, res) => {
|
||||
|
||||
if (cancellato) {
|
||||
// Do extra things after deleted
|
||||
let ris2 = await actions.doOtherlasThingsAfterDeleted(tablename, myrec, notifBot, req);
|
||||
//let ris2 = await actions.doOtherlasThingsAfterDeleted(tablename, myrec, notifBot, req);
|
||||
if (!!ris) {
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, msg: '' });
|
||||
}
|
||||
@@ -1108,6 +1113,7 @@ router.get('/loadsite/:userId/:idapp/:sall', authenticate_noerror, (req, res) =>
|
||||
let producers = Producer.findAllIdApp(idapp);
|
||||
let storehouses = Storehouse.findAllIdApp(idapp);
|
||||
let cart = null;
|
||||
let orderscart = null;
|
||||
if (sall) {
|
||||
newstosent = Newstosent.findAllIdApp(idapp);
|
||||
}
|
||||
@@ -1116,10 +1122,11 @@ router.get('/loadsite/:userId/:idapp/:sall', authenticate_noerror, (req, res) =>
|
||||
if (req.user) {
|
||||
calcstat = User.calculateStat(idapp, req.user.username);
|
||||
cart = Cart.getCartByUserId(req.user.id, idapp);
|
||||
orderscart = OrdersCart.getOrdersCartByUserId(req.user.id, idapp);
|
||||
}
|
||||
|
||||
|
||||
return Promise.all([bookedevent, eventlist, operators, wheres, contribtype, settings, permissions, disciplines, newstosent, mailinglist, mypage, gallery, paymenttype, calcstat, calzoom, producers, cart, storehouses])
|
||||
return Promise.all([bookedevent, eventlist, operators, wheres, contribtype, settings, permissions, disciplines, newstosent, mailinglist, mypage, gallery, paymenttype, calcstat, calzoom, producers, cart, storehouses, orderscart])
|
||||
.then((arrdata) => {
|
||||
// console.table(arrdata);
|
||||
const myuser = req.user;
|
||||
@@ -1146,6 +1153,7 @@ router.get('/loadsite/:userId/:idapp/:sall', authenticate_noerror, (req, res) =>
|
||||
producers: arrdata[15],
|
||||
cart: arrdata[16],
|
||||
storehouses: arrdata[17],
|
||||
orders: arrdata[18],
|
||||
myuser,
|
||||
});
|
||||
})
|
||||
|
||||
@@ -33,7 +33,7 @@ const Cart = require('../models/cart');
|
||||
router.post('/', auth_default, async function (req, res, next) {
|
||||
const idapp = req.body.idapp;
|
||||
|
||||
var products = await Product.findAllIdApp(idapp);
|
||||
var products = await Product.findAllIdApp(idapp, "");
|
||||
|
||||
if (products)
|
||||
res.send({ code: server_constants.RIS_CODE_OK, products });
|
||||
@@ -56,5 +56,16 @@ router.post('/', auth_default, async function (req, res, next) {
|
||||
|
||||
});
|
||||
|
||||
router.post('/:code', auth_default, async function (req, res, next) {
|
||||
const idapp = req.body.idapp;
|
||||
const code = req.body.code;
|
||||
|
||||
var product = await Product.findAllIdApp(idapp, code);
|
||||
|
||||
if (product.length > 0) {
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, product: product[0] });
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -83,6 +83,58 @@ router.post('/', (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
async function SendMsgToAll(idapp, params) {
|
||||
const arrusers = await User.find({ idapp },
|
||||
{
|
||||
username: 1,
|
||||
name: 1,
|
||||
surname: 1,
|
||||
}
|
||||
);
|
||||
|
||||
let msgsent = 0;
|
||||
|
||||
for (const user of arrusers) {
|
||||
await SendMsgTo(idapp, user.username, params);
|
||||
msgsent++;
|
||||
}
|
||||
|
||||
return msgsent;
|
||||
}
|
||||
|
||||
async function SendMsgToParam(idapp, params) {
|
||||
const arrusers = await User.find({ idapp },
|
||||
{
|
||||
username: 1,
|
||||
name: 1,
|
||||
surname: 1,
|
||||
'profile.socio': 1,
|
||||
'profile.socioresidente': 1,
|
||||
}
|
||||
);
|
||||
|
||||
let msgsent = 0;
|
||||
|
||||
for (const user of arrusers) {
|
||||
let invia = false;
|
||||
if (params.typemsg === shared_consts.TypeMsg.SEND_TO_SOCI) {
|
||||
invia = user.profile.socio;
|
||||
}else if (params.typemsg === shared_consts.TypeMsg.SEND_TO_SOCIO_RESIDENTE) {
|
||||
invia = user.profile.socioresidente;
|
||||
}else if (params.typemsg === shared_consts.TypeMsg.SEND_TO_NON_SOCI) {
|
||||
invia = !user.profile.socio;
|
||||
} else {
|
||||
invia = true;
|
||||
}
|
||||
if (invia) {
|
||||
await SendMsgTo(idapp, user.username, params);
|
||||
msgsent++;
|
||||
}
|
||||
}
|
||||
|
||||
return msgsent;
|
||||
}
|
||||
|
||||
async function SendMsgTo(idapp, username, params) {
|
||||
|
||||
return await User.find({ idapp, username }).then((arrusers) => {
|
||||
@@ -105,25 +157,6 @@ async function SendMsgTo(idapp, username, params) {
|
||||
|
||||
}
|
||||
|
||||
async function SendMsgToAll(idapp, params) {
|
||||
const arrusers = await User.find({ idapp },
|
||||
{
|
||||
username: 1,
|
||||
name: 1,
|
||||
surname: 1,
|
||||
}
|
||||
);
|
||||
|
||||
let msgsent = 0;
|
||||
|
||||
for (const user of arrusers) {
|
||||
await SendMsgTo(idapp, user.username, params);
|
||||
msgsent++;
|
||||
}
|
||||
|
||||
return msgsent;
|
||||
}
|
||||
|
||||
router.post('/send', authenticate, async (req, res) => {
|
||||
const idapp = req.body.idapp;
|
||||
const params = req.body.params;
|
||||
@@ -138,9 +171,11 @@ router.post('/send', authenticate, async (req, res) => {
|
||||
try {
|
||||
if (params.typemsg === shared_consts.TypeMsg.SEND_TO_ALL) {
|
||||
nummsg = await SendMsgToAll(idapp, params);
|
||||
} else {
|
||||
nummsg = await SendMsgToParam(idapp, params);
|
||||
}
|
||||
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, msg: nummsg + ' Msg Inviati !' });
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, msg: nummsg + ' Msg Inviati !', nummsg });
|
||||
}catch (e) {
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, msg: nummsg + ' Msg Inviati !' });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user