- Categorie
- ProductInfo
This commit is contained in:
@@ -9,6 +9,8 @@ const tools = require('../tools/general');
|
||||
|
||||
const { City } = require('../models/city');
|
||||
const Product = require('../models/product');
|
||||
const ProductInfo = require('../models/productinfo');
|
||||
const CatProd = require('../models/catprod');
|
||||
|
||||
var { authenticate } = require('../middleware/authenticate');
|
||||
|
||||
@@ -55,32 +57,90 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
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: [],
|
||||
img: 'upload/products/' + product.code + '.jpg',
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
delete product.code;
|
||||
delete product.name;
|
||||
delete product.link;
|
||||
|
||||
if (risrec) {
|
||||
if (risrec._id) {
|
||||
// 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(product, recnew);
|
||||
let arrfieldchange = tools.differentObjects(productInfo, recnewInfo);
|
||||
if (arrfieldchange.length > 0) {
|
||||
updated++;
|
||||
console.log('Changed: ', product.name + ': ' + arrfieldchange);
|
||||
// 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++;
|
||||
}
|
||||
} else {
|
||||
// Record didn't exist, so it was created
|
||||
imported++;
|
||||
// risrec is null or undefined, indicating an error
|
||||
console.error('Error: ', product.productInfo.name);
|
||||
errors++;
|
||||
}
|
||||
|
||||
await Product.singlerecconvert_AfterImport(idapp, recnew, isnuovo);
|
||||
} else {
|
||||
// risrec is null or undefined, indicating an error
|
||||
console.error('Error: ', product.name);
|
||||
console.error('Error ProductInfo: ', product.code);
|
||||
errors++;
|
||||
}
|
||||
|
||||
await Product.singlerecconvert_AfterImport(idapp, recnew);
|
||||
}
|
||||
|
||||
// L'opzione ordered: false gestisce gli errori senza interrompere l'inserimento
|
||||
|
||||
Reference in New Issue
Block a user