Carrello con scontistica aggiornata
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user