Catalogo, versione di libro/ebook, ecc... quantità

This commit is contained in:
Surya Paolo
2024-05-05 13:55:43 +02:00
parent 07973fbf0a
commit 3fcff43af0
3 changed files with 143 additions and 74 deletions

View File

@@ -56,9 +56,49 @@ const productSchema = new Schema({
type: Number, type: Number,
required: true, required: true,
}, },
sale_price: { arrvariazioni: [
type: Number, {
}, 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: { price_acquistato: {
type: Number, type: Number,
required: true, required: true,
@@ -80,14 +120,10 @@ const productSchema = new Schema({
type: Number, type: Number,
default: 0, default: 0,
}, },
stockQty: { // in magazzino stockQty: { // in magazzino
type: Number, type: Number,
default: 0, default: 0,
}, },
status: { //publish
type: String,
},
stockBloccatiQty: { // Prenotati Bloccati stockBloccatiQty: { // Prenotati Bloccati
type: Number, type: Number,
default: 0, default: 0,
@@ -149,9 +185,6 @@ const productSchema = new Schema({
note: { note: {
type: String, type: String,
}, },
versione: {
type: Number,
},
producer_name: { producer_name: {
type: String, type: String,
}, },
@@ -332,12 +365,6 @@ module.exports.findAllIdApp = async function (idapp, code, id, all) {
} }
} }
}, },
{
$unwind: {
path: '$producer',
preserveNullAndEmptyArrays: true,
},
},
{ {
$lookup: { $lookup: {
from: 'providers', from: 'providers',

View File

@@ -39,6 +39,9 @@ const productInfoSchema = new Schema({
description: { description: {
type: String, type: String,
}, },
short_descr: {
type: String,
},
idCatProds: [{ type: Schema.Types.ObjectId, ref: 'CatProd' }], idCatProds: [{ type: Schema.Types.ObjectId, ref: 'CatProd' }],
idSubCatProds: [{ type: Schema.Types.ObjectId, ref: 'SubCatProd' }], idSubCatProds: [{ type: Schema.Types.ObjectId, ref: 'SubCatProd' }],
color: { color: {
@@ -82,6 +85,9 @@ const productInfoSchema = new Schema({
link: { link: {
type: String, type: String,
}, },
checkout_link: {
type: String,
},
img2: { img2: {
type: String, type: String,
}, },
@@ -108,9 +114,6 @@ const productInfoSchema = new Schema({
date_publishing: { date_publishing: {
type: Date, type: Date,
}, },
numpages: {
type: Number,
},
productType: { productType: {
type: Number, type: Number,
}, },

View File

@@ -341,6 +341,7 @@ router.post('/import', authenticate, async (req, res) => {
id_wp: product._id, id_wp: product._id,
name: product.title, name: product.title,
description: product.description, description: product.description,
short_descr: product.short_descr,
publisher: product.Editore, publisher: product.Editore,
collezione: product.Collezione, collezione: product.Collezione,
// author: product.Autore ? product.Autore : '', // author: product.Autore ? product.Autore : '',
@@ -352,10 +353,45 @@ router.post('/import', authenticate, async (req, res) => {
img2: product.img2 ? product.img2 : '', img2: product.img2 ? product.img2 : '',
img3: product.img3 ? product.img3 : '', img3: product.img3 ? product.img3 : '',
img4: product.img4 ? product.img4 : '', 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) if (product.Data)
productInfo.date_publishing = new Date(product.Data * 1000); 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) { if (product.subcat_name) {
arrsubcat = product.subcat_name.trim().split(','); arrsubcat = product.subcat_name.trim().split(',');
for (const mysubcat of arrsubcat) { for (const mysubcat of arrsubcat) {
@@ -484,13 +517,6 @@ router.post('/import', authenticate, async (req, res) => {
product.active = true; 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(); let esisteindb = await ProductInfo.findOne({ code: productInfo.code }).lean();
// Update ProductInfo // Update ProductInfo
let risrecInfo = await ProductInfo.findOneAndUpdate({ code: productInfo.code }, { $set: productInfo }, { new: true, upsert: true }); 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 = {}; 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; let recProductExist = null;
// ISBN e versione del prodotto sono le chiavi uniche // ISBN e versione del prodotto sono le chiavi uniche
let queryprod = { idProductInfo: product.idProductInfo, versione: myproduct.versione }; let queryprod = { idProductInfo: product.idProductInfo };
if (recGas) { if (recGas) {
queryprod = { ...queryprod, idGasordine: recGas._id }; queryprod = { ...queryprod, idGasordine: recGas._id };
} }
recProductExist = await Product.findOne({ queryprod }).lean(); recProductExist = await Product.findOne(queryprod).lean();
if (!recProductExist) { if (!recProductExist) {
isnuovo = true; isnuovo = true;
} }
if (!options.aggiornaStockQty && esisteindb && !isnuovo) { let arrvariazioni = [];
delete product.Quantità; if (isnuovo) {
// delete product.maxbookableGASQty; 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; let variazione = {};
myproduct.active = product.active; variazione.active = true; // ++ ??
myproduct.idGasordine = recGas ? recGas._id : null; variazione.versione = versione;
myproduct.idProductInfo = product.idProductInfo; variazione.status = product.Stato ? product.Stato : null;
myproduct.price = product.price ? parseFloat(tools.convertPriceEurToValue(product.price)) : null; variazione.price = product.price ? parseFloat(tools.convertPriceEurToValue(product.price)) : null;
myproduct.sale_price = product.sale_price ? parseFloat(tools.convertPriceEurToValue(product.sale_price)) : null; variazione.sale_price = product.sale_price ? parseFloat(tools.convertPriceEurToValue(product.sale_price)) : null;
myproduct.idapp = idapp; variazione.formato = product.formato ? product.formato : '';
myproduct.status = product.status ? product.status : null; 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à) { // cerca l'indice della versione in arrvariazioni
myproduct.stockQty = parseInt(product.Quantità); 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(); let recold = await Product.findOne(queryprod).lean();
// AGGIORNA PRODUCT let risrec = null;
let risrec = await Product.findOneAndUpdate(queryprod, { $set: myproduct }, { new: true, upsert: true }); 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(); let recnew = await Product.findOne(queryprod).lean();
@@ -625,7 +664,7 @@ router.post('/import', authenticate, async (req, res) => {
indprod++; 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 }); return res.status(200).send({ updated, imported, errors });
@@ -745,7 +784,7 @@ router.post('/import', authenticate, async (req, res) => {
queryprod = { ...queryprod, idGasordine: recGas._id }; queryprod = { ...queryprod, idGasordine: recGas._id };
} }
recProductExist = await Product.findOne({ queryprod }).lean(); recProductExist = await Product.findOne(queryprod).lean();
if (!recProductExist) { if (!recProductExist) {
isnuovo = true; isnuovo = true;
@@ -912,7 +951,7 @@ router.post('/import', authenticate, async (req, res) => {
queryprod = { ...queryprod, idGasordine: recGas._id }; queryprod = { ...queryprod, idGasordine: recGas._id };
} }
recProductExist = await Product.findOne({ queryprod }).lean(); recProductExist = await Product.findOne(queryprod).lean();
if (!recProductExist) { if (!recProductExist) {
isnuovo = true; isnuovo = true;