2019-02-22 10:23:39 +01:00
|
|
|
const express = require('express');
|
|
|
|
|
const router = express.Router();
|
2022-09-14 11:32:04 +02:00
|
|
|
const mongoose = require('mongoose').set('debug', false);
|
2023-12-14 15:20:21 +01:00
|
|
|
const { CfgServer } = require('../models/cfgserver');
|
2019-02-22 10:23:39 +01:00
|
|
|
|
2022-01-20 00:39:06 +01:00
|
|
|
const shared_consts = require('../tools/shared_nodejs');
|
|
|
|
|
|
2019-02-22 10:23:39 +01:00
|
|
|
const tools = require('../tools/general');
|
|
|
|
|
|
2023-12-14 15:20:21 +01:00
|
|
|
const { City } = require('../models/city');
|
|
|
|
|
const Product = require('../models/product');
|
2023-12-27 02:58:15 +01:00
|
|
|
const ProductInfo = require('../models/productinfo');
|
|
|
|
|
const CatProd = require('../models/catprod');
|
2022-01-20 00:39:06 +01:00
|
|
|
|
2023-12-14 15:20:21 +01:00
|
|
|
var { authenticate } = require('../middleware/authenticate');
|
2019-02-22 10:23:39 +01:00
|
|
|
|
2020-04-24 10:29:25 +02:00
|
|
|
router.post('/updateval', authenticate, async (req, res) => {
|
2019-02-22 10:23:39 +01:00
|
|
|
console.log('/updateval', req.body.pairval);
|
2020-04-24 10:29:25 +02:00
|
|
|
idapp = req.body.idapp;
|
2019-02-22 10:23:39 +01:00
|
|
|
pair = req.body.pairval;
|
|
|
|
|
|
2022-05-14 00:31:53 +02:00
|
|
|
return await CfgServer.findOneAndUpdate(
|
2023-12-14 15:20:21 +01:00
|
|
|
{ chiave: pair.chiave, idapp, userId: pair.userId }, { $set: pair },
|
|
|
|
|
{ new: false }).then((item) => {
|
|
|
|
|
// CfgServer.find({ chiave: pair.chiave }, (err, item) => {
|
|
|
|
|
if (!!item) {
|
|
|
|
|
res.status(200).send();
|
|
|
|
|
} else {
|
|
|
|
|
res.status(400).send();
|
|
|
|
|
}
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
console.log('ERR:', err);
|
2022-01-20 00:39:06 +01:00
|
|
|
res.status(400).send();
|
2023-12-14 15:20:21 +01:00
|
|
|
});
|
2019-02-22 10:23:39 +01:00
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2022-01-20 00:39:06 +01:00
|
|
|
router.post('/import', authenticate, async (req, res) => {
|
|
|
|
|
const cmd = req.body.cmd;
|
2023-12-15 00:57:14 +01:00
|
|
|
const idapp = req.body.idapp;
|
2023-12-14 15:20:21 +01:00
|
|
|
const data = req.body.data;
|
2022-01-20 00:39:06 +01:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const liste = require('../data/liste');
|
|
|
|
|
|
|
|
|
|
if (cmd === shared_consts.Cmd.CITIES_SERVER) {
|
2022-09-11 11:45:33 +02:00
|
|
|
return await City.insertMany(liste.Comuni).then((ris) => {
|
2022-01-20 00:39:06 +01:00
|
|
|
return res.status(200).send(true);
|
|
|
|
|
});
|
|
|
|
|
|
2023-12-14 15:20:21 +01:00
|
|
|
} else if (cmd === shared_consts.Cmd.PRODUCTS) {
|
|
|
|
|
|
|
|
|
|
let dataObjects = JSON.parse(`[${data}]`);
|
|
|
|
|
|
2023-12-18 12:11:12 +01:00
|
|
|
let updated = 0;
|
|
|
|
|
let imported = 0;
|
|
|
|
|
let errors = 0;
|
|
|
|
|
|
|
|
|
|
for (const product of dataObjects) {
|
2023-12-27 02:58:15 +01:00
|
|
|
let isnuovo = false
|
|
|
|
|
let setta = false
|
|
|
|
|
|
|
|
|
|
let productInfo = {
|
|
|
|
|
idapp: product.idapp,
|
|
|
|
|
code: product.code,
|
|
|
|
|
name: product.name,
|
|
|
|
|
link: product.link,
|
|
|
|
|
idCatProds: [],
|
|
|
|
|
img: 'upload/products/' + product.code + '.jpg',
|
2023-12-28 00:50:35 +01:00
|
|
|
weight: product.weight,
|
|
|
|
|
unit: tools.getIdUnitsByText(product.unit),
|
2023-12-27 02:58:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (product.cat_name) {
|
|
|
|
|
// Cerca la Categoria
|
|
|
|
|
let reccateg = await CatProd.findOne({ idapp, name: product.cat_name }).lean();
|
|
|
|
|
if (!reccateg) {
|
|
|
|
|
// Non esiste questo produttore, quindi lo creo !
|
|
|
|
|
reccateg = new CatProd({ idapp, name: product.cat_name });
|
|
|
|
|
ris = await reccateg.save();
|
|
|
|
|
reccateg = await CatProd.findOne({ idapp, name: product.cat_name }).lean();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (reccateg) {
|
|
|
|
|
productInfo.idCatProds.push(reccateg._id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-18 12:11:12 +01:00
|
|
|
if (!product.hasOwnProperty('active')) {
|
|
|
|
|
product.active = true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-27 02:58:15 +01:00
|
|
|
delete product.code;
|
|
|
|
|
delete product.name;
|
|
|
|
|
delete product.link;
|
|
|
|
|
|
|
|
|
|
// Update ProductInfo
|
|
|
|
|
let risrecInfo = await ProductInfo.findOneAndUpdate({ code: productInfo.code }, { $set: productInfo }, { new: true, upsert: true });
|
|
|
|
|
if (risrecInfo) {
|
|
|
|
|
product.idProductInfo = risrecInfo._id;
|
2023-12-18 12:11:12 +01:00
|
|
|
|
2023-12-27 02:58:15 +01:00
|
|
|
recnewInfo = await ProductInfo.findOne({ code: productInfo.code }).lean();
|
|
|
|
|
|
|
|
|
|
if (risrecInfo._id) {
|
2023-12-18 12:11:12 +01:00
|
|
|
// Record existed, so it was updated
|
2023-12-27 02:58:15 +01:00
|
|
|
let arrfieldchange = tools.differentObjects(productInfo, recnewInfo);
|
2023-12-18 12:11:12 +01:00
|
|
|
if (arrfieldchange.length > 0) {
|
2023-12-27 02:58:15 +01:00
|
|
|
// updated++;
|
|
|
|
|
console.log('Changed: ', recnewInfo.name + ': ' + arrfieldchange);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let recProductExist = await Product.findOne({ idProductInfo: product.idProductInfo }).lean();
|
|
|
|
|
if (!recProductExist) {
|
|
|
|
|
isnuovo = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let risrec = await Product.findOneAndUpdate({ idProductInfo: product.idProductInfo }, { $set: product }, { new: true, upsert: true });
|
|
|
|
|
|
|
|
|
|
let recnew = await Product.findOne({ idProductInfo: product.idProductInfo }).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.idProductInfo + ': ' + arrfieldchange);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Record didn't exist, so it was created
|
|
|
|
|
imported++;
|
2023-12-18 12:11:12 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2023-12-27 02:58:15 +01:00
|
|
|
// risrec is null or undefined, indicating an error
|
|
|
|
|
console.error('Error: ', product.productInfo.name);
|
|
|
|
|
errors++;
|
2023-12-18 12:11:12 +01:00
|
|
|
}
|
2023-12-27 02:58:15 +01:00
|
|
|
|
|
|
|
|
await Product.singlerecconvert_AfterImport(idapp, recnew, isnuovo);
|
2023-12-18 12:11:12 +01:00
|
|
|
} else {
|
2023-12-27 02:58:15 +01:00
|
|
|
console.error('Error ProductInfo: ', product.code);
|
2023-12-18 12:11:12 +01:00
|
|
|
errors++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-15 00:57:14 +01:00
|
|
|
// L'opzione ordered: false gestisce gli errori senza interrompere l'inserimento
|
2023-12-18 12:11:12 +01:00
|
|
|
/*return await Product.insertMany(dataObjects, { ordered: false })
|
2023-12-15 00:57:14 +01:00
|
|
|
.then((ris) => {
|
2023-12-14 15:20:21 +01:00
|
|
|
|
2023-12-15 00:57:14 +01:00
|
|
|
Product.convertAfterImport(idapp, dataObjects).then((ris) => {
|
|
|
|
|
return res.status(200).send(true);
|
|
|
|
|
});
|
2023-12-14 15:20:21 +01:00
|
|
|
|
|
|
|
|
|
2023-12-15 00:57:14 +01:00
|
|
|
})
|
|
|
|
|
.catch((errors) => {
|
|
|
|
|
console.error(errors);
|
|
|
|
|
Product.convertAfterImport(idapp).then((ris) => {
|
|
|
|
|
return res.status(200).send(true);
|
|
|
|
|
});
|
2023-12-18 12:11:12 +01:00
|
|
|
});*/
|
|
|
|
|
|
|
|
|
|
return res.status(200).send({ updated, imported, errors });
|
2023-12-14 15:20:21 +01:00
|
|
|
|
2022-01-20 00:39:06 +01:00
|
|
|
}
|
2023-12-14 15:20:21 +01:00
|
|
|
} catch (e) {
|
2022-01-20 00:39:06 +01:00
|
|
|
console.error('e', e);
|
|
|
|
|
res.status(400).send();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.status(400).send();
|
|
|
|
|
|
|
|
|
|
});
|
2019-02-22 10:23:39 +01:00
|
|
|
|
|
|
|
|
module.exports = router;
|