Carrello con scontistica aggiornata

This commit is contained in:
Surya Paolo
2023-12-18 12:11:12 +01:00
parent 56b5bac5f0
commit 7628fb97fc
7 changed files with 154 additions and 66 deletions

View File

@@ -50,8 +50,41 @@ router.post('/import', authenticate, async (req, res) => {
let dataObjects = JSON.parse(`[${data}]`);
let updated = 0;
let imported = 0;
let errors = 0;
for (const product of dataObjects) {
if (!product.hasOwnProperty('active')) {
product.active = true;
}
let risrec = await Product.findOneAndUpdate({ code: product.code }, { $set: product }, { new: true, upsert: true });
let recnew = await Product.findOne({ code: product.code }).lean();
if (risrec) {
if (risrec._id) {
// Record existed, so it was updated
let arrfieldchange = tools.differentObjects(product, recnew);
if (arrfieldchange.length > 0) {
updated++;
console.log('Changed: ', product.name + ': ' + arrfieldchange);
}
} else {
// Record didn't exist, so it was created
imported++;
}
} else {
// risrec is null or undefined, indicating an error
console.error('Error: ', product.name);
errors++;
}
await Product.singlerecconvert_AfterImport(idapp, recnew);
}
// L'opzione ordered: false gestisce gli errori senza interrompere l'inserimento
return await Product.insertMany(dataObjects, { ordered: false })
/*return await Product.insertMany(dataObjects, { ordered: false })
.then((ris) => {
Product.convertAfterImport(idapp, dataObjects).then((ris) => {
@@ -65,7 +98,9 @@ router.post('/import', authenticate, async (req, res) => {
Product.convertAfterImport(idapp).then((ris) => {
return res.status(200).send(true);
});
});
});*/
return res.status(200).send({ updated, imported, errors });
}
} catch (e) {

View File

@@ -58,8 +58,12 @@ router.post('/:userId', authenticate, async function (req, res, next) {
let mycart = await Cart.getCartByUserId(userId, idapp);
// const myorder = Order.getOrderByID(order._id);
if (!addqty && !subqty)
if (!addqty && !subqty) {
order._id = await Order.createOrder(order);
if (!order._id) {
return res.send({ code: server_constants.RIS_CODE_ERR, cart: 0 });
}
}
let cart = null;
let product = null;
@@ -222,7 +226,7 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
let note = req.body.note;
try {
const mycart = await Cart.findOne({ _id: cart_id });
let mycart = await Cart.findOne({ _id: cart_id });
let numorder = await OrdersCart.getLastNumOrder(idapp);
let numord_pers = await OrdersCart.getLastNumOrdPers(userId, idapp);
@@ -301,6 +305,7 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
recOrderCart: myorderCart
});
} catch (e) {
console.error('Err', e);
return res.send({ code: server_constants.RIS_CODE_ERR, status: 0, recOrderCart: null });
}