From d22e80862627fbef89ca44d4da30f5e9da7dc002 Mon Sep 17 00:00:00 2001 From: Surya Paolo Date: Tue, 16 Jan 2024 16:56:39 +0100 Subject: [PATCH] aggio gasordine --- src/server/models/product.js | 22 ++++++++++------------ src/server/router/admin_router.js | 19 ++++++++++++++++++- src/server/router/products_router.js | 2 +- src/server/router/users_router.js | 10 ++++++++++ 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/server/models/product.js b/src/server/models/product.js index 9960e5a..ac05318 100755 --- a/src/server/models/product.js +++ b/src/server/models/product.js @@ -38,9 +38,7 @@ const productSchema = new Schema({ idStorehouses: [ { type: Schema.Types.ObjectId, ref: 'Storehouse' } ], - idGasordines: [ - { type: Schema.Types.ObjectId, ref: 'Gasordine' } - ], + idGasordine: { type: Schema.Types.ObjectId, ref: 'Gasordine' }, idScontisticas: [ { type: Schema.Types.ObjectId, ref: 'Scontistica' } ], @@ -200,7 +198,7 @@ module.exports.findAllIdApp = async function (idapp, code, id, all) { try { - if (idapp){ + if (idapp) { myfind = { idapp }; } @@ -266,36 +264,36 @@ module.exports.findAllIdApp = async function (idapp, code, id, all) { { $lookup: { from: 'gasordines', - localField: 'idGasordines', + localField: 'idGasordine', foreignField: '_id', - as: 'gasordines' + as: 'gasordine' } }, { $unwind: { - path: '$gasordines', + path: '$gasordine', preserveNullAndEmptyArrays: true, }, }, { $match: { $or: [ - { 'gasordines.active': true }, // Include documents where gasordines.active is true - { 'gasordines': { $exists: false } } // Include documents where gasordines array doesn't exist + { 'gasordine.active': true }, // Include documents where gasordines.active is true + { 'gasordine': { $exists: false } } // Include documents where gasordines array doesn't exist ] } }, { $group: { _id: '$_id', - gasordines: { $push: '$gasordines' }, + gasordine: { $first: '$gasordine' }, originalFields: { $first: '$$ROOT' } // Preserve all existing fields } }, { $replaceRoot: { newRoot: { - $mergeObjects: ['$originalFields', { gasordines: '$gasordines' }] + $mergeObjects: ['$originalFields', { gasordine: '$gasordine' }] } } }, @@ -649,7 +647,7 @@ module.exports.singlerecconvert_AfterImport_AndSave = async function (idapp, pro if (rec) { objtoset = { ...objtoset, - idGasordines: [rec._id], + idGasordine: rec._id, } setta = true; } diff --git a/src/server/router/admin_router.js b/src/server/router/admin_router.js index 0507cfc..edf786d 100755 --- a/src/server/router/admin_router.js +++ b/src/server/router/admin_router.js @@ -144,7 +144,24 @@ router.post('/import', authenticate, async (req, res) => { } } - let recProductExist = await Product.findOne({ idProductInfo: product.idProductInfo }).lean(); + // Cerca il GAS + let recGas = null; + if (prod.gas_name) { + // Cerca il GAS + recGas = await Gasordine.findOne({ idapp, name: prod.gas_name }).lean(); + } + + let recProductExist = null; + let queryprod = { idProductInfo: product.idProductInfo }; + + if (recGas) { + queryprod = {...queryprod, gas} + } else { + recProductExist = await Product.findOne().lean(); + } + + recProductExist = await Product.findOne({ queryprod }).lean(); + if (!recProductExist) { isnuovo = true; } diff --git a/src/server/router/products_router.js b/src/server/router/products_router.js index 1057adb..aa1fe34 100755 --- a/src/server/router/products_router.js +++ b/src/server/router/products_router.js @@ -50,7 +50,7 @@ router.post('/', auth_default, async function (req, res, next) { if (products) res.send({ code: server_constants.RIS_CODE_OK, products, orders }); else - res.status(400).send(e); + res.status(400).send({ code: server_constants.RIS_CODE_OK, products, orders }); /* const { query, order } = tools.categorizeQueryString(req.query) diff --git a/src/server/router/users_router.js b/src/server/router/users_router.js index 7e1b135..f2818fd 100755 --- a/src/server/router/users_router.js +++ b/src/server/router/users_router.js @@ -1330,6 +1330,16 @@ async function eseguiDbOp(idapp, mydata, locale, req, res) { } catch (e) { console.error('Err:', e); } + } else if (mydata.dbop === 'SistemaGasOrdine') { + const arrrec = await Product.find({}).lean(); + for (const rec of arrrec) { + if (tools.isArray(rec.idGasordines) && rec.idGasordines.length > 0) { + await Product.findByIdAndUpdate(rec._id, { $set: { idGasordine: rec.idGasordines[0] } }) + } else { + await Product.findByIdAndUpdate(rec._id, { $set: { idGasordine: null } }) + } + } + } else if (mydata.dbop === 'CopyPriceToCalc') { try {