From e1f2e799d6bec70f09636a6c14c08b8afcdc38c1 Mon Sep 17 00:00:00 2001 From: Surya Paolo Date: Mon, 29 Apr 2024 14:58:45 +0200 Subject: [PATCH] - catalogo macro, importazione dati --- .env.dev.pcb | 6 +- .env.dev.riso | 2 +- .env.development | 8 +- .env.prod.pcb | 6 +- .env.test.pcb | 6 +- .env.test.risosrv | 2 +- .env.test2 | 2 +- src/server/models/importamacro.js | 63 ++++++++ src/server/models/product.js | 6 + src/server/models/productInfo.js | 20 ++- src/server/router/admin_router.js | 251 +++++++++++++++++++++++++++++- src/server/server.js | 3 + src/server/tools/shared_nodejs.js | 6 + 13 files changed, 358 insertions(+), 23 deletions(-) create mode 100755 src/server/models/importamacro.js diff --git a/.env.dev.pcb b/.env.dev.pcb index c65d8fb..b7b5087 100644 --- a/.env.dev.pcb +++ b/.env.dev.pcb @@ -4,8 +4,8 @@ PDB=mypassword@1A SEND_EMAIL=0 SEND_EMAIL_ORDERS=1 PORT=3000 -appTelegram_TEST=["1","17"] -appTelegram=["1","17"] +appTelegram_TEST=["1","17","18"] +appTelegram=["1","17","18"] DOMAIN=mongodb://localhost:27017/ AUTH_MONGODB=1 MONGODB_USER=admin @@ -33,7 +33,7 @@ PROJECT_DESCR_MAIN='__PROJECTS' SECRK=Askb38v23jjDFaoskBOWj92axXCQ SECRTK=prova123prova567ASDADASDAS TOKEN_LIFE=2h -REFRESH_TOKEN_LIFE=7d +REFRESH_TOKEN_LIFE=14d FTPSERVER_HOST=139.162.166.31 FTPSERVER_PORT=21 FTPSERVER_USER=ftpusrsrv_ diff --git a/.env.dev.riso b/.env.dev.riso index a134a6e..b85e237 100644 --- a/.env.dev.riso +++ b/.env.dev.riso @@ -33,7 +33,7 @@ PROJECT_DESCR_MAIN='__PROJECTS' SECRK=Askb38v23jjDFaoskBOWj92axXCQ SECRTK=prova123prova567ASDADASDAS TOKEN_LIFE=2h -REFRESH_TOKEN_LIFE=7d +REFRESH_TOKEN_LIFE=14d FTPSERVER_HOST=139.162.166.31 FTPSERVER_PORT=21 FTPSERVER_USER=ftpusrsrv_ diff --git a/.env.development b/.env.development index a134a6e..b7b5087 100644 --- a/.env.development +++ b/.env.development @@ -1,11 +1,11 @@ -DATABASE=test_FreePlanet +DATABASE=test_PiuCheBuono UDB=paofreeplanet PDB=mypassword@1A SEND_EMAIL=0 SEND_EMAIL_ORDERS=1 PORT=3000 -appTelegram_TEST=["1","13"] -appTelegram=["1","13"] +appTelegram_TEST=["1","17","18"] +appTelegram=["1","17","18"] DOMAIN=mongodb://localhost:27017/ AUTH_MONGODB=1 MONGODB_USER=admin @@ -33,7 +33,7 @@ PROJECT_DESCR_MAIN='__PROJECTS' SECRK=Askb38v23jjDFaoskBOWj92axXCQ SECRTK=prova123prova567ASDADASDAS TOKEN_LIFE=2h -REFRESH_TOKEN_LIFE=7d +REFRESH_TOKEN_LIFE=14d FTPSERVER_HOST=139.162.166.31 FTPSERVER_PORT=21 FTPSERVER_USER=ftpusrsrv_ diff --git a/.env.prod.pcb b/.env.prod.pcb index 1a841b9..a926258 100644 --- a/.env.prod.pcb +++ b/.env.prod.pcb @@ -4,8 +4,8 @@ PDB=suerteFreePlanet@1A SEND_EMAIL=1 SEND_EMAIL_ORDERS=1 PORT=3000 -appTelegram_TEST=["17"] -appTelegram=["17"] +appTelegram_TEST=["17","18"] +appTelegram=["17","18"] ENABLE_PUSHNOTIFICATION=1 DIRECTORY_SERVER=freeplanet_serverside SERVERDIR_WEBSITE=www.freeplanet_server @@ -31,5 +31,5 @@ PROJECT_DESCR_MAIN='__PROJECTS' SECRK=iUUb38v23jjDFaosWj92axkBOXCQ SECRTK=jAxKm02emx5SeJvz2IGmtRf6YqCgope TOKEN_LIFE=2h -REFRESH_TOKEN_LIFE=7d +REFRESH_TOKEN_LIFE=14d AUTH_NEW_SITES=B234HDSAOJ734ndcsdKWNVZZ \ No newline at end of file diff --git a/.env.test.pcb b/.env.test.pcb index 80fd07e..fac2e91 100644 --- a/.env.test.pcb +++ b/.env.test.pcb @@ -4,8 +4,8 @@ PDB=suerteFreePlanet@1A SEND_EMAIL=1 SEND_EMAIL_ORDERS=1 PORT=3001 -appTelegram_TEST=["17"] -appTelegram=["17"] +appTelegram_TEST=["17","18"] +appTelegram=["17","18"] ENABLE_PUSHNOTIFICATION=1 DIRECTORY_SERVER=freeplanet_serverside SERVERDIR_WEBSITE=test.freeplanet_server @@ -30,5 +30,5 @@ PROD=0 PROJECT_DESCR_MAIN='__PROJECTS' SECRK=iUUb38v23jjDFaosWj92axkBOXCQ TOKEN_LIFE=2h -REFRESH_TOKEN_LIFE=7d +REFRESH_TOKEN_LIFE=14d AUTH_NEW_SITES=B234HDSAOJ734ndcsdKWNV \ No newline at end of file diff --git a/.env.test.risosrv b/.env.test.risosrv index 61e8c50..bc8a768 100644 --- a/.env.test.risosrv +++ b/.env.test.risosrv @@ -27,5 +27,5 @@ PROD=0 PROJECT_DESCR_MAIN='__PROJECTS' SECRK=iUUb38v23jjDFaosWj92axkBOXCQ TOKEN_LIFE=2h -REFRESH_TOKEN_LIFE=7d +REFRESH_TOKEN_LIFE=14d AUTH_NEW_SITES=B234HDSAOJ734ndcsdKWNV \ No newline at end of file diff --git a/.env.test2 b/.env.test2 index 631ef59..af6bd62 100644 --- a/.env.test2 +++ b/.env.test2 @@ -28,5 +28,5 @@ PROD=0 PROJECT_DESCR_MAIN='__PROJECTS' SECRK=iUUb38v23jjDFaosWj92axkBOXCQ TOKEN_LIFE=2h -REFRESH_TOKEN_LIFE=7d +REFRESH_TOKEN_LIFE=14d AUTH_NEW_SITES=B234HDSAOJ734ndcsdKWNV \ No newline at end of file diff --git a/src/server/models/importamacro.js b/src/server/models/importamacro.js new file mode 100755 index 0000000..88613ed --- /dev/null +++ b/src/server/models/importamacro.js @@ -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 ImportaMacroSchema = new Schema({ + idapp: { + type: String, + }, + _id: { + type: String, + }, + +}); + +var ImportaMacro = module.exports = mongoose.model('ImportaMacro', ImportaMacroSchema); + +ImportaMacroSchema.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.getImportaMacroByCode = function (idapp, code) { + return ImportaMacro.findAllIdApp(idapp, code); +} + +module.exports.getImportaMacroById = async function (id) { + const arrris = await ImportaMacro.findAllIdApp('', '', id); + return arrris && arrris.length > 0 ? arrris[0] : null +} + +module.exports.findAllIdApp = async function (idapp) { + const ImportaMacro = this; + + const myfind = { idapp, deleted: false }; + + return await ImportaMacro.find(myfind, (err, arrrec) => { + return arrrec; + }); +}; \ No newline at end of file diff --git a/src/server/models/product.js b/src/server/models/product.js index 831008a..65cd789 100755 --- a/src/server/models/product.js +++ b/src/server/models/product.js @@ -53,6 +53,9 @@ const productSchema = new Schema({ type: Number, required: true, }, + sale_price: { + type: Number, + }, price_acquistato: { type: Number, required: true, @@ -79,6 +82,9 @@ const productSchema = new Schema({ type: Number, default: 0, }, + status: { //publish + type: String, + }, stockBloccatiQty: { // Prenotati Bloccati type: Number, default: 0, diff --git a/src/server/models/productInfo.js b/src/server/models/productInfo.js index 49051c7..73fb05d 100755 --- a/src/server/models/productInfo.js +++ b/src/server/models/productInfo.js @@ -24,6 +24,9 @@ const productInfoSchema = new Schema({ unique: true, required: true, }, + id_wp: { // id in wordpress + type: String, + }, codice_EAN: { type: String, }, @@ -67,7 +70,10 @@ const productInfoSchema = new Schema({ icon: { type: String, }, - img: { + img: { // Se esiste img (sul server) visualizza questa, altrimenti vedi se esiste image_link + type: String, + }, + image_link: { type: String, }, link_scheda: { @@ -91,6 +97,18 @@ const productInfoSchema = new Schema({ note: { type: String, }, + author: { + type: String, + }, + collezione: { + type: String, + }, + publisher: { //editore + type: String, + }, + numpages: { + type: Number, + }, }); diff --git a/src/server/router/admin_router.js b/src/server/router/admin_router.js index f77ed3f..abb1799 100755 --- a/src/server/router/admin_router.js +++ b/src/server/router/admin_router.js @@ -10,6 +10,7 @@ const tools = require('../tools/general'); const { City } = require('../models/city'); const Product = require('../models/product'); const Inventariogm = require('../models/inventariogm'); +const Importamacro = require('../models/importamacro'); const ProductInfo = require('../models/productInfo'); const CatProd = require('../models/catprod'); const SubCatProd = require('../models/subcatprod'); @@ -215,12 +216,13 @@ async function extractArrayDataFromCSV(idapp, rec) { }; 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 cmd = req.body.cmd; + const idapp = req.body.idapp; + const data = req.body.data; + const options = req.body.data.options; + const liste = require('../data/liste'); let dataObjects = null; @@ -245,6 +247,243 @@ router.post('/import', authenticate, async (req, res) => { let risrec = await Inventariogm.findOneAndUpdate(queryprod, { $set: inventario }, { new: true, upsert: true }); } + } else if (cmd === shared_consts.Cmd.MACRO_CATALOGO_JSON) { + let updated = 0; + let imported = 0; + let errors = 0; + + dataObjects = null; + + try { + dataObjects = JSON.parse(`[${data.arrdata}]`); + } catch (e) { + dataObjects = null; + } + + 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 + let setta = false + + let recmacro = recinv; + + recmacro.idapp = idapp; + + recmacro._id = recmacro.id; + + delete recmacro.id; + + let queryprod = { idapp, _id: recmacro._id }; + + try { + let risrec = await Importamacro.findOneAndUpdate(queryprod, { $set: recmacro }, { new: true, upsert: true, strict: false }); + if (risrec) { + imported++; + } + + } catch (e) { + console.error(e); + errors++; + } + } + } + + const cancella_categorie = true; + + if (cancella_categorie) { + await CatProd.deleteMany({ idapp }); + } + + + // Rileggi tutti i record di ImportaMacros + dataObjects = await Importamacro.find({ idapp }).lean(); + + 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, + } + + if (product.NumPagine) + productInfo.numpages = product.NumPagine; + + 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); + } + } + + // 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; + } + + 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) { + // Record existed, so it was updated + let arrfieldchange = tools.differentObjects(myproduct, recnew); + if (arrfieldchange.length > 0) { + updated++; + console.log('Changed: ', myproduct.idProductInfo + ': ' + arrfieldchange); + } + } else { + // Record didn't exist, so it was created + imported++; + } + } else { + // risrec is null or undefined, indicating an error + console.error('Error: ', myproduct.productInfo.name); + errors++; + } + + await Product.singlerecconvert_AfterImport_AndSave(idapp, recnew, isnuovo); + } else { + console.error('Error ProductInfo: ', product.code); + errors++; + } + + } + + console.log('*** IMPORTATI: ', imported); + + return res.status(200).send({ updated, imported, errors }); } else if (cmd === shared_consts.Cmd.PRODUCTS) { dataObjects = JSON.parse(`[${data.arrdata}]`); @@ -591,10 +830,10 @@ router.post('/import', authenticate, async (req, res) => { } } catch (e) { console.error('e', e); - res.status(400).send(); + return res.status(400).send(); } - res.status(400).send(); + return res.status(400).send(); }); diff --git a/src/server/server.js b/src/server/server.js index 87a4e70..ab3b77a 100755 --- a/src/server/server.js +++ b/src/server/server.js @@ -134,6 +134,9 @@ myLoad().then(ris => { const { MyEvent } = require('./models/myevent'); + app.use(bodyParser.json({ limit: '50mb' })); + app.use(bodyParser.urlencoded({ limit: '50mb', extended: true })); + app.use(express.static('views')); // app.use(express.static(path.join(__dirname, 'client'))); diff --git a/src/server/tools/shared_nodejs.js b/src/server/tools/shared_nodejs.js index 1d4a9c5..0571fd1 100755 --- a/src/server/tools/shared_nodejs.js +++ b/src/server/tools/shared_nodejs.js @@ -296,6 +296,7 @@ module.exports = { PRODUCTS: 20, PRODUCTS_V2: 22, INVENTARIO: 30, + MACRO_CATALOGO_JSON: 40, }, WalletFinalStatusType: { @@ -1004,9 +1005,14 @@ module.exports = { {name: 'vegan', type: ''}, {name: 'sfuso', type: ''}, {name: 'img', type: ''}, + {name: 'image_link', type: ''}, {name: 'link', type: ''}, {name: 'ingredienti', type: ''}, {name: 'valori_nutrizionali', type: ''}, + {name: 'author', type: ''}, + {name: 'collezione', type: ''}, + {name: 'publisher', type: ''}, + {name: 'numpages', type: ''}, {name: 'note', type: ''}, ],