diff --git a/src/server/models/product.js b/src/server/models/product.js index e6143fb..77c991f 100755 --- a/src/server/models/product.js +++ b/src/server/models/product.js @@ -56,9 +56,49 @@ const productSchema = new Schema({ type: Number, required: true, }, - sale_price: { - type: Number, - }, + arrvariazioni: [ + { + active: { + type: Boolean, + }, + versione: { + type: Number, + }, + status: { //publish + type: String, + }, + price: { + type: Number, + }, + sale_price: { + type: Number, + }, + quantita: { // in magazzino + type: Number, + }, + numpages: { + type: Number, + }, + formato: { + type: String, + }, + tipologia: { + type: String, + }, + edizione: { + type: String, + }, + preOrderDate: { + type: Date + }, + addtocart_link: { + type: String + }, + eta: { + type: String + }, + } + ], price_acquistato: { type: Number, required: true, @@ -80,14 +120,10 @@ const productSchema = new Schema({ type: Number, default: 0, }, - stockQty: { // in magazzino type: Number, default: 0, }, - status: { //publish - type: String, - }, stockBloccatiQty: { // Prenotati Bloccati type: Number, default: 0, @@ -149,9 +185,6 @@ const productSchema = new Schema({ note: { type: String, }, - versione: { - type: Number, - }, producer_name: { type: String, }, @@ -332,12 +365,6 @@ module.exports.findAllIdApp = async function (idapp, code, id, all) { } } }, - { - $unwind: { - path: '$producer', - preserveNullAndEmptyArrays: true, - }, - }, { $lookup: { from: 'providers', diff --git a/src/server/models/productInfo.js b/src/server/models/productInfo.js index c211b14..1a495e3 100755 --- a/src/server/models/productInfo.js +++ b/src/server/models/productInfo.js @@ -39,6 +39,9 @@ const productInfoSchema = new Schema({ description: { type: String, }, + short_descr: { + type: String, + }, idCatProds: [{ type: Schema.Types.ObjectId, ref: 'CatProd' }], idSubCatProds: [{ type: Schema.Types.ObjectId, ref: 'SubCatProd' }], color: { @@ -82,6 +85,9 @@ const productInfoSchema = new Schema({ link: { type: String, }, + checkout_link: { + type: String, + }, img2: { type: String, }, @@ -108,9 +114,6 @@ const productInfoSchema = new Schema({ date_publishing: { type: Date, }, - numpages: { - type: Number, - }, productType: { type: Number, }, diff --git a/src/server/router/admin_router.js b/src/server/router/admin_router.js index a8de147..e729d32 100755 --- a/src/server/router/admin_router.js +++ b/src/server/router/admin_router.js @@ -341,6 +341,7 @@ router.post('/import', authenticate, async (req, res) => { id_wp: product._id, name: product.title, description: product.description, + short_descr: product.short_descr, publisher: product.Editore, collezione: product.Collezione, // author: product.Autore ? product.Autore : '', @@ -352,10 +353,45 @@ router.post('/import', authenticate, async (req, res) => { img2: product.img2 ? product.img2 : '', img3: product.img3 ? product.img3 : '', img4: product.img4 ? product.img4 : '', - + checkout_link: product.checkout_link ? product.checkout_link : '', } - productInfo.productType = shared_consts.PRODUCTTYPE.LIBRO; + let versione = 0; + + // 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; + + 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; if (product.Data) productInfo.date_publishing = new Date(product.Data * 1000); @@ -456,9 +492,6 @@ router.post('/import', authenticate, async (req, res) => { } } - // Cancella l'Autore dal record product - delete product.Autore; - if (product.subcat_name) { arrsubcat = product.subcat_name.trim().split(','); for (const mysubcat of arrsubcat) { @@ -484,13 +517,6 @@ router.post('/import', authenticate, async (req, res) => { 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 }); @@ -523,66 +549,79 @@ router.post('/import', authenticate, async (req, res) => { let myproduct = {}; - // Download, DVD, Epub, Mobi, Nuovo, PDF, Streaming, Usato - if (product.Versione === 'Nuovo') - myproduct.versione = shared_consts.VERSIONE.NUOVO - else if (product.Versione === 'Usato') - myproduct.versione = shared_consts.VERSIONE.USATO; - else if (product.Versione === 'Download') - myproduct.versione = shared_consts.VERSIONE.DOWNLOAD; - else if (product.Versione === 'DVD') - myproduct.versione = shared_consts.VERSIONE.DVD; - else if (product.Versione === 'Epub') - myproduct.versione = shared_consts.VERSIONE.EPUB; - else if (product.Versione === 'Mobi') - myproduct.versione = shared_consts.VERSIONE.MOBI; - else if (product.Versione === 'PDF') - myproduct.versione = shared_consts.VERSIONE.PDF; - else if (product.Versione === 'Streaming') - myproduct.versione = shared_consts.VERSIONE.STREAMING; - else - myproduct.versione = 100; - - delete product.Versione; let recProductExist = null; // ISBN e versione del prodotto sono le chiavi uniche - let queryprod = { idProductInfo: product.idProductInfo, versione: myproduct.versione }; + let queryprod = { idProductInfo: product.idProductInfo }; if (recGas) { queryprod = { ...queryprod, idGasordine: recGas._id }; } - recProductExist = await Product.findOne({ queryprod }).lean(); + recProductExist = await Product.findOne(queryprod).lean(); if (!recProductExist) { isnuovo = true; } - - if (!options.aggiornaStockQty && esisteindb && !isnuovo) { - delete product.Quantità; - // delete product.maxbookableGASQty; + + let arrvariazioni = []; + if (isnuovo) { + myproduct.isbn = product.isbn; + myproduct.maxbookableGASQty = product.maxbookableGASQty ? product.maxbookableGASQty : null; + myproduct.active = product.active; + myproduct.idGasordine = recGas ? recGas._id : null; + myproduct.idProductInfo = product.idProductInfo; + myproduct.idapp = idapp; + } else { + arrvariazioni = recProductExist.arrvariazioni; + if (!arrvariazioni) { + arrvariazioni = []; + } } - myproduct.isbn = product.isbn; - myproduct.maxbookableGASQty = product.maxbookableGASQty ? product.maxbookableGASQty : null; - myproduct.active = product.active; - myproduct.idGasordine = recGas ? recGas._id : null; - myproduct.idProductInfo = product.idProductInfo; - myproduct.price = product.price ? parseFloat(tools.convertPriceEurToValue(product.price)) : null; - myproduct.sale_price = product.sale_price ? parseFloat(tools.convertPriceEurToValue(product.sale_price)) : null; - myproduct.idapp = idapp; - myproduct.status = product.status ? product.status : null; + // + let variazione = {}; + variazione.active = true; // ++ ?? + variazione.versione = versione; + 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; + variazione.formato = product.formato ? product.formato : ''; + variazione.tipologia = product.Tipologia ? product.Tipologia : ''; + variazione.edizione = product.Edizione ? product.Edizione : ''; + variazione.eta = product.eta ? product.eta : ''; + variazione.addtocart_link = product.addtocart_link ? product.addtocart_link : ''; + if (!options.aggiornaStockQty && esisteindb && !isnuovo) { + // non aggiornare la Quantita in magazzino + } else if (product.Quantita) { + variazione.quantita = parseInt(product.Quantita); + } + variazione.preOrderDate = product.preOrderDate ? product.preOrderDate : null; + // ----------------------------- - if (product.Quantità) { - myproduct.stockQty = parseInt(product.Quantità); + // cerca l'indice della versione in arrvariazioni + let ind = arrvariazioni.findIndex(x => x.versione === versione); + if (ind >= 0) + arrvariazioni[ind] = variazione; // aggiorna il record "ind" in arrvariazioni + else + arrvariazioni.push(variazione); // aggiunge un nuovo record in arrvariazioni + + // Inserisce o aggiorna il record variazione nell'array delle variazioni, in base a "versione" + if (isnuovo) { + myproduct.arrvariazioni = arrvariazioni; } let recold = await Product.findOne(queryprod).lean(); - // AGGIORNA PRODUCT - let risrec = await Product.findOneAndUpdate(queryprod, { $set: myproduct }, { new: true, upsert: true }); + let risrec = null; + if (isnuovo) { + // CREA PRODUCT + risrec = await Product.findOneAndUpdate(queryprod, { $set: myproduct }, { new: true, upsert: true }); + } else { + // Esiste già, pertanto ci aggiorno arrvariazioni + risrec = await Product.findOneAndUpdate(queryprod, { $set: { arrvariazioni } }, { new: true, upsert: true }); + } let recnew = await Product.findOne(queryprod).lean(); @@ -625,7 +664,7 @@ router.post('/import', authenticate, async (req, res) => { indprod++; } - console.log('*** IMPORTATI: ', imported, 'AGGIORNATI = ' + updated + ' (su ' + dataObjects.length + ' RECORD)'); + console.log('*** IMPORTATI: ', imported, '*** NUOVI: ', newprod, 'AGGIORNATI = ' + updated + ' (su ' + dataObjects.length + ' RECORD)'); return res.status(200).send({ updated, imported, errors }); @@ -745,7 +784,7 @@ router.post('/import', authenticate, async (req, res) => { queryprod = { ...queryprod, idGasordine: recGas._id }; } - recProductExist = await Product.findOne({ queryprod }).lean(); + recProductExist = await Product.findOne(queryprod).lean(); if (!recProductExist) { isnuovo = true; @@ -912,7 +951,7 @@ router.post('/import', authenticate, async (req, res) => { queryprod = { ...queryprod, idGasordine: recGas._id }; } - recProductExist = await Product.findOne({ queryprod }).lean(); + recProductExist = await Product.findOne(queryprod).lean(); if (!recProductExist) { isnuovo = true;