- prima bozza catalogo
This commit is contained in:
63
src/server/models/importadescr.js
Executable file
63
src/server/models/importadescr.js
Executable file
@@ -0,0 +1,63 @@
|
||||
mongoose = require('mongoose').set('debug', false)
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
const shared_consts = require('../tools/shared_nodejs');
|
||||
|
||||
const { ObjectID } = require('mongodb');
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.level = "F";
|
||||
|
||||
// A1P
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
mongoose.plugin(schema => {
|
||||
schema.options.usePushEach = true
|
||||
});
|
||||
|
||||
const ImportaDescrSchema = new Schema({
|
||||
idapp: {
|
||||
type: String,
|
||||
},
|
||||
_id: {
|
||||
type: String,
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
var ImportaDescr = module.exports = mongoose.model('ImportaDescr', ImportaDescrSchema);
|
||||
|
||||
ImportaDescrSchema.index({ idapp: 1 });
|
||||
|
||||
module.exports.getFieldsForSearch = function () {
|
||||
return [
|
||||
{ field: 'name', type: tools.FieldType.string },
|
||||
{ field: 'description', type: tools.FieldType.string },
|
||||
]
|
||||
};
|
||||
|
||||
module.exports.executeQueryTable = function (idapp, params) {
|
||||
params.fieldsearch = this.getFieldsForSearch();
|
||||
return tools.executeQueryTable(this, idapp, params);
|
||||
};
|
||||
|
||||
module.exports.getImportaDescrByCode = function (idapp, code) {
|
||||
return ImportaDescr.findAllIdApp(idapp, code);
|
||||
}
|
||||
|
||||
module.exports.getImportaDescrById = async function (id) {
|
||||
const arrris = await ImportaDescr.findAllIdApp('', '', id);
|
||||
return arrris && arrris.length > 0 ? arrris[0] : null
|
||||
}
|
||||
|
||||
module.exports.findAllIdApp = async function (idapp) {
|
||||
const ImportaDescr = this;
|
||||
|
||||
const myfind = { idapp, deleted: false };
|
||||
|
||||
return await ImportaDescr.find(myfind, (err, arrrec) => {
|
||||
return arrrec;
|
||||
});
|
||||
};
|
||||
63
src/server/models/importaisbn.js
Executable file
63
src/server/models/importaisbn.js
Executable file
@@ -0,0 +1,63 @@
|
||||
mongoose = require('mongoose').set('debug', false)
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
const tools = require('../tools/general');
|
||||
|
||||
const shared_consts = require('../tools/shared_nodejs');
|
||||
|
||||
const { ObjectID } = require('mongodb');
|
||||
|
||||
mongoose.Promise = global.Promise;
|
||||
mongoose.level = "F";
|
||||
|
||||
// A1P
|
||||
|
||||
// Resolving error Unknown modifier: $pushAll
|
||||
mongoose.plugin(schema => {
|
||||
schema.options.usePushEach = true
|
||||
});
|
||||
|
||||
const ImportaIsbnSchema = new Schema({
|
||||
idapp: {
|
||||
type: String,
|
||||
},
|
||||
_id: {
|
||||
type: String,
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
var ImportaIsbn = module.exports = mongoose.model('ImportaIsbn', ImportaIsbnSchema);
|
||||
|
||||
ImportaIsbnSchema.index({ idapp: 1 });
|
||||
|
||||
module.exports.getFieldsForSearch = function () {
|
||||
return [
|
||||
{ field: 'name', type: tools.FieldType.string },
|
||||
{ field: 'description', type: tools.FieldType.string },
|
||||
]
|
||||
};
|
||||
|
||||
module.exports.executeQueryTable = function (idapp, params) {
|
||||
params.fieldsearch = this.getFieldsForSearch();
|
||||
return tools.executeQueryTable(this, idapp, params);
|
||||
};
|
||||
|
||||
module.exports.getImportaIsbnByCode = function (idapp, code) {
|
||||
return ImportaIsbn.findAllIdApp(idapp, code);
|
||||
}
|
||||
|
||||
module.exports.getImportaIsbnById = async function (id) {
|
||||
const arrris = await ImportaIsbn.findAllIdApp('', '', id);
|
||||
return arrris && arrris.length > 0 ? arrris[0] : null
|
||||
}
|
||||
|
||||
module.exports.findAllIdApp = async function (idapp) {
|
||||
const ImportaIsbn = this;
|
||||
|
||||
const myfind = { idapp, deleted: false };
|
||||
|
||||
return await ImportaIsbn.find(myfind, (err, arrrec) => {
|
||||
return arrrec;
|
||||
});
|
||||
};
|
||||
@@ -19,6 +19,9 @@ const productInfoSchema = new Schema({
|
||||
department: {
|
||||
type: String, ref: 'Department'
|
||||
},
|
||||
sku: {
|
||||
type: String,
|
||||
},
|
||||
code: {
|
||||
type: String,
|
||||
unique: true,
|
||||
|
||||
@@ -13,6 +13,8 @@ const { City } = require('../models/city');
|
||||
const Product = require('../models/product');
|
||||
const Inventariogm = require('../models/inventariogm');
|
||||
const Importamacro = require('../models/importamacro');
|
||||
const ImportaDescr = require('../models/importadescr');
|
||||
const ImportaIsbn = require('../models/importaIsbn');
|
||||
const ProductInfo = require('../models/productInfo');
|
||||
const CatProd = require('../models/catprod');
|
||||
const Author = require('../models/author');
|
||||
@@ -355,6 +357,8 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
let imported = 0;
|
||||
let errors = 0;
|
||||
|
||||
const ripopola = false;
|
||||
|
||||
dataObjects = null;
|
||||
|
||||
let myarr = null;
|
||||
@@ -367,12 +371,10 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
dataObjects = null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (myarr) {
|
||||
if (myarr && ripopola) {
|
||||
|
||||
// Cancella la collection ImportaMacros
|
||||
await Importamacro.deleteMany({ idapp });
|
||||
await ImportaDescr.deleteMany({ idapp });
|
||||
|
||||
// Aggiungi i record su ImportaMacros
|
||||
for (const recinv of myarr) {
|
||||
@@ -389,14 +391,14 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
|
||||
let queryprod = { idapp, code: recmacro._id };
|
||||
|
||||
|
||||
try {
|
||||
let risrec = await Importamacro.findOneAndUpdate(queryprod, { $set: recmacro }, { new: true, upsert: true, strict: false });
|
||||
let risrec = await ImportaDescr.findOneAndUpdate(queryprod, { $set: recmacro }, { new: true, upsert: true, strict: false });
|
||||
if (risrec) {
|
||||
imported++;
|
||||
// if (imported > 1000)
|
||||
// break;
|
||||
console.log('importato ', imported, 'su ', myarr.length);
|
||||
if (imported % 10 === 0)
|
||||
console.log('importato ', imported, 'su ', myarr.length);
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
@@ -407,10 +409,10 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
// Rileggi tutti i record di ImportaMacros
|
||||
dataObjects = await Importamacro.find({ idapp }).lean();
|
||||
// Rileggi tutti i record di ImportaDescr
|
||||
dataObjects = await ImportaDescr.find({ idapp }).lean();
|
||||
|
||||
console.log('*** INIZIO IMPORT PRODOTTI ... ');
|
||||
console.log('*** INIZIO IMPORT MACRO_DESCRELINKSITOWEB ... ');
|
||||
|
||||
let indprod = 0;
|
||||
let newprod = 0;
|
||||
@@ -446,70 +448,98 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
productInfo.sottotitolo = getvalueByJsonText(product.sottotitolo);
|
||||
}
|
||||
|
||||
const pagine = getvalueByJsonText(product.pagine);
|
||||
|
||||
let recisbn = {};
|
||||
|
||||
if (pagine && pagine !== '0') {
|
||||
recisbn.Pagine = pagine;
|
||||
}
|
||||
|
||||
recisbn.link_macro = productInfo.link_macro;
|
||||
recisbn.descrizione_breve_macro = productInfo.descrizione_breve_macro;
|
||||
recisbn.descrizione_completa_macro = productInfo.descrizione_completa_macro;
|
||||
recisbn.sottotitolo = productInfo.sottotitolo;
|
||||
|
||||
let risisbn = await ImportaIsbn.findOneAndUpdate({ isbn: product.code }, { $set: recisbn }, { new: true, upsert: true, strict: false });
|
||||
|
||||
// Update ProductInfo
|
||||
let risrecInfo = await ProductInfo.findOneAndUpdate({ code: productInfo.code }, { $set: productInfo }, { new: true, upsert: true });
|
||||
|
||||
indprod++;
|
||||
console.log(indprod + '/' + numprod);
|
||||
if (indprod % 10 === 0)
|
||||
console.log(indprod + '/' + numprod);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('*** IMPORTATI: ', imported, '*** NUOVI: ', newprod, 'AGGIORNATI = ' + updated + ' (su ' + dataObjects.length + ' RECORD)');
|
||||
|
||||
return res.status(200).send({ updated, imported, errors });
|
||||
|
||||
} else if (cmd === shared_consts.Cmd.MACRO_RANKING) {
|
||||
let updated = 0;
|
||||
let imported = 0;
|
||||
let errors = 0;
|
||||
|
||||
dataObjects = null;
|
||||
const ripopola = true;
|
||||
|
||||
try {
|
||||
dataObjects = JSON.parse(`[${data.arrdata}]`);
|
||||
} catch (e) {
|
||||
if (ripopola) {
|
||||
dataObjects = null;
|
||||
}
|
||||
|
||||
if (dataObjects && dataObjects[0]) {
|
||||
// Cancella la collection ImportaMacros
|
||||
await Importamacro.deleteMany({ idapp });
|
||||
try {
|
||||
dataObjects = JSON.parse(`[${data.arrdata}]`);
|
||||
} catch (e) {
|
||||
dataObjects = null;
|
||||
}
|
||||
|
||||
// Aggiungi i record su ImportaMacros
|
||||
for (const recinv of dataObjects[0]) {
|
||||
let isnuovo = false
|
||||
let setta = false
|
||||
if (dataObjects && dataObjects[0]) {
|
||||
// Cancella la collection ImportaMacros
|
||||
await ImportaIsbn.deleteMany({ idapp });
|
||||
|
||||
let recmacro = recinv;
|
||||
const numtot = dataObjects[0].length
|
||||
|
||||
recmacro.idapp = idapp;
|
||||
// Aggiungi i record su ImportaMacros
|
||||
for (const recinv of dataObjects[0]) {
|
||||
let recmacro = recinv;
|
||||
|
||||
recmacro._id = recmacro.id;
|
||||
recmacro.idapp = idapp;
|
||||
|
||||
delete recmacro.id;
|
||||
//recmacro._id = recmacro.id;
|
||||
recmacro.sku = recmacro.idArticolo;
|
||||
|
||||
let queryprod = { idapp, _id: recmacro._id };
|
||||
if (recmacro.DataPubblicazione) {
|
||||
|
||||
try {
|
||||
let risrec = await Importamacro.findOneAndUpdate(queryprod, { $set: recmacro }, { new: true, upsert: true, strict: false });
|
||||
if (risrec) {
|
||||
imported++;
|
||||
// delete recmacro.id;
|
||||
|
||||
let queryprod = { idapp, sku: recmacro.sku };
|
||||
|
||||
try {
|
||||
let risrec = await ImportaIsbn.findOneAndUpdate(queryprod, { $set: recmacro }, { new: true, upsert: true, strict: false });
|
||||
if (risrec) {
|
||||
imported++;
|
||||
console.log('Importati dir TEMPORANEA ... ', imported + '/' + numtot);
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
errors++;
|
||||
}
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
errors++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rileggi tutti i record di ImportaMacros
|
||||
dataObjects = await Importamacro.find({ idapp }).lean();
|
||||
// Rileggi tutti i record di ImportaIsbn
|
||||
dataObjects = await ImportaIsbn.find({ idapp }).lean();
|
||||
|
||||
console.log('*** INIZIO IMPORT PRODOTTI ... ');
|
||||
console.log('*** INIZIO IMPORT RANKING ... ');
|
||||
|
||||
let indprod = 0;
|
||||
let newprod = 0;
|
||||
|
||||
let numprod = dataObjects.length;
|
||||
|
||||
imported = 0;
|
||||
|
||||
for (const product of dataObjects) {
|
||||
let isnuovo = false
|
||||
@@ -517,7 +547,8 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
|
||||
let importa = true;
|
||||
|
||||
if (!product.title || !product.isbn)
|
||||
//if (!product.title || !product.isbn)
|
||||
if (!product.sku || !product.DataPubblicazione)
|
||||
importa = false;
|
||||
|
||||
if (importa) {
|
||||
@@ -528,83 +559,118 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
|
||||
let productInfo = {
|
||||
idapp: product.idapp,
|
||||
code: product.isbn,
|
||||
id_wp: product._id,
|
||||
code: product.isbn ? product.isbn : product.code,
|
||||
sku: product.sku,
|
||||
|
||||
// id_wp: product._id,
|
||||
|
||||
// name: product.title,
|
||||
totaleVenduti: product.totaleVenduti,
|
||||
totaleVenduti: product.totaleaVenduti,
|
||||
venditeLastM: product.venditeLastM,
|
||||
venditeLast6M: product.venditeLast6M,
|
||||
venditeLastY: product.venditeLastY,
|
||||
venditeLast2Y: product.venditeLast2Y,
|
||||
dataUltimoOrdine: product.dataUltimoOrdine,
|
||||
rank3M: product.rank3M,
|
||||
rank6M: product.rank6M,
|
||||
rank1Y: product.rank1Y,
|
||||
|
||||
}
|
||||
|
||||
console.log(indprod + '/' + numprod);
|
||||
if (product.DataPubblicazione) {
|
||||
productInfo.date_publishing = new Date(product.DataPubblicazione);
|
||||
// convert data to timestamp
|
||||
productInfo.date_publishing_ts = productInfo.date_publishing.getTime();
|
||||
}
|
||||
if (product.dataUltimoOrdine) {
|
||||
productInfo.dataUltimoOrdine = new Date(product.dataUltimoOrdine);
|
||||
}
|
||||
|
||||
// Update ProductInfo
|
||||
let risrecInfo = await ProductInfo.findOneAndUpdate({ code: productInfo.code }, { $set: productInfo }, { new: true, upsert: true });
|
||||
|
||||
indprod++;
|
||||
if (risrecInfo) {
|
||||
imported++;
|
||||
console.log('Importati... ', imported + '/' + numprod);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log('*** IMPORTATI: ', imported, ' [Prodotti = ' + indprod + '] *** NUOVI: ', newprod, 'AGGIORNATI = ' + updated + ' (su ' + dataObjects.length + ' RECORD)');
|
||||
|
||||
return res.status(200).send({ updated, imported, errors });
|
||||
|
||||
} else if (cmd === shared_consts.Cmd.MACRO_CATALOGO_JSON) {
|
||||
let updated = 0;
|
||||
let imported = 0;
|
||||
let errors = 0;
|
||||
|
||||
dataObjects = null;
|
||||
const ripopola = true;
|
||||
|
||||
try {
|
||||
dataObjects = JSON.parse(`[${data.arrdata}]`);
|
||||
} catch (e) {
|
||||
|
||||
if (ripopola) {
|
||||
dataObjects = null;
|
||||
}
|
||||
|
||||
if (dataObjects && dataObjects[0]) {
|
||||
// Cancella la collection ImportaMacros
|
||||
await Importamacro.deleteMany({ idapp });
|
||||
try {
|
||||
dataObjects = JSON.parse(`[${data.arrdata}]`);
|
||||
} catch (e) {
|
||||
dataObjects = null;
|
||||
}
|
||||
|
||||
// Aggiungi i record su ImportaMacros
|
||||
for (const recinv of dataObjects[0]) {
|
||||
let isnuovo = false
|
||||
let setta = false
|
||||
if (dataObjects && dataObjects[0]) {
|
||||
// Cancella la collection ImportaMacros
|
||||
await Importamacro.deleteMany({ idapp });
|
||||
|
||||
let recmacro = recinv;
|
||||
// Aggiungi i record su ImportaMacros
|
||||
for (const recinv of dataObjects[0]) {
|
||||
let isnuovo = false
|
||||
let setta = false
|
||||
|
||||
recmacro.idapp = idapp;
|
||||
let recmacro = recinv;
|
||||
|
||||
recmacro._id = recmacro.id;
|
||||
recmacro.idapp = idapp;
|
||||
|
||||
delete recmacro.id;
|
||||
recmacro._id = recmacro.id;
|
||||
|
||||
let queryprod = { idapp, _id: recmacro._id };
|
||||
delete recmacro.id;
|
||||
|
||||
try {
|
||||
let risrec = await Importamacro.findOneAndUpdate(queryprod, { $set: recmacro }, { new: true, upsert: true, strict: false });
|
||||
if (risrec) {
|
||||
imported++;
|
||||
// Ottengo isbn e Pagine da ImportaIsbn
|
||||
|
||||
const recrankingisbn = await ImportaIsbn.findOne({ sku: recmacro.sku }).lean();
|
||||
if (recrankingisbn) {
|
||||
if (!recmacro.isbn) {
|
||||
recmacro.isbn = recrankingisbn.isbn;
|
||||
}
|
||||
if ((!recmacro.Pagine || recmacro.Pagine === 0) && recrankingisbn.Pagine)
|
||||
recmacro.Pagine = recrankingisbn.Pagine;
|
||||
|
||||
if (!recmacro.misure && recrankingisbn.misure) {
|
||||
recmacro.misure = recrankingisbn.misure;
|
||||
}
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
errors++;
|
||||
let queryprod = { idapp, _id: recmacro._id };
|
||||
|
||||
try {
|
||||
let risrec = await Importamacro.findOneAndUpdate(queryprod, { $set: recmacro }, { new: true, upsert: true, strict: false });
|
||||
if (risrec) {
|
||||
imported++;
|
||||
if (imported % 10 === 0)
|
||||
console.log('Importati su dir Temporanea ', imported);
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
errors++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const cancella_categorie = false;
|
||||
|
||||
if (cancella_categorie) {
|
||||
await CatProd.deleteMany({ idapp });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const cancella_categorie = false;
|
||||
|
||||
if (cancella_categorie) {
|
||||
await CatProd.deleteMany({ idapp });
|
||||
}
|
||||
|
||||
|
||||
// Rileggi tutti i record di ImportaMacros
|
||||
dataObjects = await Importamacro.find({ idapp }).lean();
|
||||
|
||||
@@ -621,7 +687,7 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
|
||||
let importa = true;
|
||||
|
||||
if (!product.title || !product.isbn)
|
||||
if (!product.title || !product.sku)
|
||||
importa = false;
|
||||
|
||||
if (importa) {
|
||||
@@ -634,6 +700,7 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
idapp: product.idapp,
|
||||
code: product.isbn,
|
||||
id_wp: product._id,
|
||||
sku: product.sku,
|
||||
|
||||
name: product.title,
|
||||
description: product.description,
|
||||
@@ -654,7 +721,8 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
|
||||
let versione = 0;
|
||||
|
||||
console.log(indprod + '/' + numprod);
|
||||
if (indprod % 10 === 0)
|
||||
console.log(indprod + '/' + numprod);
|
||||
|
||||
|
||||
productInfo.productTypes = [];
|
||||
@@ -688,11 +756,11 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
}
|
||||
|
||||
|
||||
if (product.Data) {
|
||||
/*if (product.Data) {
|
||||
productInfo.date_publishing = new Date(product.Data * 1000);
|
||||
// convert data to timestamp
|
||||
productInfo.date_publishing_ts = productInfo.date_publishing.getTime();
|
||||
}
|
||||
}*/
|
||||
|
||||
productInfo.name = productInfo.name.replace(/ - Usato$| - Nuovo$| - Epub$| - Ebook$| - Mobi$| - DVD$| - Streaming$| - Download$/, "");
|
||||
|
||||
@@ -916,7 +984,7 @@ router.post('/import', authenticate, async (req, res) => {
|
||||
variazione.formato = product.formato ? product.formato : '';
|
||||
variazione.tipologia = product.Tipologia ? product.Tipologia : '';
|
||||
variazione.edizione = product.Edizione ? product.Edizione : '';
|
||||
variazione.pagine = product.Pagine ? parseInt(product.Pagine) : '';
|
||||
variazione.pagine = tools.isValidNumber(product.Pagine) ? tools.convstrToInt(product.Pagine) : 0;
|
||||
variazione.misure = product.misure ? product.misure : '';
|
||||
variazione.eta = product.eta ? product.eta : '';
|
||||
variazione.addtocart_link = product.addtocart_link ? product.addtocart_link : '';
|
||||
|
||||
@@ -4387,6 +4387,20 @@ module.exports = {
|
||||
return parseInt(version);
|
||||
},
|
||||
|
||||
convstrToInt(mystr) {
|
||||
try {
|
||||
const parsed = parseInt(mystr, 10); // Specifica la base come 10
|
||||
return isNaN(parsed) ? 0 : parsed; // Restituisce 0 se il valore è NaN
|
||||
} catch (e) {
|
||||
return 0; // Se c'è un errore, restituisce 0
|
||||
}
|
||||
},
|
||||
|
||||
isValidNumber(value) {
|
||||
// Controlla se il valore è un numero valido
|
||||
return !isNaN(value) && value !== null && value !== '';
|
||||
},
|
||||
|
||||
invertescapeslash(mystr) {
|
||||
return mystr.replace(/Ç/g, '/');
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user