- 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
|
||||
|
||||
@@ -57,6 +57,7 @@ router.post('/:userId', authenticate, async function (req, res, next) {
|
||||
try {
|
||||
let mycart = await Cart.getCartByUserId(userId, idapp);
|
||||
|
||||
|
||||
if (!order) {
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, cart: null });
|
||||
}
|
||||
@@ -272,7 +273,6 @@ router.post('/:userId/createorderscart', authenticate, async function (req, res,
|
||||
})
|
||||
}
|
||||
statusOrderCart = myorderCart.status;
|
||||
|
||||
const idordercart = myorderCart._id;
|
||||
|
||||
if (!!mycart) {
|
||||
|
||||
@@ -66,7 +66,9 @@ const Cart = require('../models/cart');
|
||||
const OrdersCart = require('../models/orderscart');
|
||||
const Storehouse = require('../models/storehouse');
|
||||
const Provider = require('../models/provider');
|
||||
const CatProd = require('../models/catprod');
|
||||
const Gasordine = require('../models/gasordine');
|
||||
const ProductInfo = require('../models/productinfo');
|
||||
const Scontistica = require('../models/scontistica');
|
||||
const Department = require('../models/department');
|
||||
const { Category } = require('../models/category');
|
||||
@@ -1435,7 +1437,9 @@ function load(req, res, version) {
|
||||
let workers = User.getusersWorkersList(idapp);
|
||||
let storehouses = Storehouse.findAllIdApp(idapp);
|
||||
let providers = Provider.findAllIdApp(idapp);
|
||||
let catprods = CatProd.findAllIdApp(idapp);
|
||||
let gasordines = Gasordine.findAllIdApp(idapp);
|
||||
let productInfos = ProductInfo.findAllIdApp(idapp);
|
||||
let scontisticas = Scontistica.findAllIdApp(idapp);
|
||||
let departments = Department.findAllIdApp(idapp);
|
||||
let categories = Category.findAllIdApp(idapp);
|
||||
@@ -1526,6 +1530,8 @@ function load(req, res, version) {
|
||||
providers,
|
||||
scontisticas,
|
||||
gasordines,
|
||||
productInfos,
|
||||
catprods,
|
||||
]).then((arrdata) => {
|
||||
// console.table(arrdata);
|
||||
let myuser = req.user;
|
||||
@@ -1611,6 +1617,8 @@ function load(req, res, version) {
|
||||
providers: arrdata[40],
|
||||
scontisticas: arrdata[41],
|
||||
gasordines: arrdata[42],
|
||||
productInfos: arrdata[43],
|
||||
catprods: arrdata[44],
|
||||
});
|
||||
|
||||
const prova = 1;
|
||||
|
||||
@@ -36,6 +36,8 @@ const { authenticate, authenticate_noerror } = require('../middleware/authentica
|
||||
const Cart = require('../models/cart');
|
||||
const CartClass = require('../modules/Cart');
|
||||
const Product = require('../models/product');
|
||||
const Order = require('../models/order');
|
||||
const OrdersCart = require('../models/orderscart');
|
||||
const Variant = require('../models/variant');
|
||||
const TypedError = require('../modules/ErrorHandler');
|
||||
|
||||
@@ -1303,6 +1305,25 @@ async function eseguiDbOp(idapp, mydata, locale, req, res) {
|
||||
|
||||
ris = populate.popolaTabelleNuove();
|
||||
|
||||
} else if (mydata.dbop === 'dropProducts') {
|
||||
|
||||
ris = Product.collection.drop();
|
||||
ris = ProductInfo.collection.drop();
|
||||
|
||||
} else if (mydata.dbop === 'dropAllOrders') {
|
||||
|
||||
try {
|
||||
ris = Order.collection.drop();
|
||||
ris = OrdersCart.collection.drop();
|
||||
ris = Cart.collection.drop();
|
||||
} catch (e) {
|
||||
console.error('Err:', e);
|
||||
}
|
||||
|
||||
} else if (mydata.dbop === 'dropAllCarts') {
|
||||
|
||||
ris = Cart.collection.drop();
|
||||
|
||||
} else if (mydata.dbop === 'RewriteCitiesTable') {
|
||||
|
||||
ris = populate.rewriteTable('cities');
|
||||
|
||||
Reference in New Issue
Block a user