- catalogo avanti, parte 1
This commit is contained in:
@@ -13,6 +13,8 @@ const Inventariogm = require('../models/inventariogm');
|
||||
const Importamacro = require('../models/importamacro');
|
||||
const ProductInfo = require('../models/productInfo');
|
||||
const CatProd = require('../models/catprod');
|
||||
const Author = require('../models/author');
|
||||
const Publisher = require('../models/publisher');
|
||||
const SubCatProd = require('../models/subcatprod');
|
||||
const Gasordine = require('../models/gasordine');
|
||||
|
||||
@@ -113,6 +115,11 @@ async function completaSettaggioProduct_AndProductInfo(arrcampi_productInfo, arr
|
||||
productInfo.code = preccode;
|
||||
}
|
||||
}
|
||||
if (rec.hasOwnProperty('productType')) {
|
||||
productInfo.productType = rec.hasOwnProperty('productType');
|
||||
} else {
|
||||
productInfo.productType = shared_consts.PRODUCTTYPE.PRODUCT;
|
||||
}
|
||||
|
||||
return { product, productInfo };
|
||||
|
||||
@@ -215,6 +222,17 @@ async function extractArrayDataFromCSV(idapp, rec) {
|
||||
|
||||
};
|
||||
|
||||
function extractNameAndSurnameByComplete(name_complete) {
|
||||
|
||||
if (name_complete) {
|
||||
const name = name_complete.split(' ')[0];
|
||||
const surname = name_complete.split(' ')[1];
|
||||
return { name, surname };
|
||||
} else {
|
||||
return { name: '', surname: '' };
|
||||
}
|
||||
}
|
||||
|
||||
router.post('/import', authenticate, async (req, res) => {
|
||||
|
||||
try {
|
||||
@@ -263,7 +281,7 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
if (dataObjects && dataObjects[0]) {
|
||||
// Cancella la collection ImportaMacros
|
||||
await Importamacro.deleteMany({ idapp });
|
||||
|
||||
|
||||
// Aggiungi i record su ImportaMacros
|
||||
for (const recinv of dataObjects[0]) {
|
||||
let isnuovo = false
|
||||
@@ -297,191 +315,317 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
if (cancella_categorie) {
|
||||
await CatProd.deleteMany({ idapp });
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Rileggi tutti i record di ImportaMacros
|
||||
dataObjects = await Importamacro.find({ idapp }).lean();
|
||||
|
||||
console.log('*** INIZIO IMPORT PRODOTTI ... ');
|
||||
|
||||
let indprod = 0;
|
||||
let newprod = 0;
|
||||
|
||||
for (const product of dataObjects) {
|
||||
let isnuovo = false
|
||||
let setta = false
|
||||
|
||||
let productInfo = {
|
||||
idapp: product.idapp,
|
||||
code: product.isbn,
|
||||
id_wp: product._id,
|
||||
name: product.title,
|
||||
description: product.description,
|
||||
publish: product.Editore,
|
||||
collezione: product.Collezione,
|
||||
numpages: product.NumPagine,
|
||||
author: product.autore,
|
||||
link: product.link,
|
||||
idCatProds: [],
|
||||
idSubCatProds: [],
|
||||
//img: 'upload/products/' + product.code + '.jpg',
|
||||
image_link: product.image_link,
|
||||
}
|
||||
let importa = true;
|
||||
|
||||
if (product.NumPagine)
|
||||
productInfo.numpages = product.NumPagine;
|
||||
if (!product.title || !product.isbn)
|
||||
importa = false;
|
||||
|
||||
let reccateg = null;
|
||||
if (product.categories) {
|
||||
arrcat = product.categories.trim().split(',');
|
||||
for (const mycat of arrcat) {
|
||||
let mycatstr = mycat.trim();
|
||||
if (importa) {
|
||||
let productInfo = {
|
||||
idapp: product.idapp,
|
||||
code: product.isbn,
|
||||
id_wp: product._id,
|
||||
name: product.title,
|
||||
description: product.description,
|
||||
publisher: product.Editore,
|
||||
collezione: product.Collezione,
|
||||
// author: product.Autore ? product.Autore : '',
|
||||
link: product.link ? product.link : '',
|
||||
idCatProds: [],
|
||||
idSubCatProds: [],
|
||||
//img: 'upload/products/' + product.code + '.jpg',
|
||||
image_link: product.image_link ? product.image_link : '',
|
||||
img2: product.img2 ? product.img2 : '',
|
||||
img3: product.img3 ? product.img3 : '',
|
||||
img4: product.img4 ? product.img4 : '',
|
||||
|
||||
// Controlla se ci sono le sottocategorie:
|
||||
arrsubcat = mycatstr.trim().split('>');
|
||||
}
|
||||
|
||||
if (arrsubcat.length > 1) {
|
||||
// Ci sono delle sottocategorie
|
||||
mycatstr = arrsubcat[0].trim();
|
||||
product.subcat_name = arrsubcat[1].trim();;
|
||||
productInfo.productType = shared_consts.PRODUCTTYPE.LIBRO;
|
||||
|
||||
if (product.Data)
|
||||
productInfo.date_publishing = new Date(product.Data * 1000);
|
||||
|
||||
productInfo.name = productInfo.name.replace(/ - Usato$| - Nuovo$| - Epub$| - Ebook$| - Mobi$| - DVD$| - Streaming$| - Download$/, "");
|
||||
|
||||
if (product.Pagine) {
|
||||
try {
|
||||
productInfo.numpages = 0;
|
||||
productInfo.numpages = parseInt(product.Pagine);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
// 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();
|
||||
let reccateg = null;
|
||||
if (product.categories) {
|
||||
arrcat = product.categories.trim().split(',');
|
||||
for (const mycat of arrcat) {
|
||||
let mycatstr = mycat.trim();
|
||||
|
||||
// Controlla se ci sono le sottocategorie:
|
||||
arrsubcat = mycatstr.trim().split('>');
|
||||
|
||||
if (arrsubcat.length > 1) {
|
||||
// Ci sono delle sottocategorie
|
||||
mycatstr = arrsubcat[0].trim();
|
||||
product.subcat_name = arrsubcat[1].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);
|
||||
if (reccateg) {
|
||||
productInfo.idCatProds.push(reccateg._id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cerca il GAS
|
||||
let recGas = null;
|
||||
if (product.gas_name) {
|
||||
// Cerca il GAS
|
||||
recGas = await Gasordine.findOne({ idapp, name: product.gas_name }).lean();
|
||||
// "Autore" : "Silia,Marucelli, Stefano,Cattinelli",
|
||||
let arrAuthor = [];
|
||||
if (product.Autore) {
|
||||
const arrrecauthor = product.Autore.trim().split(',');
|
||||
|
||||
if (arrrecauthor.length >= 1) {
|
||||
try {
|
||||
let author = {
|
||||
name: arrrecauthor[0].trim(),
|
||||
}
|
||||
if (arrrecauthor.length === 1) {
|
||||
author = extractNameAndSurnameByComplete(arrrecauthor[0].trim());
|
||||
} else {
|
||||
author.surname = arrrecauthor[1].trim();
|
||||
}
|
||||
arrAuthor.push(author);
|
||||
|
||||
if (arrrecauthor.length > 1) {
|
||||
for (let i = 2; i < arrrecauthor.length; i = i + 2) {
|
||||
try {
|
||||
author = {
|
||||
name: arrrecauthor[i].trim()
|
||||
}
|
||||
if (arrrecauthor.length > i + 1) {
|
||||
surname = arrrecauthor[i + 1].trim()
|
||||
}
|
||||
arrAuthor.push(author);
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
productInfo.idAuthors = [];
|
||||
|
||||
for (const myauthor of arrAuthor) {
|
||||
// Cerca l'Autore
|
||||
recauthor = await Author.findOne({ idapp, name: myauthor.name, surname: myauthor.surname }).lean();
|
||||
if (!recauthor) {
|
||||
// Non esiste questo Autore, quindi lo creo !
|
||||
recauthor = new Author({ idapp, name: myauthor.name, surname: myauthor.surname });
|
||||
ris = await recauthor.save();
|
||||
recauthor = await Author.findOne({ idapp, name: myauthor.name, surname: myauthor.surname }).lean();
|
||||
}
|
||||
|
||||
if (recauthor) {
|
||||
productInfo.idAuthors.push(recauthor._id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
// Cancella l'Autore dal record product
|
||||
delete product.Autore;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let recProductExist = null;
|
||||
let queryprod = { idProductInfo: product.idProductInfo };
|
||||
|
||||
if (recGas) {
|
||||
queryprod = { ...queryprod, idGasordine: recGas._id };
|
||||
if (!product.hasOwnProperty('active')) {
|
||||
product.active = true;
|
||||
}
|
||||
|
||||
recProductExist = await Product.findOne({ queryprod }).lean();
|
||||
if (product.code)
|
||||
delete product.code;
|
||||
if (product.name)
|
||||
delete product.name;
|
||||
if (product.link)
|
||||
delete product.link;
|
||||
|
||||
if (!recProductExist) {
|
||||
isnuovo = true;
|
||||
}
|
||||
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 (!options.aggiornaStockQty && esisteindb && !isnuovo) {
|
||||
delete product.stockQty;
|
||||
delete product.maxbookableGASQty;
|
||||
}
|
||||
|
||||
let myproduct = {
|
||||
stockQty: product.Quantita,
|
||||
maxbookableGASQty: product.maxbookableGASQty,
|
||||
active: product.active,
|
||||
idGasordine: recGas ? recGas._id : null,
|
||||
idProductInfo: product.idProductInfo,
|
||||
idapp,
|
||||
price: tools.convertPriceEurToValue(product.price),
|
||||
sale_price: tools.convertPriceEurToValue(product.sale_price),
|
||||
status: product.status,
|
||||
}
|
||||
|
||||
// AGGIORNA PRODUCT
|
||||
let risrec = await Product.findOneAndUpdate(queryprod, { $set: myproduct }, { new: true, upsert: true });
|
||||
|
||||
let recnew = await Product.findOne(queryprod).lean();
|
||||
|
||||
if (risrec) {
|
||||
if (risrec._id) {
|
||||
if (risrecInfo._id) {
|
||||
// Record existed, so it was updated
|
||||
let arrfieldchange = tools.differentObjects(myproduct, recnew);
|
||||
if (arrfieldchange.length > 0) {
|
||||
let arrfieldchange = tools.differentObjects(productInfo, recnewInfo);
|
||||
if (arrfieldchange && arrfieldchange.length > 0) {
|
||||
updated++;
|
||||
console.log('Changed: ', myproduct.idProductInfo + ': ' + arrfieldchange);
|
||||
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 myproduct = {};
|
||||
|
||||
// Download, DVD, Epub, Mobi, Nuovo, PDF, Streaming, Usato
|
||||
if (product.Versione === 'Nuovo')
|
||||
myproduct.versione = shared_consts.VERSIONE.NUOVO
|
||||
else if (product.Versione === 'Usato')
|
||||
myproduct.versione = shared_consts.VERSIONE.USATO;
|
||||
else if (product.Versione === 'Download')
|
||||
myproduct.versione = shared_consts.VERSIONE.DOWNLOAD;
|
||||
else if (product.Versione === 'DVD')
|
||||
myproduct.versione = shared_consts.VERSIONE.DVD;
|
||||
else if (product.Versione === 'Epub')
|
||||
myproduct.versione = shared_consts.VERSIONE.EPUB;
|
||||
else if (product.Versione === 'Mobi')
|
||||
myproduct.versione = shared_consts.VERSIONE.MOBI;
|
||||
else if (product.Versione === 'PDF')
|
||||
myproduct.versione = shared_consts.VERSIONE.PDF;
|
||||
else if (product.Versione === 'Streaming')
|
||||
myproduct.versione = shared_consts.VERSIONE.STREAMING;
|
||||
else
|
||||
myproduct.versione = 100;
|
||||
|
||||
delete product.Versione;
|
||||
|
||||
let recProductExist = null;
|
||||
// ISBN e versione del prodotto sono le chiavi uniche
|
||||
let queryprod = { idProductInfo: product.idProductInfo, versione: myproduct.versione };
|
||||
|
||||
if (recGas) {
|
||||
queryprod = { ...queryprod, idGasordine: recGas._id };
|
||||
}
|
||||
|
||||
recProductExist = await Product.findOne({ queryprod }).lean();
|
||||
|
||||
if (!recProductExist) {
|
||||
isnuovo = true;
|
||||
}
|
||||
|
||||
if (!options.aggiornaStockQty && esisteindb && !isnuovo) {
|
||||
delete product.Quantità;
|
||||
// delete product.maxbookableGASQty;
|
||||
}
|
||||
|
||||
myproduct.isbn = product.isbn;
|
||||
myproduct.maxbookableGASQty = product.maxbookableGASQty ? product.maxbookableGASQty : null;
|
||||
myproduct.active = product.active;
|
||||
myproduct.idGasordine = recGas ? recGas._id : null;
|
||||
myproduct.idProductInfo = product.idProductInfo;
|
||||
myproduct.price = product.price ? parseFloat(tools.convertPriceEurToValue(product.price)) : null;
|
||||
myproduct.sale_price = product.sale_price ? parseFloat(tools.convertPriceEurToValue(product.sale_price)) : null;
|
||||
myproduct.idapp = idapp;
|
||||
myproduct.status = product.status ? product.status : null;
|
||||
|
||||
|
||||
if (product.Quantità) {
|
||||
myproduct.stockQty = parseInt(product.Quantità);
|
||||
}
|
||||
|
||||
let recold = await Product.findOne(queryprod).lean();
|
||||
|
||||
// AGGIORNA PRODUCT
|
||||
let risrec = await Product.findOneAndUpdate(queryprod, { $set: myproduct }, { new: true, upsert: true });
|
||||
|
||||
let recnew = await Product.findOne(queryprod).lean();
|
||||
|
||||
if (risrec) {
|
||||
if (risrec._id) {
|
||||
|
||||
if (recold) {
|
||||
// Record existed, so it was updated
|
||||
let arrfieldchange = tools.differentObjects(recold, recnew);
|
||||
if (arrfieldchange.length > 0) {
|
||||
updated++;
|
||||
let modif = '';
|
||||
for (const field of arrfieldchange) {
|
||||
modif += field + ': ' + recold[field] + ' -> ' + recnew[field] + ' | ';
|
||||
}
|
||||
|
||||
console.log('Changed: [' + indprod + '/' + dataObjects.length + ']', productInfo.name, '[' + myproduct.idProductInfo + '] : ' + modif);
|
||||
}
|
||||
} else {
|
||||
newprod++;
|
||||
console.log('Nuovo Prodotto : [' + newprod + '/' + dataObjects.length + ']', productInfo.name);
|
||||
}
|
||||
} else {
|
||||
// Record didn't exist, so it was created
|
||||
imported++;
|
||||
}
|
||||
} else {
|
||||
// Record didn't exist, so it was created
|
||||
imported++;
|
||||
// risrec is null or undefined, indicating an error
|
||||
console.error('Error: ', myproduct.productInfo.name);
|
||||
errors++;
|
||||
}
|
||||
|
||||
await Product.singlerecconvert_AfterImport_AndSave(idapp, recnew, isnuovo);
|
||||
} else {
|
||||
// risrec is null or undefined, indicating an error
|
||||
console.error('Error: ', myproduct.productInfo.name);
|
||||
console.error('Error ProductInfo: ', product.code);
|
||||
errors++;
|
||||
}
|
||||
|
||||
await Product.singlerecconvert_AfterImport_AndSave(idapp, recnew, isnuovo);
|
||||
} else {
|
||||
console.error('Error ProductInfo: ', product.code);
|
||||
errors++;
|
||||
}
|
||||
|
||||
indprod++;
|
||||
}
|
||||
|
||||
console.log('*** IMPORTATI: ', imported);
|
||||
|
||||
console.log('*** IMPORTATI: ', imported, 'AGGIORNATI = ' + updated + ' (su ' + dataObjects.length + ' RECORD)');
|
||||
|
||||
return res.status(200).send({ updated, imported, errors });
|
||||
|
||||
@@ -506,6 +650,7 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
img: 'upload/products/' + product.code + '.jpg',
|
||||
weight: product.weight,
|
||||
unit: tools.getIdUnitsByText(product.unit),
|
||||
productType: shared_consts.PRODUCTTYPE.PRODUCT,
|
||||
}
|
||||
|
||||
let reccateg = null;
|
||||
@@ -622,7 +767,7 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
let arrfieldchange = tools.differentObjects(product, recnew);
|
||||
if (arrfieldchange.length > 0) {
|
||||
updated++;
|
||||
console.log('Changed: ', product.idProductInfo + ': ' + arrfieldchange);
|
||||
console.log('Changed:', product.idProductInfo + ': ' + arrfieldchange);
|
||||
}
|
||||
} else {
|
||||
// Record didn't exist, so it was created
|
||||
@@ -669,6 +814,8 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
let product = risprod.product;
|
||||
let productInfo = risprod.productInfo;
|
||||
|
||||
|
||||
|
||||
let isnuovo = false
|
||||
let setta = false
|
||||
|
||||
@@ -811,12 +958,12 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
// 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);
|
||||
|
||||
Reference in New Issue
Block a user