diff --git a/.env.development b/.env.development index 3136acd..61bd7e9 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"] +appTelegram=["1","17"] DOMAIN=mongodb://localhost:27017/ AUTH_MONGODB=true MONGODB_USER=admin diff --git a/src/server/router/admin_router.js b/src/server/router/admin_router.js index 089b387..0f8a188 100755 --- a/src/server/router/admin_router.js +++ b/src/server/router/admin_router.js @@ -43,33 +43,41 @@ function fixURL(url) { } -function completaSettaggioProduct_AndProductInfo(arrcampi_productInfo, arrcampi_product, rec, product, productInfo) { +async function completaSettaggioProduct_AndProductInfo(arrcampi_productInfo, arrcampi_product, rec, product, productInfo) { try { if (shared_consts.CAMPI_PRODUCTINFO_CONVERT.includes('weight_and_unit')) { - const ris1 = tools.getWeightAndUnitByText(rec['weight_and_unit']); - if (ris1) { - productInfo.weight = ris1.weight; - productInfo.unit = ris1.unit; + if (rec.hasOwnProperty('weight_and_unit')) { + const ris1 = tools.getWeightAndUnitByText(rec['weight_and_unit']); + if (ris1) { + productInfo.weight = ris1.weight; + productInfo.unit = ris1.unit; + } } } if (shared_consts.CAMPI_PRODUCTINFO_CONVERT.includes('weight_and_unit_lordo')) { - const ris2 = tools.getWeightAndUnitByText(rec['weight_and_unit_lordo']); - if (ris2) { - productInfo.weight_lordo = ris2.weight; - productInfo.unit_lordo = ris2.unit; + if (rec.hasOwnProperty('weight_and_unit_lordo')) { + const ris2 = tools.getWeightAndUnitByText(rec['weight_and_unit_lordo']); + if (ris2) { + productInfo.weight_lordo = ris2.weight; + productInfo.unit_lordo = ris2.unit; + } } } if (rec.hasOwnProperty('link_scheda')) { - productInfo['link_scheda'] = fixURL(rec['link_scheda']) + if (fixURL(rec['link_scheda'])) + productInfo['link_scheda'] = fixURL(rec['link_scheda']) } for (const campo of shared_consts.PRODUCTINFO.CAMPI_FIRST_UPPERCASE) { + if (rec.hasOwnProperty(campo)) { - productInfo[campo] = tools.capitalize(rec[campo]); + let mystr = tools.capitalize(rec[campo]).trim(); + if (mystr) + productInfo[campo] = mystr; } } @@ -77,7 +85,6 @@ function completaSettaggioProduct_AndProductInfo(arrcampi_productInfo, arrcampi_ for (campoprezzo of shared_consts.CAMPI_EURO) { if (rec.hasOwnProperty(campoprezzo)) { product[campoprezzo] = tools.convertPriceEurToValue(rec[campoprezzo]) - } } @@ -89,6 +96,23 @@ function completaSettaggioProduct_AndProductInfo(arrcampi_productInfo, arrcampi_ } } + if (rec.hasOwnProperty('old_code')) { + const old_code = rec['old_code']; + let oldrec = await ProductInfo.findOne({ code: old_code }).lean(); + if (oldrec) { + const precid = productInfo._id; + const preccode = productInfo.code; + + productInfo = oldrec; + if (precid) + productInfo._id = precid; + else + delete productInfo._id; + + productInfo.code = preccode; + } + } + return { product, productInfo }; } catch (e) { @@ -133,7 +157,7 @@ function getValoriAndIndice_Product(dati) { return shared_consts.CAMPI_PRODUCT; } -function extractArrayDataFromCSV(idapp, rec) { +async function extractArrayDataFromCSV(idapp, rec) { try { // la prima riga contiene il nome della proprietà: @@ -163,7 +187,7 @@ function extractArrayDataFromCSV(idapp, rec) { product[campoobj] = rec[campoobj]; } - const ris = completaSettaggioProduct_AndProductInfo(arrcampi_productInfo, arrcampi_product, rec, product, productInfo); + const ris = await completaSettaggioProduct_AndProductInfo(arrcampi_productInfo, arrcampi_product, rec, product, productInfo); /* // code: product.code, @@ -399,7 +423,7 @@ router.post('/import', authenticate, async (req, res) => { for (const rec of myarrshift) { - let risprod = extractArrayDataFromCSV(idapp, rec); + let risprod = await extractArrayDataFromCSV(idapp, rec); let product = risprod.product; let productInfo = risprod.productInfo; diff --git a/src/server/tools/shared_nodejs.js b/src/server/tools/shared_nodejs.js index 5108615..38eb317 100755 --- a/src/server/tools/shared_nodejs.js +++ b/src/server/tools/shared_nodejs.js @@ -973,6 +973,10 @@ module.exports = { 'weight_and_unit_lordo', ], + CAMPI_PRODUCTINFO_RICOPIA_DATI: [ + 'old_code', + ], + PRODUCTINFO: { CAMPI_FIRST_UPPERCASE: [ 'name',