259 lines
8.2 KiB
JavaScript
Executable File
259 lines
8.2 KiB
JavaScript
Executable File
const express = require('express');
|
|
const router = express.Router();
|
|
const mongoose = require('mongoose').set('debug', false);
|
|
const { CfgServer } = require('../models/cfgserver');
|
|
|
|
const shared_consts = require('../tools/shared_nodejs');
|
|
|
|
const tools = require('../tools/general');
|
|
|
|
const { City } = require('../models/city');
|
|
const Product = require('../models/product');
|
|
const Inventariogm = require('../models/inventariogm');
|
|
const ProductInfo = require('../models/productInfo');
|
|
const CatProd = require('../models/catprod');
|
|
const SubCatProd = require('../models/subcatprod');
|
|
const Gasordine = require('../models/gasordine');
|
|
|
|
var { authenticate } = require('../middleware/authenticate');
|
|
|
|
router.post('/updateval', authenticate, async (req, res) => {
|
|
console.log('/updateval', req.body.pairval);
|
|
idapp = req.body.idapp;
|
|
pair = req.body.pairval;
|
|
|
|
return await CfgServer.findOneAndUpdate(
|
|
{ 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);
|
|
res.status(400).send();
|
|
});
|
|
|
|
});
|
|
|
|
router.post('/import', authenticate, async (req, res) => {
|
|
const cmd = req.body.cmd;
|
|
const idapp = req.body.idapp;
|
|
const data = req.body.data;
|
|
const options = req.body.data.options;
|
|
|
|
try {
|
|
const liste = require('../data/liste');
|
|
|
|
if (cmd === shared_consts.Cmd.CITIES_SERVER) {
|
|
return await City.insertMany(liste.Comuni).then((ris) => {
|
|
return res.status(200).send(true);
|
|
});
|
|
} else if (cmd === shared_consts.Cmd.INVENTARIO) {
|
|
let dataObjects = JSON.parse(`[${data.arrdata}]`);
|
|
let updated = 0;
|
|
let imported = 0;
|
|
let errors = 0;
|
|
|
|
for (const recinv of dataObjects) {
|
|
let isnuovo = false
|
|
let setta = false
|
|
|
|
let inventario = recinv;
|
|
|
|
inventario.idapp = idapp;
|
|
|
|
let risrec = await Inventariogm.findOneAndUpdate(queryprod, { $set: inventario }, { new: true, upsert: true });
|
|
}
|
|
|
|
|
|
} else if (cmd === shared_consts.Cmd.PRODUCTS) {
|
|
let dataObjects = JSON.parse(`[${data.arrdata}]`);
|
|
|
|
let updated = 0;
|
|
let imported = 0;
|
|
let errors = 0;
|
|
|
|
for (const product of dataObjects) {
|
|
let isnuovo = false
|
|
let setta = false
|
|
|
|
let productInfo = {
|
|
idapp: product.idapp,
|
|
code: product.code,
|
|
name: product.name,
|
|
link: product.link,
|
|
idCatProds: [],
|
|
idSubCatProds: [],
|
|
img: 'upload/products/' + product.code + '.jpg',
|
|
weight: product.weight,
|
|
unit: tools.getIdUnitsByText(product.unit),
|
|
}
|
|
|
|
let reccateg = null;
|
|
if (product.cat_name) {
|
|
arrcat = product.cat_name.trim().split(',');
|
|
for (const mycat of arrcat) {
|
|
let mycatstr = mycat.trim();
|
|
|
|
// Cerca la Categoria
|
|
reccateg = await CatProd.findOne({ idapp, name: mycatstr }).lean();
|
|
if (!reccateg) {
|
|
// Non esiste questo produttore, quindi lo creo !
|
|
reccateg = new CatProd({ idapp, name: mycatstr });
|
|
ris = await reccateg.save();
|
|
reccateg = await CatProd.findOne({ idapp, name: mycatstr }).lean();
|
|
}
|
|
|
|
if (reccateg) {
|
|
productInfo.idCatProds.push(reccateg._id);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (product.subcat_name) {
|
|
arrsubcat = product.subcat_name.trim().split(',');
|
|
for (const mysubcat of arrsubcat) {
|
|
let mysubcatstr = mysubcat.trim();
|
|
|
|
// Cerca la Sotto Categoria
|
|
let recsubcateg = await SubCatProd.findOne({ idapp, name: mysubcatstr }).lean();
|
|
if (!recsubcateg) {
|
|
// Non esiste questa Sotto Categoria, quindi la creo !
|
|
const idCatProd = reccateg ? reccateg._id : ''
|
|
recsubcateg = new SubCatProd({ idapp, name: mysubcatstr, idCatProd });
|
|
ris = await recsubcateg.save();
|
|
recsubcateg = await SubCatProd.findOne({ idapp, name: mysubcatstr, idCatProd }).lean();
|
|
}
|
|
|
|
if (recsubcateg) {
|
|
productInfo.idSubCatProds.push(recsubcateg._id);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!product.hasOwnProperty('active')) {
|
|
product.active = true;
|
|
}
|
|
|
|
if (product.code)
|
|
delete product.code;
|
|
if (product.name)
|
|
delete product.name;
|
|
if (product.link)
|
|
delete product.link;
|
|
|
|
let esisteindb = await ProductInfo.findOne({ code: productInfo.code }).lean();
|
|
|
|
// Update ProductInfo
|
|
let risrecInfo = await ProductInfo.findOneAndUpdate({ code: productInfo.code }, { $set: productInfo }, { new: true, upsert: true });
|
|
if (risrecInfo) {
|
|
product.idProductInfo = risrecInfo._id;
|
|
|
|
recnewInfo = await ProductInfo.findOne({ code: productInfo.code }).lean();
|
|
|
|
if (risrecInfo._id) {
|
|
// Record existed, so it was updated
|
|
let arrfieldchange = tools.differentObjects(productInfo, recnewInfo);
|
|
if (arrfieldchange && arrfieldchange.length > 0) {
|
|
// updated++;
|
|
console.log('Changed: ', recnewInfo.name + ': ' + arrfieldchange);
|
|
}
|
|
}
|
|
|
|
// Cerca il GAS
|
|
let recGas = null;
|
|
if (product.gas_name) {
|
|
// Cerca il GAS
|
|
recGas = await Gasordine.findOne({ idapp, name: product.gas_name }).lean();
|
|
}
|
|
|
|
if (!recGas && !!product.gas_name) {
|
|
recGas = new Gasordine({ idapp, name: product.gas_name, active: true });
|
|
// Non esiste questo GAS, quindi lo creo !
|
|
ris = await recGas.save();
|
|
recGas = await Gasordine.findOne({ idapp, name: product.gas_name }).lean();
|
|
}
|
|
|
|
let recProductExist = null;
|
|
let queryprod = { idProductInfo: product.idProductInfo };
|
|
|
|
if (recGas) {
|
|
queryprod = {...queryprod, idGasordine: recGas._id};
|
|
}
|
|
|
|
recProductExist = await Product.findOne({ queryprod }).lean();
|
|
|
|
if (!recProductExist) {
|
|
isnuovo = true;
|
|
}
|
|
|
|
if (!options.aggiornaStockQty && esisteindb && !isnuovo) {
|
|
delete product.stockQty;
|
|
delete product.maxbookableGASQty;
|
|
}
|
|
|
|
// AGGIORNA PRODUCT
|
|
let risrec = await Product.findOneAndUpdate(queryprod, { $set: product }, { new: true, upsert: true });
|
|
|
|
let recnew = await Product.findOne(queryprod).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++;
|
|
}
|
|
} else {
|
|
// risrec is null or undefined, indicating an error
|
|
console.error('Error: ', product.productInfo.name);
|
|
errors++;
|
|
}
|
|
|
|
await Product.singlerecconvert_AfterImport_AndSave(idapp, recnew, isnuovo);
|
|
} else {
|
|
console.error('Error ProductInfo: ', product.code);
|
|
errors++;
|
|
}
|
|
|
|
}
|
|
|
|
// L'opzione ordered: false gestisce gli errori senza interrompere l'inserimento
|
|
/*return await Product.insertMany(dataObjects, { ordered: false })
|
|
.then((ris) => {
|
|
|
|
Product.convertAfterImportALLPROD(idapp, dataObjects).then((ris) => {
|
|
return res.status(200).send(true);
|
|
});
|
|
|
|
|
|
})
|
|
.catch((errors) => {
|
|
console.error(errors);
|
|
Product.convertAfterImportALLPROD(idapp).then((ris) => {
|
|
return res.status(200).send(true);
|
|
});
|
|
});*/
|
|
|
|
return res.status(200).send({ updated, imported, errors });
|
|
|
|
}
|
|
} catch (e) {
|
|
console.error('e', e);
|
|
res.status(400).send();
|
|
}
|
|
|
|
res.status(400).send();
|
|
|
|
});
|
|
|
|
module.exports = router;
|