diff --git a/src/server/models/myelem.js b/src/server/models/myelem.js index 463b240..fda3931 100755 --- a/src/server/models/myelem.js +++ b/src/server/models/myelem.js @@ -44,6 +44,17 @@ const elemText = new Schema( } ); +const catalogo = new Schema( + { + //++AddCATALOGO_FIELDS + productTypes: [{ type: Number }], + excludeproductTypes: [{ type: Number }], + // formato: [{ type: String, default: '' }], + // categoria: [{ type: String, default: '' }], + // Editore: [{ type: String, default: '' }], + } +); + const MyElemSchema = new Schema({ _id: { type: ObjectId, @@ -164,6 +175,7 @@ const MyElemSchema = new Schema({ type: String, }, listcards: [myCard], + catalogo: catalogo, list: [ { imagefile: { diff --git a/src/server/models/product.js b/src/server/models/product.js index f3ec0c5..230e428 100755 --- a/src/server/models/product.js +++ b/src/server/models/product.js @@ -61,9 +61,9 @@ const productSchema = new Schema({ active: { type: Boolean, }, - versione: { + arrversioni: [{ type: Number, - }, + }], status: { //publish type: String, }, diff --git a/src/server/models/productInfo.js b/src/server/models/productInfo.js index daabef9..a33417c 100755 --- a/src/server/models/productInfo.js +++ b/src/server/models/productInfo.js @@ -88,6 +88,9 @@ const productInfoSchema = new Schema({ checkout_link: { type: String, }, + versioneGM: { + type: String, + }, img2: { type: String, }, @@ -114,9 +117,9 @@ const productInfoSchema = new Schema({ date_publishing: { type: Date, }, - productType: { + productTypes: [{ type: Number, - }, + }], }); @@ -215,6 +218,51 @@ module.exports.findAllIdApp = async function (idapp, code, id) { module.exports.getProductByCode = function (idapp, code) { return productInfo.findAllIdApp(idapp, code); } +module.exports.correggiProductTypes = async function() { + const ProductInfo = this; + const bulkOps = []; + + try { + // Fase di aggregazione + const cursor = ProductInfo.aggregate([ + { + $project: { + _id: 1, + productType: 1, + productTypes: [{ $ifNull: ['$productType', 10] }] + } + } + ]).cursor({ batchSize: 1000 }).exec(); + + // Itera attraverso ogni documento utilizzando il cursore + for await (const doc of cursor) { + bulkOps.push({ + updateOne: { + filter: { _id: doc._id }, + update: { + $set: { productTypes: doc.productTypes }, + $unset: { productType: "" } + } + } + }); + + // Scrivi i batch di operazioni ogni 1000 documenti + if (bulkOps.length === 1000) { + await ProductInfo.bulkWrite(bulkOps); + bulkOps.length = 0; + } + } + + // Scrivi eventuali operazioni rimanenti + if (bulkOps.length > 0) { + await ProductInfo.bulkWrite(bulkOps); + } + + console.log('ProductInfo.productType converted to productTypes array and saved'); + } catch (err) { + console.error('Error converting ProductInfo.productType:', err); + } +} module.exports.createIndexes((err) => { if (err) throw err; diff --git a/src/server/router/admin_router.js b/src/server/router/admin_router.js index 30cf61f..f443831 100755 --- a/src/server/router/admin_router.js +++ b/src/server/router/admin_router.js @@ -99,7 +99,7 @@ async function completaSettaggioProduct_AndProductInfo(arrcampi_productInfo, arr productInfo.img = 'upload/products/' + rec['img']; } else { productInfo.img = ''; - } + } } } @@ -119,10 +119,10 @@ async function completaSettaggioProduct_AndProductInfo(arrcampi_productInfo, arr productInfo.code = preccode; } } - if (rec.hasOwnProperty('productType')) { - productInfo.productType = rec.hasOwnProperty('productType'); + if (rec.hasOwnProperty('productTypes')) { + productInfo.productTypes = productInfo.productTypes; } else { - productInfo.productType = shared_consts.PRODUCTTYPE.PRODUCT; + productInfo.productTypes = [shared_consts.PRODUCTTYPE.PRODUCT]; } return { product, productInfo }; @@ -339,6 +339,11 @@ router.post('/import', authenticate, async (req, res) => { importa = false; if (importa) { + let versGM = product.Versione ? product.Versione : ''; + + // split versioneGM in array with separated "," + let arrversGM = versGM.split(",").map(x => x.trim()); + let productInfo = { idapp: product.idapp, code: product.isbn, @@ -361,41 +366,53 @@ router.post('/import', authenticate, async (req, res) => { } let versione = 0; + let arrversioni = []; - // Download, DVD, Epub, Mobi, Nuovo, PDF, Streaming, Usato - if (product.Versione === 'Nuovo') - versione = shared_consts.VERSIONE.NUOVO - else if (product.Versione === 'Usato') - versione = shared_consts.VERSIONE.USATO; - else if (product.Versione === 'Download') - versione = shared_consts.VERSIONE.DOWNLOAD; - else if (product.Versione === 'DVD') - versione = shared_consts.VERSIONE.DVD; - else if (product.Versione === 'Epub') - versione = shared_consts.VERSIONE.EPUB; - else if (product.Versione === 'Mobi') - versione = shared_consts.VERSIONE.MOBI; - else if (product.Versione === 'PDF') - versione = shared_consts.VERSIONE.PDF; - else if (product.Versione === 'Streaming') - versione = shared_consts.VERSIONE.STREAMING; - else - versione = 100; + productInfo.productTypes = []; + // console.log('indprod', indprod, 'arrversGM', arrversGM, 'versione', product.Versione); - if (versione === shared_consts.VERSIONE.DOWNLOAD) - productInfo.productType = shared_consts.PRODUCTTYPE.DOWNLOAD; - else if (versione === shared_consts.VERSIONE.DVD) - productInfo.productType = shared_consts.PRODUCTTYPE.DVD; - else if (versione === shared_consts.VERSIONE.EPUB) - productInfo.productType = shared_consts.PRODUCTTYPE.EPUB; - else if (versione === shared_consts.VERSIONE.MOBI) - productInfo.productType = shared_consts.PRODUCTTYPE.MOBI; - else if (versione === shared_consts.VERSIONE.PDF) - productInfo.productType = shared_consts.PRODUCTTYPE.PDF; - else if (versione === shared_consts.VERSIONE.STREAMING) - productInfo.productType = shared_consts.PRODUCTTYPE.STREAMING; - else - productInfo.productType = shared_consts.PRODUCTTYPE.LIBRO; + for (let i = 0; i < arrversGM.length; i++) { + // Download, DVD, Epub, Mobi, Nuovo, PDF, Streaming, Usato + if (arrversGM[i] === 'Nuovo') + versione = shared_consts.VERSIONE.NUOVO + else if (arrversGM[i] === 'Usato') + versione = shared_consts.VERSIONE.USATO; + else if (arrversGM[i] === 'Download') + versione = shared_consts.VERSIONE.DOWNLOAD; + else if (arrversGM[i] === 'DVD') + versione = shared_consts.VERSIONE.DVD; + else if (arrversGM[i] === 'Epub') + versione = shared_consts.VERSIONE.EPUB; + else if (arrversGM[i] === 'Mobi') + versione = shared_consts.VERSIONE.MOBI; + else if (arrversGM[i] === 'PDF') + versione = shared_consts.VERSIONE.PDF; + else if (arrversGM[i] === 'Streaming') + versione = shared_consts.VERSIONE.STREAMING; + + arrversioni.push(versione); + + if (versione === shared_consts.VERSIONE.DOWNLOAD) + productInfo.productTypes.push(shared_consts.PRODUCTTYPE.DOWNLOAD); + else if (versione === shared_consts.VERSIONE.DVD) + productInfo.productTypes.push(shared_consts.PRODUCTTYPE.DVD); + else if (versione === shared_consts.VERSIONE.EPUB) + productInfo.productTypes.push(shared_consts.PRODUCTTYPE.EPUB); + else if (versione === shared_consts.VERSIONE.MOBI) + productInfo.productTypes.push(shared_consts.PRODUCTTYPE.MOBI); + else if (versione === shared_consts.VERSIONE.PDF) + productInfo.productTypes.push(shared_consts.PRODUCTTYPE.PDF); + else if (versione === shared_consts.VERSIONE.STREAMING) + productInfo.productTypes.push(shared_consts.PRODUCTTYPE.STREAMING); + else if (versione === shared_consts.VERSIONE.USATO) + productInfo.productTypes.push(shared_consts.PRODUCTTYPE.USATO); + else if (versione === shared_consts.VERSIONE.NUOVO) + productInfo.productTypes.push(shared_consts.PRODUCTTYPE.NUOVO); + } + + /*if (productInfo.productTypes.length === 0) { + productInfo.productTypes.push(shared_consts.PRODUCTTYPE.LIBRO); + }*/ if (product.Data) productInfo.date_publishing = new Date(product.Data * 1000); @@ -604,7 +621,7 @@ router.post('/import', authenticate, async (req, res) => { } // cerca l'indice della versione in arrvariazioni - let ind = arrvariazioni.findIndex(x => x.versione === versione); + let ind = arrvariazioni.findIndex(x => arrversioni.includes(x.versione)); let nuovaVariazione = ind < 0; // @@ -615,7 +632,7 @@ router.post('/import', authenticate, async (req, res) => { } variazione.active = true; // ++ ?? - variazione.versione = versione; + variazione.arrversioni = arrversioni; variazione.status = product.Stato ? product.Stato : null; variazione.price = product.price ? parseFloat(tools.convertPriceEurToValue(product.price)) : null; variazione.sale_price = product.sale_price ? parseFloat(tools.convertPriceEurToValue(product.sale_price)) : null; @@ -770,7 +787,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, + productTypes: shared_consts.PRODUCTTYPE.PRODUCT, } let reccateg = null; diff --git a/src/server/router/users_router.js b/src/server/router/users_router.js index 3679238..ca7c2b7 100755 --- a/src/server/router/users_router.js +++ b/src/server/router/users_router.js @@ -36,9 +36,9 @@ const { authenticate, authenticate_noerror } = require('../middleware/authentica const Cart = require('../models/cart'); const CartClass = require('../modules/Cart'); const Product = require('../models/product'); +const ProductInfo = require('../models/productInfo'); const CatProd = require('../models/catprod'); const SubCatProd = require('../models/subcatprod'); -const ProductInfo = require('../models/productInfo'); const Order = require('../models/order'); const OrdersCart = require('../models/orderscart'); const Variant = require('../models/variant'); @@ -1623,6 +1623,10 @@ async function eseguiDbOp(idapp, mydata, locale, req, res) { await Circuit.createCircuitIfNotExist(req, idapp, recprov.prov); } + } else if (mydata.dbop === 'correggiProductTypes') { + + await ProductInfo.correggiProductTypes(); + } else if (mydata.dbop === 'correggiCircuitiANull') { diff --git a/src/server/tools/shared_nodejs.js b/src/server/tools/shared_nodejs.js index bbaf604..65905f6 100755 --- a/src/server/tools/shared_nodejs.js +++ b/src/server/tools/shared_nodejs.js @@ -1091,6 +1091,16 @@ module.exports = { AUDIOLIBRO: 22, VIDEO: 23, CARTE: 25, + // ----------- + NUOVO: 101, + USATO: 102, + DOWNLOAD: 103, + DVD: 104, + EPUB: 105, + MOBI: 106, + PDF: 107, + STREAMING: 108, + }, // Download, DVD, Epub, Mobi, Nuovo, PDF, Streaming, Usato